-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_skilletcli.py
229 lines (195 loc) · 6.3 KB
/
test_skilletcli.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
from Remotes import Git, Github
from skilletcli import create_context, set_at_path, check_resp, CREDS_FILENAME
from panosxml import KeyDB
from pytest import fixture
from skilletcli import Panos
import os
import pytest
from git import GitCommandError
from pathlib import Path
@fixture
def g():
"""
Test fixture, the Skillet we use for all other testing.
Iron-skillet is the most comprehensive, as such, it is used.
"""
g = Git("https://github.com/PaloAltoNetworks/iron-skillet.git")
g.clone("iron-skillet")
return g
@fixture
def gps():
"""
Another, different test fixture. This one uses a different directory structure.
"""
g = Git("https://github.com/PaloAltoNetworks/GPSkillets.git")
g.clone("gps")
g.branch("panos_v90")
return g
@fixture
def hsk():
"""
A third fixture with one more test structure.
"""
g = Git("https://github.com/PaloAltoNetworks/HomeSkillet.git")
g.clone("hks")
g.branch("panos_v9.0")
return g
def test_build(g):
"""
Test a skillet build based on the fixture.
"""
sc = g.build()
sc.print_all_skillets()
assert len(sc.skillet_map.values()) > 0
def test_context(g):
"""
Test the proper templating of snippets using the YAML configuration file.
"""
sc = g.build()
context = create_context("config_variables.yaml")
sk = sc.get_skillet("panorama")
sk.template(context)
snippets = sk.select_snippets("snippets", ["device_mgt_config"])
for snippet in snippets:
print("{} {}".format(snippet.name, snippet.rendered_xmlstr))
assert len(snippets) > 0
def test_select_entry(g):
"""
Test the selection of a specific entry within a snippet.
"""
sc = g.build()
context = create_context("config_variables.yaml")
sk = sc.get_skillet("panos")
sk.template(context)
snippets = sk.select_snippets("snippets", ["tag/Outbound"])
assert len(snippets) == 1
def test_type_switch():
"""
Test the PANOS type identification.
"""
p = Panos("", "", "", connect=False)
r = p.get_type_from_info("Panorama")
assert r == "panorama"
r = p.get_type_from_info("M-200")
assert r == "panorama"
r = p.get_type_from_info("PA-VM")
assert r == "panos"
# Below are functions for testing individual skillets.
# This provides an interface through pytest to validate that a skillet works.
def test_iron_skillet(g):
"""
Test the "iron-skillet" snippet
"""
sc = g.build()
test_stack = 'snippets'
test_snippets = ['all']
push_test(sc, test_stack, test_snippets)
def test_get_type():
if not os.getenv("SKCLI_DEVICE_TEST"):
pytest.skip("No environment to run device tests against.")
return
addr = os.getenv("SKCLI_ADDRESS")
user = os.getenv("SKCLI_USERNAME")
pw = os.getenv("SKCLI_PASSWORD")
fw = Panos(addr, user=user, pw=pw, debug=True)
fw.get_type()
assert len(fw.system_info.keys()) > 1
def push_test(sc, test_stack, test_snippets):
"""
Given a skillet collection, pushes all snippets and uses Assert to validate they work.
:param sc: SkilletCollection
:return: None
:raises: AssertionError
"""
if not os.getenv("SKCLI_DEVICE_TEST"):
pytest.skip("No environment to run device tests against.")
return
addr = os.getenv("SKCLI_ADDRESS")
user = os.getenv("SKCLI_USERNAME")
pw = os.getenv("SKCLI_PASSWORD")
fw = Panos(addr, user=user, pw=pw, debug=True)
t = fw.get_type()
skillet = sc.get_skillet(t.lower())
skillet.template(None)
snippets = skillet.select_snippets(test_stack, test_snippets)
assert len(snippets) > 0
success_count = 0
for snippet in skillet.select_snippets(test_stack, test_snippets):
print("Doing {} at {}...".format(snippet.name, snippet.rendered_xpath), end="")
r = set_at_path(fw, snippet.rendered_xpath, snippet.rendered_xmlstr)
result = check_resp(r)
assert result
success_count = success_count + 1
print(success_count)
assert success_count > 0
def test_get_creds_file():
kd = KeyDB(CREDS_FILENAME)
kd.reinit()
kd.add_key("test_device", "notarealkey")
v = kd.lookup("test_device")
# Test if not enabled
assert v == None
kd.reinit()
kd.enable()
kd.add_key("test_device", "notarealkey")
v = kd.lookup("test_device")
assert v == "notarealkey"
def test_get_first_real_dir(g, gps):
"""
This test validates the function that searches for the template directory.
"""
template_dirs = [
g.path + os.sep + "templates",
g.path + os.sep,
]
template_dir = g.get_first_real_dir(template_dirs)
# First test should return templates
assert "templates" in template_dir
type_dirs = g.get_type_directories(template_dir)
print(type_dirs)
template_dirs = [
gps.path + os.sep + "templates",
gps.path + os.sep,
]
r = gps.get_first_real_dir(template_dirs)
assert "gps" in r
def test_github():
g = Github()
r = g.index()
assert len(r) >= 1
def test_all_github_repos():
"""
This test function retrieves all of the skillets marked with the "skillets" topic from github
It then clones them, and validates it can retrieve their snippets.
"""
if not os.getenv("TEST_FROM_GITHUB"):
pytest.skip("Github tests not requested.")
return
branches = ['master', 'panos_v90', 'panos_v9.0', 'panos_v8.1', 'panos_v81']
counts = {}
github = Github()
repos = github.index()
for g in repos:
g.clone(g.github_info['name'])
sk = try_all_branches(g, branches)
if not sk:
counts[g.name] = 0
else:
counts[g.name] = sk.snippet_stack.keys()
assert len(counts['iron-skillet']) > 0
assert len(counts['HomeSkillet']) > 0
assert len(counts['GPSkillets']) > 0
def try_all_branches(g, branches):
for branch in branches:
try:
g.branch(branch)
sc = g.build()
sk = sc.get_skillet("panos")
# If it works, return it
return sk
except ValueError:
print("Invalid branch {}:{} - no valid panos objects".format(g.name,branch))
except GitCommandError:
print("Invalid branch {}:{} - branch does not exist".format(g.name,branch))
if __name__ == '__main__':
test_get_type()