#!/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 <https://www.gnu.org/licenses/>.

import testlib


@testlib.skipBeiboot("host switching disabled in beiboot mode")
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-10", "memory_mb": 512}
    }

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

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

        # 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()

        dev_m.start_cockpit()

        # dev → stock: direct login
        dev_b.open("/=10.111.113.5")
        dev_b.try_login()
        dev_b.wait_in_text("#hostkey-message-1", "10.111.113.5")
        dev_b.click('#login-button')
        dev_b.wait_in_text("#hosts-sel", "admin@stock")
        dev_b.enter_page("/system")
        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")
        dev_b.logout()

        # dev → stock: via shell Add host
        dev_b.login_and_go()
        dev_b.add_machine("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")
        dev_b.logout()

        dev_m.stop_cockpit()
        dev_b.kill()

        # stock → dev: direct login
        stock_m.start_cockpit()
        stock_b = self.new_browser(stock_m)
        stock_b.open("/=10.111.113.1")
        stock_b.try_login()
        stock_b.wait_in_text("#hostkey-message-1", "10.111.113.1")
        stock_b.click('#login-button')
        dev_hostname = dev_m.execute("hostname").strip()
        stock_b.wait_in_text("#hosts-sel", f"admin@{dev_hostname}")
        stock_b.enter_page("/system")
        stock_b.wait_in_text(".ct-overview-header-hostname", dev_hostname)
        stock_b.logout()

        # stock → dev: via shell Add host
        stock_b.login_and_go()
        stock_b.add_machine("10.111.113.1")
        stock_b.wait_in_text(".ct-overview-header-hostname", dev_hostname)


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