From aad868fc8a1deafe4c699d24e284d3646088db69 Mon Sep 17 00:00:00 2001 From: Michael Engel Date: Thu, 1 Jun 2023 09:18:11 +0200 Subject: [PATCH] added integration test for enabling a unit Signed-off-by: Michael Engel --- tests/hirte_test/container.py | 7 +++ .../tests/tier0/hirte-enable-service/main.fmf | 6 +++ .../systemd/simple.service | 10 ++++ .../test_hirte_enable_service.py | 49 +++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 tests/tests/tier0/hirte-enable-service/main.fmf create mode 100644 tests/tests/tier0/hirte-enable-service/systemd/simple.service create mode 100644 tests/tests/tier0/hirte-enable-service/test_hirte_enable_service.py diff --git a/tests/hirte_test/container.py b/tests/hirte_test/container.py index ab8b4e6095..4f696e51b7 100644 --- a/tests/hirte_test/container.py +++ b/tests/hirte_test/container.py @@ -150,3 +150,10 @@ def stop_unit(self, node_name: str, unit_name: str) -> None: result, output = self.exec_run(f"hirtectl stop {node_name} {unit_name}") if result != 0: raise Exception(f"Failed to start service {unit_name} on node {node_name}: {output}") + + def enable_unit(self, node_name: str, unit_name: str) -> None: + print(f"Enabling unit {unit_name} on node {node_name}") + + result, output = self.exec_run(f"hirtectl enable {node_name} {unit_name}") + if result != 0: + raise Exception(f"Failed to enable service {unit_name} on node {node_name}: {output}") diff --git a/tests/tests/tier0/hirte-enable-service/main.fmf b/tests/tests/tier0/hirte-enable-service/main.fmf new file mode 100644 index 0000000000..f81862b8a7 --- /dev/null +++ b/tests/tests/tier0/hirte-enable-service/main.fmf @@ -0,0 +1,6 @@ +summary: Test if a disabled systemd service can be enabled using hirtectl + +tier: 0 + +test: | + pytest -svv diff --git a/tests/tests/tier0/hirte-enable-service/systemd/simple.service b/tests/tests/tier0/hirte-enable-service/systemd/simple.service new file mode 100644 index 0000000000..3723c39be1 --- /dev/null +++ b/tests/tests/tier0/hirte-enable-service/systemd/simple.service @@ -0,0 +1,10 @@ +[Unit] +Description=I am truth + +[Service] +Type=simple +ExecStart=/bin/true +RemainAfterExit=yes + +[Install] +WantedBy=multi-user.target diff --git a/tests/tests/tier0/hirte-enable-service/test_hirte_enable_service.py b/tests/tests/tier0/hirte-enable-service/test_hirte_enable_service.py new file mode 100644 index 0000000000..2e08851921 --- /dev/null +++ b/tests/tests/tier0/hirte-enable-service/test_hirte_enable_service.py @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +import os +import pytest +from typing import Dict + +from hirte_test.config import HirteControllerConfig, HirteNodeConfig +from hirte_test.container import HirteControllerContainer, HirteNodeContainer +from hirte_test.test import HirteTest + + +node_foo_name = "node-foo" +simple_service = "simple.service" + + +def exec(ctrl: HirteControllerContainer, nodes: Dict[str, HirteNodeContainer]): + foo = nodes[node_foo_name] + + source_dir = "systemd" + target_dir = os.path.join("/", "etc", "systemd", "system") + + foo.copy_systemd_service(simple_service, source_dir, target_dir) + + _, output = foo.exec_run(f"systemctl is-enabled {simple_service}") + if output != "disabled": + raise Exception(f"Failed pre-check if unit {simple_service} is enabled: {output}") + + ctrl.enable_unit(node_foo_name, simple_service) + + _, output = foo.exec_run(f"systemctl is-enabled {simple_service}") + if output != "enabled": + raise Exception(f"Unit {simple_service} expected to be enabled, but got: {output}") + + +@pytest.mark.timeout(10) +def test_proxy_service_start( + hirte_test: HirteTest, + hirte_ctrl_default_config: HirteControllerConfig, + hirte_node_default_config: HirteNodeConfig): + + node_foo_cfg = hirte_node_default_config.deep_copy() + node_foo_cfg.node_name = node_foo_name + + hirte_ctrl_default_config.allowed_node_names = [node_foo_name] + + hirte_test.set_hirte_controller_config(hirte_ctrl_default_config) + hirte_test.add_hirte_node_config(node_foo_cfg) + + hirte_test.run(exec)