#!/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 *

@skipImage("UDisks doesn't have support for LVM", "debian-stable", "debian-testing", "ubuntu-1604", "ubuntu-stable")
class TestStorage(StorageCase):
    def checkResize(self, fsys, can_shrink, can_grow, shrink_needs_unmount = None, grow_needs_unmount = None):
        m = self.machine
        b = self.browser

        if fsys.startswith("luks+"):
            fsys_row = 2
            fsys_tab = 1
        else:
            fsys_row = 1
            fsys_tab = 2

        self.login_and_go("/storage")
        m.add_disk("500M", serial="DISK1")
        b.wait_in_text("#drives", "DISK1")

        m.execute("vgcreate TEST /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_DISK1")
        m.execute("lvcreate TEST -n vol -L 200m")
        b.wait_in_text("#vgroups", "TEST")
        b.click('#vgroups tr:contains("TEST")')
        b.wait_visible("#storage-detail")
        self.content_row_wait_in_col(1, 2, "/dev/TEST/vol")

        self.content_tab_action(1, 2, "Format")
        self.dialog({ "name": "FSYS", "type": fsys,
                      "passphrase": "vainu-reku-toma-rolle-kaja",
                      "passphrase2": "vainu-reku-toma-rolle-kaja" })
        self.content_tab_wait_in_info(fsys_row, fsys_tab, "Name", "FSYS")

        mountpoint = self.mount_root + "/admin/FSYS"
        self.content_tab_action(fsys_row, fsys_tab, "Mount")
        self.content_tab_wait_in_info(fsys_row, fsys_tab, "Mounted At", mountpoint)

        if can_grow:
            self.content_tab_action(1, 1, "Grow")
            if grow_needs_unmount:
                self.dialog_wait_open()
                b.wait_in_text("#dialog", "Proceeding will unmount all filesystems on it.")
                self.dialog_set_val("size", 400)
                self.dialog_apply()
                self.dialog_wait_close()
                self.content_tab_action(fsys_row, fsys_tab, "Mount")
                self.content_tab_wait_in_info(fsys_row, fsys_tab, "Mounted At", mountpoint)
            else:
                self.dialog({ "size": 400 })
            size = int(m.execute("df -k --output=size %s | tail -1" % mountpoint).strip())
            assert (size > 300000)
        else:
            self.wait_content_tab_action_disabled(1, 1, "Grow")

        if can_shrink:
            self.content_tab_action(1, 1, "Shrink")
            if shrink_needs_unmount:
                self.dialog_wait_open()
                b.wait_in_text("#dialog", "Proceeding will unmount all filesystems on it.")
                self.dialog_set_val("size", 200)
                self.dialog_apply()
                self.dialog_wait_close()
                self.content_tab_action(fsys_row, fsys_tab, "Mount")
                self.content_tab_wait_in_info(fsys_row, fsys_tab, "Mounted At", mountpoint)
            else:
                self.dialog({ "size": 200 })
            size = int(m.execute("df -k --output=size %s | tail -1" % mountpoint).strip())
            assert (size < 300000)
        else:
            self.wait_content_tab_action_disabled(1, 1, "Shrink")

    def testResizeExt4(self):
        self.checkResize("ext4",
                         can_shrink=True, shrink_needs_unmount=True,
                         can_grow=True, grow_needs_unmount=False)

    def testResizeXfs(self):
        self.checkResize("xfs",
                         can_shrink=False,
                         can_grow=True, grow_needs_unmount=False)

    @skipImage("No NTFS support installed", "centos-7", "rhel-7-5", "rhel-7-4", "rhel-7")
    def testResizeNtfs(self):
        if self.machine.image in [ "fedora-26" ]:
            self.checkResize("ntfs",
                             can_shrink=False,
                             can_grow=False)
        else:
            self.checkResize("ntfs",
                             can_shrink=True, shrink_needs_unmount=True,
                             can_grow=True, grow_needs_unmount=True)

    def testResizeLuks(self):
        # No version of UDisks supports resizing encrypted block
        # devices yet.  Once we get such a version, the buttons will
        # automatically be enabled and this test needs to be adapted.
        self.checkResize("luks+ext4",
                         can_shrink=False,
                         can_grow=False)

if __name__ == '__main__':
    test_main()
