#!/usr/bin/python3
#
# Copyright (C) 2022 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.

import os
import sys

# import Cockpit's machinery for test VMs and its browser test API
TEST_DIR = os.path.dirname(__file__)
sys.path.append(os.path.join(TEST_DIR, "common"))
sys.path.append(os.path.join(TEST_DIR, "helpers"))
sys.path.append(os.path.join(os.path.dirname(TEST_DIR), "bots/machine"))

from installer import Installer
from storage import Storage
from testlib import MachineCase, nondestructive, test_main  # pylint: disable=import-error
from machine_install import VirtInstallMachine

@nondestructive
class TestStorage(MachineCase):
    MachineCase.machine_class = VirtInstallMachine

    def testLocalStandardDisks(self):
        b = self.browser
        i = Installer(b, self.machine)
        s = Storage(b, self.machine)

        i.open()
        i.next()

        b.wait_visible('h3:contains(Local standard disks)')

        # Check disks table details
        s.check_disk_visible("vda")
        s.check_disk_capacity("vda", "15 GiB", "15 GiB")

        # Pixel test the storage step
        b.assert_pixels("#app", "storage-step-basic", ignore=["#betanag-icon", "#installation-destination-table-label"])

        # Try unselecting the single disk and expect and error
        s.select_disk("vda", False)
        i.next(should_fail=True)
        s.wait_no_disks()
        # Since storage configuration failed it's expected to be in the still in the storage step
        b.wait_visible(f"#{i.steps.STORAGE}.pf-m-current")

# We can't run this test case on an existing machine,
# with --machine because MachineCase is not aware of add_disk method
class TestStorageExtraDisks(MachineCase):
    MachineCase.machine_class = VirtInstallMachine

    def testLocalDisksSyncNew(self):
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        s = Storage(b, m)

        # This attaches a disk to the running VM
        # However, since the storage module initialization is long completed
        # the newly added disk, will not be visible in the UI,
        # until the test clicks on the re-scan button
        m.add_disk(2)

        i.open()
        i.next()

        s.check_disk_visible("vda")
        s.check_disk_visible("vdb", False)

        s.rescan_disks()
        s.check_disk_visible("vda")
        s.check_disk_visible("vdb")

        s.check_disk_selected("vda", False)
        s.check_disk_selected("vdb", False)

if __name__ == '__main__':
    test_main()
