#!/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 <http://www.gnu.org/licenses/>.

import storagelib
import testlib


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

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

        self.login_and_go("/storage")

        # The following block devices should not be offered for creating a
        # raid
        #
        # - filesystems
        # - extended partition containers
        # - partition tables
        #
        # So we partition a disk with two logical partitions, one of which
        # has a filesystem on it.

        disk1 = self.add_ram_disk()
        disk2 = self.add_loopback_disk()
        b.wait_visible(self.card_row("Storage", name=disk1))
        b.wait_visible(self.card_row("Storage", name=disk2))
        script = """mktable msdos \
mkpart extended 1 50 \
mkpart logical ext2 2 24 \
mkpart logical ext2 24 48"""
        m.execute(f"parted -s {disk1} {script}")
        m.execute("udevadm settle")
        m.execute(f"mke2fs -q -L TEST {disk1}5")

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

        def check_free_block_devices():
            blocks = b.eval_js("ph_texts('#dialog [data-field=\"disks\"] .select-space-details')")
            print("blocks", blocks)
            # On Ubuntu we see /dev/ram devices as well
            # On Fedora also /dev/zram0 - see https://github.com/cockpit-project/cockpit/issues/14516
            allowed = ["/dev/sda", "/dev/loop", "/dev/vda", "/dev/ram", "/dev/zram"]
            for block in blocks:
                for allow in allowed:
                    if allow in block:
                        break
                else:
                    return False

            # Require these two to be present
            return f"{disk1}6" in blocks and disk2 in blocks

        self.dialog_with_retry(trigger=lambda: self.click_dropdown(self.card_header("Storage"),
                                                                   "Create MDRAID device"),
                               expect=check_free_block_devices,
                               values=None)


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