#!/usr/bin/python3

# This file is part of Cockpit.
#
# Copyright (C) 2013 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 netlib import *
from testlib import *

from machine_core.constants import TEST_OS_DEFAULT


class TestNetworking(NetworkCase):
    provision = {
        "machine1": {},
        "machine2": {"image": TEST_OS_DEFAULT, "address": "10.111.113.2/20", "dhcp": True}
    }

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

        self.login_and_go("/network")

        iface = self.add_iface()
        self.wait_for_iface(iface)
        b.wait_not_visible("#network-interface")

        # Graphs are visible
        el_width = "(function (sel) { return ph_find(sel).offsetWidth; })"
        el_height = "(function (sel) { return ph_find(sel).offsetHeight; })"

        b.wait_visible("#networking-tx-graph")
        self.assertGreater(b.call_js_func(el_width, "#networking-tx-graph"), 200)
        self.assertGreater(b.call_js_func(el_height, "#networking-tx-graph"), 100)

        b.wait_visible("#networking-rx-graph")
        self.assertGreater(b.call_js_func(el_width, "#networking-rx-graph"), 200)
        self.assertGreater(b.call_js_func(el_height, "#networking-rx-graph"), 100)

        # Details of test iface
        b.click("#networking-interfaces tr[data-interface='%s']" % iface)
        b.wait_visible("#network-interface")

        # Configure a manual IP address
        #
        b.click("tr:contains('IPv4') a")
        b.wait_popup("network-ip-settings-dialog")
        b.select_from_dropdown("#network-ip-settings-dialog select", "Manual")

        b.set_val('#network-ip-settings-dialog input[placeholder="Address"]', "1.2.3.4")
        b.set_val('#network-ip-settings-dialog input[placeholder*="Netmask"]', "255.255.0.8")
        b.click("#network-ip-settings-dialog button:contains('Apply')")
        b.wait_text_not("#network-ip-settings-error h4", "")
        b.set_val('#network-ip-settings-dialog input[placeholder*="Netmask"]', "255.255.192.0")
        b.click("#network-ip-settings-dialog button:contains('Apply')")
        b.wait_popdown("network-ip-settings-dialog")
        b.wait_in_text("#network-interface .panel:contains('%s')" % iface, "1.2.3.4/18")

        con_id = wait(lambda: self.iface_con_id(iface))

        # Disconnect
        #
        self.wait_onoff(".panel-heading:contains('%s')" % iface, True)
        self.toggle_onoff(".panel-heading:contains('%s')" % iface)
        b.wait_in_text("tr:contains('Status')", "Inactive")

        # Switch it back to "auto" from the command line and bring it
        # up again
        #
        m.execute("nmcli connection modify '%s' ipv4.method auto" % con_id)
        m.execute("nmcli connection modify '%s' ipv4.addresses ''" % con_id)
        b.wait_in_text("tr:contains('IPv4')", "Automatic (DHCP)")
        m.execute("nmcli connection up '%s'" % con_id)
        b.wait_in_text("tr:contains('Status')", "10.111.")

        # Switch off automatic DNS
        #
        b.click("tr:contains('IPv4') a")
        b.wait_popup("network-ip-settings-dialog")
        self.wait_onoff("#network-ip-settings-dialog [data-field='dns']", True)
        self.toggle_onoff("#network-ip-settings-dialog [data-field='dns']")
        # The "DNS Search Domains" setting should follow suit
        self.wait_onoff("#network-ip-settings-dialog [data-field='dns_search']", False)
        b.click("#network-ip-settings-dialog button:contains('Apply')")
        b.wait_popdown("network-ip-settings-dialog")

        wait(lambda: "yes" in m.execute("nmcli -f ipv4.ignore-auto-dns connection show '%s'" % con_id))

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

        def assert_running():
            b.wait_not_visible("#networking-nm-crashed")
            b.wait_not_visible("#networking-nm-disabled")
            b.wait_visible("#networking-graphs")
            b.wait_visible("#networking-interfaces")
            b.wait_js_cond("ph_in_text('#networking-interfaces', 'eth1') ||"
                           "ph_in_text('#networking-interfaces', 'ens')")

        def assert_stopped(enabled):
            # should hide graphs and actions and show the appropriate notification
            b.wait_not_visible("#networking-graphs")
            b.wait_not_visible("#networking-interfaces")
            if enabled:
                b.wait_visible("#networking-nm-crashed")
                b.wait_not_visible("#networking-nm-disabled")
            else:
                b.wait_not_visible("#networking-nm-crashed")
                b.wait_visible("#networking-nm-disabled")

        self.login_and_go("/network")
        assert_running()

        # Disable NM D-Bus activation; it auto-starts at unpredictable times from background actions,
        # which makes it impossible/meaningless to write a test; copy the full unit file, Alias= cannot
        # be reliably disabled with drop-ins
        m.execute("""systemctl cat NetworkManager.service | sed '/Alias=/d' > /etc/systemd/system/NetworkManager.service
rm -f --verbose /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service
systemctl daemon-reload""")

        # stop/start NM on CLI, page should notice
        m.execute("systemctl stop NetworkManager")
        assert_stopped(True)
        m.execute("systemctl start NetworkManager")
        assert_running()

        # stop NM, test inline start button
        m.execute("systemctl stop NetworkManager")
        assert_stopped(True)
        b.click("#networking-nm-crashed button")
        assert_running()

        # stop NM, test troubleshoot button
        m.execute("systemctl stop NetworkManager")
        assert_stopped(True)
        b.click("#networking-nm-crashed a")
        b.enter_page("/system/services")
        b.wait_text(".service-name", "Network Manager")
        b.click(".service-top-panel .dropdown-kebab-pf button")
        b.click(".service-top-panel .dropdown-menu a:contains('Start')")
        b.wait_in_text("#statuses", "Running")

        # networking page should notice start from Services page
        b.go("/network")
        b.enter_page("/network")
        assert_running()

        # stop and disable NM, enablement info notification
        m.execute("systemctl stop NetworkManager; systemctl disable NetworkManager")
        assert_stopped(False)
        b.click("#networking-nm-disabled button")
        assert_running()
        wait(lambda: m.execute("systemctl is-enabled NetworkManager"))

        self.allow_journal_messages(".*org.freedesktop.NetworkManager.*")


if __name__ == '__main__':
    test_main()
