forked from jeremyschulman/nanog77-nrfu-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_02_cabling.py
99 lines (80 loc) · 3.39 KB
/
test_02_cabling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2019 Jeremy Schulman, [email protected]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This file provides the pytest framework and test functions used to dynamically
load NRFU cabling test-cases based on the --nrfu-testcasedir command line
option. This file retrieves the device specific "show" output from a file
rather than an actual device to support a dev-demo environment.
"""
import json
import pytest
# import the EOS specific NRFU cabling module so the functions in this file can
# generate the test-case names and invoke the actual NRFU validation function.
from nrfupytesteos import nrfu_cabling as nrfu
@pytest.fixture(scope='module')
def device_lldp_neighbors(device):
"""
This fixture is used to return the EOS result of the 'show lldp neighbors'
command as structured data.
Rather than running the specific EOS show command, we are going to load the
previously captured output. For a real-world example, see the file
`online-demo/test_02_cabling.py`.
Parameters
----------
device : Device instance
Returns
-------
dict
The dictionary output of the "show lldp neighbors" command that was
retrieve from a file rather than the device.
"""
show_file = device.show_outputs_dir.joinpath(
'show-lldp-neighbors.json')
return json.load(show_file.open())
def pytest_generate_tests(metafunc):
"""
pytest will invoke this hook allowing us to dynamically load the device
specific cabling test cases based on the directory the User provided as the
--nrfu-testcasedir command line argument.
Parameters
----------
metafunc : Metafunc instance used to parametrize the test function
"""
testcases_file = metafunc.config._nrfu['testcase_dir'].joinpath(
'testcases-cabling.json')
metafunc.parametrize('testcase',
json.load(testcases_file.open()),
ids=nrfu.name_test)
def test_cabling(device, device_lldp_neighbors, testcase):
"""
pytest will call this function for each test-case item loaded via the
pytest_generate_tests hook function. This function will in turn call the
actual NRFU EOS specific test function `nrfu.test_cabling` that will
validate the specific `testcase` against the actual data
`device_lldp_neighbors`. If `nrfu.test_cabling` detects a failure it will
raise a specific NRFU exception. The pytest framework will catch that
exception and report the test as failed.
Parameters
----------
device : Device instance
device_lldp_neighbors : dict
The EOS structured output of the "show lldp neighbors" command
testcase : dict
A specific test-case from the list of all test-cases loaded.
Raises
------
See `nrfu.test_cabling` docs for details
"""
nrfu.test_cabling(device, actual=device_lldp_neighbors,
testcase=testcase)