From b864e2da0ecd019cd2c2b09c127cbde4b8db58c9 Mon Sep 17 00:00:00 2001 From: Sebastian Opsahl Date: Thu, 2 May 2024 18:48:55 +0200 Subject: [PATCH] chore: updating mocks for tests to test mender-connect JWT request handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The thing to fix is to make `mender-connect` request a new JWT token on its own, so its not dependent on `mender-update` to do so. Previously **FetchJWTToken()** wasn’t called which wouldn’t let it call the JWT token independently, which made it dependent on **mender-update**. The PR (another PR) proposed to fix this issue uses **FetchJWTToken()** to fetch the token, to then for **WaitForJwtTokenStateChange()** to retrieve it in it’s waiting. The old mocks for the tests seemed to be more tailored to test the functionality with **GetJWTToken()**. This PR proposes a change in the mocks to better test the new implementation (where the 'new implementation' is the one proposed in the other PR for ticket MEN-6877) Changelog: None Ticket: MEN-6877 Signed-off-by: Sebastian Opsahl --- tests/acceptance/mocks/mock_dbus_server.py | 55 ++++++++++++---------- tests/acceptance/test_mender-connect.py | 52 +++++++++++++++----- 2 files changed, 70 insertions(+), 37 deletions(-) diff --git a/tests/acceptance/mocks/mock_dbus_server.py b/tests/acceptance/mocks/mock_dbus_server.py index 6d76adc6e..dafdd28e8 100644 --- a/tests/acceptance/mocks/mock_dbus_server.py +++ b/tests/acceptance/mocks/mock_dbus_server.py @@ -20,30 +20,33 @@ class IoMenderAuthenticationIface: """ - - - - - - - - - - - - - - - - - - - - - - - - """ + + + + + + + + + + + + + + + + + + + + + + + + + + + """ def __init__(self): self.token = "" @@ -68,6 +71,10 @@ def MockSetJwtTokenAndEmitSignal(self, token, server_url): self.JwtTokenStateChange(self.token, self.server_url) return True + def MockEmitSignal(self): + self.JwtTokenStateChange(self.token, self.server_url) + return True + loop = GLib.MainLoop() bus = SystemBus() diff --git a/tests/acceptance/test_mender-connect.py b/tests/acceptance/test_mender-connect.py index 2afc61d26..8984638ad 100644 --- a/tests/acceptance/test_mender-connect.py +++ b/tests/acceptance/test_mender-connect.py @@ -67,7 +67,9 @@ def __init__(self, connection, remote_command, unique_id) -> None: wrapper.flush() put_no_sftp( - wrapper.name, connection, remote=self._sh_file, + wrapper.name, + connection, + remote=self._sh_file, ) connection.run(f"bash {self._sh_file}") result = connection.run(f"cat {self._pid_file}") @@ -165,6 +167,15 @@ def dbus_set_token_and_url_and_emit_signal(connection, token, server_url): ) +def dbus_emit_signal(connection): + connection.run( + "dbus-send --print-reply --system " + "--dest=io.mender.AuthenticationManager " + "/io/mender/AuthenticationManager " + "io.mender.Authentication1.MockEmitSignal " + ) + + def wait_for_string_in_log(connection, since, timeout, search_string): output = "" while qemu_system_time(connection) < since + timeout: @@ -188,19 +199,27 @@ def wait_for_string_in_log(connection, since, timeout, search_string): class TestMenderConnect: @pytest.mark.min_mender_version("2.5.0") def test_mender_connect_auth_changes( - self, request, connection, with_mock_files, with_mock_servers, + self, + request, + connection, + with_mock_files, + with_mock_servers, ): """Test that mender-connect can re-establish the connection on D-Bus signals""" try: - dbus_set_token_and_url(connection, "token1", "http://localhost:5000") - # start the mender-connect service - startup_time = qemu_system_time(connection) connection.run( "systemctl --job-mode=ignore-dependencies start mender-connect" ) + dbus_set_token_and_url(connection, "token1", "http://localhost:5000") + + # sleep for a while to ensure a connection happens + time.sleep(60) + dbus_emit_signal(connection) + signal_time = qemu_system_time(connection) + # wait for first connect _ = wait_for_string_in_log( connection, @@ -222,10 +241,10 @@ def test_mender_connect_auth_changes( ) # 2. Change url + dbus_set_token_and_url(connection, "token2", "http://localhost:6000") + time.sleep(30) signal_time = qemu_system_time(connection) - dbus_set_token_and_url_and_emit_signal( - connection, "token2", "http://localhost:6000" - ) + dbus_emit_signal(connection) _ = wait_for_string_in_log( connection, signal_time, @@ -277,14 +296,18 @@ def test_mender_connect_reconnect( """Test that mender-connect can re-establish the connection on remote errors""" try: - dbus_set_token_and_url(connection, "badtoken", "http://localhost:12345") - # start the mender-connect service - startup_time = qemu_system_time(connection) connection.run( "systemctl --job-mode=ignore-dependencies start mender-connect" ) + dbus_set_token_and_url(connection, "badtoken", "http://localhost:12345") + + # sleep for a while to ensure a connection happens + time.sleep(80) + dbus_emit_signal(connection) + signal_time = qemu_system_time(connection) + # wait for error if version_is_minimum(bitbake_variables, "mender-connect", "2.3.0"): error_message = "connection manager failed to connect" @@ -305,11 +328,14 @@ def test_mender_connect_reconnect( ) dbus_set_token_and_url(connection, "", "") - kill_time = qemu_system_time(connection) # kill the server and wait for error with_mock_servers[1].kill() + kill_time = qemu_system_time(connection) _ = wait_for_string_in_log( - connection, kill_time, 300, "error reconnecting:", + connection, + kill_time, + 300, + "waiting for reconnect", ) # Signal the other server