Skip to content

Commit

Permalink
Add list unit files integration tests (TEST)
Browse files Browse the repository at this point in the history
Fixes: eclipse-bluechi#889

Signed-off-by: tallison <[email protected]>
  • Loading branch information
tallison committed Jul 30, 2024
1 parent a5f8781 commit ce5ecda
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/bluechi_test/bluechictl.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ def list_units(
return self._run(
"Listing units on all nodes", "list-units", check_result, expected_result
)

def list_unit_files(
self, node_name: str = None, check_result: bool = True, expected_result: int = 0
) -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]:
if node_name:
return self._run(
f"Listing unit files on node {node_name}",
f"list-unit-files {node_name}",
check_result,
expected_result,
)

return self._run(
"Listing unit files on all nodes", "list-unit-files", check_result, expected_result
)

def get_unit_status(
self,
Expand Down
9 changes: 9 additions & 0 deletions tests/bluechi_test/systemctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,12 @@ def list_units(
) -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]:
command = "list-units --legend=false{}".format(" --all" if all_units else "")
return self._do_operation(command, check_result, expected_result)

def list_unit_files(
self,
all_unit_files: bool = False,
check_result: bool = True,
expected_result: int = 0,
) -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]:
command = "list-unit-files --legend=false{}".format(" -all" if all_unit_files else "")
return self._do_operation(command, check_result, expected_result)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#
# Copyright Contributors to the Eclipse BlueChi project
#
# SPDX-License-Identifier: LGPL-2.1-or-later

import re
from typing import Dict, Tuple

from bluechi_test.config import BluechiAgentConfig, BluechiControllerConfig
from bluechi_test.machine import BluechiAgentMachine, BluechiControllerMachine
from bluechi_test.test import BluechiTest

node_foo_name = "node-foo"
node_bar_name = "node-bar"


def parse_bluechictl_output(output: str) -> Dict[str, Dict[str, str]]:
line_pat = re.compile(
r"""\s*(?P<node_name>[\S]+)\s*\|
\s*(?P<unit_file_path>[\S]+)\s*\|
\s*(?P<enablement_status>[\S]+)\s*""",
re.VERBOSE,
)
result = {}
for line in output.splitlines():
if line.startswith("NODE ") or line.startswith("===="):
continue

match = line_pat.match(line)
if not match:
raise Exception(
f"Error parsing bluechictl list-unit-files output, invalid line: '{line}'"
)

node_unit_files = result.get(match.group("node_name"))
if not node_unit_files:
node_unit_files = {}
result[match.group("node_name")] = node_unit_files

if match.group("unit_file_path") in node_unit_files:
raise Exception(
f"Error parsing bluechictl list-unit-files output, unit file already reported, line: '{line}'"
)

node_unit_files[match.group("unit_file_path")] = (
match.group("enablement_status"),
)

return result


def verify_unit_files(all_unit_files: Dict[str, str], output: str, node_name: str):
esc_seq = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
line_pat = re.compile(
r"""\s*(?P<unit_file_path>\S+)
\s+(?P<enablement_status>\S+)
\s+.*$
""",
re.VERBOSE,
)
for line in output.splitlines():
line = esc_seq.sub("", line)

match = line_pat.match(line)
if not match:
raise Exception(
f"Error parsing systemctl list-unit-files output, invalid line: '{line}'"
)

found = all_unit_files.get(match.group("unit_file_path"))
if (
not found
or match.group("enablement_status") != found[0]
):
raise Exception(
"Unit file '{}' with enablement status '{}' reported by systemctl"
" on node '{}', but not reported by bluechictl".format(
match.group("unit_file_path"),
match.group("enablement_status"),
node_name,
)
)


def exec(ctrl: BluechiControllerMachine, nodes: Dict[str, BluechiAgentMachine]):
node_foo = nodes[node_foo_name]
node_bar = nodes[node_bar_name]

all_res, all_out = ctrl.bluechictl.list_unit_files()
assert all_res == 0
all_unit_files = parse_bluechictl_output(all_out)

foo_res, foo_out = node_foo.systemctl.list_unit_files()
assert foo_res == 0
verify_unit_files(all_unit_files[node_foo_name], foo_out, node_foo_name)

bar_res, bar_out = node_bar.systemctl.list_unit_files()
assert bar_res == 0
verify_unit_files(all_unit_files[node_bar_name], bar_out, node_bar_name)


def test_bluechi_nodes_statuses(
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_name

node_bar_cfg = bluechi_node_default_config.deep_copy()
node_bar_cfg.node_name = node_bar_name

bluechi_ctrl_default_config.allowed_node_names = [node_foo_name, node_bar_name]

bluechi_test.set_bluechi_controller_config(bluechi_ctrl_default_config)
bluechi_test.add_bluechi_agent_config(node_foo_cfg)
bluechi_test.add_bluechi_agent_config(node_bar_cfg)

bluechi_test.run(exec)

0 comments on commit ce5ecda

Please sign in to comment.