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

    def testDosParts(self):
        b = self.browser

        self.login_and_go("/storage")

        # Add a disk
        disk = self.add_ram_disk()
        self.click_card_row("Storage", name=disk)

        # Format it with a DOS partition table
        self.click_card_dropdown("Solid State Drive", "Create partition table")
        self.dialog({"type": "dos"})
        b.wait_text(self.card_row_col("DOS partitions", 1, 1), "Free space")

        # Create a primary partition
        self.click_dropdown(self.card_row("DOS partitions", 1), "Create partition")
        self.dialog({"size": 10,
                     "type": "ext4",
                     "mount_point": f"{self.mnt_dir}/foo",
                     "name": "FIRST"},
                    secondary=True)
        b.wait_text(self.card_row_col("DOS partitions", 1, 2), "ext4 filesystem")

        # Open dialog for formatting the primary partition and check that "dos-extended" is not offered.
        self.click_card_row("DOS partitions", 1)
        self.click_card_dropdown("ext4 filesystem", "Format")
        self.dialog_wait_open()
        b.wait_not_present("select option[value='dos-extended']")
        self.dialog_cancel()
        self.dialog_wait_close()
        b.click(self.card_parent_link())

        # Create a extended partition to fill the rest of the disk
        self.click_dropdown(self.card_row("DOS partitions", 2), "Create partition")
        self.dialog_wait_open()
        self.dialog_set_val("type", "dos-extended")
        self.dialog_wait_not_present("name")
        self.dialog_wait_not_present("mount_point")
        self.dialog_wait_not_present("mount_options")
        self.dialog_apply()
        self.dialog_wait_close()
        b.wait_text(self.card_row_col("DOS partitions", 2, 1), "Extended partition")
        b.wait_text(self.card_row_col("DOS partitions", 3, 1), "Free space")

        # Create logical partitions and check that "dos-extended" is
        # not offered.
        self.click_dropdown(self.card_row("DOS partitions", 3), "Create partition")
        self.dialog_wait_open()
        b.wait_not_present("select option[value='dos-extended']")
        self.dialog_set_val("size", 10)
        self.dialog_set_val("type", "ext4")
        self.dialog_set_val("name", "LOGICAL")
        self.dialog_apply_secondary()
        self.dialog_wait_close()

        # Check that we can navigate to the logical partition. This is
        # interesting since the intervening "Extended partition" row
        # can not be navigated to and should be omitted from the
        # breadcrumb.

        self.click_card_row("DOS partitions", 4)
        b.wait_text(self.card_desc("ext4 filesystem", "Name"), "LOGICAL")
        b.click(self.card_parent_link())

        # Delete it

        self.click_dropdown(self.card_row("DOS partitions", 2), "Delete")
        self.confirm()

        b.wait_text(self.card_row_col("DOS partitions", 2, 1), "Free space")


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