#!/usr/bin/python2
# -*- coding: utf-8 -*-

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

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

        self.login_and_go("/storage")
        b.wait_in_text("#drives", "VirtIO")

        # 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.

        m.add_disk("50M", serial="DISK1")
        m.add_disk("50M", serial="DISK2")
        b.wait_in_text("#drives", "DISK1")
        b.wait_in_text("#drives", "DISK2")
        script = """mktable msdos \
mkpart extended 1 50 \
mkpart logical ext2 2 24 \
mkpart logical ext2 24 48"""
        m.execute("parted -s /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_DISK1 " + script)
        m.execute("udevadm settle")
        m.execute("mke2fs -q -L TEST /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_DISK1-part5")

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

        # Keep opening the dialog until exactly /dev/sda6 and /dev/sdb
        # are offered as free block devices.

        def check_free_block_devices():
            blocks = b.eval_js("ph_texts('#dialog [data-field=\"disks\"] .checkbox')")
            # On Ubuntu we see /dev/ram devices here as well
            blocks = [ block for block in blocks if not "/dev/ram" in block ]
            return len(blocks) == 2 and "/dev/sda6" in blocks[0] and "/dev/sdb" in blocks[1]

        self.dialog_with_retry(trigger = lambda: b.click('#create-mdraid'),
                               expect = check_free_block_devices,
                               values = None)

if __name__ == '__main__':
    test_main()
