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

# 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 testlib


def wait_addresses(b, expected):
    b.wait_js_cond(f'ph_select("#nav-hosts .nav-item a").length == {len(expected)}')
    for address in expected:
        b.wait_visible(f"#nav-hosts .nav-item a[href='/@{address}']")


def add_machine(b, address, password):
    b.click("button:contains('Add new host')")
    b.wait_visible('#hosts_setup_server_dialog')
    b.set_input_text('#add-machine-address', address)
    b.click('#hosts_setup_server_dialog .pf-m-primary:contains("Add")')
    b.wait_in_text('#hosts_setup_server_dialog', f"You are connecting to {address} for the first time.")
    b.click('#hosts_setup_server_dialog .pf-m-primary')

    if password:
        b.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
        b.set_input_text('#login-custom-password', password)
        b.click('#hosts_setup_server_dialog button:contains(Log in)')

    b.wait_not_present('#hosts_setup_server_dialog')


class TestRHEL8(testlib.MachineCase):
    provision = {
        "0": {"address": "10.111.113.1/20", "memory_mb": 768},
        "stock": {"address": "10.111.113.5/20", "image": "rhel-8-8", "memory_mb": 512}
    }

    @testlib.todoPybridgeRHEL8()
    def test(self):
        dev_m = self.machine
        dev_b = self.browser

        stock_m = self.machines['stock']
        stock_m.execute("hostnamectl set-hostname stock")

        # Wait for connectivity between the two
        stock_m.execute("ping -q -w5 -c5 10.111.113.1")
        dev_m.execute("ping -q -w5 -c5 10.111.113.5")

        self.allow_hostkey_messages()

        # New machine adding old machine

        self.login_and_go()
        dev_b.click("#hosts-sel button")
        dev_addresses = ["localhost"]
        wait_addresses(dev_b, dev_addresses)

        add_machine(dev_b, "10.111.113.5", password="foobar")
        dev_addresses.append("10.111.113.5")
        wait_addresses(dev_b, dev_addresses)

        dev_b.go("/@10.111.113.5/")
        dev_b.wait_visible("iframe.container-frame[name='cockpit1:10.111.113.5/system'][data-loaded]")
        dev_b.enter_page("/system", host="10.111.113.5")
        dev_b.wait_in_text(".ct-overview-header-hostname", "stock")
        dev_b.wait_in_text(".ct-overview-header-hostname", "running Red Hat Enterprise Linux 8")


class TestMultiOSDirect(testlib.MachineCase):
    provision = {
        "0": {"address": "10.111.113.1/20", "memory_mb": 512},
        "stock": {"address": "10.111.113.5/20", "image": "rhel-8-10", "memory_mb": 512}
    }

    @testlib.todoPybridgeRHEL8()
    def testRHEL8Direct(self):
        b = self.browser

        self.allow_hostkey_messages()

        self.login_and_go()
        b.click("#hosts-sel button")

        dev_addresses = ["localhost"]
        wait_addresses(b, dev_addresses)

        stock_m = self.machines['stock']
        stock_m.execute("hostnamectl set-hostname stock")

        add_machine(b, "10.111.113.5", password="foobar")
        dev_addresses.append("10.111.113.5")
        wait_addresses(b, dev_addresses)
        b.logout()

        # Access stock directly from dev
        b.open("/=10.111.113.5")
        b.try_login(superuser=False)
        b.wait_visible("#hostkey-group")
        b.wait_in_text("#hostkey-message-1", "You are connecting to 10.111.113.5 for the first time.")
        b.click('#login-button')

        b.wait_visible("iframe.container-frame[name='cockpit1:localhost/system'][data-loaded]")
        b.wait_not_visible(".curtains-ct")
        b.wait_visible("iframe.container-frame[name='cockpit1:localhost/system']")
        b.switch_to_frame("cockpit1:localhost/system")
        b.wait_visible("body")
        b.wait_in_text('#system_information_hostname_text', "stock")
        b.switch_to_top()

        b.wait_js_cond('window.location.pathname == "/=10.111.113.5/system"')


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