forked from eNMS-automation/eNMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_scripts.py
171 lines (149 loc) · 5.08 KB
/
test_scripts.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
from eNMS import db
from os.path import join
from eNMS.scripts.models import (
AnsibleScript,
NapalmConfigScript,
NapalmGettersScript,
NetmikoConfigScript,
FileTransferScript,
Script
)
from tests.test_base import check_blueprints
from werkzeug.datastructures import ImmutableMultiDict
# test the creation of configuration script (netmiko / napalm)
# test the creation of file transfer script (netmiko via SCP)
# test the creation of ansible script
## Netmiko configuration
netmiko_ping = ImmutableMultiDict([
('name', 'netmiko_ping'),
('waiting_time', '0'),
('description', ''),
('vendor', ''),
('operating_system', ''),
('content_type', 'simple'),
('create_script', 'netmiko_config'),
('content', 'ping 1.1.1.1'),
('netmiko_type', 'show_commands'),
('driver', 'cisco_xr_ssh'),
('global_delay_factor', '1.0'),
])
template = '''
{% for interface, properties in subinterfaces.items() %}
interface FastEthernet0/0.{{ interface }}
description {{ properties.aire }}
encapsulation dot1Q {{ properties.dot1Q }}
ip address {{ properties.address }} 255.255.255.248
no ip redirects
ip ospf cost {{ properties.cost }}
{% endfor %}
'''
result = '''
interface FastEthernet0/0.420
description 262
encapsulation dot1Q 420
ip address 10.124.33.250 255.255.255.248
no ip redirects
ip ospf cost 320
interface FastEthernet0/0.418
description 252
encapsulation dot1Q 418
ip address 10.124.33.234 255.255.255.248
no ip redirects
ip ospf cost 520
interface FastEthernet0/0.419
description 261
encapsulation dot1Q 419
ip address 10.124.33.242 255.255.255.248
no ip redirects
ip ospf cost 620
'''
netmiko_jinja2_script = dict([
('name', 'netmiko_subif'),
('waiting_time', '0'),
('description', ''),
('vendor', ''),
('operating_system', ''),
('content_type', 'j2_template'),
('create_script', 'netmiko_config'),
('content', template),
('netmiko_type', 'configuration'),
('driver', 'cisco_xr_ssh'),
('global_delay_factor', '1.0'),
])
napalm_jinja2_script = dict([
('name', 'napalm_subif'),
('waiting_time', '0'),
('description', ''),
('vendor', ''),
('operating_system', ''),
('content_type', 'j2_template'),
('create_script', 'napalm_config'),
('content', template),
('script_type', 'napalm_configuration'),
('action', 'load_merge_candidate')
])
file_transfer_script = ImmutableMultiDict([
('name', 'test'),
('waiting_time', '0'),
('description', ''),
('vendor', ''),
('operating_system', ''),
('driver', 'cisco_ios'),
('source_file', 'path/to/source'),
('dest_file', 'path/to/destination'),
('file_system', 'flash:'),
('direction', 'put'),
('create_script', 'file_transfer'),
])
@check_blueprints('/scripts')
def test_base_scripts(user_client):
user_client.post('/scripts/create_script_netmiko_config', data=netmiko_ping)
assert len(NetmikoConfigScript.query.all()) == 1
path_yaml = join(user_client.application.path, 'scripts', 'interfaces', 'parameters.yaml')
with open(path_yaml, 'rb') as f:
netmiko_jinja2_script['file'] = f
user_client.post('/scripts/create_script_netmiko_config', data=netmiko_jinja2_script)
with open(path_yaml, 'rb') as f:
napalm_jinja2_script['file'] = f
user_client.post('/scripts/create_script_napalm_config', data=napalm_jinja2_script)
assert len(NapalmConfigScript.query.all()) == 1
assert len(Script.query.all()) == 6
netmiko_j2_script = db.session.query(Script).filter_by(name='netmiko_subif').first()
napalm_j2_script = db.session.query(Script).filter_by(name='napalm_subif').first()
# simply removing the space does not work as yaml relies on dict, which
# are not ordered, we use set instead for the test to pass on python 2 and 3
assert set(netmiko_j2_script.content.split('\n')) == set(result.split('\n'))
assert set(napalm_j2_script.content.split('\n')) == set(result.split('\n'))
## file transfer script
user_client.post('scripts/create_script_file_transfer', data=file_transfer_script)
assert len(FileTransferScript.query.all()) == 1
assert len(Script.query.all()) == 7
## NAPALM getters
getters_dict = ImmutableMultiDict([
('name', 'napalm_getters_script'),
('waiting_time', '0'),
('description', ''),
('getters', 'get_interfaces'),
('getters', 'get_interfaces_ip'),
('getters', 'get_lldp_neighbors'),
('create_script', 'napalm_getters')
])
@check_blueprints('/scripts')
def test_getters_script(user_client):
user_client.post('/scripts/create_script_napalm_getters', data=getters_dict)
assert len(NapalmGettersScript.query.all()) == 1
## Ansible script
ansible_script = ImmutableMultiDict([
('name', 'testttt'),
('waiting_time', '0'),
('description', ''),
('vendor', ''),
('operating_system', ''),
('playbook_path', 'test.yml'),
('arguments', '--ask-vault')
])
@check_blueprints('/scripts')
def test_ansible_scripts(user_client):
user_client.post('/scripts/create_script_ansible_playbook', data=ansible_script)
assert len(AnsibleScript.query.all()) == 1
assert len(Script.query.all()) == 4