#!/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.nondestructive
class TestStorageRaid1(storagelib.StorageCase):

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

        self.login_and_go("/storage")

        # Create two disks and make a RAID out of them
        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.addCleanup(m.execute, "mdadm --manage --stop /dev/md/SOMERAID")
        self.click_dropdown(self.card_header("Storage"), "Create MDRAID device")
        self.dialog_wait_open()
        # No swap block devices should show up
        b.wait_not_in_text("#dialog .pf-v6-c-data-list", "zram")
        self.dialog_set_val("level", "raid1")
        self.dialog_set_val("disks", {disk1: True, disk2: True})
        self.dialog_set_val("name", "SOMERAID")
        # The dialog should make sure that the Chunk size is ignored (has to be 0 for RAID 1)
        self.dialog_apply()
        self.dialog_wait_close()
        b.wait_visible(self.card_row("Storage", name="/dev/md/SOMERAID"))

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

        self.login_and_go("/storage")

        # Create two disks and make a RAID out of them
        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.addCleanup(m.execute, "if [ -e /dev/md/SOMERAID ]; then mdadm --stop /dev/md/SOMERAID; fi")

        # Create a raid1 with metadata format version 1.0.  Such an
        # array needs double wiping.

        m.execute(f"mdadm --create SOMERAID --run --level=1 --metadata=1.0 --raid-devices=2 {disk1} {disk2}")

        # Put a GPT on it.
        self.click_dropdown(self.card_row("Storage", name="/dev/md/SOMERAID"), "Create partition table")
        self.confirm()
        b.wait_text(self.card_row_col("Storage", row_name="/dev/md/SOMERAID", col_index=3), "GPT partitions")

        # Delete the mdraid device.  Both disks should go back to "Unformatted data"
        self.click_dropdown(self.card_row("Storage", name="/dev/md/SOMERAID"), "Delete")
        self.confirm()
        b.wait_text(self.card_row_col("Storage", row_name=disk1, col_index=3), "Unformatted data")
        b.wait_text(self.card_row_col("Storage", row_name=disk2, col_index=3), "Unformatted data")


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