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

#
# Copyright (C) 2015 Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later


import storagelib
import testlib


@testlib.skipImage("No support for multipath", "debian-*", "ubuntu-*", "arch")
@testlib.skipImage("multipath support not installed", "*-bootc")
class TestStorageMultipath(storagelib.StorageCase):

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

        def check_free_block_devices(expected):
            blocks = list(b.eval_js("ph_texts('#dialog [data-field=\"disks\"] .select-space-details')"))
            self.assertEqual(len(blocks), len(expected))
            for i in range(len(expected)):
                self.assertIn(expected[i], blocks[i])

        # At least on Fedora 27, multipath only looks at SCSI_IDENT_
        # and ID_WWN properties, so we install a custom udev rule to
        # set ID_WWN to something that can identify multipathed devices.
        #
        m.write("/etc/udev/rules.d/99-fake-wwn.rules", 'SUBSYSTEM=="block", ENV{ID_WWN}="$env{ID_SCSI_SERIAL}"\n')
        m.execute("udevadm control --reload")
        m.execute("udevadm trigger")

        self.login_and_go("/storage")

        b.eval_js("""
          ph_texts = function (sel) {
            return ph_select(sel).map(function (e) { return e.textContent });
          }""")

        # Add a disk
        info1 = m.add_disk("10M", serial="MYSERIAL")
        dev1 = "/dev/" + info1["dev"]

        self.click_card_row("Storage", name=dev1)
        b.wait_text(self.card_desc("Hard Disk Drive", "Device file"), dev1)

        # Add another disk with the same serial, which fools
        # multipathd into treating it as another path to the first
        # disk.  Since we never actually write to it, this is fine.

        # The primary device file should disappear and multipathed
        # devices should be listed.
        info2 = m.add_disk("10M", serial="MYSERIAL")
        dev2 = "/dev/" + info2["dev"]

        b.wait_text(self.card_desc("Hard Disk Drive", "Device file"), "-")
        b.wait_in_text(self.card_desc("Hard Disk Drive", "Multipathed devices"), dev1)
        b.wait_in_text(self.card_desc("Hard Disk Drive", "Multipathed devices"), dev2)

        # Check that only one is offered as a free block device
        b.go("#/")
        self.click_dropdown(self.card_header("Storage"), "Create LVM2 volume group")
        self.dialog_wait_open()
        check_free_block_devices(["/dev/sda"])
        self.dialog_cancel()
        self.dialog_wait_close()

        # Switch on multipathd.  A primary device should appear.

        b.wait_visible(".pf-m-danger:contains('There are devices with multiple paths on the system, but')")
        b.click("button:contains('Start multipath')")
        b.wait_not_present(".pf-m-danger:contains('There are devices with multiple paths on the system, but')")
        b.wait_visible(self.card_row("Storage", name="/dev/mapper/mpatha"))

        # Check that (exactly) the primary device is offered as free
        self.click_dropdown(self.card_header("Storage"), "Create LVM2 volume group")
        self.dialog_wait_open()
        check_free_block_devices(["/dev/mapper/mpatha"])
        self.dialog_cancel()
        self.dialog_wait_close()


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