Skip to content

Commit 40e7aa0

Browse files
authored
Merge pull request Juniper#469 from rsmekala/tests
Added tests for juniper_junos_rpc
2 parents 47992aa + abdffad commit 40e7aa0

File tree

7 files changed

+344
-6
lines changed

7 files changed

+344
-6
lines changed

module_utils/juniper_junos_common.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,10 @@ def parse_arg_to_list_of_dicts(self,
11731173
if allow_bool_values is True:
11741174
# Try to convert it to a boolean value. Will be None if it
11751175
# can't be converted.
1176-
bool_val = boolean(v)
1176+
try:
1177+
bool_val = boolean(v)
1178+
except TypeError:
1179+
bool_val = None
11771180
if bool_val is not None:
11781181
v = bool_val
11791182
return_item[k] = v
@@ -1851,7 +1854,7 @@ def save_text_output(self, name, format, text):
18511854
if getattr(self, 'destfile', None) is None:
18521855
self.destfile = self.params.get('dest')
18531856
else:
1854-
mode = 'a'
1857+
mode = 'ab'
18551858
elif self.params.get('dest_dir') is not None:
18561859
dest_dir = self.params.get('dest_dir')
18571860
hostname = self.params.get('host')

tests/pb.juniper_junos_config.yml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
- name: Test juniper_junos_config module
3+
hosts: all
4+
connection: local
5+
gather_facts: no
6+
roles:
7+
- Juniper.junos
8+
tasks:
9+
#################
10+
- name: Retrieve the committed configuration
11+
juniper_junos_config:
12+
retrieve: 'committed'
13+
diff: false
14+
check: false
15+
commit: false
16+
register: test1
17+
ignore_errors: True
18+
tags: [ test1 ]
19+
20+
- name: Check TEST 1
21+
assert:
22+
that:
23+
- test1.config
24+
- "'host-name choc-qfx-a;' in test1.config"
25+
#################
26+
- name: Append .foo to the hostname using private config mode.
27+
juniper_junos_config:
28+
config_mode: 'private'
29+
load: 'merge'
30+
lines:
31+
- "set system host-name {{ inventory_hostname }}.foo"
32+
register: test2
33+
ignore_errors: True
34+
tags: [ test2 ]
35+
36+
- name: Check TEST 2
37+
assert:
38+
that:
39+
- test2.diff_lines
40+
- "'+ host-name choc-qfx-a.foo;' in test2.diff_lines"
41+
#################
42+
- name: Rollback to the previous config.
43+
juniper_junos_config:
44+
config_mode: 'private'
45+
rollback: "1"
46+
register: test3
47+
ignore_errors: True
48+
tags: [ test3 ]
49+
50+
- name: Check TEST 3
51+
assert:
52+
that:
53+
- test3.diff_lines
54+
- "'- host-name choc-qfx-a.foo;' in test3.diff_lines"
55+
#################
56+
- name: Creates directory
57+
file:
58+
path: out
59+
state: directory
60+
61+
- name: Configure LLDP
62+
juniper_junos_config:
63+
load: 'merge'
64+
lines:
65+
- "set protocols lldp advertisement-interval 30"
66+
- "set protocols lldp transmit-delay 2"
67+
- "set protocols lldp hold-multiplier 4"
68+
- "set protocols lldp ptopo-configuration-trap-interval 30"
69+
- "set protocols lldp ptopo-configuration-maximum-hold-time 300"
70+
- "set protocols lldp lldp-configuration-notification-interval 30"
71+
- "set protocols lldp interface all disable"
72+
- "set protocols lldp interface ge-1/1/1"
73+
format: 'set'
74+
comment: 'Start LLDP with given options'
75+
dest_dir: './out'
76+
register: test4
77+
ignore_errors: True
78+
tags: [ test4 ]
79+
80+
- name: Rollback to the rescue config.
81+
juniper_junos_config:
82+
rollback: 'rescue'
83+
84+
- name: Check out/choc-qfx-a.diff exists
85+
stat:
86+
path: out/choc-qfx-a.diff
87+
register: stat_result_1
88+
89+
- name: Check TEST 4
90+
assert:
91+
that:
92+
- stat_result_1.stat.exists == True
93+
- test4.diff_lines
94+
- "'+ interface ge-1/1/1;' in test4.diff_lines"
95+
96+
- name: Clean up TEST 4
97+
file:
98+
path: out
99+
state: absent
100+
#################
101+
- name: Retrieve [edit system services] of current committed config.
102+
juniper_junos_config:
103+
retrieve: 'committed'
104+
filter: 'system/services'
105+
diff: true
106+
check: false
107+
commit: false
108+
register: test5
109+
ignore_errors: True
110+
tags: [ test5 ]
111+
112+
- name: Check TEST 5
113+
assert:
114+
that:
115+
- test5.failed == False
116+
- "'system {' in test5.config_lines"
117+
#################
118+
#TODO: Add tests for commit check and commit confirmed workflows

tests/pb.juniper_junos_facts.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: Test junos_get_facts module
2+
- name: Test juniper_junos_facts module
33
hosts: all
44
connection: local
55
gather_facts: no

tests/pb.juniper_junos_jsnapy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: Test junos_ping module
2+
- name: Test juniper_junos_ping module
33
hosts: all
44
connection: local
55
gather_facts: no

tests/pb.juniper_junos_ping.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: Test junos_ping module
2+
- name: Test juniper_junos_ping module
33
hosts: all
44
connection: local
55
gather_facts: no

tests/pb.juniper_junos_pmtud.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: Test junos_pmtud module
2+
- name: Test juniper_junos_pmtud module
33
hosts: all
44
connection: local
55
gather_facts: no

tests/pb.juniper_junos_rpc.yml

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
---
2+
- name: Test juniper_junos_rpc module
3+
hosts: all
4+
connection: local
5+
gather_facts: no
6+
roles:
7+
- Juniper.junos
8+
9+
tasks:
10+
#################
11+
- name: "Execute single RPC get-software-information without any kwargs"
12+
juniper_junos_rpc:
13+
host: "{{ ansible_ssh_host }}"
14+
port: "{{ ansible_ssh_port }}"
15+
user: "{{ ansible_ssh_user }}"
16+
passwd: "{{ ansible_ssh_pass }}"
17+
rpcs:
18+
- "get-software-information"
19+
register: test1
20+
ignore_errors: True
21+
tags: [ test1 ]
22+
23+
- name: Check TEST 1
24+
assert:
25+
that:
26+
- test1.msg == "The RPC executed successfully."
27+
tags: [ test1 ]
28+
29+
#################
30+
- name: "Get Device Configuration with dest"
31+
juniper_junos_rpc:
32+
host: "{{ ansible_ssh_host }}"
33+
port: "{{ ansible_ssh_port }}"
34+
user: "{{ ansible_ssh_user }}"
35+
passwd: "{{ ansible_ssh_pass }}"
36+
rpc: get-config
37+
dest: get_config.conf
38+
register: test2
39+
ignore_errors: True
40+
tags: [ test2 ]
41+
42+
- name: Check that the get_config.conf exists
43+
stat:
44+
path: get_config.conf
45+
register: stat_result
46+
47+
- name: Check TEST 2
48+
assert:
49+
that:
50+
- test2.msg == "The \"get-config\" RPC executed successfully."
51+
- stat_result.stat.exists == True
52+
tags: [ test2 ]
53+
54+
- name: Clean up TEST 2
55+
file:
56+
path: get_config.conf
57+
state: absent
58+
#################
59+
60+
- name: "Get Device Configuration in text"
61+
juniper_junos_rpc:
62+
host: "{{ ansible_ssh_host }}"
63+
port: "{{ ansible_ssh_port }}"
64+
user: "{{ ansible_ssh_user }}"
65+
passwd: "{{ ansible_ssh_pass }}"
66+
rpc: get-interface-information
67+
kwargs: "interface_name=em0"
68+
format: text
69+
register: test3
70+
ignore_errors: True
71+
tags: [ test3 ]
72+
73+
- name: Check TEST 3
74+
assert:
75+
that:
76+
- test3.msg == "The RPC executed successfully."
77+
tags: [ test3 ]
78+
79+
#################
80+
81+
- name: "Execute multiple RPCs without any kwargs"
82+
juniper_junos_rpc:
83+
host: "{{ ansible_ssh_host }}"
84+
port: "{{ ansible_ssh_port }}"
85+
user: "{{ ansible_ssh_user }}"
86+
passwd: "{{ ansible_ssh_pass }}"
87+
rpcs:
88+
- "get-software-information"
89+
- "get-interface-information"
90+
dest: get_config.conf
91+
register: test4
92+
ignore_errors: True
93+
tags: [ test4 ]
94+
95+
- name: Check TEST 4
96+
assert:
97+
that:
98+
- test4.results[0].msg == "The RPC executed successfully."
99+
- test4.results[1].msg == "The RPC executed successfully."
100+
tags: [ test4 ]
101+
102+
- name: Clean up TEST 4
103+
file:
104+
path: get_config.conf
105+
state: absent
106+
107+
#################
108+
109+
- name: "Execute multiple RPCs with multiple kwargs"
110+
juniper_junos_rpc:
111+
host: "{{ ansible_ssh_host }}"
112+
port: "{{ ansible_ssh_port }}"
113+
user: "{{ ansible_ssh_user }}"
114+
passwd: "{{ ansible_ssh_pass }}"
115+
rpcs:
116+
- "get-software-information"
117+
- "get-interface-information"
118+
kwargs:
119+
- {}
120+
- "interface_name=em0"
121+
register: test5
122+
ignore_errors: True
123+
tags: [ test5 ]
124+
125+
- name: Check TEST 5
126+
assert:
127+
that:
128+
- test5.results[0].msg == "The RPC executed successfully."
129+
- test5.results[1].msg == "The RPC executed successfully."
130+
tags: [ test5 ]
131+
132+
#################
133+
- name: Creates directory
134+
file:
135+
path: out
136+
state: directory
137+
138+
- name: "Execute multiple RPCs with multiple kwargs and dest-dir"
139+
juniper_junos_rpc:
140+
host: "{{ ansible_ssh_host }}"
141+
port: "{{ ansible_ssh_port }}"
142+
user: "{{ ansible_ssh_user }}"
143+
passwd: "{{ ansible_ssh_pass }}"
144+
rpcs:
145+
- "get-software-information"
146+
- "get-interface-information"
147+
kwargs:
148+
- {}
149+
- "interface_name=em0"
150+
dest_dir: "out"
151+
register: test6
152+
ignore_errors: True
153+
tags: [ test6 ]
154+
155+
- name: Check get-interface-information.xml exists
156+
stat:
157+
path: "out/{{ ansible_ssh_host }}_get-interface-information.xml"
158+
register: stat_result_1
159+
160+
- name: Check get-software-information.xml exists
161+
stat:
162+
path: "out/{{ ansible_ssh_host }}_get-software-information.xml"
163+
register: stat_result_2
164+
165+
- name: Check TEST 6
166+
assert:
167+
that:
168+
- test6.results[0].msg == "The RPC executed successfully."
169+
- test6.results[1].msg == "The RPC executed successfully."
170+
- stat_result_1.stat.exists == True
171+
- stat_result_2.stat.exists == True
172+
tags: [ test6 ]
173+
174+
- name: Clean up TEST 6
175+
file:
176+
path: out
177+
state: absent
178+
179+
#################
180+
- name: Get Device Configuration for interface
181+
juniper_junos_rpc:
182+
host: "{{ ansible_ssh_host }}"
183+
port: "{{ ansible_ssh_port }}"
184+
user: "{{ ansible_ssh_user }}"
185+
passwd: "{{ ansible_ssh_pass }}"
186+
rpc: get-config
187+
filter_xml: "<configuration><interfaces/></configuration>"
188+
register: test7
189+
ignore_errors: True
190+
tags: [ test7 ]
191+
192+
- name: Check TEST 7
193+
assert:
194+
that:
195+
- test7.msg == "The \"get-config\" RPC executed successfully."
196+
tags: [ test7 ]
197+
198+
#################
199+
- name: "Execute wrong RPC to generate RPC error"
200+
juniper_junos_rpc:
201+
host: "{{ ansible_ssh_host }}"
202+
port: "{{ ansible_ssh_port }}"
203+
user: "{{ ansible_ssh_user }}"
204+
passwd: "{{ ansible_ssh_pass }}"
205+
rpcs:
206+
- "wrong-rpc"
207+
register: test8
208+
ignore_errors: True
209+
tags: [ test8 ]
210+
211+
- name: Check TEST 8
212+
assert:
213+
that:
214+
- '"Unable to execute the RPC" in test8.msg'
215+
tags: [ test8 ]
216+
217+
#################

0 commit comments

Comments
 (0)