From 92240acb5cc600eec60624ece9ed4b9ec43b386f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 23 May 2024 07:06:46 +0200 Subject: Adding upstream version 0.15.0. Signed-off-by: Daniel Baumann --- tests/units/test_models.py | 77 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 3 deletions(-) (limited to 'tests/units/test_models.py') 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.""" @@ -364,6 +411,31 @@ ANTATEST_DATA: list[dict[str, Any]] = [ "test": {"result": "error"}, }, }, + { + "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, @@ -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): -- cgit v1.2.3