diff --git a/tests/tests/tier0/bluechi-agent-user-bus/python/start_agent_as_user.py b/tests/tests/tier0/bluechi-agent-user-bus/python/start_agent_as_user.py index c520acfc6f..676558a314 100644 --- a/tests/tests/tier0/bluechi-agent-user-bus/python/start_agent_as_user.py +++ b/tests/tests/tier0/bluechi-agent-user-bus/python/start_agent_as_user.py @@ -3,6 +3,9 @@ # # SPDX-License-Identifier: LGPL-2.1-or-later +import signal +import subprocess +import threading import time import unittest @@ -15,20 +18,43 @@ class TestAgentStartAsUser(unittest.TestCase): - def test_agent_start_as_user(self): - result, _, _ = run_command("systemctl --user start bluechi-agent") - assert result == 0 + def run_agent(self): + command = "/usr/libexec/bluechi-agent -u" + self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) + print(f"Executing of command '{command}' started") + self.process.communicate() + print(f"Executing of command '{command}' stopped") + self.finished = True + + def timeout_guard(self): + time.sleep(10) + print("Waiting for agent to start has timed out") + self.process.send_signal(signal.SIGINT) + self.process.wait() - while True: - result, output, _ = run_command( + def test_agent_start_as_user(self): + success = False + self.finished = False + thread = threading.Thread(target=self.run_agent, daemon=True) + failsafe_thread = threading.Thread(target=self.timeout_guard, daemon=True) + thread.start() + failsafe_thread.start() + + while not self.finished: + _, output, _ = run_command( f"busctl --user get-property {service} {object} {interface} Status" ) if output == 's "offline"': - break - time.sleep(0.5) - - result, _, _ = run_command("systemctl --user stop bluechi-agent") - assert result == 0 + print("bluechi-agent connected to user bus") + success = True + self.finished = True + else: + time.sleep(0.5) + + print("Sending SIGINT to agent process") + self.process.send_signal(signal.SIGINT) + thread.join() + assert success if __name__ == "__main__": diff --git a/tests/tests/tier0/bluechi-agent-user-bus/test_bluechi_agent_user_bus.py b/tests/tests/tier0/bluechi-agent-user-bus/test_bluechi_agent_user_bus.py index 939dc88866..b8ff745dc3 100644 --- a/tests/tests/tier0/bluechi-agent-user-bus/test_bluechi_agent_user_bus.py +++ b/tests/tests/tier0/bluechi-agent-user-bus/test_bluechi_agent_user_bus.py @@ -9,7 +9,6 @@ from bluechi_test.config import BluechiAgentConfig, BluechiControllerConfig from bluechi_test.machine import BluechiAgentMachine, BluechiControllerMachine -from bluechi_test.service import Option, Section from bluechi_test.test import BluechiTest LOGGER = logging.getLogger(__name__) @@ -18,23 +17,13 @@ def exec(ctrl: BluechiControllerMachine, nodes: Dict[str, BluechiAgentMachine]): - os.system("cat /etc/redhat-release") bluechi_user = "bluechiuser" node_foo = nodes[NODE_FOO] - node_foo.exec_run("cat /etc/redhat-release") node_foo.systemctl.stop_unit("bluechi-agent") ctrl.systemctl.stop_unit("bluechi-controller") assert node_foo.wait_for_unit_state_to_be("bluechi-agent", "inactive") - bc_agent = node_foo.load_systemd_service( - directory="/usr/lib/systemd/system", name="bluechi-agent.service" - ) - exec_start = bc_agent.get_option(Section.Service, Option.ExecStart) - bc_agent.set_option(Section.Service, Option.ExecStart, exec_start + " -u") - bc_agent.set_option(Section.Service, Option.User, bluechi_user) - node_foo.install_systemd_service(bc_agent, restart=False) - - node_foo.exec_run(f"useradd {bluechi_user}") + node_foo.exec_run(f"useradd {bluechi_user} -u 55555") node_foo.exec_run("chmod -R 777 /var/tmp/bluechi-coverage") result, _ = node_foo.run_python( os.path.join("python", "start_agent_as_user.py"), bluechi_user