#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv)

#
# Copyright (C) 2013 Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later


import netlib
import testlib


@testlib.nondestructive
class TestNetworkingOther(netlib.NetworkCase):
    def testOther(self):
        b = self.browser
        m = self.machine

        iface = "tun0"

        # Create a tun device and let NetworkManager manage it
        m.execute(f"ip tuntap add mode tun dev {iface}")
        self.addCleanup(m.execute, f"ip link del dev {iface}")
        testlib.wait(lambda: m.execute(f'nmcli device | grep {iface} | grep -v unavailable'))
        m.execute(f"nmcli dev set {iface} managed yes")

        self.login_and_go("/network")
        self.wait_for_iface(iface, active=False)
        self.select_iface(iface)
        b.wait_visible("#network-interface")

        # Configure a manual IP address
        #
        self.configure_iface_setting('IPv4')
        b.wait_visible("#network-ip-settings-dialog")
        b.select_from_dropdown("#network-ip-settings-dialog select", "manual")
        b.set_input_text('#network-ip-settings-address-0', "1.2.3.4")
        b.set_input_text('#network-ip-settings-netmask-0', "24")
        b.click("#network-ip-settings-dialog button:contains('Save')")
        self.addCleanup(m.execute, "nmcli con del tun0")
        b.wait_not_present("#network-ip-settings-dialog")
        self.wait_for_iface_setting('Status', '1.2.3.4/24')


if __name__ == '__main__':
    testlib.test_main()
