/
test_utilities.py
executable file
·288 lines (227 loc) · 9.08 KB
/
test_utilities.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
import os
from os.path import dirname, join, relpath
import sys
import pytest
from netmiko import utilities
from netmiko._textfsm import _clitable as clitable
RESOURCE_FOLDER = join(dirname(dirname(__file__)), "etc")
RELATIVE_RESOURCE_FOLDER = join(dirname(dirname(relpath(__file__))), "etc")
CONFIG_FILENAME = join(RESOURCE_FOLDER, ".netmiko.yml")
def test_load_yaml_file():
"""Read a YAML file successfully"""
filename = join(RESOURCE_FOLDER, "yaml_test.yml")
expected = {
"answer": 42,
"hello": "world",
"complex": {"truth": False, "key": "value"},
}
assert utilities.load_yaml_file(filename) == expected
def test_invalid_yaml_file():
"""Try to read an invalid YAML file"""
filename = join(RESOURCE_FOLDER, "this_should_not_exist.yml")
try:
utilities.load_yaml_file(filename)
except SystemExit as exc:
assert isinstance(exc, SystemExit)
return
assert False
def test_find_cfg_file():
"""
Search for netmiko_tools config file in the following order:
NETMIKO_TOOLS_CFG environment variable
Current directory
Home directory
Look for file named: .netmiko.yml or netmiko.yml
"""
# Search using environment variable (point directly at end file)
os.environ["NETMIKO_TOOLS_CFG"] = join(RESOURCE_FOLDER, ".netmiko.yml")
assert utilities.find_cfg_file() == CONFIG_FILENAME
# Search using environment variable (pointing at directory)
os.environ["NETMIKO_TOOLS_CFG"] = RESOURCE_FOLDER
assert utilities.find_cfg_file() == CONFIG_FILENAME
try:
cwd = os.getcwd()
os.chdir(dirname(__file__))
# Environment var should be preferred over current dir
assert utilities.find_cfg_file() == CONFIG_FILENAME
# Delete env var and verify current dir is returned
del os.environ["NETMIKO_TOOLS_CFG"]
assert utilities.find_cfg_file() == "./.netmiko.yml"
finally:
# Change directory back to previous state
os.chdir(cwd)
# Verify explicit call using full filename
assert utilities.find_cfg_file(CONFIG_FILENAME) == CONFIG_FILENAME
def test_load_cfg_file():
"""Try to load a configuration file"""
expected = {
"rtr1": {
"device_type": "cisco_ios",
"ip": "10.10.10.1",
"username": "admin",
"password": "cisco123",
"secret": "cisco123",
},
"rtr2": {
"device_type": "cisco_ios",
"ip": "10.10.10.2",
"username": "admin",
"password": "cisco123",
"secret": "cisco123",
},
"cisco": ["rtr1", "rtr2"],
}
assert utilities.load_devices(CONFIG_FILENAME) == expected
def test_obtain_all_devices():
"""Dynamically create 'all' group."""
netmiko_tools_load = utilities.load_devices(CONFIG_FILENAME)
expected = {
"rtr1": {
"device_type": "cisco_ios",
"ip": "10.10.10.1",
"username": "admin",
"password": "cisco123",
"secret": "cisco123",
},
"rtr2": {
"device_type": "cisco_ios",
"ip": "10.10.10.2",
"username": "admin",
"password": "cisco123",
"secret": "cisco123",
},
}
result = utilities.obtain_all_devices(netmiko_tools_load)
assert result == expected
def test_find_netmiko_dir():
"""Try to get the netmiko_dir"""
folder = dirname(__file__)
os.environ["NETMIKO_DIR"] = folder
result = utilities.find_netmiko_dir()
assert result[0] == folder
assert result[1].endswith("/tmp")
def test_invalid_netmiko_dir():
"""Try with an invalid netmiko_base_dir"""
os.environ["NETMIKO_DIR"] = "/"
try:
utilities.find_netmiko_dir()
except ValueError as exc:
assert isinstance(exc, ValueError)
return
assert False
def test_string_to_bytes():
"""Convert string to bytes"""
assert utilities.write_bytes("test") == b"test"
def test_bytes_to_bytes():
"""Convert bytes to bytes"""
result = b"hello world"
assert utilities.write_bytes(result) == result
def test_invalid_data_to_bytes():
"""Convert an invalid data type to bytes"""
try:
utilities.write_bytes(456779)
except ValueError as exc:
assert isinstance(exc, ValueError)
return
assert False
def test_ensure_resource_dir_exists():
"""Ensure that the resource folder exists"""
utilities.ensure_dir_exists(RESOURCE_FOLDER)
def test_ensure_file_exists():
"""Ensure that a file makes ensure_dir_exists raise an error"""
try:
utilities.ensure_dir_exists(__file__)
except ValueError as exc:
assert isinstance(exc, ValueError)
return
assert False
def test_clitable_to_dict():
"""Converts TextFSM cli_table object to list of dictionaries"""
table = clitable.CliTable(template_dir=RESOURCE_FOLDER)
text_filename = join(RESOURCE_FOLDER, "textfsm.txt")
template_filename = join(RESOURCE_FOLDER, "cisco_ios_show_version.template")
with open(text_filename) as data_file:
text = data_file.read()
with open(template_filename) as template_file:
table = table._ParseCmdItem(text, template_file)
result = utilities.clitable_to_dict(table)
assert result == [{"model": "4500"}]
def test_get_structured_data():
"""Convert raw CLI output to structured data using TextFSM template"""
os.environ["NET_TEXTFSM"] = RESOURCE_FOLDER
raw_output = "Cisco IOS Software, Catalyst 4500 L3 Switch Software"
result = utilities.get_structured_data(
raw_output, platform="cisco_ios", command="show version"
)
assert result == [{"model": "4500"}]
def test_get_structured_data_relative_path():
"""Test relative path for textfsm ntc directory"""
os.environ["NET_TEXTFSM"] = RELATIVE_RESOURCE_FOLDER
raw_output = "Cisco IOS Software, Catalyst 4500 L3 Switch Software"
result = utilities.get_structured_data(
raw_output, platform="cisco_ios", command="show version"
)
assert result == [{"model": "4500"}]
@pytest.mark.skipif(
sys.version_info >= (3, 8),
reason="The genie package is not available for Python 3.8 yet",
)
def test_get_structured_data_genie():
"""Convert raw CLI output to structured data using Genie"""
raw_output = """Cisco IOS Software, C3560CX Software (C3560CX-UNIVERSALK9-M), Version 15.2(4)E7, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2018 by Cisco Systems, Inc.
Compiled Tue 18-Sep-18 13:20 by prod_rel_team
ROM: Bootstrap program is C3560CX boot loader
BOOTLDR: C3560CX Boot Loader (C3560CX-HBOOT-M) Version 15.2(4r)E5, RELEASE SOFTWARE (fc4)
3560CX uptime is 5 weeks, 1 day, 2 hours, 30 minutes
System returned to ROM by power-on
System restarted at 11:45:26 PDT Tue May 7 2019
System image file is "flash:c3560cx-universalk9-mz.152-4.E7.bin"
Last reload reason: power-on
This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.
A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html
If you require further assistance please contact us by sending email to
export@cisco.com.
License Level: ipservices
License Type: Permanent Right-To-Use
Next reload license Level: ipservices
cisco WS-C3560CX-8PC-S (APM86XXX) processor (revision A0) with 524288K bytes of memory.
Processor board ID FOCXXXXXXXX
Last reset from power-on
5 Virtual Ethernet interfaces
12 Gigabit Ethernet interfaces
The password-recovery mechanism is enabled.
512K bytes of flash-simulated non-volatile configuration memory.
Base ethernet MAC Address : 12:34:56:78:9A:BC
Motherboard assembly number : 86-75309-01
Power supply part number : 867-5309-01
Motherboard serial number : FOCXXXXXXXX
Power supply serial number : FOCXXXXXXXX
Model revision number : A0
Motherboard revision number : A0
Model number : WS-C3560CX-8PC-S
System serial number : FOCXXXXXXXX
Top Assembly Part Number : 86-7530-91
Top Assembly Revision Number : A0
Version ID : V01
CLEI Code Number : CMM1400DRA
Hardware Board Revision Number : 0x02
Switch Ports Model SW Version SW Image
------ ----- ----- ---------- ----------
* 1 12 WS-C3560CX-8PC-S 15.2(4)E7 C3560CX-UNIVERSALK9-M
Configuration register is 0xF
"""
result = utilities.get_structured_data_genie(
raw_output, platform="cisco_xe", command="show version"
)
assert result["version"]["chassis"] == "WS-C3560CX-8PC-S"