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


class TestStorage(StorageCase):

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

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

        m.execute("mkdir /home/foo /home/bar")
        m.write("/etc/exports", "/home/foo 127.0.0.0/24(rw)\n/home/bar 127.0.0.0/24(rw)\n")
        m.execute("systemctl restart nfs-server")

        # Nothing there in the beginnging
        b.wait_visible("#nfs-mounts .listing-ct-empty")

        # Add /home/foo
        b.click("#nfs-mounts button .fa-plus")
        self.dialog_wait_open()
        self.dialog_set_val("server", "127.0.0.1")
        self.dialog_set_val("remote", "/home/foo")
        self.dialog_set_val("dir", "/mnt")
        self.dialog_apply()
        self.dialog_wait_close()

        b.wait_present("#nfs-mounts td:contains(/home/foo)")
        b.wait_present("#nfs-mounts td:contains(/mnt)")
        b.wait_text_not("#nfs-mounts tr:contains(/mnt) .pf-c-progress__status", "")

        # Should be saved to fstab
        self.assertEqual(m.execute("grep -w nfs /etc/fstab").strip(),
                         "127.0.0.1:/home/foo /mnt nfs defaults")

        # Add /home/bar
        b.click("#nfs-mounts button .fa-plus")
        self.dialog_wait_open()
        self.dialog_set_val("server", "127.0.0.1")
        self.dialog_set_val("remote", "/home/bar")
        self.dialog_set_val("dir", "/mounts/bar")
        self.dialog_apply()
        self.dialog_wait_close()

        b.wait_present("#nfs-mounts td:contains(/home/bar)")
        b.wait_present("#nfs-mounts td:contains(/mounts/bar)")
        b.wait_text_not("#nfs-mounts tr:contains(/mounts/bar) .pf-c-progress__status", "")
        m.execute("test -d /mounts/bar")

        # Go to details of /home/bar
        b.mousedown("#nfs-mounts tr:contains(/home/bar)")
        b.wait_present('#storage-detail')
        b.wait_text('#detail-header label:contains("Server") + div', "127.0.0.1:/home/bar")
        b.wait_text('#detail-header label:contains("Mount Point") + div', "/mounts/bar")

        # Change mount point of /home/bar
        b.click('#detail-header button:contains("Edit")')
        self.dialog_wait_open()
        self.dialog_set_val("dir", "/mounts/barbar")
        self.dialog_apply()
        self.dialog_wait_close()

        b.wait_text('#detail-header label:contains("Mount Point") + div', "/mounts/barbar")
        m.execute("! test -e /mounts/bar")
        m.execute("test -d /mounts/barbar")
        self.assertEqual(m.execute("findmnt -s -n -o OPTIONS /mounts/barbar").strip(), "defaults")

        # Set options for /home/bar
        b.click('#detail-header button:contains("Edit")')

        def wait_checked(field):
            b.wait_present(self.dialog_field(field) + ":checked")

        def wait_not_checked(field):
            b.wait_present(self.dialog_field(field) + ":not(:checked)")

        self.dialog_wait_open()
        wait_checked("mount_options.auto")
        wait_not_checked("mount_options.ro")
        self.dialog_set_val("mount_options.auto", False)
        self.dialog_set_val("mount_options.ro", True)
        self.dialog_set_val("mount_options.extra", "ac")
        self.dialog_apply()
        self.dialog_wait_close()

        self.assertEqual(m.execute("findmnt -s -n -o OPTIONS /mounts/barbar").strip(), "noauto,ro,ac")

        # Should be saved to fstab
        self.assertEqual(m.execute("grep /barbar /etc/fstab").strip(),
                         "127.0.0.1:/home/bar /mounts/barbar nfs noauto,ro,ac")

        # Go to details of /home/foo
        b.go("#/")
        b.wait_visible("#storage")
        b.mousedown("#nfs-mounts tr:contains(/home/foo)")
        b.wait_present('#storage-detail')
        b.wait_text('#detail-header label:contains("Server") + div', "127.0.0.1:/home/foo")
        b.wait_text('#detail-header label:contains("Mount Point") + div', "/mnt")

        # Unmount and remount /home/foo
        b.click("#detail-header button:contains(Unmount)")
        b.click("#detail-header button:contains(Mount)")
        b.wait_present("#detail-header button:contains(Unmount)")

        # Remove /home/foo
        b.click("#detail-header button:contains(Remove)")
        b.wait_visible("#storage")
        b.wait_not_present("#nfs-mounts td:contains(/home/foo)")
        m.execute("! test -e /mnt")
        # Should be removed from fstab, too
        self.assertNotIn("/home/foo", m.execute("cat /etc/fstab"))

        # Picks up mounts in fstab
        m.execute("echo '1.2.3.4:/something /somewhere nfs defaults 0 0' >> /etc/fstab")
        b.wait_present("#nfs-mounts td:contains(1.2.3.4 /something)")
        b.wait_present("#nfs-mounts td:contains(/somewhere)")
        b.wait_present("#nfs-mounts td:contains(Not mounted)")

        # Ignores non-FS mounts which look similar
        m.execute("echo '2.3.4.5:/marmalade /dunno rfs defaults 0 0' >> /etc/fstab")
        # But recognizes variants like "nfs4"
        m.execute("echo '5.6.7.8:/stuff /four nfs4 defaults 0 0' >> /etc/fstab")
        b.wait_present("#nfs-mounts td:contains(5.6.7.8)")
        b.wait_present("#nfs-mounts td:contains(/four)")
        self.assertFalse(b.is_present("#nfs-mounts td:contains(marmalade)"))

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

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

        m.execute("mkdir /home/foo /home/bar")
        m.write("/etc/exports", "/home/foo 127.0.0.0/24(rw)\n/home/bar 127.0.0.0/24(rw)\n")
        m.execute("systemctl restart nfs-server")

        b.click("#nfs-mounts .fa-plus")
        self.dialog_wait_open()
        self.dialog_set_val("server", "127.0.0.1")

        def wait_for_exports():
            def check():
                choices = self.dialog_combobox_choices("remote")
                return len(choices) == 2 and "/home/foo" in choices and "/home/bar" in choices
            self.retry(None, check, None)

        b.click('#dialog [data-field="remote"] input.rbt-input-main')
        wait_for_exports()

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

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

        m.execute("mkdir /home/foo /home/bar")
        m.write("/etc/exports", "/home/foo 127.0.0.0/24(rw)\n/home/bar 127.0.0.0/24(rw)\n")
        m.execute("systemctl restart nfs-server")

        # Nothing there in the beginnging
        b.wait_visible("#nfs-mounts .listing-ct-empty")

        # Add /home/foo
        b.click("#nfs-mounts .fa-plus")
        self.dialog_wait_open()
        self.dialog_set_val("server", "127.0.0.1")
        self.dialog_set_val("remote", "/home/foo")
        self.dialog_set_val("dir", "/mounts/foo")
        self.dialog_apply()
        self.dialog_wait_close()

        b.wait_present("#nfs-mounts td:contains(/home/foo)")
        b.wait_present("#nfs-mounts td:contains(/mounts/foo)")
        b.wait_text_not("#nfs-mounts tr:contains(/mounts/foo) .pf-c-progress__status", "")

        # Go to details of /home/foo
        b.go("#/")
        b.wait_visible("#storage")
        b.mousedown("#nfs-mounts tr:contains(/home/foo)")
        b.wait_present('#storage-detail')
        b.wait_text('#detail-header label:contains("Server") + div', "127.0.0.1:/home/foo")
        b.wait_text('#detail-header label:contains("Mount Point") + div', "/mounts/foo")

        m.spawn("cd /mounts/foo; sleep infinity", "busy")
        b.click('#detail-header button:contains("Edit")')

        self.dialog_wait_open()
        self.dialog_wait_alert("This NFS mount is in use")
        self.dialog_cancel()
        self.dialog_wait_close()

        b.click("#detail-header button:contains(Unmount)")
        self.dialog_wait_open()
        b.wait_in_text("#dialog", "The filesystem is in use")
        b.wait_in_text("#dialog", "of user root")
        self.dialog_apply()

        b.wait_present("#detail-header button:contains(Mount)")


@skipImage("No udisks/cockpit-storaged on OSTree images", "fedora-coreos")
@nondestructive
class TestStoragePackages(PackageCase, StorageHelpers):

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

        # Override configuration so that we don't have to remove the
        # real package.

        self.machine.write("/usr/share/cockpit/storaged/override.json",
                           """{ "config": { "nfs_client_package": "fake-nfs-utils" } }""")
        self.addCleanup(self.machine.execute, "rm /usr/share/cockpit/storaged/override.json")

        m.execute("mv /sbin/mount.nfs /sbin/mount.nfs.off")
        self.addCleanup(m.execute, "mv /sbin/mount.nfs.off /sbin/mount.nfs")

        self.login_and_go("/storage")

        # The fake-nfs-utils package is not available yet

        b.click("button:contains('Install NFS Support')")

        self.dialog_wait_open()
        b.wait_in_text("#dialog", "fake-nfs-utils is not available from any repository.")
        self.dialog_cancel()
        self.dialog_wait_close()

        # Now make the package available

        self.createPackage("dummy", "1.0", "1")
        self.createPackage("fake-nfs-utils", "1.0", "1", depends="fake-libnfs")
        self.createPackage("fake-libnfs", "1.0", "1")
        self.enableRepo()

        # HACK
        #
        # The first simulated install seems to silently not report
        # anything on the Debian test images, for unknown reasons.  So
        # we install a dummy package to warm up all parts of the
        # machinery and distribute the fluids evenly.
        #
        if "debian" in m.image or "ubuntu" in m.image:
            m.execute("pkcon refresh && pkcon install -y dummy")

        b.reload()
        b.enter_page("/storage")
        b.click("button:contains('Install NFS Support')")
        self.dialog_wait_open()
        b.wait_in_text("#dialog", "fake-nfs-utils will be installed")
        b.wait_in_text("#dialog", "fake-libnfs")
        self.dialog_apply()
        self.dialog_wait_close()

        b.wait_present("#nfs-mounts button .fa-plus")


if __name__ == '__main__':
    test_main()
