Debian Codegraph @

tests/integration/test_run_eos_commands.py

raw symbols SCIP
1 # Copyright (c) 2024-2026 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the LICENSE file.
4 """Test examples/run_eos_commands.py script."""
5
6 from __future__ import annotations
7
8 import runpy
9 from pathlib import Path
10
11 import pytest
12 import respx
13 from yaml import safe_dump
14
15 from anta.inventory import AntaInventory
16
17 DATA = Path(__file__).parent / "data"
18 RUN_EOS_COMMANDS_PATH = Path(__file__).parents[2] / "examples/run_eos_commands.py"
19
20
21 @pytest.mark.parametrize("inventory", [{"count": 3}], indirect=["inventory"])
22 def test_run_eos_commands(capsys: pytest.CaptureFixture[str], inventory: AntaInventory) -> None:
23 """Test run_eos_commands script."""
24 # Create the inventory.yaml file expected by the script
25 # TODO: 2.0.0 this is horrendous - need to align how to dump things properly
26 inventory_path = Path.cwd() / "inventory.yaml"
27 yaml_data = {AntaInventory.INVENTORY_ROOT_KEY: inventory.dump().model_dump()}
28 with inventory_path.open("w") as f:
29 safe_dump(yaml_data, f)
30
31 try:
32 respx.post(path="/command-api", headers={"Content-Type": "application/json-rpc"}, json__params__cmds__0__cmd="show ip bgp summary").respond(
33 json={
34 "result": [
35 {
36 "mocked": "mock",
37 }
38 ],
39 }
40 )
41 # Run the script
42 runpy.run_path(str(RUN_EOS_COMMANDS_PATH), run_name="__main__")
43 captured = capsys.readouterr()
44 # This is only to make sure we get the expected output - what counts is that the script runs.
45 assert "'device-0': [AntaCommand(command='show version', version='latest', revision=None, ofmt='json', output={'modelName': 'pytest'}," in captured.out
46 assert "'device-1': [AntaCommand(command='show version', version='latest', revision=None, ofmt='json', output={'modelName': 'pytest'}," in captured.out
47 assert "'device-2': [AntaCommand(command='show version', version='latest', revision=None, ofmt='json', output={'modelName': 'pytest'}," in captured.out
48 assert "AntaCommand(command='show ip bgp summary', version='latest', revision=None, ofmt='json', output={'mocked': 'mock'}, " in captured.out
49
50 finally:
51 # Cleanup
52 inventory_path.unlink()