diff --git a/lib/pbench/test/unit/agent/test_tool_group.py b/lib/pbench/test/unit/agent/test_tool_group.py index 0d169792bd..45f7336df4 100644 --- a/lib/pbench/test/unit/agent/test_tool_group.py +++ b/lib/pbench/test/unit/agent/test_tool_group.py @@ -5,14 +5,12 @@ from pbench.agent.tool_group import BadToolGroup, ToolGroup -# from pbench.common.exceptions import BadConfig - class TestToolGroup: pbench_run = "/mock/pbench_run" group = "tool-group" - mock_tg_dir_name = Path(pbench_run, f"tools-v1-{group}") + mock_tg_dir_name = Path(pbench_run, f"{ToolGroup.TOOL_GROUP_PREFIX}-{group}") def mock_resolve(self: Path, strict: bool = False): """ @@ -26,44 +24,49 @@ def mock_resolve(self: Path, strict: bool = False): """ return Path("/mock/pbench-agent/") - def mock_is_dir(self): - """Return 'True' if Directory exist""" - return True - - def mock_listdir(self, dir_path): - """Return 'Hostname' or 'ToolNames' based on directory path + def test_pbench_run_value(self, monkeypatch): + """Check behaviour when the pbench run value does not exist""" - Args: - dir_path: Directory path - """ - if str(dir_path).endswith("hostname"): - return ["tool1", "tool2"] - else: - return ["hostname"] + monkeypatch.delenv("pbench_run", False) + expected_error_msg = f"Cannot validate tool group, '{self.group}', 'pbench_run' environment variable missing" + with pytest.raises(BadToolGroup) as exc: + ToolGroup.verify_tool_group(self.group) + assert expected_error_msg in str(exc) - def mock_read_text(self): - """Return content of File""" - return "" + def test_pbench_run_empty_value(self): + """Check behaviour when the pbench run value is an empty string""" - def test_pbench_run_value(self): - """Tests if pbench run value exists""" expected_error_msg = f"Cannot validate tool group, '{self.group}', 'pbench_run' environment variable missing" with pytest.raises(BadToolGroup) as exc: - ToolGroup.verify_tool_group(self.group) + ToolGroup.verify_tool_group(self.group, "") assert expected_error_msg in str(exc) - def test_pbench_run_dir_exist(self): + def test_pbench_run_dir_exist(self, monkeypatch): """Tests if pbench_run directory exist""" + + def mock_resolve(path: Path, strict: bool): + assert strict, "'strict' is unexpectedly false" + raise FileNotFoundError("Mock Path.resolve()") + expected_error_msg = f"Bad tool group, '{self.group}': directory {self.mock_tg_dir_name} does not exist" + monkeypatch.setattr(Path, "resolve", mock_resolve) with pytest.raises(BadToolGroup) as exc: ToolGroup.verify_tool_group(self.group, self.pbench_run) assert expected_error_msg in str(exc) def test_target_dir_exist(self, monkeypatch): """Test target directory exist with pbench run value as environment value""" + + def mock_resolve(path: Path, strict: bool): + assert strict, "'strict' is unexpectedly false" + raise FileNotFoundError("Mock Path.resolve()") + pbench_run = "/mock/environ_val/pbench_run" monkeypatch.setenv("pbench_run", pbench_run) - mock_tg_dir_name = Path(pbench_run, f"tools-v1-{self.group}") + monkeypatch.setattr(Path, "resolve", mock_resolve) + mock_tg_dir_name = Path( + pbench_run, f"{ToolGroup.TOOL_GROUP_PREFIX}-{self.group}" + ) expected_error_msg = f"Bad tool group, '{self.group}': directory {mock_tg_dir_name} does not exist" with pytest.raises(BadToolGroup) as exc: ToolGroup.verify_tool_group(self.group) @@ -74,7 +77,8 @@ def test_target_dir_exception(self, monkeypatch): def mock_resolve(self, strict=True): """Mocked the check to see if the path exists""" - return self.path + assert strict, "'strict' is unexpectedly false" + raise AttributeError("Mock Path.resolve()") expected_error_msg = f"Bad tool group, '{self.group}': error resolving {self.mock_tg_dir_name} directory" monkeypatch.setattr(Path, "resolve", mock_resolve) @@ -83,65 +87,127 @@ def mock_resolve(self, strict=True): assert expected_error_msg in str(exc) def test_target_dir_is_directory(self, monkeypatch): - """Verify target Directory as Directory""" - - def mock_is_dir(self) -> bool: - """Return 'True' if Directory exist""" - return False + """Verify target Directory is a Directory""" expected_error_msg = f"Bad tool group, '{self.group}': directory {self.mock_tg_dir_name} not valid" monkeypatch.setattr(Path, "resolve", self.mock_resolve) - monkeypatch.setattr(Path, "is_dir", mock_is_dir) + monkeypatch.setattr(Path, "is_dir", lambda self: False) with pytest.raises(BadToolGroup) as exc: ToolGroup.verify_tool_group(self.group, self.pbench_run) assert expected_error_msg in str(exc) - def test_missing_trigger_file(self, monkeypatch): - """Verify missing trigger File""" - - def mock_read_text(self): - """Return content of File""" - raise FileNotFoundError + def test_target_dir(self, monkeypatch): + """Verify target Directory as Directory""" - monkeypatch.setenv("pbench_run", "/mock/pbench_run") monkeypatch.setattr(Path, "resolve", self.mock_resolve) - monkeypatch.setattr(Path, "is_dir", self.mock_is_dir) - monkeypatch.setattr(os, "listdir", self.mock_listdir) - monkeypatch.setattr(Path, "read_text", mock_read_text) - with pytest.raises(FileNotFoundError): - tg = ToolGroup("wrong-file") - assert tg.trigger is None + monkeypatch.setattr(Path, "is_dir", lambda self: True) + tg_dir = ToolGroup.verify_tool_group(self.group, self.pbench_run) + assert tg_dir == Path("/mock/pbench-agent/") - def test_tool_group(self, monkeypatch): - """Tests ToolGroup __init__ function""" + class Test_ToolGroup: + """Verify ToolGroup class""" - monkeypatch.setenv("pbench_run", "/mock/pbench_run") - monkeypatch.setattr(Path, "resolve", self.mock_resolve) - monkeypatch.setattr(Path, "is_dir", self.mock_is_dir) - monkeypatch.setattr(os, "listdir", self.mock_listdir) - monkeypatch.setattr(Path, "read_text", self.mock_read_text) - ToolGroup("tool-group") + def mock_verify_tool_group(name: str, pbench_run: Path): + """Mocked verify_tool_group""" + return Path("/mock/pbench-agent") - def test_get_tools(self, monkeypatch): - """Test get_tools function of ToolGroup""" + def mock_listdir(self, dir_path: Path): + """Return 'Hostname' or 'ToolNames' based on directory path - monkeypatch.setenv("pbench_run", "/mock/pbench_run") - monkeypatch.setattr(Path, "resolve", self.mock_resolve) - monkeypatch.setattr(Path, "is_dir", self.mock_is_dir) - monkeypatch.setattr(os, "listdir", self.mock_listdir) - monkeypatch.setattr(Path, "read_text", self.mock_read_text) - t1 = ToolGroup("tool-group") - tools = t1.get_tools("hostname") - assert "tool1" in tools - assert "tool2" in tools - - def test_get_labels(self, monkeypatch): - """Test get_labels function of ToolGroup""" - monkeypatch.setenv("pbench_run", "/mock/pbench_run") - monkeypatch.setattr(Path, "resolve", self.mock_resolve) - monkeypatch.setattr(Path, "is_dir", self.mock_is_dir) - monkeypatch.setattr(os, "listdir", self.mock_listdir) - monkeypatch.setattr(Path, "read_text", self.mock_read_text) - t1 = ToolGroup("tool-group") - tools = t1.get_label("hostname") - assert "" in tools + Args: + dir_path: Directory path + """ + if str(dir_path).endswith("hostname"): + return ["tool1", "tool2", "__label__"] + else: + return ["hostname"] + + def mock_is_dir(self: Path): + """Mocked directory check""" + return True + + def mock_read_text(self): + """Mocked read_text module for file reading""" + return "" + + def test_target_trigger_file(self, monkeypatch): + """Verify if the trigger file exists""" + + def mock_read_text(self: Path): + """Mocked directory check""" + raise FileNotFoundError("Mock Path.resolve()") + + monkeypatch.setattr( + ToolGroup, "verify_tool_group", self.mock_verify_tool_group + ) + monkeypatch.setattr(os, "listdir", self.mock_listdir) + monkeypatch.setattr(Path, "is_dir", self.mock_is_dir) + monkeypatch.setattr(Path, "read_text", mock_read_text) + with pytest.raises(FileNotFoundError): + tg = ToolGroup("wrong-file") + assert tg.trigger is None + + def test_target_trigger_empty_file(self, monkeypatch): + """verify if the trigger file is empty""" + monkeypatch.setattr( + ToolGroup, "verify_tool_group", self.mock_verify_tool_group + ) + monkeypatch.setattr(os, "listdir", self.mock_listdir) + monkeypatch.setattr(Path, "is_dir", self.mock_is_dir) + monkeypatch.setattr(Path, "read_text", self.mock_read_text) + tg = ToolGroup("tool-group") + assert tg.trigger is None + + def test_target_trigger_file_contents(self, monkeypatch): + """Verify the contesnts of the Trigger file""" + trigger_file_content = "trigger_file_contents" + + def mock_read_text(self: Path): + """Mocked read file check""" + return trigger_file_content + + monkeypatch.setattr( + ToolGroup, "verify_tool_group", self.mock_verify_tool_group + ) + monkeypatch.setattr(os, "listdir", self.mock_listdir) + monkeypatch.setattr(Path, "is_dir", self.mock_is_dir) + monkeypatch.setattr(Path, "read_text", mock_read_text) + tg = ToolGroup("tool-group") + assert tg.trigger is trigger_file_content + + def test_tool_group_empty_dir(self, monkeypatch): + """Verify empty tool group Directory""" + + def mock_listdir(self: Path): + """Return 'Hostname' based on directory path""" + return [] + + monkeypatch.setattr( + ToolGroup, "verify_tool_group", self.mock_verify_tool_group + ) + monkeypatch.setattr(os, "listdir", mock_listdir) + monkeypatch.setattr(Path, "is_dir", self.mock_is_dir) + monkeypatch.setattr(Path, "read_text", self.mock_read_text) + tg = ToolGroup("tool-group") + assert tg.toolnames == {} + assert tg.hostnames == {} + + def test_tool_group_dir(self, monkeypatch): + """Verify Toolnames and hostnames uing get_tools and get_label modules""" + tool_name = {"tool1": {"hostname": ""}, "tool2": {"hostname": ""}} + host_name = {"hostname": {"tool1": "", "tool2": ""}} + + monkeypatch.setattr( + ToolGroup, "verify_tool_group", self.mock_verify_tool_group + ) + monkeypatch.setattr(os, "listdir", self.mock_listdir) + monkeypatch.setattr(Path, "is_dir", self.mock_is_dir) + monkeypatch.setattr(Path, "read_text", self.mock_read_text) + tg = ToolGroup("tool-group") + tools_list = tg.get_tools("hostname") + label = tg.get_label("hostname") + assert tg.toolnames == tool_name + assert tg.hostnames == host_name + assert "tool1" in tools_list + assert "tool2" in tools_list + assert label == ""