#!/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 testBridge(self):
        b = self.browser
        m = self.machine

        self.login_and_go("/network")

        # These are two independent networks. Thus there is no loop between the
        # bridge that all VMs are connected to, and the bridge we are creating here.
        iface1 = self.add_iface()
        iface2 = self.add_iface(activate=False)
        self.wait_for_iface(iface1)
        self.wait_for_iface(iface2, active=False)

        # Bridge them
        b.click("button:contains('Add Bridge')")
        b.wait_popup("network-bridge-settings-dialog")
        b.set_val("#network-bridge-settings-name-input", "tbridge")
        b.set_checked("input[data-iface='%s']" % iface1, True)
        b.set_checked("input[data-iface='%s']" % iface2, True)
        b.click("#network-bridge-settings-dialog button:contains('Apply')")
        b.wait_popdown("network-bridge-settings-dialog")
        b.wait_present("#networking-interfaces tr[data-interface='tbridge']")

        # Check that the configuration file has the expected sane name
        # on systems that use "network-scripts".
        if "ubuntu" not in m.image and "debian" not in m.image:
            m.execute("! test -d /etc/sysconfig || test -f /etc/sysconfig/network-scripts/ifcfg-tbridge")

        # Delete the bridge
        b.click("#networking-interfaces tr[data-interface='tbridge'] td:first-child")
        b.click("#network-interface button:contains('Delete')")
        b.wait_visible("#networking")
        b.wait_not_present("#networking-interfaces tr[data-interface='tbridge']")

        # Check that the former members are displayed and both On
        # Fixed in PR #12400
        if m.image not in ["rhel-8-1-distropkg"]:
            self.wait_for_iface(iface1)
            self.wait_for_iface(iface2)

    def testBridgeActive(self):
        b = self.browser

        self.login_and_go("/network")

        iface = self.add_iface()
        self.wait_for_iface(iface)
        ip = b.text("#networking-interfaces tr[data-interface='%s'] td:nth-child(2)" % iface)

        # Put an active interface into a bridge.  The bridge should
        # get the same IP as the active interface.

        b.click("button:contains('Add Bridge')")
        b.wait_popup("network-bridge-settings-dialog")
        b.set_val("#network-bridge-settings-name-input", "tbridge")
        b.set_checked("input[data-iface='%s']" % iface, True)
        b.click("#network-bridge-settings-dialog button:contains('Apply')")
        b.wait_popdown("network-bridge-settings-dialog")

        # Check that it has the interface enslaved and the right IP address
        b.click("#networking-interfaces tr[data-interface='tbridge'] td:first-child")
        b.wait_visible("#network-interface")
        b.wait_present("#network-interface-slaves tr[data-interface='%s']" % iface)
        b.wait_in_text("#network-interface .panel:contains('tbridge')", ip)


if __name__ == '__main__':
    test_main()
