#!/usr/bin/python3
#
# Copyright (C) 2024 Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

from anacondalib import INSTALLER_VM_MEMORY_MB, VirtInstallMachineCase, disk_images
from installer import Installer
from operating_systems import DualBootHelper_E2E
from review import Review
from storage import Storage
from testlib import test_main, timeout  # pylint: disable=import-error


class TestStorageReclaim_E2E(DualBootHelper_E2E, VirtInstallMachineCase):
    provision = {"machine1": {"memory_mb": INSTALLER_VM_MEMORY_MB}}

    @disk_images([("debian-testing", 20)])
    @timeout(1200)
    def testReclaimShrink(self):
        """
        Description:
            Test dual boot with Debian by reclaiming space from the debian root partition.

        Expected results:
            - The installer should allow the user to reclaim space from the debian root partition.
            - The installation should complete successfully.
            - The system should boot into both operating systems without issues.
        """
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        r = Review(b, m)
        s = Storage(b, m)

        i.open()
        i.reach(i.steps.INSTALLATION_METHOD)
        s.set_scenario("use-free-space")
        s.reclaim_set_checkbox(True)
        i.next(next_page=i.steps.INSTALLATION_METHOD)

        s.reclaim_shrink_device("vda1", "7")
        s.reclaim_modal_submit()
        # Debian /boot/efi is less than 500MB therefore we get a warning
        i.next(should_fail=True)

        i.reach(i.steps.REVIEW)
        r.check_some_resized_checkbox_label()
        vda1_original_size_bytes = m.execute("blockdev --getsize64 /dev/vda1").strip()
        vda1_original_size_gb = round(int(vda1_original_size_bytes) / 1000 / 1000 / 1000, 1)
        r.check_disk_row("vda", parent="vda1", size="7.00 GB", action=f"resized from {vda1_original_size_gb} GB")
        r.check_resized_system("Debian", ["vda1"])

        self.install(needs_confirmation=True)
        self.verifyDualBootDebian(root_one_size=11.4, root_two_size=6.5)


if __name__ == '__main__':
    test_main()
