Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_nmcli.py

raw
1 # Copyright (c) 2017 Ansible Project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import json
8
9 import pytest
10 from ansible.module_utils.basic import AnsibleModule # noqa: F401 # pylint: disable=unused-import
11 from ansible.module_utils.common.text.converters import to_text
12
13 from ansible_collections.community.general.plugins.modules import nmcli
14
15 pytestmark = pytest.mark.usefixtures("patch_ansible_module")
16
17 TESTCASE_CONNECTION = [
18 {
19 "type": "ethernet",
20 "conn_name": "non_existent_nw_device",
21 "state": "absent",
22 "_ansible_check_mode": True,
23 },
24 {
25 "type": "generic",
26 "conn_name": "non_existent_nw_device",
27 "state": "absent",
28 "_ansible_check_mode": True,
29 },
30 {
31 "type": "team",
32 "conn_name": "non_existent_nw_device",
33 "state": "absent",
34 "_ansible_check_mode": True,
35 },
36 {
37 "type": "bond",
38 "conn_name": "non_existent_nw_device",
39 "state": "absent",
40 "_ansible_check_mode": True,
41 },
42 {
43 "type": "bond-slave",
44 "conn_name": "non_existent_nw_device",
45 "state": "absent",
46 "_ansible_check_mode": True,
47 },
48 {
49 "type": "bridge",
50 "conn_name": "non_existent_nw_device",
51 "state": "absent",
52 "_ansible_check_mode": True,
53 },
54 {
55 "type": "vlan",
56 "conn_name": "non_existent_nw_device",
57 "state": "absent",
58 "_ansible_check_mode": True,
59 },
60 {
61 "type": "vxlan",
62 "conn_name": "non_existent_nw_device",
63 "state": "absent",
64 "_ansible_check_mode": True,
65 },
66 {
67 "type": "gre",
68 "conn_name": "non_existent_nw_device",
69 "state": "absent",
70 "_ansible_check_mode": True,
71 },
72 {
73 "type": "ipip",
74 "conn_name": "non_existent_nw_device",
75 "state": "absent",
76 "_ansible_check_mode": True,
77 },
78 {
79 "type": "sit",
80 "conn_name": "non_existent_nw_device",
81 "state": "absent",
82 "_ansible_check_mode": True,
83 },
84 {
85 "type": "dummy",
86 "conn_name": "non_existent_nw_device",
87 "state": "absent",
88 "_ansible_check_mode": True,
89 },
90 {
91 "type": "gsm",
92 "conn_name": "non_existent_nw_device",
93 "state": "absent",
94 "_ansible_check_mode": True,
95 },
96 {
97 "type": "wireguard",
98 "conn_name": "non_existent_nw_device",
99 "state": "absent",
100 "_ansible_check_mode": True,
101 },
102 {
103 "type": "vpn",
104 "conn_name": "non_existent_nw_device",
105 "state": "absent",
106 "_ansible_check_mode": True,
107 },
108 {
109 "type": "infiniband",
110 "conn_name": "non_existent_nw_device",
111 "state": "absent",
112 "_ansible_check_mode": True,
113 },
114 {
115 "type": "macvlan",
116 "conn_name": "non_existent_nw_device",
117 "state": "absent",
118 "_ansible_check_mode": True,
119 },
120 {
121 "type": "loopback",
122 "conn_name": "non_existent_nw_device",
123 "state": "absent",
124 "_ansible_check_mode": True,
125 },
126 ]
127
128 TESTCASE_GENERIC = [
129 {
130 "type": "generic",
131 "conn_name": "non_existent_nw_device",
132 "ifname": "generic_non_existant",
133 "ip4": "10.10.10.10/24",
134 "gw4": "10.10.10.1",
135 "state": "present",
136 "_ansible_check_mode": False,
137 },
138 ]
139
140 TESTCASE_GENERIC_SHOW_OUTPUT = """\
141 connection.id: non_existent_nw_device
142 connection.interface-name: generic_non_existant
143 connection.autoconnect: yes
144 ipv4.method: manual
145 ipv4.addresses: 10.10.10.10/24
146 ipv4.gateway: 10.10.10.1
147 ipv4.route-metric: -1
148 ipv4.ignore-auto-dns: no
149 ipv4.ignore-auto-routes: no
150 ipv4.never-default: no
151 ipv4.may-fail: yes
152 ipv6.method: auto
153 ipv6.ignore-auto-dns: no
154 ipv6.ignore-auto-routes: no
155 """
156
157 TESTCASE_GENERIC_DIFF_CHECK = [
158 {
159 "type": "generic",
160 "conn_name": "non_existent_nw_device",
161 "ifname": "generic_non_existant",
162 "ip4": "10.10.10.10/24",
163 "gw4": "10.10.10.2",
164 "route_metric4": -1,
165 "state": "present",
166 "_ansible_check_mode": False,
167 },
168 ]
169
170 TESTCASE_GENERIC_MODIFY_ROUTING_RULES4 = [
171 {
172 "type": "generic",
173 "conn_name": "non_existent_nw_device",
174 "ifname": "generic_non_existant",
175 "ip4": "10.10.10.10/24",
176 "gw4": "10.10.10.1",
177 "routing_rules4": ["priority 5 from 10.0.0.0/24 table 5000", "priority 10 from 10.0.1.0/24 table 5001"],
178 "state": "present",
179 "_ansible_check_mode": False,
180 },
181 ]
182
183 TESTCASE_GENERIC_MODIFY_ROUTING_RULES6 = [
184 {
185 "type": "generic",
186 "conn_name": "non_existent_nw_device",
187 "ifname": "generic_non_existant",
188 "ip6": "fd00::10/24",
189 "gw6": "fd00::1",
190 "routing_rules6": ["priority 5 from fd00::/24 table 5000", "priority 10 from fd01::/24 table 5001"],
191 "state": "present",
192 "_ansible_check_mode": False,
193 },
194 ]
195
196 TESTCASE_GENERIC_MODIFY_ROUTING_RULES_SHOW_OUTPUT = """\
197 connection.id: non_existent_nw_device
198 connection.interface-name: generic_non_existant
199 connection.autoconnect: yes
200 ipv4.method: manual
201 ipv4.addresses: 10.10.10.10/24
202 ipv4.gateway: 10.10.10.1
203 ipv4.routing-rules: priority 5 from 10.0.0.0/24 table 5000, priority 10 from 10.0.1.0/24 table 5001
204 ipv4.ignore-auto-dns: no
205 ipv4.ignore-auto-routes: no
206 ipv4.never-default: no
207 ipv4.may-fail: yes
208 ipv6.method: auto
209 ipv6.ignore-auto-dns: no
210 ipv6.ignore-auto-routes: no
211 """
212
213 TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE = [
214 {
215 "type": "ethernet",
216 "conn_name": "non_existent_nw_device",
217 "ifname": "ethernet_non_existant",
218 "ip6": "2001:beef:cafe:10::1/64",
219 "routes6": ["fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2"],
220 "method6": "manual",
221 "state": "present",
222 "_ansible_check_mode": False,
223 },
224 {
225 "type": "ethernet",
226 "conn_name": "non_existent_nw_device",
227 "ifname": "ethernet_non_existant",
228 "ip6": "2001:beef:cafe:10::1/64",
229 "routes6_extended": [{"ip": "fd2e:446f:d85d:5::/64", "next_hop": "2001:beef:cafe:10::2"}],
230 "method6": "manual",
231 "state": "present",
232 "_ansible_check_mode": False,
233 },
234 ]
235
236 TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_SHOW_OUTPUT = """\
237 connection.id: non_existent_nw_device
238 connection.interface-name: ethernet_non_existant
239 connection.autoconnect: yes
240 ipv4.method: auto
241 ipv4.ignore-auto-dns: no
242 ipv4.ignore-auto-routes: no
243 ipv4.never-default: no
244 ipv4.may-fail: yes
245 ipv6.method: manual
246 ipv6.addresses: 2001:beef:cafe:10::1/64
247 ipv6.routes: { ip = fd2e:446f:d85d:5::/64, nh = 2001:beef:cafe:10::2 }
248 ipv6.route-metric: -1
249 ipv6.ignore-auto-dns: no
250 ipv6.ignore-auto-routes: no
251 """
252
253 TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC = [
254 {
255 "type": "ethernet",
256 "conn_name": "non_existent_nw_device",
257 "routes4": ["192.168.200.0/24 192.168.1.1"],
258 "route_metric4": 10,
259 "state": "present",
260 "_ansible_check_mode": False,
261 },
262 {
263 "type": "ethernet",
264 "conn_name": "non_existent_nw_device",
265 "routes4_extended": [{"ip": "192.168.200.0/24", "next_hop": "192.168.1.1"}],
266 "route_metric4": 10,
267 "state": "present",
268 "_ansible_check_mode": False,
269 },
270 ]
271
272 TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT = """\
273 connection.id: non_existent_nw_device
274 connection.interface-name: ethernet_non_existant
275 connection.autoconnect: yes
276 ipv4.method: manual
277 ipv4.addresses: 192.168.1.10
278 ipv4.routes: { ip = 192.168.200.0/24, nh = 192.168.1.1 }
279 ipv4.route-metric: 10
280 """
281
282 TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC_CLEAR = [
283 {
284 "type": "ethernet",
285 "conn_name": "non_existent_nw_device",
286 "routes4": [],
287 "state": "present",
288 "_ansible_check_mode": False,
289 "_ansible_diff": True,
290 },
291 {
292 "type": "ethernet",
293 "conn_name": "non_existent_nw_device",
294 "routes4_extended": [],
295 "state": "present",
296 "_ansible_check_mode": False,
297 "_ansible_diff": True,
298 },
299 ]
300
301 TESTCASE_ETHERNET_MOD_IPV6_INT_WITH_ROUTE_AND_METRIC = [
302 {
303 "type": "ethernet",
304 "conn_name": "non_existent_nw_device",
305 "routes6": ["fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2"],
306 "route_metric6": 10,
307 "state": "present",
308 "_ansible_check_mode": False,
309 },
310 {
311 "type": "ethernet",
312 "conn_name": "non_existent_nw_device",
313 "routes6_extended": [{"ip": "fd2e:446f:d85d:5::/64", "next_hop": "2001:beef:cafe:10::2"}],
314 "route_metric6": 10,
315 "state": "present",
316 "_ansible_check_mode": False,
317 },
318 ]
319
320 TESTCASE_ETHERNET_MOD_IPV6_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT = """\
321 connection.id: non_existent_nw_device
322 connection.interface-name: ethernet_non_existant
323 connection.autoconnect: yes
324 ipv6.method: manual
325 ipv6.addresses: 2001:beef:cafe:10::1/64
326 ipv6.routes: { ip = fd2e:446f:d85d:5::/64, nh = 2001:beef:cafe:10::2 }
327 ipv6.route-metric 10
328 """
329
330 TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES = [
331 {
332 "type": "ethernet",
333 "conn_name": "non_existent_nw_device",
334 "ifname": "ethernet_non_existant",
335 "ip6": "2001:beef:cafe:10::1/64",
336 "routes6": ["fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2", "fd2e:8890:abcd:25::/64 2001:beef:cafe:10::5"],
337 "method6": "manual",
338 "state": "present",
339 "_ansible_check_mode": False,
340 },
341 {
342 "type": "ethernet",
343 "conn_name": "non_existent_nw_device",
344 "ifname": "ethernet_non_existant",
345 "ip6": "2001:beef:cafe:10::1/64",
346 "routes6_extended": [
347 {"ip": "fd2e:446f:d85d:5::/64", "next_hop": "2001:beef:cafe:10::2"},
348 {"ip": "fd2e:8890:abcd:25::/64", "next_hop": "2001:beef:cafe:10::5"},
349 ],
350 "method6": "manual",
351 "state": "present",
352 "_ansible_check_mode": False,
353 },
354 ]
355
356 TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_SHOW_OUTPUT = """\
357 connection.id: non_existent_nw_device
358 connection.interface-name: ethernet_non_existant
359 connection.autoconnect: yes
360 ipv4.method: auto
361 ipv4.ignore-auto-dns: no
362 ipv4.ignore-auto-routes: no
363 ipv4.never-default: no
364 ipv4.may-fail: yes
365 ipv6.method: manual
366 ipv6.addresses: 2001:beef:cafe:10::1/64
367 ipv6.routes: { ip = fd2e:446f:d85d:5::/64, nh = 2001:beef:cafe:10::2 }; { ip = fd2e:8890:abcd:25::/64, nh = 2001:beef:cafe:10::5 }
368 ipv6.route-metric: -1
369 ipv6.ignore-auto-dns: no
370 ipv6.ignore-auto-routes: no
371 """
372
373 TESTCASE_ETHERNET_ADD_SRIOV_VFS = [
374 {
375 "type": "ethernet",
376 "conn_name": "non_existent_nw_device",
377 "ifname": "ethernet_non_existant",
378 "sriov": {
379 "total-vfs": 16,
380 "vfs": "0 spoof-check=true vlans=100",
381 },
382 "state": "present",
383 "_ansible_check_mode": False,
384 }
385 ]
386
387 TESTCASE_ETHERNET_ADD_SRIOV_VFS_SHOW_OUTPUT = """\
388 connection.id: non_existent_nw_device
389 connection.interface-name: ethernet_non_existant
390 connection.autoconnect: yes
391 sriov.total-vfs: 16
392 sriov.vfs: 0 spoof-check=true vlans=100
393 """
394
395 TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_AND_METRIC = [
396 {
397 "type": "ethernet",
398 "conn_name": "non_existent_nw_device",
399 "ifname": "ethernet_non_existant",
400 "method4": "disabled",
401 "ip6": "2001:beef:cafe:10::1/64",
402 "routes6": ["fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2"],
403 "route_metric6": 5,
404 "method6": "manual",
405 "state": "present",
406 "_ansible_check_mode": False,
407 },
408 {
409 "type": "ethernet",
410 "conn_name": "non_existent_nw_device",
411 "ifname": "ethernet_non_existant",
412 "method4": "disabled",
413 "ip6": "2001:beef:cafe:10::1/64",
414 "routes6_extended": [{"ip": "fd2e:446f:d85d:5::/64", "next_hop": "2001:beef:cafe:10::2"}],
415 "route_metric6": 5,
416 "method6": "manual",
417 "state": "present",
418 "_ansible_check_mode": False,
419 },
420 ]
421
422 TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT = """\
423 connection.id: non_existent_nw_device
424 connection.interface-name: ethernet_non_existant
425 connection.autoconnect: yes
426 ipv4.method: auto
427 ipv4.ignore-auto-dns: no
428 ipv4.ignore-auto-routes: no
429 ipv4.never-default: no
430 ipv4.may-fail: yes
431 ipv6.method: manual
432 ipv6.addresses: 2001:beef:cafe:10::1/64
433 ipv6.routes: { ip = fd2e:446f:d85d:5::/64, nh = 2001:beef:cafe:10::2 }
434 ipv6.route-metric: 5
435 ipv6.ignore-auto-dns: no
436 ipv6.ignore-auto-routes: no
437 """
438
439 TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_AND_METRIC = [
440 {
441 "type": "ethernet",
442 "conn_name": "non_existent_nw_device",
443 "ifname": "ethernet_non_existant",
444 "method4": "disabled",
445 "ip6": "2001:beef:cafe:10::1/64",
446 "routes6": ["fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2", "fd2e:8890:abcd:25::/64 2001:beef:cafe:10::5"],
447 "route_metric6": 5,
448 "method6": "manual",
449 "state": "present",
450 "_ansible_check_mode": False,
451 },
452 {
453 "type": "ethernet",
454 "conn_name": "non_existent_nw_device",
455 "ifname": "ethernet_non_existant",
456 "method4": "disabled",
457 "ip6": "2001:beef:cafe:10::1/64",
458 "routes6_extended": [
459 {"ip": "fd2e:446f:d85d:5::/64", "next_hop": "2001:beef:cafe:10::2"},
460 {"ip": "fd2e:8890:abcd:25::/64", "next_hop": "2001:beef:cafe:10::5"},
461 ],
462 "route_metric6": 5,
463 "method6": "manual",
464 "state": "present",
465 "_ansible_check_mode": False,
466 },
467 ]
468
469 TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_AND_METRIC_SHOW_OUTPUT = """\
470 connection.id: non_existent_nw_device
471 connection.interface-name: ethernet_non_existant
472 connection.autoconnect: yes
473 ipv4.method: auto
474 ipv4.ignore-auto-dns: no
475 ipv4.ignore-auto-routes: no
476 ipv4.never-default: no
477 ipv4.may-fail: yes
478 ipv6.method: manual
479 ipv6.addresses: 2001:beef:cafe:10::1/64
480 ipv6.routes: { ip = fd2e:446f:d85d:5::/64, nh = 2001:beef:cafe:10::2 }; { ip = fd2e:8890:abcd:25::/64, nh = 2001:beef:cafe:10::5 }
481 ipv6.route-metric: 5
482 ipv6.ignore-auto-dns: no
483 ipv6.ignore-auto-routes: no
484 """
485
486 TESTCASE_GENERIC_DNS4_SEARCH = [
487 {
488 "type": "generic",
489 "conn_name": "non_existent_nw_device",
490 "ifname": "generic_non_existant",
491 "ip4": "10.10.10.10/24",
492 "gw4": "10.10.10.1",
493 "state": "present",
494 "dns4_search": "search.redhat.com",
495 "dns6_search": "search6.redhat.com",
496 "_ansible_check_mode": False,
497 }
498 ]
499
500 TESTCASE_GENERIC_DNS4_SEARCH_SHOW_OUTPUT = """\
501 connection.id: non_existent_nw_device
502 connection.interface-name: generic_non_existant
503 connection.autoconnect: yes
504 ipv4.method: manual
505 ipv4.addresses: 10.10.10.10/24
506 ipv4.gateway: 10.10.10.1
507 ipv4.ignore-auto-dns: no
508 ipv4.ignore-auto-routes: no
509 ipv4.never-default: no
510 ipv4.dns-search: search.redhat.com
511 ipv4.may-fail: yes
512 ipv6.dns-search: search6.redhat.com
513 ipv6.method: auto
514 ipv6.ignore-auto-dns: no
515 ipv6.ignore-auto-routes: no
516 """
517
518 TESTCASE_GENERIC_DNS4_OPTIONS = [
519 {
520 "type": "generic",
521 "conn_name": "non_existent_nw_device",
522 "ifname": "generic_non_existant",
523 "ip4": "10.10.10.10/24",
524 "gw4": "10.10.10.1",
525 "state": "present",
526 "dns4_options": [],
527 "dns6_options": [],
528 "_ansible_check_mode": False,
529 }
530 ]
531
532 TESTCASE_GENERIC_DNS4_OPTIONS_SHOW_OUTPUT = """\
533 connection.id: non_existent_nw_device
534 connection.interface-name: generic_non_existant
535 connection.autoconnect: yes
536 ipv4.method: manual
537 ipv4.addresses: 10.10.10.10/24
538 ipv4.gateway: 10.10.10.1
539 ipv4.ignore-auto-dns: no
540 ipv4.ignore-auto-routes: no
541 ipv4.never-default: no
542 ipv4.dns-options: --
543 ipv4.may-fail: yes
544 ipv6.dns-options: --
545 ipv6.method: auto
546 ipv6.ignore-auto-dns: no
547 ipv6.ignore-auto-routes: no
548 """
549
550 TESTCASE_GENERIC_ZONE = [
551 {
552 "type": "generic",
553 "conn_name": "non_existent_nw_device",
554 "ifname": "generic_non_existant",
555 "ip4": "10.10.10.10/24",
556 "gw4": "10.10.10.1",
557 "state": "present",
558 "zone": "external",
559 "_ansible_check_mode": False,
560 }
561 ]
562
563 TESTCASE_GENERIC_ZONE_SHOW_OUTPUT = """\
564 connection.id: non_existent_nw_device
565 connection.interface-name: generic_non_existant
566 connection.autoconnect: yes
567 connection.zone: external
568 ipv4.method: manual
569 ipv4.addresses: 10.10.10.10/24
570 ipv4.gateway: 10.10.10.1
571 ipv4.ignore-auto-dns: no
572 ipv4.ignore-auto-routes: no
573 ipv4.never-default: no
574 ipv4.may-fail: yes
575 ipv6.method: auto
576 ipv6.ignore-auto-dns: no
577 ipv6.ignore-auto-routes: no
578 """
579
580 TESTCASE_GENERIC_ZONE_ONLY = [
581 {
582 "type": "generic",
583 "conn_name": "non_existent_nw_device",
584 "ifname": "generic_non_existant",
585 "state": "present",
586 "zone": "public",
587 "_ansible_check_mode": False,
588 }
589 ]
590
591 TESTCASE_BOND = [
592 {
593 "type": "bond",
594 "conn_name": "non_existent_nw_device",
595 "ifname": "bond_non_existant",
596 "mode": "active-backup",
597 "xmit_hash_policy": "layer3+4",
598 "ip4": "10.10.10.10/24",
599 "gw4": "10.10.10.1",
600 "state": "present",
601 "primary": "non_existent_primary",
602 "_ansible_check_mode": False,
603 }
604 ]
605
606 TESTCASE_BOND_SHOW_OUTPUT = """\
607 connection.id: non_existent_nw_device
608 connection.interface-name: bond_non_existant
609 connection.autoconnect: yes
610 ipv4.method: manual
611 ipv4.addresses: 10.10.10.10/24
612 ipv4.gateway: 10.10.10.1
613 ipv4.ignore-auto-dns: no
614 ipv4.ignore-auto-routes: no
615 ipv4.never-default: no
616 ipv4.may-fail: yes
617 ipv6.method: auto
618 ipv6.ignore-auto-dns: no
619 ipv6.ignore-auto-routes: no
620 802-3-ethernet.mtu: auto
621 bond.options: mode=active-backup,primary=non_existent_primary,xmit_hash_policy=layer3+4
622 """
623
624 TESTCASE_BOND_ARP = [
625 {
626 "type": "bond",
627 "conn_name": "non_existent_nw_device",
628 "ifname": "bond_non_existant",
629 "mode": "active-backup",
630 "arp_interval": 100,
631 "arp_ip_target": "192.168.1.1",
632 "state": "present",
633 "_ansible_check_mode": False,
634 }
635 ]
636
637 TESTCASE_BOND_ARP_SHOW_OUTPUT = """\
638 connection.id: non_existent_nw_device
639 connection.interface-name: bond_non_existant
640 connection.autoconnect: yes
641 ipv4.method: auto
642 ipv4.ignore-auto-dns: no
643 ipv4.ignore-auto-routes: no
644 ipv4.never-default: no
645 ipv4.may-fail: yes
646 ipv6.method: auto
647 ipv6.ignore-auto-dns: no
648 ipv6.ignore-auto-routes: no
649 802-3-ethernet.mtu: auto
650 bond.options: mode=active-backup,arp_interval=100,arp_ip_target=192.168.1.1
651 """
652
653 TESTCASE_BOND_ARP_MODIFY = [
654 {
655 "type": "bond",
656 "conn_name": "non_existent_nw_device",
657 "ifname": "bond_non_existant",
658 "mode": "active-backup",
659 "arp_interval": 200,
660 "arp_ip_target": "192.168.1.1",
661 "state": "present",
662 "_ansible_check_mode": False,
663 }
664 ]
665
666 TESTCASE_BOND_ARP_TARGET_MODIFY = [
667 {
668 "type": "bond",
669 "conn_name": "non_existent_nw_device",
670 "ifname": "bond_non_existant",
671 "mode": "active-backup",
672 "arp_interval": 100,
673 "arp_ip_target": "192.168.1.2",
674 "state": "present",
675 "_ansible_check_mode": False,
676 }
677 ]
678
679 TESTCASE_BOND_ARP_CHECK_MODE = [
680 {
681 "type": "bond",
682 "conn_name": "non_existent_nw_device",
683 "ifname": "bond_non_existant",
684 "mode": "active-backup",
685 "arp_interval": 100,
686 "arp_ip_target": "192.168.1.1",
687 "state": "present",
688 "_ansible_check_mode": True,
689 }
690 ]
691
692 TESTCASE_BOND_ARP_ZERO = [
693 {
694 "type": "bond",
695 "conn_name": "non_existent_nw_device",
696 "ifname": "bond_non_existant",
697 "mode": "active-backup",
698 "arp_interval": 0,
699 "arp_ip_target": "192.168.1.1",
700 "state": "present",
701 "_ansible_check_mode": False,
702 }
703 ]
704
705 TESTCASE_BOND_ARP_ZERO_SHOW_OUTPUT = """\
706 connection.id: non_existent_nw_device
707 connection.interface-name: bond_non_existant
708 connection.autoconnect: yes
709 ipv4.method: auto
710 ipv4.ignore-auto-dns: no
711 ipv4.ignore-auto-routes: no
712 ipv4.never-default: no
713 ipv4.may-fail: yes
714 ipv6.method: auto
715 ipv6.ignore-auto-dns: no
716 ipv6.ignore-auto-routes: no
717 802-3-ethernet.mtu: auto
718 bond.options: mode=active-backup,arp_interval=0,arp_ip_target=192.168.1.1
719 """
720
721 TESTCASE_BRIDGE = [
722 {
723 "type": "bridge",
724 "conn_name": "non_existent_nw_device",
725 "ifname": "br0_non_existant",
726 "ip4": "10.10.10.10/24",
727 "gw4": "10.10.10.1",
728 "mac": "52:54:00:ab:cd:ef",
729 "maxage": 100,
730 "stp": True,
731 "state": "present",
732 "_ansible_check_mode": False,
733 }
734 ]
735
736 TESTCASE_BRIDGE_SHOW_OUTPUT = """\
737 connection.id: non_existent_nw_device
738 connection.interface-name: br0_non_existant
739 connection.autoconnect: yes
740 ipv4.method: manual
741 ipv4.addresses: 10.10.10.10/24
742 ipv4.gateway: 10.10.10.1
743 ipv4.ignore-auto-dns: no
744 ipv4.ignore-auto-routes: no
745 ipv4.never-default: no
746 ipv4.may-fail: yes
747 ipv6.method: auto
748 ipv6.ignore-auto-dns: no
749 ipv6.ignore-auto-routes: no
750 bridge.mac-address: 52:54:00:AB:CD:EF
751 bridge.stp: yes
752 bridge.max-age: 100
753 bridge.ageing-time: 300
754 bridge.hello-time: 2
755 bridge.priority: 128
756 bridge.forward-delay: 15
757 """
758
759 TESTCASE_BRIDGE_SLAVE = [
760 {
761 "type": "bridge-slave",
762 "conn_name": "non_existent_nw_device",
763 "ifname": "br0_non_existant",
764 "hairpin": True,
765 "path_cost": 100,
766 "state": "present",
767 "_ansible_check_mode": False,
768 }
769 ]
770
771 TESTCASE_BRIDGE_SLAVE_SHOW_OUTPUT = """\
772 connection.id: non_existent_nw_device
773 connection.interface-name: br0_non_existant
774 connection.autoconnect: yes
775 connection.slave-type: bridge
776 ipv4.never-default: no
777 bridge-port.path-cost: 100
778 bridge-port.hairpin-mode: yes
779 bridge-port.priority: 32
780 """
781
782 TESTCASE_TEAM = [
783 {
784 "type": "team",
785 "conn_name": "non_existent_nw_device",
786 "ifname": "team0_non_existant",
787 "state": "present",
788 "_ansible_check_mode": False,
789 }
790 ]
791
792 TESTCASE_TEAM_SHOW_OUTPUT = """\
793 connection.id: non_existent_nw_device
794 connection.interface-name: team0_non_existant
795 connection.autoconnect: yes
796 connection.type: team
797 ipv4.ignore-auto-dns: no
798 ipv4.ignore-auto-routes: no
799 ipv4.never-default: no
800 ipv4.may-fail: yes
801 ipv6.method: auto
802 ipv6.ignore-auto-dns: no
803 ipv6.ignore-auto-routes: no
804 team.runner: roundrobin
805 team.runner-fast-rate: no
806 """
807
808 TESTCASE_TEAM_HWADDR_POLICY_FAILS = [
809 {
810 "type": "team",
811 "conn_name": "non_existent_nw_device",
812 "ifname": "team0_non_existant",
813 "runner_hwaddr_policy": "by_active",
814 "state": "present",
815 "_ansible_check_mode": False,
816 }
817 ]
818
819 TESTCASE_TEAM_RUNNER_FAST_RATE = [
820 {
821 "type": "team",
822 "conn_name": "non_existent_nw_device",
823 "ifname": "team0_non_existant",
824 "runner": "lacp",
825 "runner_fast_rate": True,
826 "state": "present",
827 "_ansible_check_mode": False,
828 }
829 ]
830
831 TESTCASE_TEAM_RUNNER_FAST_RATE_FAILS = [
832 {
833 "type": "team",
834 "conn_name": "non_existent_nw_device",
835 "ifname": "team0_non_existant",
836 "runner_fast_rate": True,
837 "state": "present",
838 "_ansible_check_mode": False,
839 },
840 {
841 "type": "team",
842 "conn_name": "non_existent_nw_device",
843 "ifname": "team0_non_existant",
844 "state": "present",
845 "runner_fast_rate": False,
846 "_ansible_check_mode": False,
847 },
848 {
849 "type": "team",
850 "conn_name": "non_existent_nw_device",
851 "ifname": "team0_non_existant",
852 "state": "present",
853 "runner": "activebackup",
854 "runner_fast_rate": False,
855 "_ansible_check_mode": False,
856 },
857 {
858 "type": "team",
859 "conn_name": "non_existent_nw_device",
860 "ifname": "team0_non_existant",
861 "state": "present",
862 "runner": "activebackup",
863 "runner_fast_rate": True,
864 "_ansible_check_mode": False,
865 },
866 ]
867
868 TESTCASE_TEAM_RUNNER_FAST_RATE_SHOW_OUTPUT = """\
869 connection.id: non_existent_nw_device
870 connection.interface-name: team0_non_existant
871 connection.autoconnect: yes
872 connection.type: team
873 ipv4.ignore-auto-dns: no
874 ipv4.ignore-auto-routes: no
875 ipv4.never-default: no
876 ipv4.may-fail: yes
877 ipv6.method: auto
878 ipv6.ignore-auto-dns: no
879 ipv6.ignore-auto-routes: no
880 team.runner: lacp
881 team.runner-fast-rate: yes
882 """
883
884 TESTCASE_TEAM_SLAVE = [
885 {
886 "type": "team-slave",
887 "conn_name": "non_existent_nw_slaved_device",
888 "ifname": "generic_slaved_non_existant",
889 "master": "team0_non_existant",
890 "state": "present",
891 "_ansible_check_mode": False,
892 }
893 ]
894
895 TESTCASE_TEAM_SLAVE_SHOW_OUTPUT = """\
896 connection.id: non_existent_nw_slaved_device
897 connection.interface-name: generic_slaved_non_existant
898 connection.autoconnect: yes
899 connection.master: team0_non_existant
900 connection.slave-type: team
901 802-3-ethernet.mtu: auto
902 """
903
904 TESTCASE_VLAN = [
905 {
906 "type": "vlan",
907 "conn_name": "non_existent_nw_device",
908 "ifname": "vlan_not_exists",
909 "ip4": "10.10.10.10/24",
910 "gw4": "10.10.10.1",
911 "vlanid": 10,
912 "state": "present",
913 "_ansible_check_mode": False,
914 }
915 ]
916
917 TESTCASE_VLAN_SHOW_OUTPUT = """\
918 connection.id: non_existent_nw_device
919 connection.interface-name: vlan_not_exists
920 connection.autoconnect: yes
921 ipv4.method: manual
922 ipv4.addresses: 10.10.10.10/24
923 ipv4.gateway: 10.10.10.1
924 ipv4.ignore-auto-dns: no
925 ipv4.ignore-auto-routes: no
926 ipv4.never-default: no
927 ipv4.may-fail: yes
928 ipv6.method: auto
929 ipv6.ignore-auto-dns: no
930 ipv6.ignore-auto-routes: no
931 vlan.id: 10
932 802-3-ethernet.mtu: auto
933 """
934
935 TESTCASE_VXLAN = [
936 {
937 "type": "vxlan",
938 "conn_name": "non_existent_nw_device",
939 "ifname": "vxlan-existent_nw_device",
940 "vxlan_id": 11,
941 "vxlan_local": "192.168.225.5",
942 "vxlan_remote": "192.168.225.6",
943 "state": "present",
944 "_ansible_check_mode": False,
945 }
946 ]
947
948 TESTCASE_VXLAN_SHOW_OUTPUT = """\
949 connection.id: non_existent_nw_device
950 connection.interface-name: vxlan-existent_nw_device
951 connection.autoconnect: yes
952 vxlan.id: 11
953 vxlan.local: 192.168.225.5
954 vxlan.remote: 192.168.225.6
955 """
956
957 TESTCASE_VXLAN_MULTICAST = [
958 {
959 "type": "vxlan",
960 "conn_name": "vxlan_multicast_test",
961 "ifname": "vxlan-device",
962 "vxlan_id": 17,
963 "vxlan_parent": "eth1",
964 "vxlan_local": "192.168.1.2",
965 "vxlan_remote": "239.192.0.17",
966 "slave_type": "bridge",
967 "master": "br0",
968 "state": "present",
969 "_ansible_check_mode": False,
970 }
971 ]
972
973 TESTCASE_VXLAN_MULTICAST_SHOW_OUTPUT = """\
974 connection.id: vxlan_multicast_test
975 connection.interface-name: vxlan-device
976 connection.autoconnect: yes
977 connection.slave-type: bridge
978 connection.master: br0
979 vxlan.id: 17
980 vxlan.parent: eth1
981 vxlan.local: 192.168.1.2
982 vxlan.remote: 239.192.0.17
983 """
984
985 TESTCASE_GRE = [
986 {
987 "type": "gre",
988 "conn_name": "non_existent_nw_device",
989 "ifname": "gre-existent_nw_device",
990 "ip_tunnel_dev": "non_existent_gre_device",
991 "ip_tunnel_local": "192.168.225.5",
992 "ip_tunnel_remote": "192.168.225.6",
993 "ip_tunnel_input_key": "1",
994 "ip_tunnel_output_key": "2",
995 "state": "present",
996 "_ansible_check_mode": False,
997 }
998 ]
999
1000 TESTCASE_GRE_SHOW_OUTPUT = """\
1001 connection.id: non_existent_nw_device
1002 connection.interface-name: gre-existent_nw_device
1003 connection.autoconnect: yes
1004 ipv4.ignore-auto-dns: no
1005 ipv4.ignore-auto-routes: no
1006 ipv4.never-default: no
1007 ipv4.may-fail: yes
1008 ipv6.ignore-auto-dns: no
1009 ipv6.ignore-auto-routes: no
1010 ip-tunnel.mode: gre
1011 ip-tunnel.parent: non_existent_gre_device
1012 ip-tunnel.local: 192.168.225.5
1013 ip-tunnel.remote: 192.168.225.6
1014 ip-tunnel.input-key: 1
1015 ip-tunnel.output-key: 2
1016 """
1017
1018 TESTCASE_IPIP = [
1019 {
1020 "type": "ipip",
1021 "conn_name": "non_existent_nw_device",
1022 "ifname": "ipip-existent_nw_device",
1023 "ip_tunnel_dev": "non_existent_ipip_device",
1024 "ip_tunnel_local": "192.168.225.5",
1025 "ip_tunnel_remote": "192.168.225.6",
1026 "state": "present",
1027 "_ansible_check_mode": False,
1028 }
1029 ]
1030
1031 TESTCASE_IPIP_SHOW_OUTPUT = """\
1032 connection.id: non_existent_nw_device
1033 connection.interface-name: ipip-existent_nw_device
1034 connection.autoconnect: yes
1035 ipv4.ignore-auto-dns: no
1036 ipv4.ignore-auto-routes: no
1037 ipv4.never-default: no
1038 ipv4.may-fail: yes
1039 ipv6.ignore-auto-dns: no
1040 ipv6.ignore-auto-routes: no
1041 ip-tunnel.mode: ipip
1042 ip-tunnel.parent: non_existent_ipip_device
1043 ip-tunnel.local: 192.168.225.5
1044 ip-tunnel.remote: 192.168.225.6
1045 """
1046
1047 TESTCASE_SIT = [
1048 {
1049 "type": "sit",
1050 "conn_name": "non_existent_nw_device",
1051 "ifname": "sit-existent_nw_device",
1052 "ip_tunnel_dev": "non_existent_sit_device",
1053 "ip_tunnel_local": "192.168.225.5",
1054 "ip_tunnel_remote": "192.168.225.6",
1055 "state": "present",
1056 "_ansible_check_mode": False,
1057 }
1058 ]
1059
1060 TESTCASE_SIT_SHOW_OUTPUT = """\
1061 connection.id: non_existent_nw_device
1062 connection.interface-name: sit-existent_nw_device
1063 connection.autoconnect: yes
1064 ipv4.ignore-auto-dns: no
1065 ipv4.ignore-auto-routes: no
1066 ipv4.never-default: no
1067 ipv4.may-fail: yes
1068 ipv6.ignore-auto-dns: no
1069 ipv6.ignore-auto-routes: no
1070 ip-tunnel.mode: sit
1071 ip-tunnel.parent: non_existent_sit_device
1072 ip-tunnel.local: 192.168.225.5
1073 ip-tunnel.remote: 192.168.225.6
1074 """
1075
1076 TESTCASE_ETHERNET_DHCP = [
1077 {
1078 "type": "ethernet",
1079 "conn_name": "non_existent_nw_device",
1080 "ifname": "ethernet_non_existant",
1081 "dhcp_client_id": "00:11:22:AA:BB:CC:DD",
1082 "state": "present",
1083 "_ansible_check_mode": False,
1084 }
1085 ]
1086
1087 TESTCASE_ETHERNET_DHCP_SHOW_OUTPUT = """\
1088 connection.id: non_existent_nw_device
1089 connection.interface-name: ethernet_non_existant
1090 connection.autoconnect: yes
1091 802-3-ethernet.mtu: auto
1092 ipv4.method: auto
1093 ipv4.dhcp-client-id: 00:11:22:AA:BB:CC:DD
1094 ipv4.ignore-auto-dns: no
1095 ipv4.ignore-auto-routes: no
1096 ipv4.never-default: no
1097 ipv4.may-fail: yes
1098 ipv6.method: auto
1099 ipv6.ignore-auto-dns: no
1100 ipv6.ignore-auto-routes: no
1101 """
1102
1103 TESTCASE_ETHERNET_STATIC = [
1104 {
1105 "type": "ethernet",
1106 "conn_name": "non_existent_nw_device",
1107 "ifname": "ethernet_non_existant",
1108 "ip4": "10.10.10.10/24",
1109 "gw4": "10.10.10.1",
1110 "dns4": ["1.1.1.1", "8.8.8.8"],
1111 "state": "present",
1112 "_ansible_check_mode": False,
1113 }
1114 ]
1115
1116 TESTCASE_LOOPBACK = [
1117 {
1118 "type": "loopback",
1119 "conn_name": "lo",
1120 "ifname": "lo",
1121 "ip4": "127.0.0.1/8",
1122 "state": "present",
1123 "_ansible_check_mode": False,
1124 }
1125 ]
1126
1127 TESTCASE_LOOPBACK_MODIFY = [
1128 {
1129 "type": "loopback",
1130 "conn_name": "lo",
1131 "ifname": "lo",
1132 "ip4": ["127.0.0.1/8", "127.0.0.2/8"],
1133 "state": "present",
1134 "_ansible_check_mode": False,
1135 }
1136 ]
1137
1138 TESTCASE_ETHERNET_STATIC_SHOW_OUTPUT = """\
1139 connection.id: non_existent_nw_device
1140 connection.interface-name: ethernet_non_existant
1141 connection.autoconnect: yes
1142 802-3-ethernet.mtu: auto
1143 ipv4.method: manual
1144 ipv4.addresses: 10.10.10.10/24
1145 ipv4.gateway: 10.10.10.1
1146 ipv4.ignore-auto-dns: no
1147 ipv4.ignore-auto-routes: no
1148 ipv4.never-default: no
1149 ipv4.may-fail: yes
1150 ipv4.dns: 1.1.1.1,8.8.8.8
1151 ipv6.method: auto
1152 ipv6.ignore-auto-dns: no
1153 ipv6.ignore-auto-routes: no
1154 """
1155
1156 TESTCASE_LOOPBACK_SHOW_OUTPUT = """\
1157 connection.id: lo
1158 connection.interface-name: lo
1159 connection.autoconnect: yes
1160 ipv4.method: manual
1161 ipv4.addresses: 127.0.0.1/8
1162 ipv4.ignore-auto-dns: no
1163 ipv4.ignore-auto-routes: no
1164 ipv4.never-default: no
1165 ipv4.may-fail: yes
1166 ipv6.method: manual
1167 ipv6.ignore-auto-dns: no
1168 ipv6.ignore-auto-routes: no
1169 """
1170
1171 TESTCASE_ETHERNET_STATIC_MULTIPLE_IP4_ADDRESSES = [
1172 {
1173 "type": "ethernet",
1174 "conn_name": "non_existent_nw_device",
1175 "ifname": "ethernet_non_existant",
1176 "ip4": ["10.10.10.10/32", "10.10.20.10/32"],
1177 "gw4": "10.10.10.1",
1178 "dns4": ["1.1.1.1", "8.8.8.8"],
1179 "state": "present",
1180 "_ansible_check_mode": False,
1181 },
1182 {
1183 "type": "ethernet",
1184 "conn_name": "non_existent_nw_device",
1185 "ifname": "ethernet_non_existant",
1186 "ip4": ["10.10.10.10", "10.10.20.10"],
1187 "gw4": "10.10.10.1",
1188 "dns4": ["1.1.1.1", "8.8.8.8"],
1189 "state": "present",
1190 "_ansible_check_mode": False,
1191 },
1192 ]
1193
1194 TESTCASE_ETHERNET_STATIC_IP6_PRIVACY_AND_ADDR_GEN_MODE = [
1195 {
1196 "type": "ethernet",
1197 "conn_name": "non_existent_nw_device",
1198 "ifname": "ethernet_non_existant",
1199 "ip6": "2001:db8::cafe/128",
1200 "gw6": "2001:db8::cafa",
1201 "dns6": ["2001:4860:4860::8888"],
1202 "state": "present",
1203 "ip_privacy6": "prefer-public-addr",
1204 "addr_gen_mode6": "eui64",
1205 "_ansible_check_mode": False,
1206 }
1207 ]
1208
1209 TESTCASE_ETHERNET_STATIC_MULTIPLE_IP6_ADDRESSES = [
1210 {
1211 "type": "ethernet",
1212 "conn_name": "non_existent_nw_device",
1213 "ifname": "ethernet_non_existant",
1214 "ip6": ["2001:db8::cafe/128", "2002:db8::cafe/128"],
1215 "gw6": "2001:db8::cafa",
1216 "dns6": ["2001:4860:4860::8888", "2001:4860:4860::8844"],
1217 "state": "present",
1218 "_ansible_check_mode": False,
1219 },
1220 {
1221 "type": "ethernet",
1222 "conn_name": "non_existent_nw_device",
1223 "ifname": "ethernet_non_existant",
1224 "ip6": ["2001:db8::cafe", "2002:db8::cafe"],
1225 "gw6": "2001:db8::cafa",
1226 "dns6": ["2001:4860:4860::8888", "2001:4860:4860::8844"],
1227 "state": "present",
1228 "_ansible_check_mode": False,
1229 },
1230 ]
1231
1232 TESTCASE_ETHERNET_STATIC_MULTIPLE_IP4_ADDRESSES_SHOW_OUTPUT = """\
1233 connection.id: non_existent_nw_device
1234 connection.interface-name: ethernet_non_existant
1235 connection.autoconnect: yes
1236 802-3-ethernet.mtu: auto
1237 ipv4.method: manual
1238 ipv4.addresses: 10.10.10.10/32, 10.10.20.10/32
1239 ipv4.gateway: 10.10.10.1
1240 ipv4.ignore-auto-dns: no
1241 ipv4.ignore-auto-routes: no
1242 ipv4.never-default: no
1243 ipv4.may-fail: yes
1244 ipv4.dns: 1.1.1.1,8.8.8.8
1245 ipv6.method: auto
1246 ipv6.ignore-auto-dns: no
1247 ipv6.ignore-auto-routes: no
1248 """
1249
1250 TESTCASE_ETHERNET_STATIC_IP6_ADDRESS_SHOW_OUTPUT = """\
1251 connection.id: non_existent_nw_device
1252 connection.interface-name: ethernet_non_existant
1253 connection.autoconnect: yes
1254 802-3-ethernet.mtu: auto
1255 ipv6.method: manual
1256 ipv6.addresses: 2001:db8::cafe/128
1257 ipv6.gateway: 2001:db8::cafa
1258 ipv6.ignore-auto-dns: no
1259 ipv6.ignore-auto-routes: no
1260 ipv6.never-default: no
1261 ipv6.may-fail: yes
1262 ipv6.dns: 2001:4860:4860::8888,2001:4860:4860::8844
1263 ipv4.method: disabled
1264 ipv4.ignore-auto-dns: no
1265 ipv4.ignore-auto-routes: no
1266 ipv4.never-default: no
1267 ipv4.may-fail: yes
1268 """
1269
1270
1271 TESTCASE_ETHERNET_STATIC_MULTIPLE_IP6_ADDRESSES_SHOW_OUTPUT = """\
1272 connection.id: non_existent_nw_device
1273 connection.interface-name: ethernet_non_existant
1274 connection.autoconnect: yes
1275 802-3-ethernet.mtu: auto
1276 ipv6.method: manual
1277 ipv6.addresses: 2001:db8::cafe/128, 2002:db8::cafe/128
1278 ipv6.gateway: 2001:db8::cafa
1279 ipv6.ignore-auto-dns: no
1280 ipv6.ignore-auto-routes: no
1281 ipv6.never-default: no
1282 ipv6.may-fail: yes
1283 ipv6.dns: 2001:4860:4860::8888,2001:4860:4860::8844
1284 ipv4.method: disabled
1285 ipv4.ignore-auto-dns: no
1286 ipv4.ignore-auto-routes: no
1287 ipv4.never-default: no
1288 ipv4.may-fail: yes
1289 """
1290
1291 TESTCASE_WIRELESS = [
1292 {
1293 "type": "wifi",
1294 "conn_name": "non_existent_nw_device",
1295 "ifname": "wireless_non_existant",
1296 "ip4": "10.10.10.10/24",
1297 "ssid": "Brittany",
1298 "wifi": {
1299 "hidden": True,
1300 "mode": "ap",
1301 },
1302 "state": "present",
1303 "_ansible_check_mode": False,
1304 }
1305 ]
1306
1307 TESTCASE_SECURE_WIRELESS = [
1308 {
1309 "type": "wifi",
1310 "conn_name": "non_existent_nw_device",
1311 "ifname": "wireless_non_existant",
1312 "ip4": "10.10.10.10/24",
1313 "ssid": "Brittany",
1314 "wifi_sec": {
1315 "key-mgmt": "wpa-psk",
1316 "psk": "VERY_SECURE_PASSWORD",
1317 },
1318 "state": "present",
1319 "_ansible_check_mode": False,
1320 }
1321 ]
1322
1323 TESTCASE_DEFAULT_WIRELESS_SHOW_OUTPUT = """\
1324 802-11-wireless.ssid: --
1325 802-11-wireless.mode: infrastructure
1326 802-11-wireless.band: --
1327 802-11-wireless.channel: 0
1328 802-11-wireless.bssid: --
1329 802-11-wireless.rate: 0
1330 802-11-wireless.tx-power: 0
1331 802-11-wireless.mac-address: --
1332 802-11-wireless.cloned-mac-address: --
1333 802-11-wireless.generate-mac-address-mask:--
1334 802-11-wireless.mac-address-blacklist: --
1335 802-11-wireless.mac-address-randomization:default
1336 802-11-wireless.mtu: auto
1337 802-11-wireless.seen-bssids: --
1338 802-11-wireless.hidden: no
1339 802-11-wireless.powersave: 0 (default)
1340 802-11-wireless.wake-on-wlan: 0x1 (default)
1341 802-11-wireless.ap-isolation: -1 (default)
1342 """
1343
1344 TESTCASE_DEFAULT_SECURE_WIRELESS_SHOW_OUTPUT = (
1345 TESTCASE_DEFAULT_WIRELESS_SHOW_OUTPUT
1346 + """\
1347 802-11-wireless-security.key-mgmt: --
1348 802-11-wireless-security.wep-tx-keyidx: 0
1349 802-11-wireless-security.auth-alg: --
1350 802-11-wireless-security.proto: --
1351 802-11-wireless-security.pairwise: --
1352 802-11-wireless-security.group: --
1353 802-11-wireless-security.pmf: 0 (default)
1354 802-11-wireless-security.leap-username: --
1355 802-11-wireless-security.wep-key0: --
1356 802-11-wireless-security.wep-key1: --
1357 802-11-wireless-security.wep-key2: --
1358 802-11-wireless-security.wep-key3: --
1359 802-11-wireless-security.wep-key-flags: 0 (none)
1360 802-11-wireless-security.wep-key-type: unknown
1361 802-11-wireless-security.psk: testingtestingtesting
1362 802-11-wireless-security.psk-flags: 0 (none)
1363 802-11-wireless-security.leap-password: --
1364 802-11-wireless-security.leap-password-flags:0 (none)
1365 802-11-wireless-security.wps-method: 0x0 (default)
1366 802-11-wireless-security.fils: 0 (default)
1367 """
1368 )
1369
1370
1371 TESTCASE_DUMMY_STATIC = [
1372 {
1373 "type": "dummy",
1374 "conn_name": "non_existent_nw_device",
1375 "ifname": "dummy_non_existant",
1376 "ip4": "10.10.10.10/24",
1377 "gw4": "10.10.10.1",
1378 "dns4": ["1.1.1.1", "8.8.8.8"],
1379 "ip6": "2001:db8::1/128",
1380 "state": "present",
1381 "_ansible_check_mode": False,
1382 }
1383 ]
1384
1385 TESTCASE_DUMMY_STATIC_SHOW_OUTPUT = """\
1386 connection.id: non_existent_nw_device
1387 connection.interface-name: dummy_non_existant
1388 connection.autoconnect: yes
1389 802-3-ethernet.mtu: auto
1390 ipv4.method: manual
1391 ipv4.addresses: 10.10.10.10/24
1392 ipv4.gateway: 10.10.10.1
1393 ipv4.ignore-auto-dns: no
1394 ipv4.ignore-auto-routes: no
1395 ipv4.never-default: no
1396 ipv4.may-fail: yes
1397 ipv4.dns: 1.1.1.1,8.8.8.8
1398 ipv6.ignore-auto-dns: no
1399 ipv6.ignore-auto-routes: no
1400 ipv6.method: manual
1401 ipv6.addresses: 2001:db8::1/128
1402 """
1403
1404 TESTCASE_DUMMY_STATIC_WITHOUT_MTU_SHOW_OUTPUT = """\
1405 connection.id: non_existent_nw_device
1406 connection.interface-name: dummy_non_existant
1407 connection.autoconnect: yes
1408 ipv4.method: manual
1409 ipv4.addresses: 10.10.10.10/24
1410 ipv4.gateway: 10.10.10.1
1411 ipv4.ignore-auto-dns: no
1412 ipv4.ignore-auto-routes: no
1413 ipv4.never-default: no
1414 ipv4.may-fail: yes
1415 ipv4.dns: 1.1.1.1,8.8.8.8
1416 ipv6.ignore-auto-dns: no
1417 ipv6.ignore-auto-routes: no
1418 ipv6.method: manual
1419 ipv6.addresses: 2001:db8::1/128
1420 """
1421
1422 TESTCASE_DUMMY_STATIC_WITH_CUSTOM_MTU_SHOW_OUTPUT = """\
1423 connection.id: non_existent_nw_device
1424 connection.interface-name: dummy_non_existant
1425 connection.autoconnect: yes
1426 802-3-ethernet.mtu: 1500
1427 ipv4.method: manual
1428 ipv4.addresses: 10.10.10.10/24
1429 ipv4.gateway: 10.10.10.1
1430 ipv4.ignore-auto-dns: no
1431 ipv4.ignore-auto-routes: no
1432 ipv4.never-default: no
1433 ipv4.may-fail: yes
1434 ipv4.dns: 1.1.1.1,8.8.8.8
1435 ipv6.method: auto
1436 ipv6.ignore-auto-dns: no
1437 ipv6.ignore-auto-routes: no
1438 ipv6.method: manual
1439 ipv6.addresses: 2001:db8::1/128
1440 """
1441
1442 TESTCASE_ETHERNET_STATIC_IP6_PRIVACY_AND_ADDR_GEN_MODE_UNCHANGED_OUTPUT = """\
1443 connection.id: non_existent_nw_device
1444 connection.interface-name: ethernet_non_existant
1445 connection.autoconnect: yes
1446 802-3-ethernet.mtu: auto
1447 ipv6.method: manual
1448 ipv6.addresses: 2001:db8::cafe/128
1449 ipv6.gateway: 2001:db8::cafa
1450 ipv6.ignore-auto-dns: no
1451 ipv6.ignore-auto-routes: no
1452 ipv6.never-default: no
1453 ipv6.may-fail: yes
1454 ipv6.ip6-privacy: 1 (enabled, prefer public IP)
1455 ipv6.addr-gen-mode: eui64
1456 ipv6.dns: 2001:4860:4860::8888
1457 ipv4.method: disabled
1458 ipv4.ignore-auto-dns: no
1459 ipv4.ignore-auto-routes: no
1460 ipv4.never-default: no
1461 ipv4.may-fail: yes
1462 """
1463
1464 TESTCASE_GSM = [
1465 {
1466 "type": "gsm",
1467 "conn_name": "non_existent_nw_device",
1468 "ifname": "gsm_non_existant",
1469 "gsm": {
1470 "apn": "internet.telekom",
1471 "username": "t-mobile",
1472 "password": "tm",
1473 "pin": "1234",
1474 },
1475 "method4": "auto",
1476 "state": "present",
1477 "_ansible_check_mode": False,
1478 }
1479 ]
1480
1481 TESTCASE_GSM_SHOW_OUTPUT = """\
1482 connection.id: non_existent_nw_device
1483 connection.type: gsm
1484 connection.interface-name: gsm_non_existant
1485 connection.autoconnect: yes
1486 ipv4.method: auto
1487 ipv4.ignore-auto-dns: no
1488 ipv4.ignore-auto-routes: no
1489 ipv4.never-default: no
1490 ipv4.may-fail: yes
1491 ipv6.method: auto
1492 ipv6.ignore-auto-dns: no
1493 ipv6.ignore-auto-routes: no
1494 gsm.auto-config: no
1495 gsm.number: --
1496 gsm.username: t-mobile
1497 gsm.password: tm
1498 gsm.password-flags: 0 (none)
1499 gsm.apn: "internet.telekom"
1500 gsm.network-id: --
1501 gsm.pin: 1234
1502 gsm.pin-flags: 0 (none)
1503 gsm.home-only: no
1504 gsm.device-id: --
1505 gsm.sim-id: --
1506 gsm.sim-operator-id: --
1507 gsm.mtu: auto
1508 """
1509
1510 TESTCASE_WIREGUARD = [
1511 {
1512 "type": "wireguard",
1513 "conn_name": "non_existent_nw_device",
1514 "ifname": "wg_non_existant",
1515 "wireguard": {
1516 "listen-port": "51820",
1517 "private-key": "<hidden>",
1518 },
1519 "method4": "manual",
1520 "ip4": "10.10.10.10/24",
1521 "method6": "manual",
1522 "ip6": "2001:db8::1/128",
1523 "state": "present",
1524 "_ansible_check_mode": False,
1525 }
1526 ]
1527
1528 TESTCASE_WIREGUARD_SHOW_OUTPUT = """\
1529 connection.id: non_existent_nw_device
1530 connection.type: wireguard
1531 connection.interface-name: wg_non_existant
1532 connection.autoconnect: yes
1533 ipv4.method: manual
1534 ipv4.addresses: 10.10.10.10/24
1535 ipv4.never-default: no
1536 ipv4.may-fail: yes
1537 ipv4.ignore-auto-dns: no
1538 ipv4.ignore-auto-routes: no
1539 ipv6.method: manual
1540 ipv6.addresses: 2001:db8::1/128
1541 ipv6.ignore-auto-dns: no
1542 ipv6.ignore-auto-routes: no
1543 wireguard.private-key: <hidden>
1544 wireguard.private-key-flags: 0 (none)
1545 wireguard.listen-port: 51820
1546 wireguard.fwmark: 0x0
1547 wireguard.peer-routes: yes
1548 wireguard.mtu: 0
1549 wireguard.ip4-auto-default-route: -1 (default)
1550 wireguard.ip6-auto-default-route: -1 (default)
1551 """
1552
1553 TESTCASE_VPN_L2TP = [
1554 {
1555 "type": "vpn",
1556 "conn_name": "vpn_l2tp",
1557 "vpn": {
1558 "permissions": "brittany",
1559 "service-type": "org.freedesktop.NetworkManager.l2tp",
1560 "gateway": "vpn.example.com",
1561 "password-flags": "2",
1562 "user": "brittany",
1563 "ipsec-enabled": "true",
1564 "ipsec-psk": "QnJpdHRhbnkxMjM=",
1565 },
1566 "gw4_ignore_auto": True,
1567 "routes4": ["192.168.200.0/24"],
1568 "autoconnect": "false",
1569 "state": "present",
1570 "_ansible_check_mode": False,
1571 },
1572 ]
1573
1574 TESTCASE_VPN_L2TP_SHOW_OUTPUT = """\
1575 connection.id: vpn_l2tp
1576 connection.type: vpn
1577 connection.autoconnect: no
1578 connection.permissions: brittany
1579 ipv4.method: auto
1580 ipv4.routes: { ip = 192.168.200.0/24 }
1581 ipv4.never-default: no
1582 ipv4.may-fail: yes
1583 ipv4.ignore-auto-dns: no
1584 ipv4.ignore-auto-routes: yes
1585 ipv6.method: auto
1586 ipv6.ignore-auto-dns: no
1587 ipv6.ignore-auto-routes: no
1588 vpn.service-type: org.freedesktop.NetworkManager.l2tp
1589 vpn.data: gateway = vpn.example.com, ipsec-enabled = true, ipsec-psk = QnJpdHRhbnkxMjM=, password-flags = 2, user = brittany
1590 vpn.secrets: ipsec-psk = QnJpdHRhbnkxMjM=
1591 vpn.persistent: no
1592 vpn.timeout: 0
1593 """
1594
1595 TESTCASE_VPN_PPTP = [
1596 {
1597 "type": "vpn",
1598 "conn_name": "vpn_pptp",
1599 "vpn": {
1600 "permissions": "brittany",
1601 "service-type": "org.freedesktop.NetworkManager.pptp",
1602 "gateway": "vpn.example.com",
1603 "password-flags": "2",
1604 "user": "brittany",
1605 },
1606 "autoconnect": "false",
1607 "state": "present",
1608 "_ansible_check_mode": False,
1609 },
1610 ]
1611
1612 TESTCASE_VPN_PPTP_SHOW_OUTPUT = """\
1613 connection.id: vpn_pptp
1614 connection.type: vpn
1615 connection.autoconnect: no
1616 connection.permissions: brittany
1617 ipv4.method: auto
1618 ipv4.never-default: no
1619 ipv4.may-fail: yes
1620 ipv4.ignore-auto-dns: no
1621 ipv4.ignore-auto-routes: no
1622 ipv6.method: auto
1623 ipv6.ignore-auto-dns: no
1624 ipv6.ignore-auto-routes: no
1625 vpn.service-type: org.freedesktop.NetworkManager.pptp
1626 vpn.data: gateway=vpn.example.com, password-flags=2, user=brittany
1627 """
1628
1629 TESTCASE_INFINIBAND_STATIC = [
1630 {
1631 "type": "infiniband",
1632 "conn_name": "non_existent_nw_device",
1633 "ifname": "infiniband_non_existant",
1634 "ip4": "10.10.10.10/24",
1635 "gw4": "10.10.10.1",
1636 "state": "present",
1637 "_ansible_check_mode": False,
1638 }
1639 ]
1640
1641 TESTCASE_INFINIBAND_STATIC_SHOW_OUTPUT = """\
1642 connection.id: non_existent_nw_device
1643 connection.type: infiniband
1644 connection.interface-name: infiniband_non_existant
1645 connection.autoconnect: yes
1646 ipv4.method: manual
1647 ipv4.addresses: 10.10.10.10/24
1648 ipv4.gateway: 10.10.10.1
1649 ipv4.ignore-auto-dns: no
1650 ipv4.ignore-auto-routes: no
1651 ipv4.never-default: no
1652 ipv4.may-fail: yes
1653 ipv6.method: auto
1654 ipv6.ignore-auto-dns: no
1655 ipv6.ignore-auto-routes: no
1656 infiniband.mtu: auto
1657 infiniband.transport-mode: datagram
1658 """
1659
1660 TESTCASE_INFINIBAND_STATIC_MODIFY_TRANSPORT_MODE = [
1661 {
1662 "type": "infiniband",
1663 "conn_name": "non_existent_nw_device",
1664 "transport_mode": "connected",
1665 "state": "present",
1666 "_ansible_check_mode": False,
1667 },
1668 ]
1669
1670 TESTCASE_INFINIBAND_STATIC_MODIFY_TRANSPORT_MODE_SHOW_OUTPUT = """\
1671 connection.id: non_existent_nw_device
1672 connection.interface-name: infiniband_non_existant
1673 infiniband.transport_mode: connected
1674 """
1675
1676 TESTCASE_MACVLAN = [
1677 {
1678 "type": "macvlan",
1679 "conn_name": "non_existent_nw_device",
1680 "ifname": "macvlan_non_existant",
1681 "macvlan": {
1682 "mode": "2",
1683 "parent": "non_existent_parent",
1684 },
1685 "method4": "manual",
1686 "ip4": "10.10.10.10/24",
1687 "method6": "manual",
1688 "ip6": "2001:db8::1/128",
1689 "state": "present",
1690 "_ansible_check_mode": False,
1691 }
1692 ]
1693
1694 TESTCASE_MACVLAN_SHOW_OUTPUT = """\
1695 connection.id: non_existent_nw_device
1696 connection.type: macvlan
1697 connection.interface-name: macvlan_non_existant
1698 connection.autoconnect: yes
1699 ipv4.method: manual
1700 ipv4.addresses: 10.10.10.10/24
1701 ipv4.never-default: no
1702 ipv4.may-fail: yes
1703 ipv4.ignore-auto-dns: no
1704 ipv4.ignore-auto-routes: no
1705 ipv6.method: manual
1706 ipv6.addresses: 2001:db8::1/128
1707 ipv6.ignore-auto-dns: no
1708 ipv6.ignore-auto-routes: no
1709 macvlan.parent: non_existent_parent
1710 macvlan.mode: 2 (bridge)
1711 macvlan.promiscuous: yes
1712 macvlan.tap: no
1713 """
1714
1715 TESTCASE_VRF = [
1716 {
1717 "type": "vrf",
1718 "conn_name": "non_existent_nw_device",
1719 "ifname": "vrf_not_exists",
1720 "ip4": "10.10.10.10/24",
1721 "gw4": "10.10.10.1",
1722 "table": 10,
1723 "state": "present",
1724 "_ansible_check_mode": False,
1725 }
1726 ]
1727
1728 TESTCASE_VRF_SHOW_OUTPUT = """\
1729 connection.id: non_existent_nw_device
1730 connection.interface-name: vrf_not_exists
1731 connection.autoconnect: yes
1732 ipv4.method: manual
1733 ipv4.addresses: 10.10.10.10/24
1734 ipv4.gateway: 10.10.10.1
1735 ipv4.ignore-auto-dns: no
1736 ipv4.ignore-auto-routes: no
1737 ipv4.never-default: no
1738 ipv4.may-fail: yes
1739 ipv6.method: auto
1740 ipv6.ignore-auto-dns: no
1741 ipv6.ignore-auto-routes: no
1742 table: 10
1743 802-3-ethernet.mtu: auto
1744 """
1745
1746
1747 def mocker_set(
1748 mocker, connection_exists=False, execute_return=(0, "", ""), execute_side_effect=None, changed_return=None
1749 ):
1750 """
1751 Common mocker object
1752 """
1753 mocker.patch(
1754 "ansible_collections.community.general.plugins.modules.nmcli.get_best_parsable_locale", return_value="C"
1755 )
1756 get_bin_path = mocker.patch("ansible.module_utils.basic.AnsibleModule.get_bin_path")
1757 get_bin_path.return_value = "/usr/bin/nmcli"
1758 connection = mocker.patch.object(nmcli.Nmcli, "connection_exists")
1759 connection.return_value = connection_exists
1760 execute_command = mocker.patch.object(nmcli.Nmcli, "execute_command")
1761 if execute_return:
1762 execute_command.return_value = execute_return
1763 if execute_side_effect:
1764 execute_command.side_effect = execute_side_effect
1765 if changed_return:
1766 is_connection_changed = mocker.patch.object(nmcli.Nmcli, "is_connection_changed")
1767 is_connection_changed.return_value = changed_return
1768
1769
1770 @pytest.fixture
1771 def mocked_generic_connection_create(mocker):
1772 mocker_set(mocker)
1773
1774
1775 @pytest.fixture
1776 def mocked_connection_exists(mocker):
1777 mocker_set(mocker, connection_exists=True)
1778
1779
1780 @pytest.fixture
1781 def mocked_generic_connection_modify(mocker):
1782 mocker_set(mocker, connection_exists=True, changed_return=(True, dict()))
1783
1784
1785 # TODO: overridden below!
1786 # @pytest.fixture
1787 # def mocked_generic_connection_unchanged(mocker):
1788 # mocker_set(mocker,
1789 # connection_exists=True,
1790 # execute_return=(0, TESTCASE_GENERIC_SHOW_OUTPUT, ""))
1791
1792
1793 @pytest.fixture
1794 def mocked_generic_connection_unchanged(mocker):
1795 mocker_set(
1796 mocker, connection_exists=True, execute_return=(0, TESTCASE_GENERIC_MODIFY_ROUTING_RULES_SHOW_OUTPUT, "")
1797 )
1798
1799
1800 @pytest.fixture
1801 def mocked_generic_connection_dns_search_unchanged(mocker):
1802 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_GENERIC_DNS4_SEARCH_SHOW_OUTPUT, ""))
1803
1804
1805 @pytest.fixture
1806 def mocked_generic_connection_dns_options_unchanged(mocker):
1807 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_GENERIC_DNS4_OPTIONS_SHOW_OUTPUT, ""))
1808
1809
1810 @pytest.fixture
1811 def mocked_generic_connection_zone_unchanged(mocker):
1812 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_GENERIC_ZONE_SHOW_OUTPUT, ""))
1813
1814
1815 @pytest.fixture
1816 def mocked_bond_connection_unchanged(mocker):
1817 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_BOND_SHOW_OUTPUT, ""))
1818
1819
1820 @pytest.fixture
1821 def mocked_bond_arp_connection_unchanged(mocker):
1822 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_BOND_ARP_SHOW_OUTPUT, ""))
1823
1824
1825 @pytest.fixture
1826 def mocked_bond_arp_zero_connection_unchanged(mocker):
1827 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_BOND_ARP_ZERO_SHOW_OUTPUT, ""))
1828
1829
1830 @pytest.fixture
1831 def mocked_bond_arp_connection_modify(mocker):
1832 mocker_set(
1833 mocker,
1834 connection_exists=True,
1835 execute_return=None,
1836 execute_side_effect=(
1837 (0, TESTCASE_BOND_ARP_SHOW_OUTPUT, ""),
1838 (0, "", ""),
1839 ),
1840 )
1841
1842
1843 @pytest.fixture
1844 def mocked_bridge_connection_unchanged(mocker):
1845 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_BRIDGE_SHOW_OUTPUT, ""))
1846
1847
1848 @pytest.fixture
1849 def mocked_bridge_slave_unchanged(mocker):
1850 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_BRIDGE_SLAVE_SHOW_OUTPUT, ""))
1851
1852
1853 @pytest.fixture
1854 def mocked_team_connection_unchanged(mocker):
1855 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_TEAM_SHOW_OUTPUT, ""))
1856
1857
1858 @pytest.fixture
1859 def mocked_team_runner_fast_rate_connection_unchanged(mocker):
1860 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_TEAM_RUNNER_FAST_RATE_SHOW_OUTPUT, ""))
1861
1862
1863 @pytest.fixture
1864 def mocked_team_slave_connection_unchanged(mocker):
1865 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_TEAM_SLAVE_SHOW_OUTPUT, ""))
1866
1867
1868 @pytest.fixture
1869 def mocked_vlan_connection_unchanged(mocker):
1870 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_VLAN_SHOW_OUTPUT, ""))
1871
1872
1873 @pytest.fixture
1874 def mocked_vxlan_connection_unchanged(mocker):
1875 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_VXLAN_SHOW_OUTPUT, ""))
1876
1877
1878 @pytest.fixture
1879 def mocked_gre_connection_unchanged(mocker):
1880 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_GRE_SHOW_OUTPUT, ""))
1881
1882
1883 @pytest.fixture
1884 def mocked_ipip_connection_unchanged(mocker):
1885 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_IPIP_SHOW_OUTPUT, ""))
1886
1887
1888 @pytest.fixture
1889 def mocked_sit_connection_unchanged(mocker):
1890 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_SIT_SHOW_OUTPUT, ""))
1891
1892
1893 @pytest.fixture
1894 def mocked_ethernet_connection_unchanged(mocker):
1895 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_ETHERNET_DHCP, ""))
1896
1897
1898 @pytest.fixture
1899 def mocked_ethernet_connection_dhcp_unchanged(mocker):
1900 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_ETHERNET_DHCP_SHOW_OUTPUT, ""))
1901
1902
1903 @pytest.fixture
1904 def mocked_ethernet_connection_static_unchanged(mocker):
1905 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_ETHERNET_STATIC_SHOW_OUTPUT, ""))
1906
1907
1908 @pytest.fixture
1909 def mocked_ethernet_connection_static_multiple_ip4_addresses_unchanged(mocker):
1910 mocker_set(
1911 mocker,
1912 connection_exists=True,
1913 execute_return=(0, TESTCASE_ETHERNET_STATIC_MULTIPLE_IP4_ADDRESSES_SHOW_OUTPUT, ""),
1914 )
1915
1916
1917 @pytest.fixture
1918 def mocked_ethernet_connection_static_ip6_privacy_and_addr_gen_mode_unchange(mocker):
1919 mocker_set(
1920 mocker,
1921 connection_exists=True,
1922 execute_return=(0, TESTCASE_ETHERNET_STATIC_IP6_PRIVACY_AND_ADDR_GEN_MODE_UNCHANGED_OUTPUT, ""),
1923 )
1924
1925
1926 @pytest.fixture
1927 def mocked_ethernet_connection_static_multiple_ip6_addresses_unchanged(mocker):
1928 mocker_set(
1929 mocker,
1930 connection_exists=True,
1931 execute_return=(0, TESTCASE_ETHERNET_STATIC_MULTIPLE_IP6_ADDRESSES_SHOW_OUTPUT, ""),
1932 )
1933
1934
1935 @pytest.fixture
1936 def mocked_ethernet_connection_static_modify(mocker):
1937 mocker_set(
1938 mocker,
1939 connection_exists=True,
1940 execute_return=None,
1941 execute_side_effect=(
1942 (0, TESTCASE_ETHERNET_STATIC_SHOW_OUTPUT, ""),
1943 (0, "", ""),
1944 ),
1945 )
1946
1947
1948 @pytest.fixture
1949 def mocked_ethernet_connection_with_ipv6_static_address_static_route_create(mocker):
1950 mocker_set(
1951 mocker,
1952 execute_return=None,
1953 execute_side_effect=(
1954 (0, TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_SHOW_OUTPUT, ""),
1955 (0, "", ""),
1956 ),
1957 )
1958
1959
1960 @pytest.fixture
1961 def mocked_ethernet_connection_with_ipv4_static_address_static_route_metric_modify(mocker):
1962 mocker_set(
1963 mocker,
1964 connection_exists=True,
1965 execute_return=None,
1966 execute_side_effect=(
1967 (0, TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT, ""),
1968 (0, "", ""),
1969 ),
1970 )
1971
1972
1973 @pytest.fixture
1974 def mocked_ethernet_connection_with_ipv4_static_address_static_route_metric_clear(mocker):
1975 mocker_set(
1976 mocker,
1977 connection_exists=True,
1978 execute_return=None,
1979 execute_side_effect=(
1980 (0, TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT, ""),
1981 (0, "", ""),
1982 ),
1983 )
1984
1985
1986 @pytest.fixture
1987 def mocked_ethernet_connection_with_ipv6_static_address_static_route_metric_modify(mocker):
1988 mocker_set(
1989 mocker,
1990 connection_exists=True,
1991 execute_return=None,
1992 execute_side_effect=(
1993 (0, TESTCASE_ETHERNET_MOD_IPV6_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT, ""),
1994 (0, "", ""),
1995 ),
1996 )
1997
1998
1999 @pytest.fixture
2000 def mocked_ethernet_connection_with_ipv6_static_address_multiple_static_routes_create(mocker):
2001 mocker_set(
2002 mocker,
2003 execute_return=None,
2004 execute_side_effect=(
2005 (0, TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_SHOW_OUTPUT, ""),
2006 (0, "", ""),
2007 ),
2008 )
2009
2010
2011 @pytest.fixture
2012 def mocked_ethernet_connection_with_sriov_vfs_create(mocker):
2013 mocker_set(mocker, execute_return=(0, TESTCASE_ETHERNET_ADD_SRIOV_VFS_SHOW_OUTPUT, ""))
2014
2015
2016 @pytest.fixture
2017 def mocked_ethernet_connection_with_ipv6_static_address_static_route_with_metric_create(mocker):
2018 mocker_set(
2019 mocker,
2020 execute_return=None,
2021 execute_side_effect=(
2022 (0, TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT, ""),
2023 (0, "", ""),
2024 ),
2025 )
2026
2027
2028 @pytest.fixture
2029 def mocked_ethernet_connection_with_ipv6_static_address_multiple_static_routes_with_metric_create(mocker):
2030 mocker_set(
2031 mocker,
2032 execute_return=None,
2033 execute_side_effect=(
2034 (0, TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_AND_METRIC_SHOW_OUTPUT, ""),
2035 (0, "", ""),
2036 ),
2037 )
2038
2039
2040 @pytest.fixture
2041 def mocked_ethernet_connection_with_ipv6_address_static_modify(mocker):
2042 mocker_set(
2043 mocker,
2044 connection_exists=True,
2045 execute_return=None,
2046 execute_side_effect=(
2047 (0, TESTCASE_ETHERNET_STATIC_IP6_ADDRESS_SHOW_OUTPUT, ""),
2048 (0, "", ""),
2049 ),
2050 )
2051
2052
2053 @pytest.fixture
2054 def mocked_ethernet_connection_dhcp_to_static(mocker):
2055 mocker_set(
2056 mocker,
2057 connection_exists=True,
2058 execute_return=None,
2059 execute_side_effect=(
2060 (0, TESTCASE_ETHERNET_DHCP_SHOW_OUTPUT, ""),
2061 (0, "", ""),
2062 ),
2063 )
2064
2065
2066 @pytest.fixture
2067 def mocked_wireless_create(mocker):
2068 mocker_set(
2069 mocker,
2070 execute_return=None,
2071 execute_side_effect=(
2072 (0, TESTCASE_DEFAULT_WIRELESS_SHOW_OUTPUT, ""),
2073 (0, "", ""),
2074 ),
2075 )
2076
2077
2078 @pytest.fixture
2079 def mocked_secure_wireless_create(mocker):
2080 mocker_set(
2081 mocker,
2082 execute_return=None,
2083 execute_side_effect=(
2084 (0, TESTCASE_DEFAULT_SECURE_WIRELESS_SHOW_OUTPUT, ""),
2085 (0, "", ""),
2086 (0, "", ""),
2087 ),
2088 )
2089
2090
2091 @pytest.fixture
2092 def mocked_secure_wireless_create_failure(mocker):
2093 mocker_set(
2094 mocker,
2095 execute_return=None,
2096 execute_side_effect=(
2097 (0, TESTCASE_DEFAULT_SECURE_WIRELESS_SHOW_OUTPUT, ""),
2098 (1, "", ""),
2099 ),
2100 )
2101
2102
2103 @pytest.fixture
2104 def mocked_secure_wireless_modify(mocker):
2105 mocker_set(
2106 mocker,
2107 connection_exists=True,
2108 execute_return=None,
2109 execute_side_effect=(
2110 (0, TESTCASE_DEFAULT_SECURE_WIRELESS_SHOW_OUTPUT, ""),
2111 (0, "", ""),
2112 (0, "", ""),
2113 (0, "", ""),
2114 ),
2115 )
2116
2117
2118 @pytest.fixture
2119 def mocked_secure_wireless_modify_failure(mocker):
2120 mocker_set(
2121 mocker,
2122 connection_exists=True,
2123 execute_return=None,
2124 execute_side_effect=(
2125 (0, TESTCASE_DEFAULT_SECURE_WIRELESS_SHOW_OUTPUT, ""),
2126 (0, "", ""),
2127 (1, "", ""),
2128 ),
2129 )
2130
2131
2132 @pytest.fixture
2133 def mocked_dummy_connection_static_unchanged(mocker):
2134 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_DUMMY_STATIC_SHOW_OUTPUT, ""))
2135
2136
2137 @pytest.fixture
2138 def mocked_dummy_connection_static_without_mtu_unchanged(mocker):
2139 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_DUMMY_STATIC_WITHOUT_MTU_SHOW_OUTPUT, ""))
2140
2141
2142 @pytest.fixture
2143 def mocked_dummy_connection_static_with_custom_mtu_modify(mocker):
2144 mocker_set(
2145 mocker,
2146 connection_exists=True,
2147 execute_return=None,
2148 execute_side_effect=(
2149 (0, TESTCASE_DUMMY_STATIC_WITH_CUSTOM_MTU_SHOW_OUTPUT, ""),
2150 (0, "", ""),
2151 ),
2152 )
2153
2154
2155 @pytest.fixture
2156 def mocked_gsm_connection_unchanged(mocker):
2157 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_GSM_SHOW_OUTPUT, ""))
2158
2159
2160 @pytest.fixture
2161 def mocked_wireguard_connection_unchanged(mocker):
2162 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_WIREGUARD_SHOW_OUTPUT, ""))
2163
2164
2165 @pytest.fixture
2166 def mocked_vpn_l2tp_connection_unchanged(mocker):
2167 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_VPN_L2TP_SHOW_OUTPUT, ""))
2168
2169
2170 @pytest.fixture
2171 def mocked_vpn_pptp_connection_unchanged(mocker):
2172 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_VPN_PPTP_SHOW_OUTPUT, ""))
2173
2174
2175 @pytest.fixture
2176 def mocked_infiniband_connection_static_unchanged(mocker):
2177 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_INFINIBAND_STATIC_SHOW_OUTPUT, ""))
2178
2179
2180 @pytest.fixture
2181 def mocked_infiniband_connection_static_transport_mode_connected_modify(mocker):
2182 mocker_set(
2183 mocker,
2184 connection_exists=True,
2185 execute_return=None,
2186 execute_side_effect=(
2187 (0, TESTCASE_INFINIBAND_STATIC_MODIFY_TRANSPORT_MODE_SHOW_OUTPUT, ""),
2188 (0, "", ""),
2189 ),
2190 )
2191
2192
2193 @pytest.fixture
2194 def mocked_macvlan_connection_unchanged(mocker):
2195 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_MACVLAN_SHOW_OUTPUT, ""))
2196
2197
2198 @pytest.fixture
2199 def mocked_generic_connection_diff_check(mocker):
2200 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_GENERIC_SHOW_OUTPUT, ""))
2201
2202
2203 @pytest.fixture
2204 def mocked_loopback_connection_unchanged(mocker):
2205 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_LOOPBACK_SHOW_OUTPUT, ""))
2206
2207
2208 @pytest.fixture
2209 def mocked_loopback_connection_modify(mocker):
2210 mocker_set(
2211 mocker,
2212 connection_exists=True,
2213 execute_return=None,
2214 execute_side_effect=(
2215 (0, TESTCASE_LOOPBACK_SHOW_OUTPUT, ""),
2216 (0, "", ""),
2217 ),
2218 )
2219
2220
2221 @pytest.fixture
2222 def mocked_vrf_connection_unchanged(mocker):
2223 mocker_set(mocker, connection_exists=True, execute_return=(0, TESTCASE_VRF_SHOW_OUTPUT, ""))
2224
2225
2226 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND, indirect=["patch_ansible_module"])
2227 def test_bond_connection_create(mocked_generic_connection_create, capfd):
2228 """
2229 Test : Bond connection created
2230 """
2231 with pytest.raises(SystemExit):
2232 nmcli.main()
2233
2234 assert nmcli.Nmcli.execute_command.call_count == 1
2235 arg_list = nmcli.Nmcli.execute_command.call_args_list
2236 args, kwargs = arg_list[0]
2237
2238 assert args[0][0] == "/usr/bin/nmcli"
2239 assert args[0][1] == "con"
2240 assert args[0][2] == "add"
2241 assert args[0][3] == "type"
2242 assert args[0][4] == "bond"
2243 assert args[0][5] == "con-name"
2244 assert args[0][6] == "non_existent_nw_device"
2245
2246 for param in [
2247 "ipv4.gateway",
2248 "primary",
2249 "connection.autoconnect",
2250 "connection.interface-name",
2251 "bond_non_existant",
2252 "mode",
2253 "active-backup",
2254 "ipv4.addresses",
2255 "+bond.options",
2256 "xmit_hash_policy=layer3+4",
2257 ]:
2258 assert param in args[0]
2259
2260 out, err = capfd.readouterr()
2261 results = json.loads(out)
2262 assert not results.get("failed")
2263 assert results["changed"]
2264
2265
2266 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND, indirect=["patch_ansible_module"])
2267 def test_bond_connection_unchanged(mocked_bond_connection_unchanged, capfd):
2268 """
2269 Test : Bond connection unchanged
2270 """
2271 with pytest.raises(SystemExit):
2272 nmcli.main()
2273
2274 out, err = capfd.readouterr()
2275 results = json.loads(out)
2276 assert not results.get("failed")
2277 assert not results["changed"]
2278
2279
2280 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND_ARP, indirect=["patch_ansible_module"])
2281 def test_bond_arp_connection_unchanged(mocked_bond_arp_connection_unchanged):
2282 """
2283 Regression test for #11588:
2284 Bond ARP options must not be reported as changed when already configured.
2285 """
2286 module = nmcli.create_module()
2287 nmcli_module = nmcli.Nmcli(module)
2288
2289 changed, diff = nmcli_module.is_connection_changed()
2290
2291 mismatches = {
2292 key: (diff["before"][key], diff["after"][key])
2293 for key in diff["before"]
2294 if diff["before"][key] != diff["after"][key]
2295 }
2296 assert not changed, f"unexpected changes: {mismatches}"
2297 assert diff["before"]["arp_interval"] == "100"
2298 assert diff["after"]["arp_interval"] == "100"
2299 assert diff["before"]["arp_ip_target"] == "192.168.1.1"
2300 assert diff["after"]["arp_ip_target"] == "192.168.1.1"
2301
2302
2303 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND_ARP, indirect=["patch_ansible_module"])
2304 def test_bond_arp_connection_unchanged_main(mocked_bond_arp_connection_unchanged, capfd):
2305 """
2306 Regression test for #11588 via main():
2307 Second run of a bond with ARP options must report changed=false.
2308 """
2309 with pytest.raises(SystemExit):
2310 nmcli.main()
2311
2312 out, err = capfd.readouterr()
2313 results = json.loads(out)
2314 assert not results.get("failed")
2315 assert not results["changed"]
2316
2317
2318 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND_ARP_ZERO, indirect=["patch_ansible_module"])
2319 def test_bond_arp_interval_zero_unchanged(mocked_bond_arp_zero_connection_unchanged):
2320 """
2321 arp_interval=0 is a valid value and must not be treated as unset.
2322 """
2323 module = nmcli.create_module()
2324 nmcli_module = nmcli.Nmcli(module)
2325
2326 changed, diff = nmcli_module.is_connection_changed()
2327
2328 assert not changed
2329 assert diff["before"]["arp_interval"] == "0"
2330 assert diff["after"]["arp_interval"] == "0"
2331
2332
2333 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND_ARP_MODIFY, indirect=["patch_ansible_module"])
2334 def test_bond_arp_connection_changed(mocked_bond_arp_connection_unchanged):
2335 """
2336 When ARP interval differs from the existing connection, a change must be reported.
2337 """
2338 module = nmcli.create_module()
2339 nmcli_module = nmcli.Nmcli(module)
2340
2341 changed, diff = nmcli_module.is_connection_changed()
2342
2343 assert changed
2344 assert diff["before"]["arp_interval"] == "100"
2345 assert diff["after"]["arp_interval"] == "200"
2346 assert diff["before"]["arp_ip_target"] == diff["after"]["arp_ip_target"] == "192.168.1.1"
2347
2348
2349 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND_ARP_TARGET_MODIFY, indirect=["patch_ansible_module"])
2350 def test_bond_arp_ip_target_changed(mocked_bond_arp_connection_unchanged):
2351 """
2352 When only arp_ip_target differs from the existing connection, a change must be reported.
2353 """
2354 module = nmcli.create_module()
2355 nmcli_module = nmcli.Nmcli(module)
2356
2357 changed, diff = nmcli_module.is_connection_changed()
2358
2359 assert changed
2360 assert diff["before"]["arp_interval"] == diff["after"]["arp_interval"] == "100"
2361 assert diff["before"]["arp_ip_target"] == "192.168.1.1"
2362 assert diff["after"]["arp_ip_target"] == "192.168.1.2"
2363
2364
2365 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND_ARP_CHECK_MODE, indirect=["patch_ansible_module"])
2366 def test_bond_arp_connection_unchanged_check_mode(mocked_bond_arp_connection_unchanged, capfd):
2367 """
2368 Check mode must not report changed when bond ARP options already match.
2369 """
2370 with pytest.raises(SystemExit):
2371 nmcli.main()
2372
2373 assert nmcli.Nmcli.execute_command.call_count == 1
2374 out, err = capfd.readouterr()
2375 results = json.loads(out)
2376 assert not results.get("failed")
2377 assert not results["changed"]
2378
2379
2380 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND_ARP_MODIFY, indirect=["patch_ansible_module"])
2381 def test_bond_arp_connection_modify(mocked_bond_arp_connection_modify, capfd):
2382 """
2383 Test : Bond connection modify uses bond.options syntax for ARP options
2384 """
2385 with pytest.raises(SystemExit):
2386 nmcli.main()
2387
2388 assert nmcli.Nmcli.execute_command.call_count == 2
2389 arg_list = nmcli.Nmcli.execute_command.call_args_list
2390 modify_args, modify_kw = arg_list[1]
2391
2392 modify_args_text = [to_text(x) for x in modify_args[0]]
2393 assert modify_args[0][2] == "modify"
2394 assert "+bond.options" in modify_args_text
2395 assert "arp_interval=200" in modify_args_text
2396
2397 out, err = capfd.readouterr()
2398 results = json.loads(out)
2399 assert not results.get("failed")
2400 assert results["changed"]
2401
2402
2403 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BOND_ARP, indirect=["patch_ansible_module"])
2404 def test_bond_arp_connection_create(mocked_generic_connection_create, capfd):
2405 """
2406 Test : Bond connection with ARP options created via bond.options syntax
2407 """
2408 with pytest.raises(SystemExit):
2409 nmcli.main()
2410
2411 assert nmcli.Nmcli.execute_command.call_count == 1
2412 arg_list = nmcli.Nmcli.execute_command.call_args_list
2413 args, kwargs = arg_list[0]
2414
2415 args_text = [to_text(x) for x in args[0]]
2416 assert "+bond.options" in args_text
2417 assert "arp_interval=100" in args_text
2418 assert "arp_ip_target=192.168.1.1" in args_text
2419
2420 out, err = capfd.readouterr()
2421 results = json.loads(out)
2422 assert not results.get("failed")
2423 assert results["changed"]
2424
2425
2426 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC, indirect=["patch_ansible_module"])
2427 def test_generic_connection_create(mocked_generic_connection_create, capfd):
2428 """
2429 Test : Generic connection created
2430 """
2431 with pytest.raises(SystemExit):
2432 nmcli.main()
2433
2434 assert nmcli.Nmcli.execute_command.call_count == 1
2435 arg_list = nmcli.Nmcli.execute_command.call_args_list
2436 args, kwargs = arg_list[0]
2437
2438 assert args[0][0] == "/usr/bin/nmcli"
2439 assert args[0][1] == "con"
2440 assert args[0][2] == "add"
2441 assert args[0][3] == "type"
2442 assert args[0][4] == "generic"
2443 assert args[0][5] == "con-name"
2444 assert args[0][6] == "non_existent_nw_device"
2445
2446 for param in ["connection.autoconnect", "ipv4.gateway", "ipv4.addresses"]:
2447 assert param in args[0]
2448
2449 out, err = capfd.readouterr()
2450 results = json.loads(out)
2451 assert not results.get("failed")
2452 assert results["changed"]
2453
2454
2455 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC, indirect=["patch_ansible_module"])
2456 def test_generic_connection_modify(mocked_generic_connection_modify, capfd):
2457 """
2458 Test : Generic connection modify
2459 """
2460 with pytest.raises(SystemExit):
2461 nmcli.main()
2462
2463 assert nmcli.Nmcli.execute_command.call_count == 1
2464 arg_list = nmcli.Nmcli.execute_command.call_args_list
2465 args, kwargs = arg_list[0]
2466
2467 assert args[0][0] == "/usr/bin/nmcli"
2468 assert args[0][1] == "con"
2469 assert args[0][2] == "modify"
2470 assert args[0][3] == "non_existent_nw_device"
2471
2472 for param in ["ipv4.gateway", "ipv4.addresses"]:
2473 assert param in args[0]
2474
2475 out, err = capfd.readouterr()
2476 results = json.loads(out)
2477 assert not results.get("failed")
2478 assert results["changed"]
2479
2480
2481 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC, indirect=["patch_ansible_module"])
2482 def test_generic_connection_unchanged(mocked_generic_connection_unchanged, capfd):
2483 """
2484 Test : Generic connection unchanged
2485 """
2486 with pytest.raises(SystemExit):
2487 nmcli.main()
2488
2489 out, err = capfd.readouterr()
2490 results = json.loads(out)
2491 assert not results.get("failed")
2492 assert not results["changed"]
2493
2494
2495 @pytest.mark.parametrize(
2496 "patch_ansible_module", TESTCASE_GENERIC_MODIFY_ROUTING_RULES4, indirect=["patch_ansible_module"]
2497 )
2498 def test_generic_connection_modify_routing_rules4(mocked_generic_connection_create, capfd):
2499 """
2500 Test : Generic connection modified with routing-rules4
2501 """
2502 with pytest.raises(SystemExit):
2503 nmcli.main()
2504
2505 assert nmcli.Nmcli.execute_command.call_count == 1
2506 arg_list = nmcli.Nmcli.execute_command.call_args_list
2507 args, kwargs = arg_list[0]
2508
2509 assert "ipv4.routing-rules" in args[0]
2510
2511 out, err = capfd.readouterr()
2512 results = json.loads(out)
2513 assert not results.get("failed")
2514 assert results["changed"]
2515
2516
2517 @pytest.mark.parametrize(
2518 "patch_ansible_module", TESTCASE_GENERIC_MODIFY_ROUTING_RULES6, indirect=["patch_ansible_module"]
2519 )
2520 def test_generic_connection_modify_routing_rules6(mocked_generic_connection_create, capfd):
2521 """
2522 Test : Generic connection modified with routing-rules6
2523 """
2524 with pytest.raises(SystemExit):
2525 nmcli.main()
2526
2527 assert nmcli.Nmcli.execute_command.call_count == 1
2528 arg_list = nmcli.Nmcli.execute_command.call_args_list
2529 args, kwargs = arg_list[0]
2530
2531 assert "ipv6.routing-rules" in args[0]
2532 idx = args[0].index("ipv6.routing-rules")
2533 assert args[0][idx + 1] == "priority 5 from fd00::/24 table 5000,priority 10 from fd01::/24 table 5001"
2534
2535 out, err = capfd.readouterr()
2536 results = json.loads(out)
2537 assert not results.get("failed")
2538 assert results["changed"]
2539
2540
2541 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_DNS4_SEARCH, indirect=["patch_ansible_module"])
2542 def test_generic_connection_create_dns_search(mocked_generic_connection_create, capfd):
2543 """
2544 Test : Generic connection created with dns search
2545 """
2546 with pytest.raises(SystemExit):
2547 nmcli.main()
2548
2549 assert nmcli.Nmcli.execute_command.call_count == 1
2550 arg_list = nmcli.Nmcli.execute_command.call_args_list
2551 args, kwargs = arg_list[0]
2552
2553 assert "ipv4.dns-search" in args[0]
2554 assert "ipv6.dns-search" in args[0]
2555
2556 out, err = capfd.readouterr()
2557 results = json.loads(out)
2558 assert not results.get("failed")
2559 assert results["changed"]
2560
2561
2562 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_DNS4_SEARCH, indirect=["patch_ansible_module"])
2563 def test_generic_connection_modify_dns_search(mocked_generic_connection_create, capfd):
2564 """
2565 Test : Generic connection modified with dns search
2566 """
2567 with pytest.raises(SystemExit):
2568 nmcli.main()
2569
2570 assert nmcli.Nmcli.execute_command.call_count == 1
2571 arg_list = nmcli.Nmcli.execute_command.call_args_list
2572 args, kwargs = arg_list[0]
2573
2574 assert "ipv4.dns-search" in args[0]
2575 assert "ipv6.dns-search" in args[0]
2576
2577 out, err = capfd.readouterr()
2578 results = json.loads(out)
2579 assert not results.get("failed")
2580 assert results["changed"]
2581
2582
2583 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_DNS4_SEARCH, indirect=["patch_ansible_module"])
2584 def test_generic_connection_dns_search_unchanged(mocked_generic_connection_dns_search_unchanged, capfd):
2585 """
2586 Test : Generic connection with dns search unchanged
2587 """
2588 with pytest.raises(SystemExit):
2589 nmcli.main()
2590
2591 out, err = capfd.readouterr()
2592 results = json.loads(out)
2593 assert not results.get("failed")
2594 assert not results["changed"]
2595
2596
2597 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_DNS4_OPTIONS, indirect=["patch_ansible_module"])
2598 def test_generic_connection_create_dns_options(mocked_generic_connection_create, capfd):
2599 """
2600 Test : Generic connection created with dns options
2601 """
2602 with pytest.raises(SystemExit):
2603 nmcli.main()
2604
2605 assert nmcli.Nmcli.execute_command.call_count == 1
2606 arg_list = nmcli.Nmcli.execute_command.call_args_list
2607 args, kwargs = arg_list[0]
2608
2609 assert "ipv4.dns-options" in args[0]
2610 assert "ipv6.dns-options" in args[0]
2611
2612 out, err = capfd.readouterr()
2613 results = json.loads(out)
2614 assert not results.get("failed")
2615 assert results["changed"]
2616
2617
2618 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_DNS4_OPTIONS, indirect=["patch_ansible_module"])
2619 def test_generic_connection_modify_dns_options(mocked_generic_connection_create, capfd):
2620 """
2621 Test : Generic connection modified with dns options
2622 """
2623 with pytest.raises(SystemExit):
2624 nmcli.main()
2625
2626 assert nmcli.Nmcli.execute_command.call_count == 1
2627 arg_list = nmcli.Nmcli.execute_command.call_args_list
2628 args, kwargs = arg_list[0]
2629
2630 assert "ipv4.dns-options" in args[0]
2631 assert "ipv6.dns-options" in args[0]
2632
2633 out, err = capfd.readouterr()
2634 results = json.loads(out)
2635 assert not results.get("failed")
2636 assert results["changed"]
2637
2638
2639 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_DNS4_OPTIONS, indirect=["patch_ansible_module"])
2640 def test_generic_connection_dns_options_unchanged(mocked_generic_connection_dns_options_unchanged, capfd):
2641 """
2642 Test : Generic connection with dns options unchanged
2643 """
2644 with pytest.raises(SystemExit):
2645 nmcli.main()
2646
2647 out, err = capfd.readouterr()
2648 results = json.loads(out)
2649 assert not results.get("failed")
2650 assert not results["changed"]
2651
2652
2653 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_CONNECTION, indirect=["patch_ansible_module"])
2654 def test_dns4_none(mocked_connection_exists, capfd):
2655 """
2656 Test if DNS4 param is None
2657 """
2658 with pytest.raises(SystemExit):
2659 nmcli.main()
2660
2661 out, err = capfd.readouterr()
2662 results = json.loads(out)
2663 assert not results.get("failed")
2664 assert results["changed"]
2665
2666
2667 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_ZONE, indirect=["patch_ansible_module"])
2668 def test_generic_connection_create_zone(mocked_generic_connection_create, capfd):
2669 """
2670 Test : Generic connection created with zone
2671 """
2672 with pytest.raises(SystemExit):
2673 nmcli.main()
2674
2675 assert nmcli.Nmcli.execute_command.call_count == 1
2676 arg_list = nmcli.Nmcli.execute_command.call_args_list
2677 args, kwargs = arg_list[0]
2678
2679 assert "connection.zone" in args[0]
2680
2681 out, err = capfd.readouterr()
2682 results = json.loads(out)
2683 assert not results.get("failed")
2684 assert results["changed"]
2685
2686
2687 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_ZONE, indirect=["patch_ansible_module"])
2688 def test_generic_connection_modify_zone(mocked_generic_connection_create, capfd):
2689 """
2690 Test : Generic connection modified with zone
2691 """
2692 with pytest.raises(SystemExit):
2693 nmcli.main()
2694
2695 assert nmcli.Nmcli.execute_command.call_count == 1
2696 arg_list = nmcli.Nmcli.execute_command.call_args_list
2697 args, kwargs = arg_list[0]
2698
2699 assert "connection.zone" in args[0]
2700
2701 out, err = capfd.readouterr()
2702 results = json.loads(out)
2703 assert not results.get("failed")
2704 assert results["changed"]
2705
2706
2707 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_ZONE, indirect=["patch_ansible_module"])
2708 def test_generic_connection_zone_unchanged(mocked_generic_connection_zone_unchanged, capfd):
2709 """
2710 Test : Generic connection with zone unchanged
2711 """
2712 with pytest.raises(SystemExit):
2713 nmcli.main()
2714
2715 out, err = capfd.readouterr()
2716 results = json.loads(out)
2717 assert not results.get("failed")
2718 assert not results["changed"]
2719
2720
2721 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_ZONE_ONLY, indirect=["patch_ansible_module"])
2722 def test_generic_connection_modify_zone_only(mocked_generic_connection_modify, capfd):
2723 """
2724 Test : Generic connection modified with zone only
2725 """
2726 with pytest.raises(SystemExit):
2727 nmcli.main()
2728
2729 assert nmcli.Nmcli.execute_command.call_count == 1
2730 arg_list = nmcli.Nmcli.execute_command.call_args_list
2731 args, kwargs = arg_list[0]
2732
2733 assert "connection.zone" in args[0]
2734 assert "ipv4.addresses" not in args[0]
2735 assert "ipv4.gateway" not in args[0]
2736 assert "ipv6.addresses" not in args[0]
2737 assert "ipv6.gateway" not in args[0]
2738
2739 out, err = capfd.readouterr()
2740 results = json.loads(out)
2741 assert not results.get("failed")
2742 assert results["changed"]
2743
2744
2745 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_CONNECTION, indirect=["patch_ansible_module"])
2746 def test_zone_none(mocked_connection_exists, capfd):
2747 """
2748 Test if zone param is None
2749 """
2750 with pytest.raises(SystemExit):
2751 nmcli.main()
2752
2753 out, err = capfd.readouterr()
2754 results = json.loads(out)
2755 assert not results.get("failed")
2756 assert results["changed"]
2757
2758
2759 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BRIDGE, indirect=["patch_ansible_module"])
2760 def test_create_bridge(mocked_generic_connection_create, capfd):
2761 """
2762 Test if Bridge created
2763 """
2764 with pytest.raises(SystemExit):
2765 nmcli.main()
2766
2767 assert nmcli.Nmcli.execute_command.call_count == 1
2768 arg_list = nmcli.Nmcli.execute_command.call_args_list
2769 args, kwargs = arg_list[0]
2770
2771 assert args[0][0] == "/usr/bin/nmcli"
2772 assert args[0][1] == "con"
2773 assert args[0][2] == "add"
2774 assert args[0][3] == "type"
2775 assert args[0][4] == "bridge"
2776 assert args[0][5] == "con-name"
2777 assert args[0][6] == "non_existent_nw_device"
2778
2779 args_text = [to_text(x) for x in args[0]]
2780 for param in [
2781 "ipv4.addresses",
2782 "10.10.10.10/24",
2783 "ipv4.gateway",
2784 "10.10.10.1",
2785 "bridge.max-age",
2786 "100",
2787 "bridge.stp",
2788 "yes",
2789 ]:
2790 assert param in args_text
2791
2792 out, err = capfd.readouterr()
2793 results = json.loads(out)
2794 assert not results.get("failed")
2795 assert results["changed"]
2796
2797
2798 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BRIDGE, indirect=["patch_ansible_module"])
2799 def test_mod_bridge(mocked_generic_connection_modify, capfd):
2800 """
2801 Test if Bridge modified
2802 """
2803 with pytest.raises(SystemExit):
2804 nmcli.main()
2805
2806 assert nmcli.Nmcli.execute_command.call_count == 1
2807
2808 arg_list = nmcli.Nmcli.execute_command.call_args_list
2809 args, kwargs = arg_list[0]
2810
2811 assert args[0][0] == "/usr/bin/nmcli"
2812 assert args[0][1] == "con"
2813 assert args[0][2] == "modify"
2814 assert args[0][3] == "non_existent_nw_device"
2815
2816 args_text = [to_text(x) for x in args[0]]
2817 for param in [
2818 "ipv4.addresses",
2819 "10.10.10.10/24",
2820 "ipv4.gateway",
2821 "10.10.10.1",
2822 "bridge.max-age",
2823 "100",
2824 "bridge.stp",
2825 "yes",
2826 ]:
2827 assert param in args_text
2828
2829 out, err = capfd.readouterr()
2830 results = json.loads(out)
2831 assert not results.get("failed")
2832 assert results["changed"]
2833
2834
2835 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BRIDGE, indirect=["patch_ansible_module"])
2836 def test_bridge_connection_unchanged(mocked_bridge_connection_unchanged, capfd):
2837 """
2838 Test : Bridge connection unchanged
2839 """
2840 with pytest.raises(SystemExit):
2841 nmcli.main()
2842
2843 out, err = capfd.readouterr()
2844 results = json.loads(out)
2845 assert not results.get("failed")
2846 assert not results["changed"]
2847
2848
2849 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BRIDGE_SLAVE, indirect=["patch_ansible_module"])
2850 def test_create_bridge_slave(mocked_generic_connection_create, capfd):
2851 """
2852 Test if Bridge_slave created
2853 """
2854
2855 with pytest.raises(SystemExit):
2856 nmcli.main()
2857
2858 assert nmcli.Nmcli.execute_command.call_count == 1
2859 arg_list = nmcli.Nmcli.execute_command.call_args_list
2860 args, kwargs = arg_list[0]
2861
2862 assert args[0][0] == "/usr/bin/nmcli"
2863 assert args[0][1] == "con"
2864 assert args[0][2] == "add"
2865 assert args[0][3] == "type"
2866 assert args[0][4] == "bridge-slave"
2867 assert args[0][5] == "con-name"
2868 assert args[0][6] == "non_existent_nw_device"
2869
2870 args_text = [to_text(x) for x in args[0]]
2871 for param in ["bridge-port.path-cost", "100"]:
2872 assert param in args_text
2873
2874 out, err = capfd.readouterr()
2875 results = json.loads(out)
2876 assert not results.get("failed")
2877 assert results["changed"]
2878
2879
2880 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BRIDGE_SLAVE, indirect=["patch_ansible_module"])
2881 def test_mod_bridge_slave(mocked_generic_connection_modify, capfd):
2882 """
2883 Test if Bridge_slave modified
2884 """
2885
2886 with pytest.raises(SystemExit):
2887 nmcli.main()
2888
2889 assert nmcli.Nmcli.execute_command.call_count == 1
2890 arg_list = nmcli.Nmcli.execute_command.call_args_list
2891 args, kwargs = arg_list[0]
2892
2893 assert args[0][0] == "/usr/bin/nmcli"
2894 assert args[0][1] == "con"
2895 assert args[0][2] == "modify"
2896 assert args[0][3] == "non_existent_nw_device"
2897
2898 args_text = [to_text(x) for x in args[0]]
2899 for param in ["bridge-port.path-cost", "100"]:
2900 assert param in args_text
2901
2902 out, err = capfd.readouterr()
2903 results = json.loads(out)
2904 assert not results.get("failed")
2905 assert results["changed"]
2906
2907
2908 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_BRIDGE_SLAVE, indirect=["patch_ansible_module"])
2909 def test_bridge_slave_unchanged(mocked_bridge_slave_unchanged, capfd):
2910 """
2911 Test : Bridge-slave connection unchanged
2912 """
2913 with pytest.raises(SystemExit):
2914 nmcli.main()
2915
2916 out, err = capfd.readouterr()
2917 results = json.loads(out)
2918 assert not results.get("failed")
2919 assert not results["changed"]
2920
2921
2922 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_TEAM, indirect=["patch_ansible_module"])
2923 def test_team_connection_create(mocked_generic_connection_create, capfd):
2924 """
2925 Test : Team connection created
2926 """
2927 with pytest.raises(SystemExit):
2928 nmcli.main()
2929
2930 assert nmcli.Nmcli.execute_command.call_count == 1
2931 arg_list = nmcli.Nmcli.execute_command.call_args_list
2932 args, kwargs = arg_list[0]
2933
2934 assert args[0][0] == "/usr/bin/nmcli"
2935 assert args[0][1] == "con"
2936 assert args[0][2] == "add"
2937 assert args[0][3] == "type"
2938 assert args[0][4] == "team"
2939 assert args[0][5] == "con-name"
2940 assert args[0][6] == "non_existent_nw_device"
2941
2942 for param in ["connection.autoconnect", "connection.interface-name", "team0_non_existant"]:
2943 assert param in args[0]
2944
2945 out, err = capfd.readouterr()
2946 results = json.loads(out)
2947 assert not results.get("failed")
2948 assert results["changed"]
2949
2950
2951 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_TEAM, indirect=["patch_ansible_module"])
2952 def test_team_connection_unchanged(mocked_team_connection_unchanged, capfd):
2953 """
2954 Test : Team connection unchanged
2955 """
2956 with pytest.raises(SystemExit):
2957 nmcli.main()
2958
2959 out, err = capfd.readouterr()
2960 results = json.loads(out)
2961 assert not results.get("failed")
2962 assert not results["changed"]
2963
2964
2965 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_TEAM_HWADDR_POLICY_FAILS, indirect=["patch_ansible_module"])
2966 def test_team_connection_create_hwaddr_policy_fails(mocked_generic_connection_create, capfd):
2967 """
2968 Test : Team connection created
2969 """
2970 with pytest.raises(SystemExit):
2971 nmcli.main()
2972
2973 out, err = capfd.readouterr()
2974 results = json.loads(out)
2975 assert results.get("failed")
2976 assert results["msg"] == "Runner-hwaddr-policy is only allowed for runner activebackup"
2977
2978
2979 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_TEAM_RUNNER_FAST_RATE, indirect=["patch_ansible_module"])
2980 def test_team_runner_fast_rate_connection_create(mocked_generic_connection_create, capfd):
2981 """
2982 Test : Team connection created with runner_fast_rate parameter
2983 """
2984 with pytest.raises(SystemExit):
2985 nmcli.main()
2986
2987 assert nmcli.Nmcli.execute_command.call_count == 1
2988 arg_list = nmcli.Nmcli.execute_command.call_args_list
2989 args, kwargs = arg_list[0]
2990
2991 assert args[0][0] == "/usr/bin/nmcli"
2992 assert args[0][1] == "con"
2993 assert args[0][2] == "add"
2994 assert args[0][3] == "type"
2995 assert args[0][4] == "team"
2996 assert args[0][5] == "con-name"
2997 assert args[0][6] == "non_existent_nw_device"
2998
2999 for param in [
3000 "connection.autoconnect",
3001 "connection.interface-name",
3002 "team0_non_existant",
3003 "team.runner",
3004 "lacp",
3005 "team.runner-fast-rate",
3006 "yes",
3007 ]:
3008 assert param in args[0]
3009
3010 out, err = capfd.readouterr()
3011 results = json.loads(out)
3012 assert not results.get("failed")
3013 assert results["changed"]
3014
3015
3016 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_TEAM_RUNNER_FAST_RATE, indirect=["patch_ansible_module"])
3017 def test_team_runner_fast_rate_connection_unchanged(mocked_team_runner_fast_rate_connection_unchanged, capfd):
3018 """
3019 Test : Team connection unchanged with runner_fast_rate parameter
3020 """
3021 with pytest.raises(SystemExit):
3022 nmcli.main()
3023
3024 out, err = capfd.readouterr()
3025 results = json.loads(out)
3026 assert not results.get("failed")
3027 assert not results["changed"]
3028
3029
3030 @pytest.mark.parametrize(
3031 "patch_ansible_module", TESTCASE_TEAM_RUNNER_FAST_RATE_FAILS, indirect=["patch_ansible_module"]
3032 )
3033 def test_team_connection_create_runner_fast_rate_fails(mocked_generic_connection_create, capfd):
3034 """
3035 Test : Team connection with runner_fast_rate enabled
3036 """
3037 with pytest.raises(SystemExit):
3038 nmcli.main()
3039
3040 out, err = capfd.readouterr()
3041 results = json.loads(out)
3042 assert results.get("failed")
3043 assert results["msg"] == "runner-fast-rate is only allowed for runner lacp"
3044
3045
3046 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_TEAM_SLAVE, indirect=["patch_ansible_module"])
3047 def test_create_team_slave(mocked_generic_connection_create, capfd):
3048 """
3049 Test if Team_slave created
3050 """
3051
3052 with pytest.raises(SystemExit):
3053 nmcli.main()
3054
3055 assert nmcli.Nmcli.execute_command.call_count == 1
3056 arg_list = nmcli.Nmcli.execute_command.call_args_list
3057 args, kwargs = arg_list[0]
3058
3059 assert args[0][0] == "/usr/bin/nmcli"
3060 assert args[0][1] == "con"
3061 assert args[0][2] == "add"
3062 assert args[0][3] == "type"
3063 assert args[0][4] == "team-slave"
3064 assert args[0][5] == "con-name"
3065 assert args[0][6] == "non_existent_nw_slaved_device"
3066
3067 for param in [
3068 "connection.autoconnect",
3069 "connection.interface-name",
3070 "connection.master",
3071 "team0_non_existant",
3072 "connection.slave-type",
3073 ]:
3074 assert param in args[0]
3075
3076 out, err = capfd.readouterr()
3077 results = json.loads(out)
3078 assert not results.get("failed")
3079 assert results["changed"]
3080
3081
3082 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_TEAM_SLAVE, indirect=["patch_ansible_module"])
3083 def test_team_slave_connection_unchanged(mocked_team_slave_connection_unchanged, capfd):
3084 """
3085 Test : Team slave connection unchanged
3086 """
3087 with pytest.raises(SystemExit):
3088 nmcli.main()
3089
3090 out, err = capfd.readouterr()
3091 results = json.loads(out)
3092 assert not results.get("failed")
3093 assert not results["changed"]
3094
3095
3096 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VLAN, indirect=["patch_ansible_module"])
3097 def test_create_vlan_con(mocked_generic_connection_create, capfd):
3098 """
3099 Test if VLAN created
3100 """
3101
3102 with pytest.raises(SystemExit):
3103 nmcli.main()
3104
3105 assert nmcli.Nmcli.execute_command.call_count == 1
3106 arg_list = nmcli.Nmcli.execute_command.call_args_list
3107 args, kwargs = arg_list[0]
3108
3109 assert args[0][0] == "/usr/bin/nmcli"
3110 assert args[0][1] == "con"
3111 assert args[0][2] == "add"
3112 assert args[0][3] == "type"
3113 assert args[0][4] == "vlan"
3114 assert args[0][5] == "con-name"
3115 assert args[0][6] == "non_existent_nw_device"
3116
3117 args_text = [to_text(x) for x in args[0]]
3118 for param in ["ipv4.addresses", "10.10.10.10/24", "ipv4.gateway", "10.10.10.1", "vlan.id", "10"]:
3119 assert param in args_text
3120
3121 out, err = capfd.readouterr()
3122 results = json.loads(out)
3123 assert not results.get("failed")
3124 assert results["changed"]
3125
3126
3127 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VLAN, indirect=["patch_ansible_module"])
3128 def test_mod_vlan_conn(mocked_generic_connection_modify, capfd):
3129 """
3130 Test if VLAN modified
3131 """
3132
3133 with pytest.raises(SystemExit):
3134 nmcli.main()
3135
3136 assert nmcli.Nmcli.execute_command.call_count == 1
3137 arg_list = nmcli.Nmcli.execute_command.call_args_list
3138 args, kwargs = arg_list[0]
3139
3140 assert args[0][0] == "/usr/bin/nmcli"
3141 assert args[0][1] == "con"
3142 assert args[0][2] == "modify"
3143 assert args[0][3] == "non_existent_nw_device"
3144
3145 args_text = [to_text(x) for x in args[0]]
3146 for param in ["ipv4.addresses", "10.10.10.10/24", "ipv4.gateway", "10.10.10.1", "vlan.id", "10"]:
3147 assert param in args_text
3148
3149 out, err = capfd.readouterr()
3150 results = json.loads(out)
3151 assert not results.get("failed")
3152 assert results["changed"]
3153
3154
3155 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VLAN, indirect=["patch_ansible_module"])
3156 def test_vlan_connection_unchanged(mocked_vlan_connection_unchanged, capfd):
3157 """
3158 Test : VLAN connection unchanged
3159 """
3160 with pytest.raises(SystemExit):
3161 nmcli.main()
3162
3163 out, err = capfd.readouterr()
3164 results = json.loads(out)
3165 assert not results.get("failed")
3166 assert not results["changed"]
3167
3168
3169 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VXLAN, indirect=["patch_ansible_module"])
3170 def test_create_vxlan(mocked_generic_connection_create, capfd):
3171 """
3172 Test if vxlan created
3173 """
3174 with pytest.raises(SystemExit):
3175 nmcli.main()
3176
3177 assert nmcli.Nmcli.execute_command.call_count == 1
3178 arg_list = nmcli.Nmcli.execute_command.call_args_list
3179 args, kwargs = arg_list[0]
3180
3181 assert args[0][0] == "/usr/bin/nmcli"
3182 assert args[0][1] == "con"
3183 assert args[0][2] == "add"
3184 assert args[0][3] == "type"
3185 assert args[0][4] == "vxlan"
3186 assert args[0][5] == "con-name"
3187 assert args[0][6] == "non_existent_nw_device"
3188
3189 args_text = [to_text(x) for x in args[0]]
3190 for param in [
3191 "connection.interface-name",
3192 "vxlan-existent_nw_device",
3193 "vxlan.local",
3194 "192.168.225.5",
3195 "vxlan.remote",
3196 "192.168.225.6",
3197 "vxlan.id",
3198 "11",
3199 ]:
3200 assert param in args_text
3201
3202 out, err = capfd.readouterr()
3203 results = json.loads(out)
3204 assert not results.get("failed")
3205 assert results["changed"]
3206
3207
3208 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VXLAN, indirect=["patch_ansible_module"])
3209 def test_vxlan_mod(mocked_generic_connection_modify, capfd):
3210 """
3211 Test if vxlan modified
3212 """
3213 with pytest.raises(SystemExit):
3214 nmcli.main()
3215
3216 assert nmcli.Nmcli.execute_command.call_count == 1
3217 arg_list = nmcli.Nmcli.execute_command.call_args_list
3218 args, kwargs = arg_list[0]
3219
3220 assert args[0][0] == "/usr/bin/nmcli"
3221 assert args[0][1] == "con"
3222 assert args[0][2] == "modify"
3223 assert args[0][3] == "non_existent_nw_device"
3224
3225 args_text = [to_text(x) for x in args[0]]
3226 for param in ["vxlan.local", "192.168.225.5", "vxlan.remote", "192.168.225.6", "vxlan.id", "11"]:
3227 assert param in args_text
3228
3229 out, err = capfd.readouterr()
3230 results = json.loads(out)
3231 assert not results.get("failed")
3232 assert results["changed"]
3233
3234
3235 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VXLAN, indirect=["patch_ansible_module"])
3236 def test_vxlan_connection_unchanged(mocked_vxlan_connection_unchanged, capfd):
3237 """
3238 Test : VxLAN connection unchanged
3239 """
3240 with pytest.raises(SystemExit):
3241 nmcli.main()
3242
3243 out, err = capfd.readouterr()
3244 results = json.loads(out)
3245 assert not results.get("failed")
3246 assert not results["changed"]
3247
3248
3249 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VXLAN_MULTICAST, indirect=["patch_ansible_module"])
3250 def test_create_vxlan_multicast(mocked_generic_connection_create, capfd):
3251 """
3252 Test if vxlan with multicast and parent device created
3253 """
3254 with pytest.raises(SystemExit):
3255 nmcli.main()
3256
3257 assert nmcli.Nmcli.execute_command.call_count == 1
3258 arg_list = nmcli.Nmcli.execute_command.call_args_list
3259 args, kwargs = arg_list[0]
3260
3261 assert args[0][0] == "/usr/bin/nmcli"
3262 assert args[0][1] == "con"
3263 assert args[0][2] == "add"
3264 assert args[0][3] == "type"
3265 assert args[0][4] == "vxlan"
3266 assert args[0][5] == "con-name"
3267 assert args[0][6] == "vxlan_multicast_test"
3268
3269 args_text = [to_text(x) for x in args[0]]
3270 for param in [
3271 "connection.interface-name",
3272 "vxlan-device",
3273 "vxlan.local",
3274 "192.168.1.2",
3275 "vxlan.remote",
3276 "239.192.0.17",
3277 "vxlan.id",
3278 "17",
3279 "vxlan.parent",
3280 "eth1",
3281 "connection.slave-type",
3282 "bridge",
3283 "connection.master",
3284 "br0",
3285 ]:
3286 assert param in args_text
3287
3288 out, err = capfd.readouterr()
3289 results = json.loads(out)
3290 assert not results.get("failed")
3291 assert results["changed"]
3292
3293
3294 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_IPIP, indirect=["patch_ansible_module"])
3295 def test_create_ipip(mocked_generic_connection_create, capfd):
3296 """
3297 Test if ipip created
3298 """
3299 with pytest.raises(SystemExit):
3300 nmcli.main()
3301
3302 assert nmcli.Nmcli.execute_command.call_count == 1
3303 arg_list = nmcli.Nmcli.execute_command.call_args_list
3304 args, kwargs = arg_list[0]
3305
3306 assert args[0][0] == "/usr/bin/nmcli"
3307 assert args[0][1] == "con"
3308 assert args[0][2] == "add"
3309 assert args[0][3] == "type"
3310 assert args[0][4] == "ip-tunnel"
3311 assert args[0][5] == "con-name"
3312 assert args[0][6] == "non_existent_nw_device"
3313
3314 args_text = [to_text(x) for x in args[0]]
3315 for param in [
3316 "connection.interface-name",
3317 "ipip-existent_nw_device",
3318 "ip-tunnel.local",
3319 "192.168.225.5",
3320 "ip-tunnel.mode",
3321 "ipip",
3322 "ip-tunnel.parent",
3323 "non_existent_ipip_device",
3324 "ip-tunnel.remote",
3325 "192.168.225.6",
3326 ]:
3327 assert param in args_text
3328
3329 out, err = capfd.readouterr()
3330 results = json.loads(out)
3331 assert not results.get("failed")
3332 assert results["changed"]
3333
3334
3335 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_IPIP, indirect=["patch_ansible_module"])
3336 def test_ipip_mod(mocked_generic_connection_modify, capfd):
3337 """
3338 Test if ipip modified
3339 """
3340 with pytest.raises(SystemExit):
3341 nmcli.main()
3342
3343 assert nmcli.Nmcli.execute_command.call_count == 1
3344 arg_list = nmcli.Nmcli.execute_command.call_args_list
3345 args, kwargs = arg_list[0]
3346
3347 assert args[0][0] == "/usr/bin/nmcli"
3348 assert args[0][1] == "con"
3349 assert args[0][2] == "modify"
3350 assert args[0][3] == "non_existent_nw_device"
3351
3352 args_text = [to_text(x) for x in args[0]]
3353 for param in ["ip-tunnel.local", "192.168.225.5", "ip-tunnel.remote", "192.168.225.6"]:
3354 assert param in args_text
3355
3356 out, err = capfd.readouterr()
3357 results = json.loads(out)
3358 assert not results.get("failed")
3359 assert results["changed"]
3360
3361
3362 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_IPIP, indirect=["patch_ansible_module"])
3363 def test_ipip_connection_unchanged(mocked_ipip_connection_unchanged, capfd):
3364 """
3365 Test : IPIP connection unchanged
3366 """
3367 with pytest.raises(SystemExit):
3368 nmcli.main()
3369
3370 out, err = capfd.readouterr()
3371 results = json.loads(out)
3372 assert not results.get("failed")
3373 assert not results["changed"]
3374
3375
3376 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SIT, indirect=["patch_ansible_module"])
3377 def test_create_sit(mocked_generic_connection_create, capfd):
3378 """
3379 Test if sit created
3380 """
3381 with pytest.raises(SystemExit):
3382 nmcli.main()
3383
3384 assert nmcli.Nmcli.execute_command.call_count == 1
3385 arg_list = nmcli.Nmcli.execute_command.call_args_list
3386 args, kwargs = arg_list[0]
3387
3388 assert args[0][0] == "/usr/bin/nmcli"
3389 assert args[0][1] == "con"
3390 assert args[0][2] == "add"
3391 assert args[0][3] == "type"
3392 assert args[0][4] == "ip-tunnel"
3393 assert args[0][5] == "con-name"
3394 assert args[0][6] == "non_existent_nw_device"
3395
3396 args_text = [to_text(x) for x in args[0]]
3397 for param in [
3398 "connection.interface-name",
3399 "sit-existent_nw_device",
3400 "ip-tunnel.local",
3401 "192.168.225.5",
3402 "ip-tunnel.mode",
3403 "sit",
3404 "ip-tunnel.parent",
3405 "non_existent_sit_device",
3406 "ip-tunnel.remote",
3407 "192.168.225.6",
3408 ]:
3409 assert param in args_text
3410
3411 out, err = capfd.readouterr()
3412 results = json.loads(out)
3413 assert not results.get("failed")
3414 assert results["changed"]
3415
3416
3417 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SIT, indirect=["patch_ansible_module"])
3418 def test_sit_mod(mocked_generic_connection_modify, capfd):
3419 """
3420 Test if sit modified
3421 """
3422 with pytest.raises(SystemExit):
3423 nmcli.main()
3424
3425 assert nmcli.Nmcli.execute_command.call_count == 1
3426 arg_list = nmcli.Nmcli.execute_command.call_args_list
3427 args, kwargs = arg_list[0]
3428
3429 assert args[0][0] == "/usr/bin/nmcli"
3430 assert args[0][1] == "con"
3431 assert args[0][2] == "modify"
3432 assert args[0][3] == "non_existent_nw_device"
3433
3434 args_text = [to_text(x) for x in args[0]]
3435 for param in ["ip-tunnel.local", "192.168.225.5", "ip-tunnel.remote", "192.168.225.6"]:
3436 assert param in args_text
3437
3438 out, err = capfd.readouterr()
3439 results = json.loads(out)
3440 assert not results.get("failed")
3441 assert results["changed"]
3442
3443
3444 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SIT, indirect=["patch_ansible_module"])
3445 def test_sit_connection_unchanged(mocked_sit_connection_unchanged, capfd):
3446 """
3447 Test : SIT connection unchanged
3448 """
3449 with pytest.raises(SystemExit):
3450 nmcli.main()
3451
3452 out, err = capfd.readouterr()
3453 results = json.loads(out)
3454 assert not results.get("failed")
3455 assert not results["changed"]
3456
3457
3458 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_ETHERNET_DHCP, indirect=["patch_ansible_module"])
3459 def test_eth_dhcp_client_id_con_create(mocked_generic_connection_create, capfd):
3460 """
3461 Test : Ethernet connection created with DHCP_CLIENT_ID
3462 """
3463 with pytest.raises(SystemExit):
3464 nmcli.main()
3465
3466 assert nmcli.Nmcli.execute_command.call_count == 1
3467 arg_list = nmcli.Nmcli.execute_command.call_args_list
3468 args, kwargs = arg_list[0]
3469
3470 assert "ipv4.dhcp-client-id" in args[0]
3471
3472 out, err = capfd.readouterr()
3473 results = json.loads(out)
3474 assert not results.get("failed")
3475 assert results["changed"]
3476
3477
3478 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GRE, indirect=["patch_ansible_module"])
3479 def test_create_gre(mocked_generic_connection_create, capfd):
3480 """
3481 Test if gre created
3482 """
3483 with pytest.raises(SystemExit):
3484 nmcli.main()
3485
3486 assert nmcli.Nmcli.execute_command.call_count == 1
3487 arg_list = nmcli.Nmcli.execute_command.call_args_list
3488 args, kwargs = arg_list[0]
3489
3490 assert args[0][0] == "/usr/bin/nmcli"
3491 assert args[0][1] == "con"
3492 assert args[0][2] == "add"
3493 assert args[0][3] == "type"
3494 assert args[0][4] == "ip-tunnel"
3495 assert args[0][5] == "con-name"
3496 assert args[0][6] == "non_existent_nw_device"
3497
3498 args_text = [to_text(x) for x in args[0]]
3499 for param in [
3500 "connection.interface-name",
3501 "gre-existent_nw_device",
3502 "ip-tunnel.local",
3503 "192.168.225.5",
3504 "ip-tunnel.mode",
3505 "gre",
3506 "ip-tunnel.parent",
3507 "non_existent_gre_device",
3508 "ip-tunnel.remote",
3509 "192.168.225.6",
3510 "ip-tunnel.input-key",
3511 "1",
3512 "ip-tunnel.output-key",
3513 "2",
3514 ]:
3515 assert param in args_text
3516
3517 out, err = capfd.readouterr()
3518 results = json.loads(out)
3519 assert not results.get("failed")
3520 assert results["changed"]
3521
3522
3523 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GRE, indirect=["patch_ansible_module"])
3524 def test_gre_mod(mocked_generic_connection_modify, capfd):
3525 """
3526 Test if gre modified
3527 """
3528 with pytest.raises(SystemExit):
3529 nmcli.main()
3530
3531 assert nmcli.Nmcli.execute_command.call_count == 1
3532 arg_list = nmcli.Nmcli.execute_command.call_args_list
3533 args, kwargs = arg_list[0]
3534
3535 assert args[0][0] == "/usr/bin/nmcli"
3536 assert args[0][1] == "con"
3537 assert args[0][2] == "modify"
3538 assert args[0][3] == "non_existent_nw_device"
3539
3540 args_text = [to_text(x) for x in args[0]]
3541 for param in ["ip-tunnel.local", "192.168.225.5", "ip-tunnel.remote", "192.168.225.6"]:
3542 assert param in args_text
3543
3544 out, err = capfd.readouterr()
3545 results = json.loads(out)
3546 assert not results.get("failed")
3547 assert results["changed"]
3548
3549
3550 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GRE, indirect=["patch_ansible_module"])
3551 def test_gre_connection_unchanged(mocked_gre_connection_unchanged, capfd):
3552 """
3553 Test : GRE connection unchanged
3554 """
3555 with pytest.raises(SystemExit):
3556 nmcli.main()
3557
3558 out, err = capfd.readouterr()
3559 results = json.loads(out)
3560 assert not results.get("failed")
3561 assert not results["changed"]
3562
3563
3564 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_ETHERNET_DHCP, indirect=["patch_ansible_module"])
3565 def test_ethernet_connection_dhcp_unchanged(mocked_ethernet_connection_dhcp_unchanged, capfd):
3566 """
3567 Test : Ethernet connection with DHCP_CLIENT_ID unchanged
3568 """
3569 with pytest.raises(SystemExit):
3570 nmcli.main()
3571
3572 out, err = capfd.readouterr()
3573 results = json.loads(out)
3574 assert not results.get("failed")
3575 assert not results["changed"]
3576
3577
3578 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_ETHERNET_STATIC, indirect=["patch_ansible_module"])
3579 def test_modify_ethernet_dhcp_to_static(mocked_ethernet_connection_dhcp_to_static, capfd):
3580 """
3581 Test : Modify ethernet connection from DHCP to static
3582 """
3583 with pytest.raises(SystemExit):
3584 nmcli.main()
3585
3586 assert nmcli.Nmcli.execute_command.call_count == 2
3587 arg_list = nmcli.Nmcli.execute_command.call_args_list
3588 args, kwargs = arg_list[1]
3589
3590 assert args[0][0] == "/usr/bin/nmcli"
3591 assert args[0][1] == "con"
3592 assert args[0][2] == "modify"
3593 assert args[0][3] == "non_existent_nw_device"
3594
3595 for param in ["ipv4.method", "ipv4.gateway", "ipv4.addresses"]:
3596 assert param in args[0]
3597
3598 out, err = capfd.readouterr()
3599 results = json.loads(out)
3600 assert not results.get("failed")
3601 assert results["changed"]
3602
3603
3604 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_ETHERNET_STATIC, indirect=["patch_ansible_module"])
3605 def test_create_ethernet_static(mocked_generic_connection_create, capfd):
3606 """
3607 Test : Create ethernet connection with static IP configuration
3608 """
3609
3610 with pytest.raises(SystemExit):
3611 nmcli.main()
3612
3613 assert nmcli.Nmcli.execute_command.call_count == 2
3614 arg_list = nmcli.Nmcli.execute_command.call_args_list
3615 add_args, add_kw = arg_list[0]
3616
3617 assert add_args[0][0] == "/usr/bin/nmcli"
3618 assert add_args[0][1] == "con"
3619 assert add_args[0][2] == "add"
3620 assert add_args[0][3] == "type"
3621 assert add_args[0][4] == "ethernet"
3622 assert add_args[0][5] == "con-name"
3623 assert add_args[0][6] == "non_existent_nw_device"
3624
3625 add_args_text = [to_text(x) for x in add_args[0]]
3626 for param in [
3627 "connection.interface-name",
3628 "ethernet_non_existant",
3629 "ipv4.addresses",
3630 "10.10.10.10/24",
3631 "ipv4.gateway",
3632 "10.10.10.1",
3633 "ipv4.dns",
3634 "1.1.1.1,8.8.8.8",
3635 ]:
3636 assert param in add_args_text
3637
3638 up_args, up_kw = arg_list[1]
3639 assert up_args[0][0] == "/usr/bin/nmcli"
3640 assert up_args[0][1] == "con"
3641 assert up_args[0][2] == "up"
3642 assert up_args[0][3] == "non_existent_nw_device"
3643
3644 out, err = capfd.readouterr()
3645 results = json.loads(out)
3646 assert not results.get("failed")
3647 assert results["changed"]
3648
3649
3650 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_ETHERNET_STATIC, indirect=["patch_ansible_module"])
3651 def test_ethernet_connection_static_unchanged(mocked_ethernet_connection_static_unchanged, capfd):
3652 """
3653 Test : Ethernet connection with static IP configuration unchanged
3654 """
3655 with pytest.raises(SystemExit):
3656 nmcli.main()
3657
3658 out, err = capfd.readouterr()
3659 results = json.loads(out)
3660 assert not results.get("failed")
3661 assert not results["changed"]
3662
3663
3664 @pytest.mark.parametrize(
3665 "patch_ansible_module", TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC, indirect=["patch_ansible_module"]
3666 )
3667 def test_ethernet_connection_static_ipv4_address_static_route_with_metric_modify(
3668 mocked_ethernet_connection_with_ipv4_static_address_static_route_metric_modify, capfd
3669 ):
3670 """
3671 Test : Modify ethernet connection with static IPv4 address and static route
3672 """
3673 with pytest.raises(SystemExit):
3674 nmcli.main()
3675
3676 arg_list = nmcli.Nmcli.execute_command.call_args_list
3677 add_args, add_kw = arg_list[1]
3678
3679 assert add_args[0][0] == "/usr/bin/nmcli"
3680 assert add_args[0][1] == "con"
3681 assert add_args[0][2] == "modify"
3682 assert add_args[0][3] == "non_existent_nw_device"
3683
3684 add_args_text = [to_text(x) for x in add_args[0]]
3685
3686 for param in ["ipv4.routes", "192.168.200.0/24 192.168.1.1", "ipv4.route-metric", "10"]:
3687 assert param in add_args_text
3688
3689 out, err = capfd.readouterr()
3690 results = json.loads(out)
3691
3692 assert results.get("changed") is True
3693 assert not results.get("failed")
3694
3695
3696 @pytest.mark.parametrize(
3697 "patch_ansible_module",
3698 TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC_CLEAR,
3699 indirect=["patch_ansible_module"],
3700 )
3701 def test_ethernet_connection_static_ipv4_address_static_route_with_metric_clear(
3702 mocked_ethernet_connection_with_ipv4_static_address_static_route_metric_clear, capfd
3703 ):
3704 """
3705 Test : Modify ethernet connection with static IPv4 address and static route
3706 """
3707 with pytest.raises(SystemExit):
3708 nmcli.main()
3709
3710 arg_list = nmcli.Nmcli.execute_command.call_args_list
3711 add_args, add_kw = arg_list[1]
3712
3713 assert add_args[0][0] == "/usr/bin/nmcli"
3714 assert add_args[0][1] == "con"
3715 assert add_args[0][2] == "modify"
3716 assert add_args[0][3] == "non_existent_nw_device"
3717
3718 add_args_text = [to_text(x) for x in add_args[0]]
3719
3720 for param in ["ipv4.routes", ""]:
3721 assert param in add_args_text
3722
3723 out, err = capfd.readouterr()
3724 results = json.loads(out)
3725
3726 assert "ipv4.routes" in results["diff"]["before"]
3727 assert "ipv4.routes" in results["diff"]["after"]
3728
3729 assert results.get("changed") is True
3730 assert not results.get("failed")
3731
3732
3733 @pytest.mark.parametrize(
3734 "patch_ansible_module", TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE, indirect=["patch_ansible_module"]
3735 )
3736 def test_ethernet_connection_static_ipv6_address_static_route_create(
3737 mocked_ethernet_connection_with_ipv6_static_address_static_route_create, capfd
3738 ):
3739 """
3740 Test : Create ethernet connection with static IPv6 address and static route
3741 """
3742 with pytest.raises(SystemExit):
3743 nmcli.main()
3744
3745 assert nmcli.Nmcli.execute_command.call_count == 1
3746 arg_list = nmcli.Nmcli.execute_command.call_args_list
3747 add_args, add_kw = arg_list[0]
3748
3749 assert add_args[0][0] == "/usr/bin/nmcli"
3750 assert add_args[0][1] == "con"
3751 assert add_args[0][2] == "add"
3752 assert add_args[0][3] == "type"
3753 assert add_args[0][4] == "ethernet"
3754 assert add_args[0][5] == "con-name"
3755 assert add_args[0][6] == "non_existent_nw_device"
3756
3757 add_args_text = [to_text(x) for x in add_args[0]]
3758
3759 for param in [
3760 "connection.interface-name",
3761 "ethernet_non_existant",
3762 "con-name",
3763 "non_existent_nw_device",
3764 "ipv6.addresses",
3765 "2001:beef:cafe:10::1/64",
3766 "ipv6.method",
3767 "manual",
3768 "ipv6.routes",
3769 "fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2",
3770 ]:
3771 assert param in add_args_text
3772
3773 out, err = capfd.readouterr()
3774 results = json.loads(out)
3775 assert not results.get("failed")
3776 assert results["changed"]
3777
3778
3779 @pytest.mark.parametrize(
3780 "patch_ansible_module", TESTCASE_ETHERNET_MOD_IPV6_INT_WITH_ROUTE_AND_METRIC, indirect=["patch_ansible_module"]
3781 )
3782 def test_ethernet_connection_static_ipv6_address_static_route_metric_modify(
3783 mocked_ethernet_connection_with_ipv6_static_address_static_route_metric_modify, capfd
3784 ):
3785 """
3786 Test : Modify ethernet connection with static IPv6 address and static route
3787 """
3788 with pytest.raises(SystemExit):
3789 nmcli.main()
3790
3791 arg_list = nmcli.Nmcli.execute_command.call_args_list
3792 add_args, add_kw = arg_list[1]
3793
3794 assert add_args[0][0] == "/usr/bin/nmcli"
3795 assert add_args[0][1] == "con"
3796 assert add_args[0][2] == "modify"
3797 assert add_args[0][3] == "non_existent_nw_device"
3798
3799 add_args_text = [to_text(x) for x in add_args[0]]
3800
3801 for param in ["ipv6.routes", "fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2", "ipv6.route-metric", "10"]:
3802 assert param in add_args_text
3803
3804 out, err = capfd.readouterr()
3805 results = json.loads(out)
3806
3807 assert results.get("changed") is True
3808 assert not results.get("failed")
3809
3810
3811 @pytest.mark.parametrize(
3812 "patch_ansible_module", TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES, indirect=["patch_ansible_module"]
3813 )
3814 def test_ethernet_connection_static_ipv6_address_multiple_static_routes_with_metric_create(
3815 mocked_ethernet_connection_with_ipv6_static_address_multiple_static_routes_with_metric_create, capfd
3816 ):
3817 """
3818 Test : Create ethernet connection with static IPv6 address and multiple static routes
3819 """
3820 with pytest.raises(SystemExit):
3821 nmcli.main()
3822
3823 assert nmcli.Nmcli.execute_command.call_count == 1
3824 arg_list = nmcli.Nmcli.execute_command.call_args_list
3825 add_args, add_kw = arg_list[0]
3826
3827 assert add_args[0][0] == "/usr/bin/nmcli"
3828 assert add_args[0][1] == "con"
3829 assert add_args[0][2] == "add"
3830 assert add_args[0][3] == "type"
3831 assert add_args[0][4] == "ethernet"
3832 assert add_args[0][5] == "con-name"
3833 assert add_args[0][6] == "non_existent_nw_device"
3834
3835 add_args_text = [to_text(x) for x in add_args[0]]
3836
3837 for param in [
3838 "connection.interface-name",
3839 "ethernet_non_existant",
3840 "con-name",
3841 "non_existent_nw_device",
3842 "ipv6.addresses",
3843 "2001:beef:cafe:10::1/64",
3844 "ipv6.method",
3845 "manual",
3846 "ipv6.routes",
3847 "fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2,fd2e:8890:abcd:25::/64 2001:beef:cafe:10::5",
3848 ]:
3849 assert param in add_args_text
3850
3851 out, err = capfd.readouterr()
3852 results = json.loads(out)
3853 assert not results.get("failed")
3854 assert results["changed"]
3855
3856
3857 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_ETHERNET_ADD_SRIOV_VFS, indirect=["patch_ansible_module"])
3858 def test_ethernet_connection_sriov_vfs_create(mocked_ethernet_connection_with_sriov_vfs_create, capfd):
3859 """
3860 Test : Create ethernet connection with SR-IOV VFs
3861 """
3862 with pytest.raises(SystemExit):
3863 nmcli.main()
3864
3865 assert nmcli.Nmcli.execute_command.call_count == 1
3866 arg_list = nmcli.Nmcli.execute_command.call_args_list
3867 add_args, add_kw = arg_list[0]
3868
3869 assert add_args[0][0] == "/usr/bin/nmcli"
3870 assert add_args[0][1] == "con"
3871 assert add_args[0][2] == "add"
3872 assert add_args[0][3] == "type"
3873 assert add_args[0][4] == "ethernet"
3874 assert add_args[0][5] == "con-name"
3875 assert add_args[0][6] == "non_existent_nw_device"
3876
3877 add_args_text = [to_text(x) for x in add_args[0]]
3878
3879 for param in [
3880 "connection.interface-name",
3881 "ethernet_non_existant",
3882 "con-name",
3883 "non_existent_nw_device",
3884 "sriov.total-vfs",
3885 "16",
3886 "sriov.vfs",
3887 "0 spoof-check=true vlans=100",
3888 ]:
3889 assert param in add_args_text
3890
3891 out, err = capfd.readouterr()
3892 results = json.loads(out)
3893 assert not results.get("failed")
3894 assert results["changed"]
3895
3896
3897 @pytest.mark.parametrize(
3898 "patch_ansible_module", TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_AND_METRIC, indirect=["patch_ansible_module"]
3899 )
3900 def test_ethernet_connection_static_ipv6_address_static_route_with_metric_create(
3901 mocked_ethernet_connection_with_ipv6_static_address_static_route_with_metric_create, capfd
3902 ):
3903 """
3904 Test : Create ethernet connection with static IPv6 address and static route with metric
3905 """
3906 with pytest.raises(SystemExit):
3907 nmcli.main()
3908
3909 assert nmcli.Nmcli.execute_command.call_count == 1
3910 arg_list = nmcli.Nmcli.execute_command.call_args_list
3911 add_args, add_kw = arg_list[0]
3912
3913 assert add_args[0][0] == "/usr/bin/nmcli"
3914 assert add_args[0][1] == "con"
3915 assert add_args[0][2] == "add"
3916 assert add_args[0][3] == "type"
3917 assert add_args[0][4] == "ethernet"
3918 assert add_args[0][5] == "con-name"
3919 assert add_args[0][6] == "non_existent_nw_device"
3920
3921 add_args_text = [to_text(x) for x in add_args[0]]
3922
3923 for param in [
3924 "connection.interface-name",
3925 "ethernet_non_existant",
3926 "con-name",
3927 "non_existent_nw_device",
3928 "ipv6.addresses",
3929 "2001:beef:cafe:10::1/64",
3930 "ipv6.method",
3931 "manual",
3932 "ipv6.routes",
3933 "fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2",
3934 "ipv6.route-metric",
3935 "5",
3936 ]:
3937 assert param in add_args_text
3938
3939 out, err = capfd.readouterr()
3940 results = json.loads(out)
3941 assert not results.get("failed")
3942 assert results["changed"]
3943
3944
3945 @pytest.mark.parametrize(
3946 "patch_ansible_module",
3947 TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_AND_METRIC,
3948 indirect=["patch_ansible_module"],
3949 )
3950 def test_ethernet_connection_static_ipv6_address_static_route_create_2(
3951 mocked_ethernet_connection_with_ipv6_static_address_static_route_create, capfd
3952 ):
3953 """
3954 Test : Create ethernet connection with static IPv6 address and multiple static routes with metric
3955 """
3956 with pytest.raises(SystemExit):
3957 nmcli.main()
3958
3959 assert nmcli.Nmcli.execute_command.call_count == 1
3960 arg_list = nmcli.Nmcli.execute_command.call_args_list
3961 add_args, add_kw = arg_list[0]
3962
3963 assert add_args[0][0] == "/usr/bin/nmcli"
3964 assert add_args[0][1] == "con"
3965 assert add_args[0][2] == "add"
3966 assert add_args[0][3] == "type"
3967 assert add_args[0][4] == "ethernet"
3968 assert add_args[0][5] == "con-name"
3969 assert add_args[0][6] == "non_existent_nw_device"
3970
3971 add_args_text = [to_text(x) for x in add_args[0]]
3972
3973 for param in [
3974 "connection.interface-name",
3975 "ethernet_non_existant",
3976 "con-name",
3977 "non_existent_nw_device",
3978 "ipv6.addresses",
3979 "2001:beef:cafe:10::1/64",
3980 "ipv6.method",
3981 "manual",
3982 "ipv6.routes",
3983 "fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2,fd2e:8890:abcd:25::/64 2001:beef:cafe:10::5",
3984 "ipv6.route-metric",
3985 "5",
3986 ]:
3987 assert param in add_args_text
3988
3989 out, err = capfd.readouterr()
3990 results = json.loads(out)
3991 assert not results.get("failed")
3992 assert results["changed"]
3993
3994
3995 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_WIRELESS, indirect=["patch_ansible_module"])
3996 def test_create_wireless(mocked_wireless_create, capfd):
3997 """
3998 Test : Create wireless connection
3999 """
4000
4001 with pytest.raises(SystemExit):
4002 nmcli.main()
4003
4004 assert nmcli.Nmcli.execute_command.call_count == 2
4005 arg_list = nmcli.Nmcli.execute_command.call_args_list
4006
4007 get_available_options_args, get_available_options_kw = arg_list[0]
4008 assert get_available_options_args[0][0] == "/usr/bin/nmcli"
4009 assert get_available_options_args[0][1] == "con"
4010 assert get_available_options_args[0][2] == "edit"
4011 assert get_available_options_args[0][3] == "type"
4012 assert get_available_options_args[0][4] == "wifi"
4013
4014 get_available_options_data = get_available_options_kw["data"].split()
4015 for param in ["print", "802-11-wireless", "quit", "yes"]:
4016 assert param in get_available_options_data
4017
4018 add_args, add_kw = arg_list[1]
4019 assert add_args[0][0] == "/usr/bin/nmcli"
4020 assert add_args[0][1] == "con"
4021 assert add_args[0][2] == "add"
4022 assert add_args[0][3] == "type"
4023 assert add_args[0][4] == "wifi"
4024 assert add_args[0][5] == "con-name"
4025 assert add_args[0][6] == "non_existent_nw_device"
4026
4027 add_args_text = [to_text(x) for x in add_args[0]]
4028 for param in [
4029 "connection.interface-name",
4030 "wireless_non_existant",
4031 "ipv4.addresses",
4032 "10.10.10.10/24",
4033 "802-11-wireless.ssid",
4034 "Brittany",
4035 "802-11-wireless.mode",
4036 "ap",
4037 "802-11-wireless.hidden",
4038 "yes",
4039 ]:
4040 assert param in add_args_text
4041
4042 out, err = capfd.readouterr()
4043 results = json.loads(out)
4044 assert not results.get("failed")
4045 assert results["changed"]
4046
4047
4048 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SECURE_WIRELESS, indirect=["patch_ansible_module"])
4049 def test_create_secure_wireless(mocked_secure_wireless_create, capfd):
4050 """
4051 Test : Create secure wireless connection
4052 """
4053
4054 with pytest.raises(SystemExit):
4055 nmcli.main()
4056
4057 assert nmcli.Nmcli.execute_command.call_count == 3
4058 arg_list = nmcli.Nmcli.execute_command.call_args_list
4059
4060 get_available_options_args, get_available_options_kw = arg_list[0]
4061 assert get_available_options_args[0][0] == "/usr/bin/nmcli"
4062 assert get_available_options_args[0][1] == "con"
4063 assert get_available_options_args[0][2] == "edit"
4064 assert get_available_options_args[0][3] == "type"
4065 assert get_available_options_args[0][4] == "wifi"
4066
4067 get_available_options_data = get_available_options_kw["data"].split()
4068 for param in ["print", "802-11-wireless-security", "quit", "yes"]:
4069 assert param in get_available_options_data
4070
4071 add_args, add_kw = arg_list[1]
4072 assert add_args[0][0] == "/usr/bin/nmcli"
4073 assert add_args[0][1] == "con"
4074 assert add_args[0][2] == "add"
4075 assert add_args[0][3] == "type"
4076 assert add_args[0][4] == "wifi"
4077 assert add_args[0][5] == "con-name"
4078 assert add_args[0][6] == "non_existent_nw_device"
4079
4080 add_args_text = [to_text(x) for x in add_args[0]]
4081 for param in [
4082 "connection.interface-name",
4083 "wireless_non_existant",
4084 "ipv4.addresses",
4085 "10.10.10.10/24",
4086 "802-11-wireless.ssid",
4087 "Brittany",
4088 "802-11-wireless-security.key-mgmt",
4089 "wpa-psk",
4090 ]:
4091 assert param in add_args_text
4092
4093 edit_args, edit_kw = arg_list[2]
4094 assert edit_args[0][0] == "/usr/bin/nmcli"
4095 assert edit_args[0][1] == "con"
4096 assert edit_args[0][2] == "edit"
4097 assert edit_args[0][3] == "non_existent_nw_device"
4098
4099 edit_kw_data = edit_kw["data"].split()
4100 for param in ["802-11-wireless-security.psk", "VERY_SECURE_PASSWORD", "save", "quit"]:
4101 assert param in edit_kw_data
4102
4103 out, err = capfd.readouterr()
4104 results = json.loads(out)
4105 assert not results.get("failed")
4106 assert results["changed"]
4107
4108
4109 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SECURE_WIRELESS, indirect=["patch_ansible_module"])
4110 def test_create_secure_wireless_failure(mocked_secure_wireless_create_failure, capfd):
4111 """
4112 Test : Create secure wireless connection w/failure
4113 """
4114
4115 with pytest.raises(SystemExit):
4116 nmcli.main()
4117
4118 assert nmcli.Nmcli.execute_command.call_count == 2
4119 arg_list = nmcli.Nmcli.execute_command.call_args_list
4120
4121 get_available_options_args, get_available_options_kw = arg_list[0]
4122 assert get_available_options_args[0][0] == "/usr/bin/nmcli"
4123 assert get_available_options_args[0][1] == "con"
4124 assert get_available_options_args[0][2] == "edit"
4125 assert get_available_options_args[0][3] == "type"
4126 assert get_available_options_args[0][4] == "wifi"
4127
4128 get_available_options_data = get_available_options_kw["data"].split()
4129 for param in ["print", "802-11-wireless-security", "quit", "yes"]:
4130 assert param in get_available_options_data
4131
4132 add_args, add_kw = arg_list[1]
4133 assert add_args[0][0] == "/usr/bin/nmcli"
4134 assert add_args[0][1] == "con"
4135 assert add_args[0][2] == "add"
4136 assert add_args[0][3] == "type"
4137 assert add_args[0][4] == "wifi"
4138 assert add_args[0][5] == "con-name"
4139 assert add_args[0][6] == "non_existent_nw_device"
4140
4141 add_args_text = [to_text(x) for x in add_args[0]]
4142 for param in [
4143 "connection.interface-name",
4144 "wireless_non_existant",
4145 "ipv4.addresses",
4146 "10.10.10.10/24",
4147 "802-11-wireless.ssid",
4148 "Brittany",
4149 "802-11-wireless-security.key-mgmt",
4150 "wpa-psk",
4151 ]:
4152 assert param in add_args_text
4153
4154 out, err = capfd.readouterr()
4155 results = json.loads(out)
4156 assert results.get("failed")
4157 assert "changed" not in results
4158
4159
4160 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SECURE_WIRELESS, indirect=["patch_ansible_module"])
4161 def test_modify_secure_wireless(mocked_secure_wireless_modify, capfd):
4162 """
4163 Test : Modify secure wireless connection
4164 """
4165
4166 with pytest.raises(SystemExit):
4167 nmcli.main()
4168 assert nmcli.Nmcli.execute_command.call_count == 4
4169 arg_list = nmcli.Nmcli.execute_command.call_args_list
4170
4171 get_available_options_args, get_available_options_kw = arg_list[0]
4172 assert get_available_options_args[0][0] == "/usr/bin/nmcli"
4173 assert get_available_options_args[0][1] == "con"
4174 assert get_available_options_args[0][2] == "edit"
4175 assert get_available_options_args[0][3] == "type"
4176 assert get_available_options_args[0][4] == "wifi"
4177
4178 get_available_options_data = get_available_options_kw["data"].split()
4179 for param in ["print", "802-11-wireless-security", "quit", "yes"]:
4180 assert param in get_available_options_data
4181
4182 show_args, show_kw = arg_list[1]
4183 assert show_args[0][0] == "/usr/bin/nmcli"
4184 assert show_args[0][1] == "--show-secrets"
4185 assert show_args[0][2] == "con"
4186 assert show_args[0][3] == "show"
4187 assert show_args[0][4] == "non_existent_nw_device"
4188
4189 add_args, add_kw = arg_list[2]
4190 assert add_args[0][0] == "/usr/bin/nmcli"
4191 assert add_args[0][1] == "con"
4192 assert add_args[0][2] == "modify"
4193 assert add_args[0][3] == "non_existent_nw_device"
4194
4195 add_args_text = [to_text(x) for x in add_args[0]]
4196 for param in [
4197 "connection.interface-name",
4198 "wireless_non_existant",
4199 "ipv4.addresses",
4200 "10.10.10.10/24",
4201 "802-11-wireless.ssid",
4202 "Brittany",
4203 "802-11-wireless-security.key-mgmt",
4204 "wpa-psk",
4205 ]:
4206 assert param in add_args_text
4207
4208 edit_args, edit_kw = arg_list[3]
4209 assert edit_args[0][0] == "/usr/bin/nmcli"
4210 assert edit_args[0][1] == "con"
4211 assert edit_args[0][2] == "edit"
4212 assert edit_args[0][3] == "non_existent_nw_device"
4213
4214 edit_kw_data = edit_kw["data"].split()
4215 for param in ["802-11-wireless-security.psk", "VERY_SECURE_PASSWORD", "save", "quit"]:
4216 assert param in edit_kw_data
4217
4218 out, err = capfd.readouterr()
4219 results = json.loads(out)
4220 assert not results.get("failed")
4221 assert results["changed"]
4222
4223
4224 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SECURE_WIRELESS, indirect=["patch_ansible_module"])
4225 def test_modify_secure_wireless_failure(mocked_secure_wireless_modify_failure, capfd):
4226 """
4227 Test : Modify secure wireless connection w/failure
4228 """
4229
4230 with pytest.raises(SystemExit):
4231 nmcli.main()
4232
4233 assert nmcli.Nmcli.execute_command.call_count == 3
4234 arg_list = nmcli.Nmcli.execute_command.call_args_list
4235
4236 get_available_options_args, get_available_options_kw = arg_list[0]
4237 assert get_available_options_args[0][0] == "/usr/bin/nmcli"
4238 assert get_available_options_args[0][1] == "con"
4239 assert get_available_options_args[0][2] == "edit"
4240 assert get_available_options_args[0][3] == "type"
4241 assert get_available_options_args[0][4] == "wifi"
4242
4243 get_available_options_data = get_available_options_kw["data"].split()
4244 for param in ["print", "802-11-wireless-security", "quit", "yes"]:
4245 assert param in get_available_options_data
4246
4247 show_args, show_kw = arg_list[1]
4248 assert show_args[0][0] == "/usr/bin/nmcli"
4249 assert show_args[0][1] == "--show-secrets"
4250 assert show_args[0][2] == "con"
4251 assert show_args[0][3] == "show"
4252 assert show_args[0][4] == "non_existent_nw_device"
4253
4254 add_args, add_kw = arg_list[2]
4255 assert add_args[0][0] == "/usr/bin/nmcli"
4256 assert add_args[0][1] == "con"
4257 assert add_args[0][2] == "modify"
4258 assert add_args[0][3] == "non_existent_nw_device"
4259
4260 add_args_text = [to_text(x) for x in add_args[0]]
4261 for param in [
4262 "connection.interface-name",
4263 "wireless_non_existant",
4264 "ipv4.addresses",
4265 "10.10.10.10/24",
4266 "802-11-wireless.ssid",
4267 "Brittany",
4268 "802-11-wireless-security.key-mgmt",
4269 "wpa-psk",
4270 ]:
4271 assert param in add_args_text
4272
4273 out, err = capfd.readouterr()
4274 results = json.loads(out)
4275 assert results.get("failed")
4276 assert "changed" not in results
4277
4278
4279 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_DUMMY_STATIC, indirect=["patch_ansible_module"])
4280 def test_create_dummy_static(mocked_generic_connection_create, capfd):
4281 """
4282 Test : Create dummy connection with static IP configuration
4283 """
4284
4285 with pytest.raises(SystemExit):
4286 nmcli.main()
4287
4288 assert nmcli.Nmcli.execute_command.call_count == 2
4289 arg_list = nmcli.Nmcli.execute_command.call_args_list
4290 add_args, add_kw = arg_list[0]
4291
4292 assert add_args[0][0] == "/usr/bin/nmcli"
4293 assert add_args[0][1] == "con"
4294 assert add_args[0][2] == "add"
4295 assert add_args[0][3] == "type"
4296 assert add_args[0][4] == "dummy"
4297 assert add_args[0][5] == "con-name"
4298 assert add_args[0][6] == "non_existent_nw_device"
4299
4300 add_args_text = [to_text(x) for x in add_args[0]]
4301 for param in [
4302 "connection.interface-name",
4303 "dummy_non_existant",
4304 "ipv4.addresses",
4305 "10.10.10.10/24",
4306 "ipv4.gateway",
4307 "10.10.10.1",
4308 "ipv4.dns",
4309 "1.1.1.1,8.8.8.8",
4310 "ipv6.addresses",
4311 "2001:db8::1/128",
4312 ]:
4313 assert param in add_args_text
4314
4315 up_args, up_kw = arg_list[1]
4316 assert up_args[0][0] == "/usr/bin/nmcli"
4317 assert up_args[0][1] == "con"
4318 assert up_args[0][2] == "up"
4319 assert up_args[0][3] == "non_existent_nw_device"
4320
4321 out, err = capfd.readouterr()
4322 results = json.loads(out)
4323 assert not results.get("failed")
4324 assert results["changed"]
4325
4326
4327 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_DUMMY_STATIC, indirect=["patch_ansible_module"])
4328 def test_dummy_connection_static_unchanged(mocked_dummy_connection_static_unchanged, capfd):
4329 """
4330 Test : Dummy connection with static IP configuration unchanged
4331 """
4332 with pytest.raises(SystemExit):
4333 nmcli.main()
4334
4335 out, err = capfd.readouterr()
4336 results = json.loads(out)
4337 assert not results.get("failed")
4338 assert not results["changed"]
4339
4340
4341 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_DUMMY_STATIC, indirect=["patch_ansible_module"])
4342 def test_dummy_connection_static_without_mtu_unchanged(mocked_dummy_connection_static_without_mtu_unchanged, capfd):
4343 """
4344 Test : Dummy connection with static IP configuration and no mtu set unchanged
4345 """
4346 with pytest.raises(SystemExit):
4347 nmcli.main()
4348
4349 out, err = capfd.readouterr()
4350 results = json.loads(out)
4351 assert not results.get("failed")
4352 assert not results["changed"]
4353
4354
4355 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_DUMMY_STATIC, indirect=["patch_ansible_module"])
4356 def test_dummy_connection_static_with_custom_mtu_modify(mocked_dummy_connection_static_with_custom_mtu_modify, capfd):
4357 """
4358 Test : Dummy connection with static IP configuration and no mtu set modify
4359 """
4360 with pytest.raises(SystemExit):
4361 nmcli.main()
4362
4363 assert nmcli.Nmcli.execute_command.call_count == 2
4364
4365 arg_list = nmcli.Nmcli.execute_command.call_args_list
4366 args, kwargs = arg_list[1]
4367
4368 assert args[0][0] == "/usr/bin/nmcli"
4369 assert args[0][1] == "con"
4370 assert args[0][2] == "modify"
4371 assert args[0][3] == "non_existent_nw_device"
4372
4373 args_text = [to_text(x) for x in args[0]]
4374 for param in ["802-3-ethernet.mtu", "0"]:
4375 assert param in args_text
4376
4377 out, err = capfd.readouterr()
4378 results = json.loads(out)
4379 assert not results.get("failed")
4380 assert results["changed"]
4381
4382
4383 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GSM, indirect=["patch_ansible_module"])
4384 def test_create_gsm(mocked_generic_connection_create, capfd):
4385 """
4386 Test if gsm created
4387 """
4388 with pytest.raises(SystemExit):
4389 nmcli.main()
4390
4391 assert nmcli.Nmcli.execute_command.call_count == 1
4392 arg_list = nmcli.Nmcli.execute_command.call_args_list
4393 args, kwargs = arg_list[0]
4394
4395 assert args[0][0] == "/usr/bin/nmcli"
4396 assert args[0][1] == "con"
4397 assert args[0][2] == "add"
4398 assert args[0][3] == "type"
4399 assert args[0][4] == "gsm"
4400 assert args[0][5] == "con-name"
4401 assert args[0][6] == "non_existent_nw_device"
4402
4403 args_text = [to_text(x) for x in args[0]]
4404 for param in [
4405 "connection.interface-name",
4406 "gsm_non_existant",
4407 "gsm.apn",
4408 "internet.telekom",
4409 "gsm.username",
4410 "t-mobile",
4411 "gsm.password",
4412 "tm",
4413 "gsm.pin",
4414 "1234",
4415 ]:
4416 assert param in args_text
4417
4418 out, err = capfd.readouterr()
4419 results = json.loads(out)
4420 assert not results.get("failed")
4421 assert results["changed"]
4422
4423
4424 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GSM, indirect=["patch_ansible_module"])
4425 def test_gsm_mod(mocked_generic_connection_modify, capfd):
4426 """
4427 Test if gsm modified
4428 """
4429 with pytest.raises(SystemExit):
4430 nmcli.main()
4431
4432 assert nmcli.Nmcli.execute_command.call_count == 1
4433 arg_list = nmcli.Nmcli.execute_command.call_args_list
4434 args, kwargs = arg_list[0]
4435
4436 assert args[0][0] == "/usr/bin/nmcli"
4437 assert args[0][1] == "con"
4438 assert args[0][2] == "modify"
4439 assert args[0][3] == "non_existent_nw_device"
4440
4441 args_text = [to_text(x) for x in args[0]]
4442 for param in ["gsm.username", "t-mobile", "gsm.password", "tm"]:
4443 assert param in args_text
4444
4445 out, err = capfd.readouterr()
4446 results = json.loads(out)
4447 assert not results.get("failed")
4448 assert results["changed"]
4449
4450
4451 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GSM, indirect=["patch_ansible_module"])
4452 def test_gsm_connection_unchanged(mocked_gsm_connection_unchanged, capfd):
4453 """
4454 Test if gsm connection unchanged
4455 """
4456 with pytest.raises(SystemExit):
4457 nmcli.main()
4458
4459 out, err = capfd.readouterr()
4460 results = json.loads(out)
4461 assert not results.get("failed")
4462 assert not results["changed"]
4463
4464
4465 @pytest.mark.parametrize(
4466 "patch_ansible_module", TESTCASE_ETHERNET_STATIC_MULTIPLE_IP4_ADDRESSES, indirect=["patch_ansible_module"]
4467 )
4468 def test_create_ethernet_with_multiple_ip4_addresses_static(mocked_generic_connection_create, capfd):
4469 """
4470 Test : Create ethernet connection with static IP configuration
4471 """
4472
4473 with pytest.raises(SystemExit):
4474 nmcli.main()
4475
4476 assert nmcli.Nmcli.execute_command.call_count == 2
4477 arg_list = nmcli.Nmcli.execute_command.call_args_list
4478 add_args, add_kw = arg_list[0]
4479
4480 assert add_args[0][0] == "/usr/bin/nmcli"
4481 assert add_args[0][1] == "con"
4482 assert add_args[0][2] == "add"
4483 assert add_args[0][3] == "type"
4484 assert add_args[0][4] == "ethernet"
4485 assert add_args[0][5] == "con-name"
4486 assert add_args[0][6] == "non_existent_nw_device"
4487
4488 add_args_text = [to_text(x) for x in add_args[0]]
4489 for param in [
4490 "connection.interface-name",
4491 "ethernet_non_existant",
4492 "ipv4.addresses",
4493 "10.10.10.10/32,10.10.20.10/32",
4494 "ipv4.gateway",
4495 "10.10.10.1",
4496 "ipv4.dns",
4497 "1.1.1.1,8.8.8.8",
4498 ]:
4499 assert param in add_args_text
4500
4501 up_args, up_kw = arg_list[1]
4502 assert up_args[0][0] == "/usr/bin/nmcli"
4503 assert up_args[0][1] == "con"
4504 assert up_args[0][2] == "up"
4505 assert up_args[0][3] == "non_existent_nw_device"
4506
4507 out, err = capfd.readouterr()
4508 results = json.loads(out)
4509 assert not results.get("failed")
4510 assert results["changed"]
4511
4512
4513 @pytest.mark.parametrize(
4514 "patch_ansible_module", TESTCASE_ETHERNET_STATIC_MULTIPLE_IP6_ADDRESSES, indirect=["patch_ansible_module"]
4515 )
4516 def test_create_ethernet_with_multiple_ip6_addresses_static(mocked_generic_connection_create, capfd):
4517 """
4518 Test : Create ethernet connection with multiple IPv6 addresses configuration
4519 """
4520
4521 with pytest.raises(SystemExit):
4522 nmcli.main()
4523
4524 assert nmcli.Nmcli.execute_command.call_count == 2
4525 arg_list = nmcli.Nmcli.execute_command.call_args_list
4526 add_args, add_kw = arg_list[0]
4527
4528 assert add_args[0][0] == "/usr/bin/nmcli"
4529 assert add_args[0][1] == "con"
4530 assert add_args[0][2] == "add"
4531 assert add_args[0][3] == "type"
4532 assert add_args[0][4] == "ethernet"
4533 assert add_args[0][5] == "con-name"
4534 assert add_args[0][6] == "non_existent_nw_device"
4535
4536 add_args_text = [to_text(x) for x in add_args[0]]
4537 for param in [
4538 "connection.interface-name",
4539 "ethernet_non_existant",
4540 "ipv6.addresses",
4541 "2001:db8::cafe/128,2002:db8::cafe/128",
4542 "ipv6.gateway",
4543 "2001:db8::cafa",
4544 "ipv6.dns",
4545 "2001:4860:4860::8888,2001:4860:4860::8844",
4546 ]:
4547 assert param in add_args_text
4548
4549 up_args, up_kw = arg_list[1]
4550 assert up_args[0][0] == "/usr/bin/nmcli"
4551 assert up_args[0][1] == "con"
4552 assert up_args[0][2] == "up"
4553 assert up_args[0][3] == "non_existent_nw_device"
4554
4555 out, err = capfd.readouterr()
4556 results = json.loads(out)
4557 assert not results.get("failed")
4558 assert results["changed"]
4559
4560
4561 @pytest.mark.parametrize(
4562 "patch_ansible_module", TESTCASE_ETHERNET_STATIC_MULTIPLE_IP4_ADDRESSES, indirect=["patch_ansible_module"]
4563 )
4564 def test_ethernet_connection_static_with_multiple_ip4_addresses_unchanged(
4565 mocked_ethernet_connection_static_multiple_ip4_addresses_unchanged, capfd
4566 ):
4567 """
4568 Test : Ethernet connection with static IP configuration unchanged
4569 """
4570 with pytest.raises(SystemExit):
4571 nmcli.main()
4572
4573 out, err = capfd.readouterr()
4574 results = json.loads(out)
4575 assert not results.get("failed")
4576 assert not results["changed"]
4577
4578
4579 @pytest.mark.parametrize(
4580 "patch_ansible_module", TESTCASE_ETHERNET_STATIC_MULTIPLE_IP6_ADDRESSES, indirect=["patch_ansible_module"]
4581 )
4582 def test_ethernet_connection_static_with_multiple_ip6_addresses_unchanged(
4583 mocked_ethernet_connection_static_multiple_ip6_addresses_unchanged, capfd
4584 ):
4585 """
4586 Test : Ethernet connection with multiple IPv6 addresses configuration unchanged
4587 """
4588 with pytest.raises(SystemExit):
4589 nmcli.main()
4590
4591 out, err = capfd.readouterr()
4592 results = json.loads(out)
4593 assert not results.get("failed")
4594 assert not results["changed"]
4595
4596
4597 @pytest.mark.parametrize(
4598 "patch_ansible_module", TESTCASE_ETHERNET_STATIC_MULTIPLE_IP4_ADDRESSES, indirect=["patch_ansible_module"]
4599 )
4600 def test_add_second_ip4_address_to_ethernet_connection(mocked_ethernet_connection_static_modify, capfd):
4601 """
4602 Test : Modify ethernet connection from DHCP to static
4603 """
4604 with pytest.raises(SystemExit):
4605 nmcli.main()
4606
4607 assert nmcli.Nmcli.execute_command.call_count == 2
4608 arg_list = nmcli.Nmcli.execute_command.call_args_list
4609 args, kwargs = arg_list[1]
4610
4611 assert args[0][0] == "/usr/bin/nmcli"
4612 assert args[0][1] == "con"
4613 assert args[0][2] == "modify"
4614 assert args[0][3] == "non_existent_nw_device"
4615
4616 for param in ["ipv4.addresses", "10.10.10.10/32,10.10.20.10/32"]:
4617 assert param in args[0]
4618
4619 out, err = capfd.readouterr()
4620 results = json.loads(out)
4621 assert not results.get("failed")
4622 assert results["changed"]
4623
4624
4625 @pytest.mark.parametrize(
4626 "patch_ansible_module", TESTCASE_ETHERNET_STATIC_IP6_PRIVACY_AND_ADDR_GEN_MODE, indirect=["patch_ansible_module"]
4627 )
4628 def test_create_ethernet_addr_gen_mode_and_ip6_privacy_static(mocked_generic_connection_create, capfd):
4629 """
4630 Test : Create ethernet connection with static IP configuration
4631 """
4632
4633 with pytest.raises(SystemExit):
4634 nmcli.main()
4635
4636 assert nmcli.Nmcli.execute_command.call_count == 2
4637 arg_list = nmcli.Nmcli.execute_command.call_args_list
4638 add_args, add_kw = arg_list[0]
4639
4640 assert add_args[0][0] == "/usr/bin/nmcli"
4641 assert add_args[0][1] == "con"
4642 assert add_args[0][2] == "add"
4643 assert add_args[0][3] == "type"
4644 assert add_args[0][4] == "ethernet"
4645 assert add_args[0][5] == "con-name"
4646 assert add_args[0][6] == "non_existent_nw_device"
4647
4648 add_args_text = [to_text(x) for x in add_args[0]]
4649 for param in [
4650 "connection.interface-name",
4651 "ethernet_non_existant",
4652 "ipv6.addresses",
4653 "2001:db8::cafe/128",
4654 "ipv6.gateway",
4655 "2001:db8::cafa",
4656 "ipv6.dns",
4657 "2001:4860:4860::8888",
4658 "ipv6.ip6-privacy",
4659 "prefer-public-addr",
4660 "ipv6.addr-gen-mode",
4661 "eui64",
4662 ]:
4663 assert param in add_args_text
4664
4665 up_args, up_kw = arg_list[1]
4666 assert up_args[0][0] == "/usr/bin/nmcli"
4667 assert up_args[0][1] == "con"
4668 assert up_args[0][2] == "up"
4669 assert up_args[0][3] == "non_existent_nw_device"
4670
4671 out, err = capfd.readouterr()
4672 results = json.loads(out)
4673 assert not results.get("failed")
4674 assert results["changed"]
4675
4676
4677 @pytest.mark.parametrize(
4678 "patch_ansible_module", TESTCASE_ETHERNET_STATIC_IP6_PRIVACY_AND_ADDR_GEN_MODE, indirect=["patch_ansible_module"]
4679 )
4680 def test_ethernet_connection_static_with_multiple_ip4_addresses_unchanged_2(
4681 mocked_ethernet_connection_static_ip6_privacy_and_addr_gen_mode_unchange, capfd
4682 ):
4683 """
4684 Test : Ethernet connection with static IP configuration unchanged
4685 """
4686 with pytest.raises(SystemExit):
4687 nmcli.main()
4688
4689 out, err = capfd.readouterr()
4690 results = json.loads(out)
4691 assert not results.get("failed")
4692 assert not results["changed"]
4693
4694
4695 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_WIREGUARD, indirect=["patch_ansible_module"])
4696 def test_create_wireguard(mocked_generic_connection_create, capfd):
4697 """
4698 Test : Create wireguard connection with static IP configuration
4699 """
4700
4701 with pytest.raises(SystemExit):
4702 nmcli.main()
4703
4704 assert nmcli.Nmcli.execute_command.call_count == 1
4705 arg_list = nmcli.Nmcli.execute_command.call_args_list
4706 add_args, add_kw = arg_list[0]
4707
4708 assert add_args[0][0] == "/usr/bin/nmcli"
4709 assert add_args[0][1] == "con"
4710 assert add_args[0][2] == "add"
4711 assert add_args[0][3] == "type"
4712 assert add_args[0][4] == "wireguard"
4713 assert add_args[0][5] == "con-name"
4714 assert add_args[0][6] == "non_existent_nw_device"
4715
4716 add_args_text = [to_text(x) for x in add_args[0]]
4717 for param in [
4718 "connection.interface-name",
4719 "wg_non_existant",
4720 "ipv4.method",
4721 "manual",
4722 "ipv4.addresses",
4723 "10.10.10.10/24",
4724 "ipv6.method",
4725 "manual",
4726 "ipv6.addresses",
4727 "2001:db8::1/128",
4728 "wireguard.listen-port",
4729 "51820",
4730 "wireguard.private-key",
4731 "<hidden>",
4732 ]:
4733 assert param in add_args_text
4734
4735 out, err = capfd.readouterr()
4736 results = json.loads(out)
4737 assert not results.get("failed")
4738 assert results["changed"]
4739
4740
4741 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_WIREGUARD, indirect=["patch_ansible_module"])
4742 def test_wireguard_connection_unchanged(mocked_wireguard_connection_unchanged, capfd):
4743 """
4744 Test : Wireguard connection with static IP configuration unchanged
4745 """
4746 with pytest.raises(SystemExit):
4747 nmcli.main()
4748
4749 out, err = capfd.readouterr()
4750 results = json.loads(out)
4751 assert not results.get("failed")
4752 assert not results["changed"]
4753
4754
4755 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_WIREGUARD, indirect=["patch_ansible_module"])
4756 def test_wireguard_mod(mocked_generic_connection_modify, capfd):
4757 """
4758 Test : Modify wireguard connection
4759 """
4760 with pytest.raises(SystemExit):
4761 nmcli.main()
4762
4763 assert nmcli.Nmcli.execute_command.call_count == 1
4764 arg_list = nmcli.Nmcli.execute_command.call_args_list
4765 args, kwargs = arg_list[0]
4766
4767 assert args[0][0] == "/usr/bin/nmcli"
4768 assert args[0][1] == "con"
4769 assert args[0][2] == "modify"
4770 assert args[0][3] == "non_existent_nw_device"
4771
4772 args_text = [to_text(x) for x in args[0]]
4773 for param in ["wireguard.listen-port", "51820"]:
4774 assert param in args_text
4775
4776 out, err = capfd.readouterr()
4777 results = json.loads(out)
4778 assert not results.get("failed")
4779 assert results["changed"]
4780
4781
4782 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VPN_L2TP, indirect=["patch_ansible_module"])
4783 def test_vpn_l2tp_connection_unchanged(mocked_vpn_l2tp_connection_unchanged, capfd):
4784 """
4785 Test : L2TP VPN connection unchanged
4786 """
4787 with pytest.raises(SystemExit):
4788 nmcli.main()
4789
4790 out, err = capfd.readouterr()
4791 results = json.loads(out)
4792 assert not results.get("failed")
4793 assert not results["changed"]
4794
4795
4796 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VPN_PPTP, indirect=["patch_ansible_module"])
4797 def test_vpn_pptp_connection_unchanged(mocked_vpn_pptp_connection_unchanged, capfd):
4798 """
4799 Test : PPTP VPN connection unchanged
4800 """
4801 with pytest.raises(SystemExit):
4802 nmcli.main()
4803
4804 out, err = capfd.readouterr()
4805 results = json.loads(out)
4806 assert not results.get("failed")
4807 assert not results["changed"]
4808
4809
4810 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VPN_L2TP, indirect=["patch_ansible_module"])
4811 def test_create_vpn_l2tp(mocked_generic_connection_create, capfd):
4812 """
4813 Test : Create L2TP VPN connection
4814 """
4815
4816 with pytest.raises(SystemExit):
4817 nmcli.main()
4818
4819 assert nmcli.Nmcli.execute_command.call_count == 1
4820 arg_list = nmcli.Nmcli.execute_command.call_args_list
4821 add_args, add_kw = arg_list[0]
4822
4823 assert add_args[0][0] == "/usr/bin/nmcli"
4824 assert add_args[0][1] == "con"
4825 assert add_args[0][2] == "add"
4826 assert add_args[0][3] == "type"
4827 assert add_args[0][4] == "vpn"
4828 assert add_args[0][5] == "con-name"
4829 assert add_args[0][6] == "vpn_l2tp"
4830
4831 add_args_text = [to_text(x) for x in add_args[0]]
4832
4833 for param in [
4834 "connection.autoconnect",
4835 "no",
4836 "connection.permissions",
4837 "brittany",
4838 "vpn.data",
4839 "vpn.service-type",
4840 "org.freedesktop.NetworkManager.l2tp",
4841 ]:
4842 assert param in add_args_text
4843
4844 vpn_data_index = add_args_text.index("vpn.data") + 1
4845 args_vpn_data = add_args_text[vpn_data_index]
4846 for vpn_data in [
4847 "gateway=vpn.example.com",
4848 "password-flags=2",
4849 "user=brittany",
4850 "ipsec-enabled=true",
4851 "ipsec-psk=QnJpdHRhbnkxMjM=",
4852 ]:
4853 assert vpn_data in args_vpn_data
4854
4855 out, err = capfd.readouterr()
4856 results = json.loads(out)
4857 assert not results.get("failed")
4858 assert results["changed"]
4859
4860
4861 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VPN_PPTP, indirect=["patch_ansible_module"])
4862 def test_create_vpn_pptp(mocked_generic_connection_create, capfd):
4863 """
4864 Test : Create PPTP VPN connection
4865 """
4866
4867 with pytest.raises(SystemExit):
4868 nmcli.main()
4869
4870 assert nmcli.Nmcli.execute_command.call_count == 1
4871 arg_list = nmcli.Nmcli.execute_command.call_args_list
4872 add_args, add_kw = arg_list[0]
4873
4874 assert add_args[0][0] == "/usr/bin/nmcli"
4875 assert add_args[0][1] == "con"
4876 assert add_args[0][2] == "add"
4877 assert add_args[0][3] == "type"
4878 assert add_args[0][4] == "vpn"
4879 assert add_args[0][5] == "con-name"
4880 assert add_args[0][6] == "vpn_pptp"
4881
4882 add_args_text = [to_text(x) for x in add_args[0]]
4883
4884 for param in [
4885 "connection.autoconnect",
4886 "no",
4887 "connection.permissions",
4888 "brittany",
4889 "vpn.data",
4890 "vpn.service-type",
4891 "org.freedesktop.NetworkManager.pptp",
4892 ]:
4893 assert param in add_args_text
4894
4895 vpn_data_index = add_args_text.index("vpn.data") + 1
4896 args_vpn_data = add_args_text[vpn_data_index]
4897 for vpn_data in ["password-flags=2", "gateway=vpn.example.com", "user=brittany"]:
4898 assert vpn_data in args_vpn_data
4899
4900 out, err = capfd.readouterr()
4901 results = json.loads(out)
4902 assert not results.get("failed")
4903 assert results["changed"]
4904
4905
4906 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_INFINIBAND_STATIC, indirect=["patch_ansible_module"])
4907 def test_infiniband_connection_static_unchanged(mocked_infiniband_connection_static_unchanged, capfd):
4908 """
4909 Test : Infiniband connection unchanged
4910 """
4911 with pytest.raises(SystemExit):
4912 nmcli.main()
4913
4914 out, err = capfd.readouterr()
4915 results = json.loads(out)
4916 assert not results.get("failed")
4917 assert not results["changed"]
4918
4919
4920 @pytest.mark.parametrize(
4921 "patch_ansible_module", TESTCASE_INFINIBAND_STATIC_MODIFY_TRANSPORT_MODE, indirect=["patch_ansible_module"]
4922 )
4923 def test_infiniband_connection_static_transport_mode_connected(
4924 mocked_infiniband_connection_static_transport_mode_connected_modify, capfd
4925 ):
4926 """
4927 Test : Modify Infiniband connection to use connected as transport_mode
4928 """
4929 with pytest.raises(SystemExit):
4930 nmcli.main()
4931
4932 arg_list = nmcli.Nmcli.execute_command.call_args_list
4933 add_args, add_kw = arg_list[1]
4934
4935 assert add_args[0][0] == "/usr/bin/nmcli"
4936 assert add_args[0][1] == "con"
4937 assert add_args[0][2] == "modify"
4938 assert add_args[0][3] == "non_existent_nw_device"
4939
4940 add_args_text = [to_text(x) for x in add_args[0]]
4941
4942 for param in ["infiniband.transport-mode", "connected"]:
4943 assert param in add_args_text
4944
4945 out, err = capfd.readouterr()
4946 results = json.loads(out)
4947
4948 assert results.get("changed") is True
4949 assert not results.get("failed")
4950
4951
4952 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_GENERIC_DIFF_CHECK, indirect=["patch_ansible_module"])
4953 def test_bond_connection_unchanged_2(mocked_generic_connection_diff_check, capfd):
4954 """
4955 Test : Bond connection unchanged
4956 """
4957
4958 module = nmcli.create_module()
4959
4960 nmcli_module = nmcli.Nmcli(module)
4961
4962 changed, diff = nmcli_module.is_connection_changed()
4963
4964 assert changed
4965
4966 num_of_diff_params = 0
4967 for parameter, value in diff.get("before").items():
4968 if value != diff["after"][parameter]:
4969 num_of_diff_params += 1
4970
4971 assert num_of_diff_params == 1
4972
4973
4974 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_MACVLAN, indirect=["patch_ansible_module"])
4975 def test_create_macvlan(mocked_generic_connection_create, capfd):
4976 """
4977 Test : Create macvlan connection with static IP configuration
4978 """
4979
4980 with pytest.raises(SystemExit):
4981 nmcli.main()
4982
4983 assert nmcli.Nmcli.execute_command.call_count == 1
4984 arg_list = nmcli.Nmcli.execute_command.call_args_list
4985 add_args, add_kw = arg_list[0]
4986
4987 assert add_args[0][0] == "/usr/bin/nmcli"
4988 assert add_args[0][1] == "con"
4989 assert add_args[0][2] == "add"
4990 assert add_args[0][3] == "type"
4991 assert add_args[0][4] == "macvlan"
4992 assert add_args[0][5] == "con-name"
4993 assert add_args[0][6] == "non_existent_nw_device"
4994
4995 add_args_text = [to_text(x) for x in add_args[0]]
4996 for param in [
4997 "connection.interface-name",
4998 "macvlan_non_existant",
4999 "ipv4.method",
5000 "manual",
5001 "ipv4.addresses",
5002 "10.10.10.10/24",
5003 "ipv6.method",
5004 "manual",
5005 "ipv6.addresses",
5006 "2001:db8::1/128",
5007 "macvlan.mode",
5008 "2",
5009 "macvlan.parent",
5010 "non_existent_parent",
5011 ]:
5012 assert param in add_args_text
5013
5014 out, err = capfd.readouterr()
5015 results = json.loads(out)
5016 assert not results.get("failed")
5017 assert results["changed"]
5018
5019
5020 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_MACVLAN, indirect=["patch_ansible_module"])
5021 def test_macvlan_connection_unchanged(mocked_macvlan_connection_unchanged, capfd):
5022 """
5023 Test : Macvlan connection with static IP configuration unchanged
5024 """
5025 with pytest.raises(SystemExit):
5026 nmcli.main()
5027
5028 out, err = capfd.readouterr()
5029 results = json.loads(out)
5030 assert not results.get("failed")
5031 assert not results["changed"]
5032
5033
5034 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_MACVLAN, indirect=["patch_ansible_module"])
5035 def test_macvlan_mod(mocked_generic_connection_modify, capfd):
5036 """
5037 Test : Modify macvlan connection
5038 """
5039 with pytest.raises(SystemExit):
5040 nmcli.main()
5041
5042 assert nmcli.Nmcli.execute_command.call_count == 1
5043 arg_list = nmcli.Nmcli.execute_command.call_args_list
5044 args, kwargs = arg_list[0]
5045
5046 assert args[0][0] == "/usr/bin/nmcli"
5047 assert args[0][1] == "con"
5048 assert args[0][2] == "modify"
5049 assert args[0][3] == "non_existent_nw_device"
5050
5051 args_text = [to_text(x) for x in args[0]]
5052 for param in ["macvlan.mode", "2"]:
5053 assert param in args_text
5054
5055 out, err = capfd.readouterr()
5056 results = json.loads(out)
5057 assert not results.get("failed")
5058 assert results["changed"]
5059
5060
5061 TESTCASE_SLAVE_TYPE_BRIDGE_CONNECTION = [
5062 {
5063 "type": "ethernet",
5064 "conn_name": "fake_conn",
5065 "ifname": "fake_eth0",
5066 "state": "present",
5067 "slave_type": "bridge",
5068 "master": "fake_br0",
5069 "_ansible_check_mode": False,
5070 }
5071 ]
5072
5073
5074 TESTCASE_SLAVE_TYPE_BRIDGE_CONNECTION_SHOW_OUTPUT = """\
5075 connection.id: fake_conn
5076 connection.type: 802-3-ethernet
5077 connection.interface-name: fake_eth0
5078 connection.autoconnect: yes
5079 connection.master: --
5080 connection.slave-type: --
5081 802-3-ethernet.mtu: auto
5082 """
5083
5084
5085 TESTCASE_SLAVE_TYPE_BRIDGE_CONNECTION_UNCHANGED_SHOW_OUTPUT = """\
5086 connection.id: fake_conn
5087 connection.type: 802-3-ethernet
5088 connection.interface-name: fake_eth0
5089 connection.autoconnect: yes
5090 connection.master: fake_br0
5091 connection.slave-type: bridge
5092 802-3-ethernet.mtu: auto
5093 """
5094
5095
5096 @pytest.fixture
5097 def mocked_slave_type_bridge_create(mocker):
5098 mocker_set(
5099 mocker,
5100 execute_return=None,
5101 execute_side_effect=(
5102 (0, TESTCASE_SLAVE_TYPE_BRIDGE_CONNECTION_SHOW_OUTPUT, ""),
5103 (0, "", ""),
5104 ),
5105 )
5106
5107
5108 @pytest.mark.parametrize(
5109 "patch_ansible_module", TESTCASE_SLAVE_TYPE_BRIDGE_CONNECTION, indirect=["patch_ansible_module"]
5110 )
5111 def test_create_slave_type_bridge(mocked_slave_type_bridge_create, capfd):
5112 """
5113 Test : slave for bridge created
5114 """
5115
5116 with pytest.raises(SystemExit):
5117 nmcli.main()
5118
5119 assert nmcli.Nmcli.execute_command.call_count == 1
5120 arg_list = nmcli.Nmcli.execute_command.call_args_list
5121 args, kwargs = arg_list[0]
5122
5123 assert args[0][0] == "/usr/bin/nmcli"
5124 assert args[0][1] == "con"
5125 assert args[0][2] == "add"
5126 assert args[0][3] == "type"
5127 assert args[0][4] == "ethernet"
5128 assert args[0][5] == "con-name"
5129 assert args[0][6] == "fake_conn"
5130 con_master_index = args[0].index("connection.master")
5131 slave_type_index = args[0].index("connection.slave-type")
5132 assert args[0][con_master_index + 1] == "fake_br0"
5133 assert args[0][slave_type_index + 1] == "bridge"
5134
5135 out, err = capfd.readouterr()
5136 results = json.loads(out)
5137 assert not results.get("failed")
5138 assert results["changed"]
5139
5140
5141 @pytest.fixture
5142 def mocked_create_slave_type_bridge_unchanged(mocker):
5143 mocker_set(
5144 mocker,
5145 connection_exists=True,
5146 execute_return=(0, TESTCASE_SLAVE_TYPE_BRIDGE_CONNECTION_UNCHANGED_SHOW_OUTPUT, ""),
5147 )
5148
5149
5150 @pytest.mark.parametrize(
5151 "patch_ansible_module", TESTCASE_SLAVE_TYPE_BRIDGE_CONNECTION, indirect=["patch_ansible_module"]
5152 )
5153 def test_slave_type_bridge_unchanged(mocked_create_slave_type_bridge_unchanged, capfd):
5154 """
5155 Test : Existent slave for bridge unchanged
5156 """
5157 with pytest.raises(SystemExit):
5158 nmcli.main()
5159
5160 out, err = capfd.readouterr()
5161 results = json.loads(out)
5162 assert not results.get("failed")
5163 assert not results["changed"]
5164
5165
5166 TESTCASE_SLAVE_TYPE_BOND_CONNECTION = [
5167 {
5168 "type": "ethernet",
5169 "conn_name": "fake_conn",
5170 "ifname": "fake_eth0",
5171 "state": "present",
5172 "slave_type": "bond",
5173 "master": "fake_bond0",
5174 "_ansible_check_mode": False,
5175 }
5176 ]
5177
5178
5179 TESTCASE_SLAVE_TYPE_BOND_CONNECTION_SHOW_OUTPUT = """\
5180 connection.id: fake_conn
5181 connection.type: 802-3-ethernet
5182 connection.interface-name: fake_eth0
5183 connection.autoconnect: yes
5184 connection.master: --
5185 connection.slave-type: --
5186 802-3-ethernet.mtu: auto
5187 """
5188
5189
5190 TESTCASE_SLAVE_TYPE_BOND_CONNECTION_UNCHANGED_SHOW_OUTPUT = """\
5191 connection.id: fake_conn
5192 connection.type: 802-3-ethernet
5193 connection.interface-name: fake_eth0
5194 connection.autoconnect: yes
5195 connection.master: fake_bond0
5196 connection.slave-type: bond
5197 802-3-ethernet.mtu: auto
5198 """
5199
5200
5201 @pytest.fixture
5202 def mocked_slave_type_bond_create(mocker):
5203 mocker_set(
5204 mocker,
5205 execute_return=None,
5206 execute_side_effect=(
5207 (0, TESTCASE_SLAVE_TYPE_BOND_CONNECTION_SHOW_OUTPUT, ""),
5208 (0, "", ""),
5209 ),
5210 )
5211
5212
5213 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SLAVE_TYPE_BOND_CONNECTION, indirect=["patch_ansible_module"])
5214 def test_create_slave_type_bond(mocked_slave_type_bond_create, capfd):
5215 """
5216 Test : slave for bond created
5217 """
5218
5219 with pytest.raises(SystemExit):
5220 nmcli.main()
5221
5222 assert nmcli.Nmcli.execute_command.call_count == 1
5223 arg_list = nmcli.Nmcli.execute_command.call_args_list
5224 args, kwargs = arg_list[0]
5225
5226 assert args[0][0] == "/usr/bin/nmcli"
5227 assert args[0][1] == "con"
5228 assert args[0][2] == "add"
5229 assert args[0][3] == "type"
5230 assert args[0][4] == "ethernet"
5231 assert args[0][5] == "con-name"
5232 assert args[0][6] == "fake_conn"
5233 con_master_index = args[0].index("connection.master")
5234 slave_type_index = args[0].index("connection.slave-type")
5235 assert args[0][con_master_index + 1] == "fake_bond0"
5236 assert args[0][slave_type_index + 1] == "bond"
5237
5238 out, err = capfd.readouterr()
5239 results = json.loads(out)
5240 assert not results.get("failed")
5241 assert results["changed"]
5242
5243
5244 @pytest.fixture
5245 def mocked_create_slave_type_bond_unchanged(mocker):
5246 mocker_set(
5247 mocker,
5248 connection_exists=True,
5249 execute_return=(0, TESTCASE_SLAVE_TYPE_BOND_CONNECTION_UNCHANGED_SHOW_OUTPUT, ""),
5250 )
5251
5252
5253 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SLAVE_TYPE_BOND_CONNECTION, indirect=["patch_ansible_module"])
5254 def test_slave_type_bond_unchanged(mocked_create_slave_type_bond_unchanged, capfd):
5255 """
5256 Test : Existent slave for bridge unchanged
5257 """
5258 with pytest.raises(SystemExit):
5259 nmcli.main()
5260
5261 out, err = capfd.readouterr()
5262 results = json.loads(out)
5263 assert not results.get("failed")
5264 assert not results["changed"]
5265
5266
5267 TESTCASE_SLAVE_TYPE_TEAM_CONNECTION = [
5268 {
5269 "type": "ethernet",
5270 "conn_name": "fake_conn",
5271 "ifname": "fake_eth0",
5272 "state": "present",
5273 "slave_type": "team",
5274 "master": "fake_team0",
5275 "_ansible_check_mode": False,
5276 }
5277 ]
5278
5279
5280 TESTCASE_SLAVE_TYPE_TEAM_CONNECTION_SHOW_OUTPUT = """\
5281 connection.id: fake_conn
5282 connection.type: 802-3-ethernet
5283 connection.interface-name: fake_eth0
5284 connection.autoconnect: yes
5285 connection.master: --
5286 connection.slave-type: --
5287 802-3-ethernet.mtu: auto
5288 """
5289
5290
5291 TESTCASE_SLAVE_TYPE_TEAM_CONNECTION_UNCHANGED_SHOW_OUTPUT = """\
5292 connection.id: fake_conn
5293 connection.type: 802-3-ethernet
5294 connection.interface-name: fake_eth0
5295 connection.autoconnect: yes
5296 connection.master: fake_team0
5297 connection.slave-type: team
5298 802-3-ethernet.mtu: auto
5299 """
5300
5301
5302 @pytest.fixture
5303 def mocked_slave_type_team_create(mocker):
5304 mocker_set(
5305 mocker,
5306 execute_return=None,
5307 execute_side_effect=(
5308 (0, TESTCASE_SLAVE_TYPE_TEAM_CONNECTION_SHOW_OUTPUT, ""),
5309 (0, "", ""),
5310 ),
5311 )
5312
5313
5314 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SLAVE_TYPE_TEAM_CONNECTION, indirect=["patch_ansible_module"])
5315 def test_create_slave_type_team(mocked_slave_type_team_create, capfd):
5316 """
5317 Test : slave for bond created
5318 """
5319
5320 with pytest.raises(SystemExit):
5321 nmcli.main()
5322
5323 assert nmcli.Nmcli.execute_command.call_count == 1
5324 arg_list = nmcli.Nmcli.execute_command.call_args_list
5325 args, kwargs = arg_list[0]
5326
5327 assert args[0][0] == "/usr/bin/nmcli"
5328 assert args[0][1] == "con"
5329 assert args[0][2] == "add"
5330 assert args[0][3] == "type"
5331 assert args[0][4] == "ethernet"
5332 assert args[0][5] == "con-name"
5333 assert args[0][6] == "fake_conn"
5334 con_master_index = args[0].index("connection.master")
5335 slave_type_index = args[0].index("connection.slave-type")
5336 assert args[0][con_master_index + 1] == "fake_team0"
5337 assert args[0][slave_type_index + 1] == "team"
5338
5339 out, err = capfd.readouterr()
5340 results = json.loads(out)
5341 assert not results.get("failed")
5342 assert results["changed"]
5343
5344
5345 @pytest.fixture
5346 def mocked_create_slave_type_team_unchanged(mocker):
5347 mocker_set(
5348 mocker,
5349 connection_exists=True,
5350 execute_return=(0, TESTCASE_SLAVE_TYPE_TEAM_CONNECTION_UNCHANGED_SHOW_OUTPUT, ""),
5351 )
5352
5353
5354 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_SLAVE_TYPE_TEAM_CONNECTION, indirect=["patch_ansible_module"])
5355 def test_slave_type_team_unchanged(mocked_create_slave_type_team_unchanged, capfd):
5356 """
5357 Test : Existent slave for bridge unchanged
5358 """
5359 with pytest.raises(SystemExit):
5360 nmcli.main()
5361
5362 out, err = capfd.readouterr()
5363 results = json.loads(out)
5364 assert not results.get("failed")
5365 assert not results["changed"]
5366
5367
5368 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_LOOPBACK, indirect=["patch_ansible_module"])
5369 def test_create_loopback(mocked_generic_connection_create, capfd):
5370 """
5371 Test : Create loopback connection
5372 """
5373
5374 with pytest.raises(SystemExit):
5375 nmcli.main()
5376
5377 assert nmcli.Nmcli.execute_command.call_count == 1
5378 arg_list = nmcli.Nmcli.execute_command.call_args_list
5379 add_args, add_kw = arg_list[0]
5380
5381 assert add_args[0][0] == "/usr/bin/nmcli"
5382 assert add_args[0][1] == "con"
5383 assert add_args[0][2] == "add"
5384 assert add_args[0][3] == "type"
5385 assert add_args[0][4] == "loopback"
5386 assert add_args[0][5] == "con-name"
5387 assert add_args[0][6] == "lo"
5388
5389 add_args_text = [to_text(x) for x in add_args[0]]
5390 for param in ["connection.interface-name", "lo", "ipv4.addresses", "127.0.0.1/8"]:
5391 assert param in add_args_text
5392
5393 out, err = capfd.readouterr()
5394 results = json.loads(out)
5395 assert not results.get("failed")
5396 assert results["changed"]
5397
5398
5399 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_LOOPBACK, indirect=["patch_ansible_module"])
5400 def test_unchanged_loopback(mocked_loopback_connection_unchanged, capfd):
5401 """
5402 Test : loopback connection unchanged
5403 """
5404 with pytest.raises(SystemExit):
5405 nmcli.main()
5406
5407 out, err = capfd.readouterr()
5408 results = json.loads(out)
5409 assert not results.get("failed")
5410 assert not results["changed"]
5411
5412
5413 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_LOOPBACK_MODIFY, indirect=["patch_ansible_module"])
5414 def test_add_second_ip4_address_to_loopback_connection(mocked_loopback_connection_modify, capfd):
5415 """
5416 Test : Modify loopback connection
5417 """
5418 with pytest.raises(SystemExit):
5419 nmcli.main()
5420
5421 assert nmcli.Nmcli.execute_command.call_count == 2
5422 arg_list = nmcli.Nmcli.execute_command.call_args_list
5423 args, kwargs = arg_list[1]
5424
5425 assert args[0][0] == "/usr/bin/nmcli"
5426 assert args[0][1] == "con"
5427 assert args[0][2] == "modify"
5428 assert args[0][3] == "lo"
5429
5430 for param in ["ipv4.addresses", "127.0.0.1/8,127.0.0.2/8"]:
5431 assert param in args[0]
5432
5433 out, err = capfd.readouterr()
5434 results = json.loads(out)
5435 assert not results.get("failed")
5436 assert results["changed"]
5437
5438
5439 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VRF, indirect=["patch_ansible_module"])
5440 def test_create_vrf_con(mocked_generic_connection_create, capfd):
5441 """
5442 Test if VRF created
5443 """
5444
5445 with pytest.raises(SystemExit):
5446 nmcli.main()
5447
5448 assert nmcli.Nmcli.execute_command.call_count == 1
5449 arg_list = nmcli.Nmcli.execute_command.call_args_list
5450 args, kwargs = arg_list[0]
5451
5452 assert args[0][0] == "/usr/bin/nmcli"
5453 assert args[0][1] == "con"
5454 assert args[0][2] == "add"
5455 assert args[0][3] == "type"
5456 assert args[0][4] == "vrf"
5457 assert args[0][5] == "con-name"
5458 assert args[0][6] == "non_existent_nw_device"
5459
5460 args_text = [to_text(x) for x in args[0]]
5461 for param in ["ipv4.addresses", "10.10.10.10/24", "ipv4.gateway", "10.10.10.1", "table", "10"]:
5462 assert param in args_text
5463
5464 out, err = capfd.readouterr()
5465 results = json.loads(out)
5466 assert not results.get("failed")
5467 assert results["changed"]
5468
5469
5470 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VRF, indirect=["patch_ansible_module"])
5471 def test_mod_vrf_conn(mocked_generic_connection_modify, capfd):
5472 """
5473 Test if VRF modified
5474 """
5475
5476 with pytest.raises(SystemExit):
5477 nmcli.main()
5478
5479 assert nmcli.Nmcli.execute_command.call_count == 1
5480 arg_list = nmcli.Nmcli.execute_command.call_args_list
5481 args, kwargs = arg_list[0]
5482
5483 assert args[0][0] == "/usr/bin/nmcli"
5484 assert args[0][1] == "con"
5485 assert args[0][2] == "modify"
5486 assert args[0][3] == "non_existent_nw_device"
5487
5488 args_text = [to_text(x) for x in args[0]]
5489 for param in ["ipv4.addresses", "10.10.10.10/24", "ipv4.gateway", "10.10.10.1", "table", "10"]:
5490 assert param in args_text
5491
5492 out, err = capfd.readouterr()
5493 results = json.loads(out)
5494 assert not results.get("failed")
5495 assert results["changed"]
5496
5497
5498 @pytest.mark.parametrize("patch_ansible_module", TESTCASE_VRF, indirect=["patch_ansible_module"])
5499 def test_vrf_connection_unchanged(mocked_vrf_connection_unchanged, capfd):
5500 """
5501 Test : VRF connection unchanged
5502 """
5503 with pytest.raises(SystemExit):
5504 nmcli.main()
5505
5506 out, err = capfd.readouterr()
5507 results = json.loads(out)
5508 assert not results.get("failed")
5509 assert not results["changed"]