Skip to content

Commit

Permalink
Fix test bluechi-agent-user-bus
Browse files Browse the repository at this point in the history
Run agent as subprocess as user and not via systemd

Signed-off-by: Mark Kemel <[email protected]>
  • Loading branch information
mkemel committed Dec 9, 2024
1 parent fc5bec5 commit d3975bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#
# SPDX-License-Identifier: LGPL-2.1-or-later

import signal
import subprocess
import threading
import time
import unittest

Expand All @@ -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__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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
Expand Down

0 comments on commit d3975bc

Please sign in to comment.