Skip to content

Commit

Permalink
test: allow add_machine/start_machine_troubleshoot to be re-usable
Browse files Browse the repository at this point in the history
Refactor the tests to re-use start_machine_troubleshoot where possible
and allow it to be used in machines for adding a second host.
  • Loading branch information
jelly authored and martinpitt committed Nov 29, 2023
1 parent 706acbc commit 516cc08
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 78 deletions.
27 changes: 27 additions & 0 deletions test/common/testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,33 @@ def login_and_go(self, path: Optional[str] = None, user: Optional[str] = None, h
with self.browser.wait_timeout(30):
self.browser.login_and_go(path, user=user, host=host, superuser=superuser, urlroot=urlroot, tls=tls)

def start_machine_troubleshoot(self, new=False, known_host=False, password=None, expect_closed_dialog=True, browser=None):
b = browser or self.browser

b.wait_visible("#machine-troubleshoot")
b.click('#machine-troubleshoot')

b.wait_visible('#hosts_setup_server_dialog')
if new:
b.click('#hosts_setup_server_dialog button:contains(Add)')
if not known_host:
b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to")
b.wait_in_text('#hosts_setup_server_dialog', "for the first time.")
b.click("#hosts_setup_server_dialog button:contains('Trust and add host')")
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)')
if expect_closed_dialog:
b.wait_not_present('#hosts_setup_server_dialog')

def add_machine(self, address, known_host=False, password="foobar", browser=None):
b = browser or self.browser
b.switch_to_top()
b.go(f"/@{address}")
self.start_machine_troubleshoot(new=True, known_host=known_host, password=password, browser=browser)
b.enter_page("/system", host=address)

# List of allowed journal messages during tests; these need to match the *entire* message
default_allowed_messages = [
# This is a failed login, which happens every time
Expand Down
22 changes: 11 additions & 11 deletions test/verify/check-shell-host-switching
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class HostSwitcherHelpers:
return true;
})""", address)

def add_machine(self, b, address, known_host=False, pixel_label=None):
def add_new_machine(self, b, address, known_host=False, pixel_label=None):
b.click("button:contains('Add new host')")
b.wait_visible('#hosts_setup_server_dialog')
b.set_input_text('#add-machine-address', address)
Expand Down Expand Up @@ -139,7 +139,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
b.wait_in_text("#nav-hosts .nav-item a", "mydhcpname")
m1.execute("hostnamectl set-hostname 'localhost'")

self.add_machine(b, "10.111.113.2", pixel_label="host-add-dialog")
self.add_new_machine(b, "10.111.113.2", pixel_label="host-add-dialog")
self.wait_host_addresses(b, ["localhost", "10.111.113.2"])
self.connect_and_wait(b, "10.111.113.2")

Expand All @@ -156,7 +156,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):

b.wait_not_present(".nav-item a[href='/@10.111.113.2'] .nav-status")

self.add_machine(b, "10.111.113.3")
self.add_new_machine(b, "10.111.113.3")
self.wait_host_addresses(b, ["localhost", "10.111.113.3", "10.111.113.2"])
self.connect_and_wait(b, "10.111.113.3")

Expand All @@ -173,15 +173,15 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
self.check_discovered_addresses(b, ["10.111.113.2", "10.111.113.3"])

# Add one back, check addresses on both browsers
self.add_machine(b, "10.111.113.2", known_host=True)
self.add_new_machine(b, "10.111.113.2", known_host=True)
self.wait_host_addresses(b, ["localhost", "10.111.113.2"])
self.connect_and_wait(b, "10.111.113.2")
self.check_discovered_addresses(b, ["10.111.113.3"])

b.wait_not_present(".nav-item a[href='/@10.111.113.2'] .nav-status")

# And the second one, check addresses
self.add_machine(b, "10.111.113.3", known_host=True)
self.add_new_machine(b, "10.111.113.3", known_host=True)
self.wait_host_addresses(b, ["localhost", "10.111.113.2", "10.111.113.3"])
self.connect_and_wait(b, "10.111.113.3")
self.check_discovered_addresses(b, [])
Expand Down Expand Up @@ -260,7 +260,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
b2.click("#hosts-sel button")
self.wait_host_addresses(b2, ["localhost"])

self.add_machine(b, "10.111.113.2")
self.add_new_machine(b, "10.111.113.2")
self.wait_host_addresses(b, ["localhost", "10.111.113.2"])
self.wait_host_addresses(b2, ["localhost", "10.111.113.2"])
self.connect_and_wait(b, "10.111.113.2")
Expand All @@ -278,7 +278,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):

b.wait_not_present(".nav-item a[href='/@10.111.113.2'] .nav-status")

self.add_machine(b, "10.111.113.3")
self.add_new_machine(b, "10.111.113.3")
self.wait_host_addresses(b, ["localhost", "10.111.113.3", "10.111.113.2"])
self.wait_host_addresses(b2, ["localhost", "10.111.113.3", "10.111.113.2"])
self.connect_and_wait(b, "10.111.113.3")
Expand All @@ -299,7 +299,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
self.check_discovered_addresses(b2, ["10.111.113.2", "10.111.113.3"])

# Add one back, check addresses on both browsers
self.add_machine(b, "10.111.113.2", known_host=True)
self.add_new_machine(b, "10.111.113.2", known_host=True)
self.wait_host_addresses(b, ["localhost", "10.111.113.2"])
self.wait_host_addresses(b2, ["localhost", "10.111.113.2"])
self.connect_and_wait(b, "10.111.113.2")
Expand All @@ -309,7 +309,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
b.wait_not_present(".nav-item a[href='/@10.111.113.2'] .nav-status")

# And the second one, check addresses on both browsers
self.add_machine(b, "10.111.113.3", known_host=True)
self.add_new_machine(b, "10.111.113.3", known_host=True)
self.wait_host_addresses(b, ["localhost", "10.111.113.2", "10.111.113.3"])
self.wait_host_addresses(b2, ["localhost", "10.111.113.2", "10.111.113.3"])
self.connect_and_wait(b, "10.111.113.3")
Expand Down Expand Up @@ -338,7 +338,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
self.login_and_go(superuser=False)

b.click("#hosts-sel button")
self.add_machine(b, "10.111.113.3")
self.add_new_machine(b, "10.111.113.3")
self.wait_host_addresses(b, ["localhost", "10.111.113.3"])
self.connect_and_wait(b, "10.111.113.3")

Expand Down Expand Up @@ -433,7 +433,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):

# And and connect to a second machine
b.click("#hosts-sel button")
self.add_machine(b, "10.111.113.2")
self.add_new_machine(b, "10.111.113.2")
b.click("a[href='/@10.111.113.2']")
b.wait_visible("iframe.container-frame[name='cockpit1:10.111.113.2/system']")
self.assertIn("admin", m2.execute("loginctl"))
Expand Down
64 changes: 18 additions & 46 deletions test/verify/check-shell-multi-machine
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,12 @@ def check_failed_state(b, expected_title):
b.wait_not_present('#hosts_setup_server_dialog')


def start_machine_troubleshoot(b, new=False, known_host=False, password=None):
b.wait_visible("#machine-troubleshoot")
b.click('#machine-troubleshoot')
b.wait_visible('#hosts_setup_server_dialog')
if new:
b.click('#hosts_setup_server_dialog button:contains(Add)')
if not known_host:
b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to")
b.wait_in_text('#hosts_setup_server_dialog', "for the first time.")
b.click("#hosts_setup_server_dialog button:contains('Trust and add host')")
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)')


def fail_login(b):
b.click('#hosts_setup_server_dialog button:contains(Log in)')
b.wait_visible('#hosts_setup_server_dialog button:contains(Log in):not([disabled])')
b.wait_in_text("#hosts_setup_server_dialog .pf-v5-c-alert", "Login failed")


def add_machine(b, address, known_host=False, password="foobar"):
b.switch_to_top()
b.go(f"/@{address}")
start_machine_troubleshoot(b, new=True, known_host=known_host, password=password)
b.wait_not_present('#hosts_setup_server_dialog')
b.enter_page("/system", host=address)


def kill_user_admin(machine):
machine.execute("loginctl terminate-user admin")

Expand Down Expand Up @@ -167,8 +143,8 @@ class TestMultiMachineAdd(testlib.MachineCase):
hostname_selector = "#system_information_hostname_text"

self.login_and_go(None)
add_machine(b, "10.111.113.2", password=None)
add_machine(b, m3_host, password=None)
self.add_machine("10.111.113.2", password=None)
self.add_machine(m3_host, password=None)

b.switch_to_top()
b.click("#hosts-sel button")
Expand Down Expand Up @@ -224,8 +200,8 @@ class TestMultiMachineAdd(testlib.MachineCase):
m.write("/etc/ssh/ssh_config", "Host m3\n\tHostName 10.111.113.3\n\tPort 2222\n", append=True)

self.login_and_go(None)
add_machine(b, "m2", password=None)
add_machine(b, "m3", password=None)
self.add_machine("m2", password=None)
self.add_machine("m3", password=None)

b.switch_to_top()
b.click("#hosts-sel button")
Expand Down Expand Up @@ -385,7 +361,7 @@ class TestMultiMachine(testlib.MachineCase):
b.wait_js_cond('window.location.pathname == "/cockpit-new/system"')

# Test 2nd machine
add_machine(b, "10.111.113.2")
self.add_machine("10.111.113.2")
b.enter_page("/system", host="10.111.113.2")
b.wait_text(hostname_selector, "machine2")
b.switch_to_top()
Expand Down Expand Up @@ -438,7 +414,7 @@ class TestMultiMachine(testlib.MachineCase):
mount -o bind /tmp/"$(basename $FILENAME)" $FILENAME""")

self.login_and_go("/system")
add_machine(b, "10.111.113.2")
self.add_machine("10.111.113.2")

b.leave_page()
b.go("/@10.111.113.2/system/terminal")
Expand All @@ -461,7 +437,7 @@ class TestMultiMachine(testlib.MachineCase):

# Add a machine
self.login_and_go(None)
add_machine(b, "10.111.113.2")
self.add_machine("10.111.113.2")

# Go to the path, remove the image
b.go(m2_path)
Expand Down Expand Up @@ -505,12 +481,7 @@ class TestMultiMachine(testlib.MachineCase):
b.go(m2_path)
with b.wait_timeout(30):
b.wait_text(".curtains-ct h1", "Not connected to host")
b.click("#machine-troubleshoot")
b.wait_visible('#hosts_setup_server_dialog')
b.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
b.set_input_text('#login-custom-password', "foobar")
b.click('#hosts_setup_server_dialog button:contains(Log in)')
b.wait_not_present('#hosts_setup_server_dialog')
self.start_machine_troubleshoot(password="foobar")

b.enter_page("/playground/test", "10.111.113.2", reconnect=True)
# image is back because it page was reloaded after disconnection
Expand Down Expand Up @@ -547,7 +518,7 @@ class TestMultiMachine(testlib.MachineCase):

# Add a machine
self.login_and_go(None)
add_machine(b, "10.111.113.2")
self.add_machine("10.111.113.2")

b.switch_to_top()
b.go(m2_path)
Expand Down Expand Up @@ -599,7 +570,7 @@ class TestMultiMachine(testlib.MachineCase):

# Bad hostkey
break_hostkey(m1, "10.111.113.2")
start_machine_troubleshoot(b, new=True, known_host=True)
self.start_machine_troubleshoot(new=True, known_host=True, expect_closed_dialog=False)
b.wait_in_text('#hosts_setup_server_dialog', "10.111.113.2 key changed")
b.click("#hosts_setup_server_dialog button:contains(Cancel)")
b.wait_not_present('#hosts_setup_server_dialog')
Expand All @@ -609,21 +580,21 @@ class TestMultiMachine(testlib.MachineCase):
m1.execute("mkdir -p /home/admin/.ssh/")
break_hostkey(m1, "10.111.113.2")

start_machine_troubleshoot(b, new=True, known_host=True)
self.start_machine_troubleshoot(new=True, known_host=True, expect_closed_dialog=False)
b.wait_in_text('#hosts_setup_server_dialog', "10.111.113.2 key changed")
b.click("#hosts_setup_server_dialog button:contains(Cancel)")
fix_hostkey(m1)

# Bad cockpit
break_bridge(m2)
start_machine_troubleshoot(b, new=True, password="foobar")
self.start_machine_troubleshoot(new=True, password="foobar", expect_closed_dialog=False)
check_failed_state(b, "Cockpit is not installed")
fix_bridge(m2)

# Troubleshoot existing
# Properly add machine
fix_hostkey(m1)
add_machine(b, "10.111.113.2")
self.add_machine("10.111.113.2")
b.logout()
b.wait_visible("#login")

Expand All @@ -632,15 +603,15 @@ class TestMultiMachine(testlib.MachineCase):
self.login_and_go(None)
b.go(machine_path)
with b.wait_timeout(240):
start_machine_troubleshoot(b, password="foobar")
self.start_machine_troubleshoot(password="foobar", expect_closed_dialog=False)

check_failed_state(b, "Cockpit is not installed")
b.wait_visible("#machine-troubleshoot")
fix_bridge(m2)

# Clear host key
fix_hostkey(m1)
start_machine_troubleshoot(b)
self.start_machine_troubleshoot(expect_closed_dialog=False)
b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to 10.111.113.2 for the first time.")

# show fingerprint validation
Expand Down Expand Up @@ -676,7 +647,7 @@ class TestMultiMachine(testlib.MachineCase):

with b.wait_timeout(120):
b.wait_visible("#machine-troubleshoot")
start_machine_troubleshoot(b)
self.start_machine_troubleshoot(expect_closed_dialog=False)
b.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
b.set_input_text('#login-custom-password', "")
fail_login(b)
Expand All @@ -701,7 +672,7 @@ class TestMultiMachine(testlib.MachineCase):
b.go(machine_path)
with b.wait_timeout(120):
b.wait_visible("#machine-troubleshoot")
start_machine_troubleshoot(b)
self.start_machine_troubleshoot(expect_closed_dialog=False)
b.wait_in_text('#hosts_setup_server_dialog h1', "Could not contact")
b.set_input_text("#edit-machine-port", "2222")
b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
Expand Down Expand Up @@ -747,6 +718,7 @@ class TestMultiMachine(testlib.MachineCase):

self.login_and_go(None)
b.go("/@10.111.113.2")
self.start_machine_troubleshoot(expect_closed_dialog=False)
b.wait_visible("#machine-troubleshoot")
b.click('#machine-troubleshoot')
b.wait_visible('#hosts_setup_server_dialog')
Expand Down
23 changes: 2 additions & 21 deletions test/verify/check-system-shutdown-restart
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,7 @@ class TestShutdownRestart(testlib.MachineCase):
b2.login_and_go("/system")

# Add machine with static IP to m2
b2.switch_to_top()
b2.go("/@10.111.113.1")
with b2.wait_timeout(30):
b2.wait_visible("#machine-troubleshoot")
b2.click('#machine-troubleshoot')
b2.wait_visible('#hosts_setup_server_dialog')
b2.wait_text(f'#hosts_setup_server_dialog {self.primary_btn_class}', "Add")
b2.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
b2.wait_in_text('#hosts_setup_server_dialog', "You are connecting to 10.111.113.1 for the first time.")
b2.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
b2.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
b2.set_input_text("#login-custom-password", "foobar")
b2.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
b2.wait_not_present('#hosts_setup_server_dialog')
b2.enter_page("/system", host="10.111.113.1")
self.add_machine("10.111.113.1", browser=b2)
b2.wait_text("#system_information_hostname_text", "machine1")

# Check auto reconnect on reboot
Expand All @@ -105,12 +91,7 @@ class TestShutdownRestart(testlib.MachineCase):

with b2.wait_timeout(30):
b2.wait_visible("#machine-troubleshoot")
b2.click('#machine-troubleshoot')
b2.wait_visible('#hosts_setup_server_dialog')
b2.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
b2.set_input_text("#login-custom-password", "foobar")
b2.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
b2.wait_not_present('#hosts_setup_server_dialog')
self.start_machine_troubleshoot(password="foobar", browser=b2)

b2.enter_page("/system", host="10.111.113.1", reconnect=False)

Expand Down

0 comments on commit 516cc08

Please sign in to comment.