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

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

class TestNetworking(NetworkCase):
    provision = {
        "machine1": { },
        "machine2": { "image": "fedora-26", "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.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.click("#network-ip-settings-dialog .btn.dropdown-toggle")
        b.click("#network-ip-settings-dialog a:contains('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 span", "")
        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 = self.iface_con_id(iface)

        # Disconnect
        #
        b.wait_present(".panel-heading:contains('%s') .btn.active:contains('On')" % iface)
        b.click(".panel-heading:contains('%s') .btn:contains('Off')" % 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")
        b.wait_text("#network-ip-settings-dialog [data-field='dns'] .btn.active", "On")
        b.click("#network-ip-settings-dialog [data-field='dns'] .btn:contains('Off')")
        # The "DNS Search Domains" setting should follow suit
        b.wait_text("#network-ip-settings-dialog [data-field='dns_search'] .btn.active", "Off")
        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))

if __name__ == '__main__':
    test_main()
