Skip to content

Commit

Permalink
WIP - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mvollmer committed Sep 4, 2024
1 parent bdccd5d commit 5751da0
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 44 deletions.
1 change: 1 addition & 0 deletions pkg/shell/hosts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ export class CockpitHosts extends React.Component {
if (this.state.edit_machine) {
if (this.state.edit_machine == this.props.machine &&
parts.address != this.state.edit_machine.address) {
this.props.loader.connect(parts.address);
this.props.jump(addr);
}
} else if (this.state.switch_machine) {
Expand Down
12 changes: 7 additions & 5 deletions pkg/shell/hosts_dialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Connect extends React.Component {
ex.problem = "not-found";
}
this.setState({ inProgress: false });
this.props.setError(ex);
this.props.setError(ex, true);
});
}

Expand Down Expand Up @@ -1035,9 +1035,11 @@ export class HostModal extends React.Component {
return host;
}

changeContent(template, error_options) {
changeContent(template, error_options, with_error_message) {
if (this.state.current_template !== template)
this.setState({ current_template: template, error_options });
this.setState({ current_template: template, error_options,
dialogError: with_error_message ? cockpit.message(error_options) : null,
});
}

try2Connect(address, options) {
Expand All @@ -1055,7 +1057,7 @@ export class HostModal extends React.Component {
this.promise_callback = callback;
}

setError(error) {
setError(error, keep_message_on_change) {
if (error === null)
return this.setState({ dialogError: null });

Expand All @@ -1064,7 +1066,7 @@ export class HostModal extends React.Component {
template = codes[error.problem];

if (template && this.state.current_template !== template)
this.changeContent(template, error);
this.changeContent(template, error, keep_message_on_change);
else
this.setState({ error_options: error, dialogError: cockpit.message(error) });
}
Expand Down
16 changes: 14 additions & 2 deletions test/common/testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,15 +1133,23 @@ def start_machine_troubleshoot(
known_host: bool = False,
password: str | None = None,
expect_closed_dialog: bool = True,
expect_warning: bool = True,
need_click: bool = True
) -> None:
if need_click:
self.click('#machine-troubleshoot')

if not new and expect_warning:
self.wait_visible('#hosts_connect_server_dialog')
self.click("#hosts_connect_server_dialog button.pf-m-warning")

self.wait_visible('#hosts_setup_server_dialog')
if new:
self.wait_text("#hosts_setup_server_dialog button.pf-m-primary", "Add")
self.click("#hosts_setup_server_dialog button.pf-m-primary")
if expect_warning:
self.wait_visible('#hosts_connect_server_dialog')
self.click("#hosts_connect_server_dialog button.pf-m-warning")
if not known_host:
self.wait_in_text('#hosts_setup_server_dialog', "You are connecting to")
self.wait_in_text('#hosts_setup_server_dialog', "for the first time.")
Expand All @@ -1155,10 +1163,14 @@ def start_machine_troubleshoot(
if expect_closed_dialog:
self.wait_not_present('#hosts_setup_server_dialog')

def add_machine(self, address: str, known_host: bool = False, password: str = "foobar") -> None:
def add_machine(self, address: str, known_host: bool = False, password: str = "foobar",
expect_warning: bool = True) -> None:
self.switch_to_top()
self.go(f"/@{address}")
self.start_machine_troubleshoot(new=True, known_host=known_host, password=password)
self.start_machine_troubleshoot(new=True,
known_host=known_host,
password=password,
expect_warning=expect_warning)
self.enter_page("/system", host=address)

def grant_permissions(self, *args: str) -> None:
Expand Down
25 changes: 16 additions & 9 deletions test/verify/check-shell-host-switching
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class HostSwitcherHelpers:
# HACK: Dropping the machine does not terminate SSH connection; https://github.com/cockpit-project/cockpit/issues/19672
self.machine.execute(f"pkill -f [s]sh.*{address}; while pgrep -f [s]sh.*{address}; do sleep 1; done")

def add_new_machine(self, b, address, known_host=False, pixel_label=None, user=None, expect_password_auth=False):
def add_new_machine(self, b, address, known_host=False, pixel_label=None, user=None, expect_password_auth=False,
expect_warning=False):
b.click("button:contains('Add new host')")
b.wait_visible('#hosts_setup_server_dialog')
b.set_input_text('#add-machine-address', address)
Expand All @@ -72,6 +73,9 @@ class HostSwitcherHelpers:
if pixel_label:
b.assert_pixels("#hosts_setup_server_dialog", pixel_label)
b.click('#hosts_setup_server_dialog .pf-m-primary:contains("Add")')
if expect_warning:
b.wait_visible('#hosts_connect_server_dialog')
b.click(f'#hosts_connect_server_dialog button.pf-m-warning')
if not known_host:
b.wait_in_text('#hosts_setup_server_dialog',
f"You are connecting to {address.removeprefix('ssh://')} for the first time")
Expand All @@ -83,8 +87,11 @@ class HostSwitcherHelpers:
with b.wait_timeout(30):
b.wait_not_present('#hosts_setup_server_dialog')

def connect_and_wait(self, b, address, expected_user=None):
def connect_and_wait(self, b, address, expected_user=None, expect_warning=False):
b.click(f"a[href='/@{address}']")
if expect_warning:
b.wait_visible('#hosts_connect_server_dialog')
b.click(f'#hosts_connect_server_dialog button.pf-m-warning')
# wait for host switcher to close after connecting
b.wait_not_present("#nav-hosts.interact")
# open it again
Expand Down Expand Up @@ -176,7 +183,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
b.wait_in_text("#nav-hosts .nav-item a", "mydhcpname")
m1.execute("hostnamectl set-hostname 'localhost'")

self.add_new_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", expect_warning=True)
self.wait_host_addresses(b, ["localhost", "10.111.113.2"])
# defaults to current host user name "admin"
self.connect_and_wait(b, "10.111.113.2", "admin")
Expand Down Expand Up @@ -304,7 +311,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
b.click("#hosts-sel button")

# ssh:// prefix and implied user
self.add_new_machine(b, "ssh://10.111.113.2", known_host=True)
self.add_new_machine(b, "ssh://10.111.113.2", known_host=True, expect_warning=True)
self.wait_host_addresses(b, ["localhost", "10.111.113.2"])
self.connect_and_wait(b, "10.111.113.2", "admin")
self.machine_remove(b, "10.111.113.2", second_to_last=True)
Expand Down Expand Up @@ -368,11 +375,11 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
b2.click("#hosts-sel button")
self.wait_host_addresses(b2, ["localhost"])

self.add_new_machine(b, "10.111.113.2")
self.add_new_machine(b, "10.111.113.2", expect_warning=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")
self.connect_and_wait(b2, "10.111.113.2")
self.connect_and_wait(b2, "10.111.113.2", expect_warning=True)

# Main host should have both buttons disabled, the second both enabled
b.click("button:contains('Edit hosts')")
Expand Down Expand Up @@ -448,7 +455,7 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
self.login_and_go(superuser=False)

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

Expand Down Expand Up @@ -529,9 +536,9 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
self.enable_multihost(self.machine)
self.login_and_go(None)

# And and connect to a second machine
# Add and connect to a second machine
b.click("#hosts-sel button")
self.add_new_machine(b, "10.111.113.2")
self.add_new_machine(b, "10.111.113.2", expect_warning=True)
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
57 changes: 30 additions & 27 deletions test/verify/check-shell-multi-machine
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TestMultiMachineAdd(testlib.MachineCase):

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

b.switch_to_top()
b.click("#hosts-sel button")
Expand Down Expand Up @@ -225,7 +225,7 @@ class TestMultiMachineAdd(testlib.MachineCase):

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

b.switch_to_top()
b.click("#hosts-sel button")
Expand Down Expand Up @@ -506,8 +506,11 @@ class TestMultiMachine(testlib.MachineCase):
# navigating there again will fail
b.go(m2_path)
with b.wait_timeout(30):
b.wait_text(".curtains-ct h1", "Not connected to host")
b.wait_text("#machine-troubleshoot", "Log in")
b.wait_visible("#hosts_setup_server_dialog")
b.start_machine_troubleshoot(need_click=False, password="foobar",
expect_closed_dialog=False, expect_warning=False)
b.wait_in_text("#hosts_setup_server_dialog .pf-v5-c-alert", "Login failed")
b.click("#hosts_setup_server_dialog button:contains(Cancel)")

# wait for system to load
b.go("/system")
Expand All @@ -522,8 +525,8 @@ class TestMultiMachine(testlib.MachineCase):
b.reload()
b.go(m2_path)
with b.wait_timeout(30):
b.wait_text(".curtains-ct h1", "Not connected to host")
b.start_machine_troubleshoot(password="foobar")
b.wait_visible("#hosts_setup_server_dialog")
b.start_machine_troubleshoot(need_click=False, password="foobar", expect_warning=False)

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 @@ -599,6 +602,8 @@ class TestMultiMachine(testlib.MachineCase):
m1 = self.machine
m2 = self.machine2

m1.upload(["/home/mvo/work/cockpit/dist/shell"], "/usr/share/cockpit")

# Logging in as root is no longer allowed by default by sshd
m2.execute("sed -ri 's/#?PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config")
m2.execute(self.restart_sshd)
Expand All @@ -622,38 +627,39 @@ class TestMultiMachine(testlib.MachineCase):
m1.execute("mkdir -p /home/admin/.ssh/")
break_hostkey(m1, "10.111.113.2")

b.start_machine_troubleshoot(new=True, known_host=True, expect_closed_dialog=False)
b.start_machine_troubleshoot(new=True, known_host=True, expect_closed_dialog=False, expect_warning=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)
b.start_machine_troubleshoot(new=True, password="foobar", expect_closed_dialog=False)
b.start_machine_troubleshoot(new=True, password="foobar", expect_closed_dialog=False, expect_warning=False)
check_failed_state(b, "Cockpit is not installed")
fix_bridge(m2)

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

# Bad cockpit
break_bridge(m2)
self.login_and_go(None)
b.go(machine_path)
with b.wait_timeout(240):
b.start_machine_troubleshoot(password="foobar", expect_closed_dialog=False)
with b.wait_timeout(20):
b.start_machine_troubleshoot(need_click=False, password="foobar", expect_closed_dialog=False)

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

# Clear host key
fix_hostkey(m1)
b.start_machine_troubleshoot(expect_closed_dialog=False)
b.click("#machine-reconnect")
b.start_machine_troubleshoot(need_click=False, expect_closed_dialog=False, expect_warning=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 @@ -687,13 +693,10 @@ class TestMultiMachine(testlib.MachineCase):
self.login_and_go(None)
b.go(machine_path)

with b.wait_timeout(120):
b.wait_visible("#machine-troubleshoot")
b.start_machine_troubleshoot(expect_closed_dialog=False)
with b.wait_timeout(20):
b.wait_visible("#hosts_connect_server_dialog")
b.start_machine_troubleshoot(need_click=False, 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)

b.set_input_text("#login-custom-password", "bad")
fail_login(b)
b.set_input_text("#login-custom-password", "alt-password")
Expand All @@ -712,11 +715,11 @@ class TestMultiMachine(testlib.MachineCase):

self.login_and_go(None)
b.go(machine_path)
with b.wait_timeout(120):
b.wait_visible("#machine-troubleshoot")
b.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")
with b.wait_timeout(20):
b.wait_visible("#hosts_connect_server_dialog")
b.start_machine_troubleshoot(need_click=False, expect_closed_dialog=False)
b.wait_in_text('#hosts_setup_server_dialog', "Unable to contact")
b.set_input_text("#add-machine-address", "10.111.113.2:2222")
b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
# ssh(1) tracks known hosts by name/IP, not by port; so not expecting a host key prompt here
b.wait_in_text('#hosts_setup_server_dialog h1', "Log in to")
Expand Down Expand Up @@ -756,9 +759,7 @@ class TestMultiMachine(testlib.MachineCase):

self.login_and_go(None)
b.go("/@10.111.113.2")
b.start_machine_troubleshoot(expect_closed_dialog=False)
b.click('#machine-troubleshoot')
b.wait_visible('#hosts_setup_server_dialog')
b.start_machine_troubleshoot(need_click=False, expect_closed_dialog=False)
b.wait_in_text('#hosts_setup_server_dialog', "new host")
b.set_input_text('#add-machine-user', "fred")
b.click('#hosts_setup_server_dialog button:contains(Add)')
Expand Down Expand Up @@ -859,6 +860,8 @@ class TestMultiMachine(testlib.MachineCase):
b.click('#machine-troubleshoot')
b.wait_visible('#hosts_setup_server_dialog')
b.click('#hosts_setup_server_dialog button:contains(Add)')
b.wait_visible('#hosts_connect_server_dialog')
b.click('#hosts_connect_server_dialog button:contains(Connect)')
b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to 10.111.113.2 for the first time.")
b.click("#hosts_setup_server_dialog button:contains('Trust and add host')")
b.wait_in_text('#hosts_setup_server_dialog', "The SSH key")
Expand Down
8 changes: 8 additions & 0 deletions test/verify/check-shell-multi-machine-key
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class TestMultiMachineKeyAuth(testlib.MachineCase):

b.wait_text(f'#hosts_setup_server_dialog {self.primary_btn_class}', "Add")
b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
b.wait_visible('#hosts_connect_server_dialog')
b.click(f'#hosts_connect_server_dialog button.pf-m-warning')
b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to 10.111.113.2 for the first time.")
b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
b.wait_in_text('#hosts_setup_server_dialog h1', "Log in to")
Expand Down Expand Up @@ -185,6 +187,8 @@ class TestMultiMachineKeyAuth(testlib.MachineCase):
self.load_key('id_rsa', 'foobar')

b.go("/@10.111.113.2")
b.wait_visible('#hosts_connect_server_dialog')
b.click(f'#hosts_connect_server_dialog button.pf-m-warning')
b.wait_visible("iframe.container-frame[name='cockpit1:10.111.113.2/system']")

# Change user
Expand Down Expand Up @@ -253,6 +257,8 @@ Host 10.111.113.2
b.wait_visible('#hosts_setup_server_dialog')
b.set_input_text('#add-machine-address', "10.111.113.2")
b.click("#hosts_setup_server_dialog .pf-m-primary")
b.wait_visible('#hosts_connect_server_dialog')
b.click(f'#hosts_connect_server_dialog button.pf-m-warning')
b.wait_in_text("#hosts_setup_server_dialog", "You are connecting to 10.111.113.2 for the first time.")
b.click("#hosts_setup_server_dialog .pf-m-primary")
b.wait_in_text("#hosts_setup_server_dialog", "/home/admin/.ssh/id_ed25519")
Expand Down Expand Up @@ -288,6 +294,8 @@ Host 10.111.113.2
b.wait_visible('#hosts_setup_server_dialog')
b.set_input_text("#add-machine-address", "10.111.113.2")
b.click("#hosts_setup_server_dialog .pf-m-primary")
b.wait_visible('#hosts_connect_server_dialog')
b.click(f'#hosts_connect_server_dialog button.pf-m-warning')
b.wait_in_text("#hosts_setup_server_dialog", "You are connecting to 10.111.113.2 for the first time.")
b.click("#hosts_setup_server_dialog .pf-m-primary")
b.wait_in_text("#hosts_setup_server_dialog", "/home/admin/.ssh/id_rsa")
Expand Down
5 changes: 4 additions & 1 deletion test/verify/check-superuser
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ class TestSuperuserDashboard(testlib.MachineCase):
b.wait_visible('#hosts_setup_server_dialog')
b.wait_visible('#hosts_setup_server_dialog button:contains("Add")')
b.click('#hosts_setup_server_dialog button:contains("Add")')
b.wait_visible('#hosts_connect_server_dialog')
b.click(f'#hosts_connect_server_dialog button.pf-m-warning')
b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to 10.111.113.2 for the first time.")
b.click('#hosts_setup_server_dialog button.pf-m-primary')
b.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
Expand Down Expand Up @@ -444,7 +446,8 @@ class TestSuperuserDashboard(testlib.MachineCase):
# superuser on m2 (once we have logged in there).
self.allow_restart_journal_messages()
b.relogin()
b.click('#machine-troubleshoot')
b.wait_visible('#hosts_connect_server_dialog')
b.click(f'#hosts_connect_server_dialog button.pf-m-warning')
b.wait_visible('#hosts_setup_server_dialog')
b.set_input_text("#login-custom-password", "foobar")
b.click('#hosts_setup_server_dialog button:contains("Log in")')
Expand Down

0 comments on commit 5751da0

Please sign in to comment.