#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv)

# This file is part of Cockpit.
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Cockpit 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.
#
# Cockpit 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 Cockpit; If not, see <https://www.gnu.org/licenses/>.

import storagelib
import testlib


@testlib.nondestructive
class TestStorageHiddenLuks(storagelib.StorageCase):
    def test(self):
        m = self.machine
        b = self.browser

        mount_point_1 = f"{self.mnt_dir}/mount1"

        self.login_and_go("/storage")

        disk = self.add_ram_disk()
        b.wait_visible(self.card_row("Storage", name=disk))

        # Create a volume group with a logical volume with a encrypted
        # filesystem.

        self.click_dropdown(self.card_header("Storage"), "Create LVM2 volume group")
        self.dialog_wait_open()
        self.dialog_set_val('name', "TEST")
        self.dialog_set_val('disks', {disk: True})
        self.dialog_apply()
        self.dialog_wait_close()

        self.click_card_row("Storage", name="TEST")
        b.click(self.card_button("LVM2 logical volumes", "Create new logical volume"))
        self.dialog({'purpose': "block",
                     'name': "lvol",
                     'size': 48})
        b.wait_text(self.card_row_col("LVM2 logical volumes", 1, 1), "lvol")

        self.click_card_row("LVM2 logical volumes", 1)
        self.click_card_dropdown("Unformatted data", "Format")
        self.dialog({"type": "ext4",
                     "crypto": self.default_crypto_type,
                     "name": "FS",
                     "passphrase": "einszweidrei",
                     "passphrase2": "einszweidrei",
                     "mount_point": mount_point_1,
                     "crypto_options": "my-crypto-tag"},
                    secondary=True)
        self.assert_in_configuration("/dev/TEST/lvol", "crypttab", "options", "my-crypto-tag")
        self.assert_in_child_configuration("/dev/TEST/lvol", "fstab", "dir", mount_point_1)
        self.assert_in_lvol_child_configuration("lvol", "crypttab", "options", "my-crypto-tag")
        self.assert_in_lvol_child_configuration("lvol", "fstab", "dir", mount_point_1)
        b.click(self.card_parent_link())
        b.wait_text(self.card_row_col("LVM2 logical volumes", 1, 2), "Filesystem (encrypted)")

        # Now the filesystem is hidden because the LUKS device is
        # locked.  Doubly hide it by deactivating /dev/TEST/lvol
        self.click_dropdown(self.card_row("LVM2 logical volumes", 1), "Deactivate")
        self.confirm()
        b.wait_text(self.card_row_col("LVM2 logical volumes", 1, 2), "Inactive logical volume")

        # Deleting the volume group should still remove the fstab entry
        self.click_card_dropdown("LVM2 volume group", "Delete")
        self.confirm()
        b.wait_visible(self.card("Storage"))
        self.assertEqual(m.execute(f"grep {mount_point_1} /etc/fstab || true"), "")
        self.assertEqual(m.execute(f"grep {'my-crypto-tag'} /etc/crypttab || true"), "")


@testlib.nondestructive
class TestStorageHidden(storagelib.StorageCase):

    def testHiddenRaid(self):
        m = self.machine
        b = self.browser

        mount_point_2 = f"{self.mnt_dir}/mount2"

        self.login_and_go("/storage")

        disk1 = self.add_loopback_disk()
        disk2 = self.add_loopback_disk()
        b.wait_visible(self.card_row("Storage", name=disk1))
        b.wait_visible(self.card_row("Storage", name=disk2))

        self.dialog_with_retry(trigger=lambda: self.click_dropdown(self.card_header("Storage"),
                                                                   "Create MDRAID device"),
                               expect=lambda: (self.dialog_is_present('disks', disk1) and
                                               self.dialog_is_present('disks', disk2)),
                               values={"name": "ARR",
                                       "disks": {disk1: True,
                                                 disk2: True}})

        self.click_card_row("Storage", name="/dev/md/ARR")
        b.wait_visible(self.card("MDRAID device"))
        self.click_card_dropdown("Unformatted data", "Format")
        self.dialog({"type": "ext4",
                     "name": "FS2",
                     "mount_point": mount_point_2})
        self.assert_in_configuration("/dev/md127", "fstab", "dir", mount_point_2)
        b.wait_visible(self.card("ext4 filesystem"))

        # we need to wait for mdadm --monitor to stop using the device before delete
        m.execute("while fuser -s /dev/md127; do sleep 0.2; done", timeout=20)

        self.click_card_dropdown("MDRAID device", "Delete")
        self.confirm()
        b.wait_visible(self.card("Storage"))
        self.assertEqual(m.execute(f"grep {mount_point_2} /etc/fstab || true"), "")

    @testlib.onlyImage("Only test snaps on Ubuntu", "ubuntu*")
    def testHiddenSnap(self):
        m = self.machine
        b = self.browser

        self.login_and_go("/storage")

        # We expect there to be at least one loop back device mounted
        # somewhere below /snap.
        snap_loops = []
        devices = storagelib.json.loads(m.execute("lsblk --json --list --output NAME,TYPE,MOUNTPOINT"))
        for d in devices["blockdevices"]:
            if d["type"] == "loop" and d["mountpoint"].startswith("/snap/"):
                snap_loops.append(d["name"])
        self.assertGreater(len(snap_loops), 0)

        # Make one more loopback device that we expect to see in the UI.
        dev = self.add_loopback_disk()

        # Now we wait until the regular loopback device is shown.  The
        # snaps should not be shown.
        b.wait_visible(self.card_row("Storage", name=dev))
        for sl in snap_loops:
            b.wait_not_present(self.card_row("Storage", name=sl))


if __name__ == '__main__':
    testlib.test_main()
