-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for agent connecting to user bus
Resolves: #799 Signed-off-by: Mark Kemel <[email protected]>
- Loading branch information
Showing
7 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# | ||
# Copyright Contributors to the Eclipse BlueChi project | ||
# | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
import signal | ||
import subprocess | ||
from typing import Tuple | ||
|
||
|
||
def run_command(command: str) -> Tuple[int, str, str]: | ||
process = subprocess.Popen( | ||
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True | ||
) | ||
print(f"Executing of command '{process.args}' started") | ||
out, err = process.communicate() | ||
|
||
out = out.decode("utf-8").strip() | ||
err = err.decode("utf-8").strip() | ||
|
||
print( | ||
f"Executing of command '{process.args}' finished with result '{process.returncode}', " | ||
f"stdout '{out}', stderr '{err}'" | ||
) | ||
|
||
return process.returncode, out.strip(), err | ||
|
||
|
||
class Timeout: | ||
def __init__(self, seconds=1, error_message="Timeout"): | ||
self.seconds = seconds | ||
self.error_message = error_message | ||
|
||
def handle_timeout(self, signum, frame): | ||
raise TimeoutError(self.error_message) | ||
|
||
def __enter__(self): | ||
signal.signal(signal.SIGALRM, self.handle_timeout) | ||
signal.alarm(self.seconds) | ||
|
||
def __exit__(self, type, value, traceback): | ||
signal.alarm(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
summary: Test agent connecting to user bus | ||
id: 6886e7c8-7548-4cf7-8dd4-d2d46b125fd3 |
36 changes: 36 additions & 0 deletions
36
tests/tests/tier0/bluechi-agent-user-bus/python/start_agent_as_user.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# | ||
# Copyright Contributors to the Eclipse BlueChi project | ||
# | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
import time | ||
import unittest | ||
|
||
from bluechi_machine_lib.util import Timeout, run_command | ||
|
||
service = "org.eclipse.bluechi.Agent" | ||
object = "/org/eclipse/bluechi" | ||
interface = "org.eclipse.bluechi.Agent" | ||
|
||
|
||
class TestAgentStartAsUser(unittest.TestCase): | ||
|
||
def test_agent_start_as_user(self): | ||
result, _, _ = run_command("systemctl --user start bluechi-agent") | ||
assert result == 0 | ||
|
||
with Timeout(5, "Timeout waiting for agent to connect to user bus"): | ||
while True: | ||
result, 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 | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
58 changes: 58 additions & 0 deletions
58
tests/tests/tier0/bluechi-agent-user-bus/test_bluechi_agent_user_bus.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# | ||
# Copyright Contributors to the Eclipse BlueChi project | ||
# | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
import logging | ||
import os | ||
from typing import Dict | ||
|
||
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__) | ||
|
||
NODE_FOO = "node-foo" | ||
|
||
|
||
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("chmod -R 777 /var/tmp/bluechi-coverage") | ||
result, _ = node_foo.run_python( | ||
os.path.join("python", "start_agent_as_user.py"), bluechi_user | ||
) | ||
assert result == 0 | ||
|
||
|
||
def test_monitor_node_disconnect( | ||
bluechi_test: BluechiTest, | ||
bluechi_ctrl_default_config: BluechiControllerConfig, | ||
bluechi_node_default_config: BluechiAgentConfig, | ||
): | ||
node_foo_cfg = bluechi_node_default_config.deep_copy() | ||
node_foo_cfg.node_name = NODE_FOO | ||
|
||
bluechi_test.add_bluechi_agent_config(node_foo_cfg) | ||
|
||
bluechi_ctrl_default_config.allowed_node_names = [NODE_FOO] | ||
bluechi_test.set_bluechi_controller_config(bluechi_ctrl_default_config) | ||
|
||
bluechi_test.run(exec) |