#!/usr/bin/python3

# 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 parent
from storagelib import *
from testlib import *


@nondestructive
class TestStorageBasic(StorageCase):

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

        self.allow_authorize_journal_messages()
        self.login_and_go("/storage", superuser=False)

        b.wait_visible("#devices")
        b.wait_not_present("#devices [data-toggle=dropdown]")

        b.relogin('/storage', superuser=True)

        self.browser.wait_visible("#devices [data-toggle=dropdown]")

        # Add a disk, partition it, format it, and finally remove it.
        disk = self.add_ram_disk()

        b.wait_in_text("#drives", disk)
        b.click('tr:contains("%s")' % disk)
        b.wait_visible('#storage-detail')
        self.content_row_wait_in_col(1, 1, "Unrecognized data")

        def info_field_value(name):
            return b.text('#detail-header dt:contains("%s") + dd' % name)

        self.assertEqual(self.inode(info_field_value("Device file")), self.inode(disk))

        m.execute('parted -s %s mktable gpt' % disk)
        m.execute('parted -s %s mkpart primary ext2 1M 8M' % disk)
        self.content_row_wait_in_col(1, 1, "Unrecognized data")
        self.content_tab_wait_in_info(1, 1, "Name", "primary")

        # create file system on the first partition
        # HACK - the block device might disappear briefly when udevd does its BLKRRPART.
        wait(lambda: m.execute('mke2fs %s1' % disk), delay=1, tries=5)
        self.content_row_wait_in_col(1, 1, "ext2 file system")

        b.go("#/")
        b.wait_visible('#storage')
        b.wait_in_text("#drives", disk)
        self.force_remove_disk(disk)
        b.wait_not_in_text("#drives", disk)


if __name__ == '__main__':
    test_main()
