diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-23 05:06:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-23 05:06:51 +0000 |
commit | 57f704845136d9d0f0266c880cfa1dd7b902e880 (patch) | |
tree | 1c7ffc0d0bf371b2e3d425efbb1c055a24340dc7 /tests/units/test_models.py | |
parent | Releasing debian version 0.14.0-2. (diff) | |
download | anta-57f704845136d9d0f0266c880cfa1dd7b902e880.tar.xz anta-57f704845136d9d0f0266c880cfa1dd7b902e880.zip |
Merging upstream version 0.15.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/units/test_models.py')
-rw-r--r-- | tests/units/test_models.py | 77 |
1 files changed, 74 insertions, 3 deletions
diff --git a/tests/units/test_models.py b/tests/units/test_models.py index bc6a1ce..180f6bf 100644 --- a/tests/units/test_models.py +++ b/tests/units/test_models.py @@ -153,10 +153,10 @@ class FakeTestWithTemplateBadRender1(AntaTest): class FakeTestWithTemplateBadRender2(AntaTest): - """ANTA test with template that raises an arbitrary exception.""" + """ANTA test with template that raises an arbitrary exception in render().""" name = "FakeTestWithTemplateBadRender2" - description = "ANTA test with template that raises an arbitrary exception" + description = "ANTA test with template that raises an arbitrary exception in render()" categories: ClassVar[list[str]] = [] commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show interface {interface}")] @@ -175,6 +175,53 @@ class FakeTestWithTemplateBadRender2(AntaTest): self.result.is_success(self.instance_commands[0].command) +class FakeTestWithTemplateBadRender3(AntaTest): + """ANTA test with template that gives extra template parameters in render().""" + + name = "FakeTestWithTemplateBadRender3" + description = "ANTA test with template that gives extra template parameters in render()" + categories: ClassVar[list[str]] = [] + commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show interface {interface}")] + + class Input(AntaTest.Input): + """Inputs for FakeTestWithTemplateBadRender3 test.""" + + interface: str + + def render(self, template: AntaTemplate) -> list[AntaCommand]: + """Render function.""" + return [template.render(interface=self.inputs.interface, extra="blah")] + + @AntaTest.anta_test + def test(self) -> None: + """Test function.""" + self.result.is_success(self.instance_commands[0].command) + + +class FakeTestWithTemplateBadTest(AntaTest): + """ANTA test with template that tries to access an undefined template parameter in test().""" + + name = "FakeTestWithTemplateBadTest" + description = "ANTA test with template that tries to access an undefined template parameter in test()" + categories: ClassVar[list[str]] = [] + commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show interface {interface}")] + + class Input(AntaTest.Input): + """Inputs for FakeTestWithTemplateBadTest test.""" + + interface: str + + def render(self, template: AntaTemplate) -> list[AntaCommand]: + """Render function.""" + return [template.render(interface=self.inputs.interface)] + + @AntaTest.anta_test + def test(self) -> None: + """Test function.""" + # The following line must raise AttributeError at runtime + self.result.is_success(self.instance_commands[0].params.wrong_template_param) + + class SkipOnPlatformTest(AntaTest): """ANTA test that is skipped.""" @@ -365,6 +412,31 @@ ANTATEST_DATA: list[dict[str, Any]] = [ }, }, { + "name": "Extra template parameters in render()", + "test": FakeTestWithTemplateBadRender3, + "inputs": {"interface": "Ethernet1"}, + "expected": { + "__init__": { + "result": "error", + "messages": [ + "Exception in tests.units.test_models.FakeTestWithTemplateBadRender3.render(): ValidationError: 1 validation error for AntaParams\n" + "extra\n" + " Extra inputs are not permitted [type=extra_forbidden, input_value='blah', input_type=str]\n" + ], + }, + "test": {"result": "error"}, + }, + }, + { + "name": "Access undefined template param in test()", + "test": FakeTestWithTemplateBadTest, + "inputs": {"interface": "Ethernet1"}, + "expected": { + "__init__": {"result": "unset"}, + "test": {"result": "error", "messages": ["AttributeError: 'AntaParams' object has no attribute 'wrong_template_param'"]}, + }, + }, + { "name": "unskip on platforms", "test": UnSkipOnPlatformTest, "inputs": None, @@ -574,7 +646,6 @@ class TestAntaComamnd: text_cmd = AntaCommand(command="show dummy", ofmt="text", output="blah") text_cmd_2 = AntaCommand(command="show dummy", ofmt="text", output={"not_a": "string"}) msg = "Output of command 'show dummy' is invalid" - msg = "Output of command 'show dummy' is invalid" with pytest.raises(RuntimeError, match=msg): json_cmd.text_output with pytest.raises(RuntimeError, match=msg): |