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

    def testDosParts(self):
        b = self.browser

        self.login_and_go("/storage")

        # Add a disk
        disk = self.add_ram_disk()
        b.click(f'#drives .sidepanel-row:contains("{disk}")')
        b.wait_visible("#storage-detail")

        # Format it with a DOS partition table
        b.click('button:contains(Create partition table)')
        self.dialog({"type": "dos"})
        self.content_row_wait_in_col(1, 0, "Free space", isExpandable=False)

        # Create a primary partition
        self.content_row_action(1, "Create partition")
        self.dialog({"size": 10,
                     "type": "ext4",
                     "mount_point": "/foo",
                     "name": "FIRST"},
                    secondary=True)
        self.content_row_wait_in_col(1, 2, "ext4 filesystem")
        self.content_tab_wait_in_info(1, 2, "Name", "FIRST")

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

        # Create a extended partition to fill the rest of the disk
        self.content_row_action(2, "Create partition", isExpandable=False)
        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()
        self.content_row_wait_in_col(2, 2, "Extended partition")
        self.content_row_wait_in_col(3, 1, "Free space", isExpandable=False)

        # Create logical partitions and check that "dos-extended" is
        # not offered.
        self.content_row_action(3, "Create partition", isExpandable=False)
        self.dialog_wait_open()
        b.wait_not_present("select option[value='dos-extended']")
        self.dialog_cancel()
        self.dialog_wait_close()

        # Delete it

        self.content_dropdown_action(2, "Delete")
        self.confirm()

        self.content_row_wait_in_col(2, 1, "Free space", isExpandable=False)


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