#!/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 TestStorage(StorageCase):

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

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

        # A loopback device ends with a digit and partitions have
        # names like "/dev/loop0p1".  Check that the storage stack has
        # no difficulties with that.
        #
        # We are especially careful to use a device name that doesn't
        # end in all zeros, because that would be too easy and
        # wouldn't trigger this bug:
        #
        # https://github.com/storaged-project/storaged/issues/97

        dev = m.execute("set -e; dd if=/dev/zero of=/var/tmp/disk1 bs=1M count=10; "
                        "losetup --show loop12 /var/tmp/disk1; "
                        "rm /var/tmp/disk1").strip()
        self.addCleanup(m.execute, "losetup -d " + dev)

        b.wait_present('tr:contains("%s")' % dev)
        b.click('tr:contains("%s")' % dev)
        b.wait_present('#storage-detail')

        b.click('button:contains(Create Partition Table)')
        self.dialog({"type": "gpt"})
        self.content_row_wait_in_col(1, 1, "Free Space")

        self.content_row_action(1, "Create Partition")
        self.dialog({"type": "ext4",
                     "mount_point": "/foo",
                     "mount_options.auto": False})
        self.content_row_wait_in_col(1, 1, "ext4 File System")

        self.content_dropdown_action(1, "Delete")
        self.confirm()
        self.content_row_wait_in_col(1, 1, "Free Space")

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

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

        disk = self.add_ram_disk()
        b.click('#drives tr:contains("%s")' % disk)
        b.wait_present('#storage-detail')

        b.click('button:contains(Create Partition Table)')
        self.dialog({"type": "gpt"})
        self.content_row_wait_in_col(1, 1, "Free Space")

        self.content_row_action(1, "Create Partition")
        self.dialog_wait_open()
        self.dialog_set_val("type", "empty")

        slider = self.dialog_field("size") + " .slider"

        # Move the slider one pixel past the middle, this should give a fractional size.
        # See https://github.com/cockpit-project/cockpit/pull/10968 for more about this.
        width = b.call_js_func('(function (sel) { return ph_find(sel).offsetWidth; })', slider)
        about_half_way = width / 2 + 1

        b.mouse(slider, "mousedown", about_half_way, 0)
        b.mouse(slider, "mouseup", about_half_way, 0)
        self.dialog_wait_val("size", 25.1)

        self.dialog_apply()
        self.dialog_wait_close()

        # With old Udisks2 versions, the partition will end up being
        # 25.1M while newer versions give us 26M.
        wait(lambda: m.execute("lsblk -no SIZE %s1" % disk).strip() in ["25.1M", "26M"])


if __name__ == '__main__':
    test_main()
