From 062328ba5a57d2f221ddef4d481ddcd61d704af1 Mon Sep 17 00:00:00 2001 From: Artiom Divak Date: Tue, 5 Mar 2024 09:12:42 +0200 Subject: [PATCH] ReloadUnit test Reload Integration Test This test will test the functionality of ReloadUnit. Two services will be added to the node container. One of them will be activated by bluechictl and then reloaded with bluechi reload and that suppose to make the other service active. Signed-off-by: Artiom Divak --- tests/bluechi_test/bluechictl.py | 9 ++++ .../tests/tier0/bluechi-reload-unit/main.fmf | 2 + .../systemd/service-for-reload.service | 7 +++ .../systemd/simple.service | 7 +++ .../test_bluechi_reload_unit_service.py | 44 +++++++++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 tests/tests/tier0/bluechi-reload-unit/main.fmf create mode 100644 tests/tests/tier0/bluechi-reload-unit/systemd/service-for-reload.service create mode 100644 tests/tests/tier0/bluechi-reload-unit/systemd/simple.service create mode 100644 tests/tests/tier0/bluechi-reload-unit/test_bluechi_reload_unit_service.py diff --git a/tests/bluechi_test/bluechictl.py b/tests/bluechi_test/bluechictl.py index 376b811714..5d04ff2ddf 100644 --- a/tests/bluechi_test/bluechictl.py +++ b/tests/bluechi_test/bluechictl.py @@ -92,6 +92,15 @@ def start_unit(self, node_name: str, unit_name: str, check_result: bool = True, expected_result ) + def reload_unit(self, node_name: str, unit_name: str, check_result: bool = True, expected_result: int = 0) \ + -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]: + return self._run( + f"Reloading unit {unit_name} on node {node_name}", + f"reload {node_name} {unit_name}", + check_result, + expected_result + ) + def stop_unit(self, node_name: str, unit_name: str, check_result: bool = True, expected_result: int = 0) \ -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]: return self._run( diff --git a/tests/tests/tier0/bluechi-reload-unit/main.fmf b/tests/tests/tier0/bluechi-reload-unit/main.fmf new file mode 100644 index 0000000000..981787d542 --- /dev/null +++ b/tests/tests/tier0/bluechi-reload-unit/main.fmf @@ -0,0 +1,2 @@ +summary: Test reload service +id: 44b0d11e-fcad-4523-acf7-ff0c0421501e diff --git a/tests/tests/tier0/bluechi-reload-unit/systemd/service-for-reload.service b/tests/tests/tier0/bluechi-reload-unit/systemd/service-for-reload.service new file mode 100644 index 0000000000..4f335648d8 --- /dev/null +++ b/tests/tests/tier0/bluechi-reload-unit/systemd/service-for-reload.service @@ -0,0 +1,7 @@ +[Unit] +Description=A very simple service +[Service] +Type=simple +ExecStart=/bin/true +RemainAfterExit=yes +ExecReload=systemctl start simple.service \ No newline at end of file diff --git a/tests/tests/tier0/bluechi-reload-unit/systemd/simple.service b/tests/tests/tier0/bluechi-reload-unit/systemd/simple.service new file mode 100644 index 0000000000..0427e38025 --- /dev/null +++ b/tests/tests/tier0/bluechi-reload-unit/systemd/simple.service @@ -0,0 +1,7 @@ +[Unit] +Description=Service For Integration Test + +[Service] +Type=simple +ExecStart=/bin/true +RemainAfterExit=yes diff --git a/tests/tests/tier0/bluechi-reload-unit/test_bluechi_reload_unit_service.py b/tests/tests/tier0/bluechi-reload-unit/test_bluechi_reload_unit_service.py new file mode 100644 index 0000000000..1f22397397 --- /dev/null +++ b/tests/tests/tier0/bluechi-reload-unit/test_bluechi_reload_unit_service.py @@ -0,0 +1,44 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +import logging +import os + +from typing import Dict +from bluechi_test.test import BluechiTest +from bluechi_test.machine import BluechiControllerMachine, BluechiAgentMachine +from bluechi_test.config import BluechiControllerConfig, BluechiAgentConfig + +LOGGER = logging.getLogger(__name__) + +NODE_FOO = "node-foo" + + +def exec(ctrl: BluechiControllerMachine, nodes: Dict[str, BluechiAgentMachine]): + + foo = nodes[NODE_FOO] + target_dir = os.path.join("/", "etc", "systemd", "system") + foo.copy_systemd_service("service-for-reload.service", "systemd", target_dir) + foo.copy_systemd_service("simple.service", "systemd", target_dir) + assert foo.wait_for_unit_state_to_be("simple.service", "inactive") + assert foo.wait_for_unit_state_to_be("service-for-reload.service", "inactive") + + ctrl.bluechictl.start_unit(NODE_FOO, "service-for-reload.service") + assert foo.wait_for_unit_state_to_be("simple.service", "inactive") + assert foo.wait_for_unit_state_to_be("service-for-reload.service", "active") + + ctrl.bluechictl.reload_unit(NODE_FOO, "service-for-reload.service") + assert foo.wait_for_unit_state_to_be("simple.service", "active") + assert foo.wait_for_unit_state_to_be("service-for-reload.service", "active") + + +def test_bluechi_reload_unit_service( + bluechi_test: BluechiTest, + bluechi_node_default_config: BluechiAgentConfig, bluechi_ctrl_default_config: BluechiControllerConfig): + 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)