forked from networktocode/ntc-templates
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test-templates.py
118 lines (99 loc) · 3.46 KB
/
test-templates.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
#!usr/bin/env python
import ansible.runner
import json
import sys
def compare(list_one, list_two):
msg = ''
for el in list_one:
if el not in list_two:
msg = str(el) + " is not found in sample input"
return 1, msg
return 0, msg
if __name__ == "__main__":
HOSTS = 'test_hosts'
runner = ansible.runner.Runner(
module_name='get_test_info',
module_args='',
pattern='localhost',
host_list=HOSTS,
#module_path='../library/')
module_path='/home/travis/build/networktocode/ntc-templates/ntc-ansible/library/')
results = runner.run()
print(results)
tests = results['contacted']['localhost']['tests']
responses = []
for test_case in tests:
filepath = test_case.get('path')
rawfile = test_case.get('rawfile')
platform = test_case.get('platform')
command = test_case.get('command')
args = dict(file=filepath + '/' + rawfile, platform=platform,
command=command, connection='offline',
template_dir='templates/')
runner = ansible.runner.Runner(
module_name='ntc_show_command',
module_args=args,
pattern='localhost',
host_list=HOSTS
)
results = runner.run()
responses.append(results)
# print(json.dumps(responses, indent=4))
with_parsed = []
# print(json.dumps(responses, indent=4))
for rsp in responses:
# print(json.dumps(rsp, indent=4))
# print(rsp['contacted']['localhost']['invocation']['module_args'])
args = rsp['contacted']['localhost']['invocation']['module_args']
split = args.split(' ')
for each in split:
if '.raw' in each:
parsed = each.replace('.raw', '.parsed')
parsed = parsed.lstrip('file=')
runner = ansible.runner.Runner(
module_name='include_vars',
module_args=parsed,
pattern='localhost',
host_list=HOSTS
)
results = runner.run()
# print(json.dumps(results, indent=4))
try:
results['response'] = rsp['contacted']['localhost']['response']
except KeyError:
print('FAILED')
print(each)
print(rsp)
sys.exit(1)
# print(rsp.get('response'))
with_parsed.append(results)
# print(json.dumps(with_parsed, indent=4))
failed = False
for each in with_parsed:
print('****')
print(each)
print('****')
text = each['contacted']['localhost']['invocation']['module_args']
command = text.split('/')[-1].split('.')[0]
parsed_sample = each['contacted']['localhost']['ansible_facts']['parsed_sample']
result = each['response']
rc, msg = compare(result, parsed_sample)
print(command)
if rc != 0:
print('----> failed')
print('msg:')
print(msg)
print('*' * 100)
print('parsed (from parsed file): ')
print(parsed_sample)
print('*' * 100)
print('result (from ntc_show_command): ')
print(result)
failed = True
else:
print('----> passed')
print('=' * 50)
if failed:
sys.exit(1)
else:
sys.exit(0)