Debian Codegraph @

ansible_collections/community/routeros/tests/unit/plugins/modules/test_api_modify.py

raw
1 # Copyright (c) 2022, Felix Fontein (@felixfontein) <[email protected]>
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 # Make coding more python3-ish
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8
9 from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch, MagicMock
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase
11
12 from ansible_collections.community.routeros.tests.unit.plugins.modules.fake_api import (
13 FAKE_ROS_VERSION, FakeLibRouterosError, fake_ros_api, massage_expected_result_data, create_fake_path,
14 )
15 from ansible_collections.community.routeros.plugins.modules import api_modify
16
17
18 START_IP_DNS_STATIC = [
19 {
20 '.id': '*1',
21 'comment': 'defconf',
22 'name': 'router',
23 'address': '192.168.88.1',
24 'dynamic': False,
25 },
26 {
27 '.id': '*A',
28 'name': 'router',
29 'text': 'Router Text Entry',
30 'dynamic': False,
31 },
32 {
33 '.id': '*7',
34 'comment': '',
35 'name': 'foo',
36 'address': '192.168.88.2',
37 'dynamic': False,
38 },
39 {
40 '.id': '*8',
41 'comment': '',
42 'name': 'dynfoo',
43 'address': '192.168.88.15',
44 'dynamic': True,
45 },
46 {
47 '.id': '*9',
48 'comment': '',
49 'name': 'foobar',
50 'type': 'SRV',
51 'srv-port': '123',
52 'srv-priority': 5,
53 'srv-target': '192.168.88.23',
54 'srv-weight': 15,
55 'dynamic': False,
56 },
57 ]
58
59 START_IP_DNS_STATIC_OLD_DATA = massage_expected_result_data(START_IP_DNS_STATIC, ('ip', 'dns', 'static'), remove_dynamic=True)
60
61 START_IP_SETTINGS = [
62 {
63 'accept-redirects': True,
64 'accept-source-route': False,
65 'allow-fast-path': True,
66 'arp-timeout': '30s',
67 'icmp-rate-limit': 20,
68 'icmp-rate-mask': '0x1818',
69 'ip-forward': True,
70 'max-neighbor-entries': 8192,
71 'route-cache': True,
72 'rp-filter': False,
73 'secure-redirects': True,
74 'send-redirects': True,
75 'tcp-syncookies': False,
76 },
77 ]
78
79 START_IP_SETTINGS_OLD_DATA = massage_expected_result_data(START_IP_SETTINGS, ('ip', 'settings'))
80
81 START_IP_ADDRESS = [
82 {
83 '.id': '*1',
84 'address': '192.168.88.0/24',
85 'interface': 'bridge',
86 'disabled': False,
87 },
88 {
89 '.id': '*3',
90 'address': '192.168.1.0/24',
91 'interface': 'LAN',
92 'disabled': False,
93 },
94 {
95 '.id': '*F',
96 'address': '10.0.0.0/16',
97 'interface': 'WAN',
98 'disabled': True,
99 },
100 ]
101
102 START_IP_ADDRESS_OLD_DATA = massage_expected_result_data(START_IP_ADDRESS, ('ip', 'address'))
103
104 START_IP_DHCP_CLIENT = [
105 {
106 "!comment": None,
107 "!script": None,
108 ".id": "*1",
109 "add-default-route": True,
110 "default-route-distance": 1,
111 "dhcp-options": "hostname,clientid",
112 "disabled": False,
113 "interface": "ether1",
114 "use-peer-dns": True,
115 "use-peer-ntp": True,
116 },
117 {
118 "!comment": None,
119 "!dhcp-options": None,
120 "!script": None,
121 ".id": "*2",
122 "add-default-route": True,
123 "default-route-distance": 1,
124 "disabled": False,
125 "interface": "ether2",
126 "use-peer-dns": True,
127 "use-peer-ntp": True,
128 },
129 {
130 "!comment": None,
131 "!script": None,
132 ".id": "*3",
133 "add-default-route": True,
134 "default-route-distance": 1,
135 "dhcp-options": "hostname",
136 "disabled": False,
137 "interface": "ether3",
138 "use-peer-dns": True,
139 "use-peer-ntp": True,
140 },
141 ]
142
143 START_IP_DHCP_CLIENT_OLD_DATA = massage_expected_result_data(START_IP_DHCP_CLIENT, ('ip', 'dhcp-client'))
144
145 START_IP_DHCP_SERVER_LEASE = [
146 {
147 '.id': '*1',
148 'address': '192.168.88.2',
149 'mac-address': '11:22:33:44:55:66',
150 'client-id': 'ff:1:2:3:4:5:6:7:8:9:a:b:c:d:e:f:0:1:2',
151 'address-lists': '',
152 'server': 'main',
153 'dhcp-option': '',
154 'status': 'waiting',
155 'last-seen': 'never',
156 'radius': False,
157 'dynamic': False,
158 'blocked': False,
159 'disabled': False,
160 'comment': 'foo',
161 },
162 {
163 '.id': '*2',
164 'address': '192.168.88.3',
165 'mac-address': '11:22:33:44:55:77',
166 'client-id': '1:2:3:4:5:6:7',
167 'address-lists': '',
168 'server': 'main',
169 'dhcp-option': '',
170 'status': 'bound',
171 'expires-after': '3d7m8s',
172 'last-seen': '1m52s',
173 'active-address': '192.168.88.14',
174 'active-mac-address': '11:22:33:44:55:76',
175 'active-client-id': '1:2:3:4:5:6:7',
176 'active-server': 'main',
177 'host-name': 'bar',
178 'radius': False,
179 'dynamic': False,
180 'blocked': False,
181 'disabled': False,
182 },
183 {
184 '.id': '*3',
185 'address': '0.0.0.1',
186 'mac-address': '00:00:00:00:00:01',
187 'address-lists': '',
188 'dhcp-option': '',
189 'status': 'waiting',
190 'last-seen': 'never',
191 'radius': False,
192 'dynamic': False,
193 'blocked': False,
194 'disabled': False,
195 },
196 {
197 '.id': '*4',
198 'address': '0.0.0.2',
199 'mac-address': '00:00:00:00:00:02',
200 'address-lists': '',
201 'dhcp-option': '',
202 'status': 'waiting',
203 'last-seen': 'never',
204 'radius': False,
205 'dynamic': False,
206 'blocked': False,
207 'disabled': False,
208 },
209 ]
210
211 START_IP_DHCP_SERVER_LEASE_OLD_DATA = massage_expected_result_data(START_IP_DHCP_SERVER_LEASE, ('ip', 'dhcp-server', 'lease'))
212
213 START_INTERFACE_LIST = [
214 {
215 '.id': '*2000000',
216 'name': 'all',
217 'dynamic': False,
218 'include': '',
219 'exclude': '',
220 'builtin': True,
221 'comment': 'contains all interfaces',
222 },
223 {
224 '.id': '*2000001',
225 'name': 'none',
226 'dynamic': False,
227 'include': '',
228 'exclude': '',
229 'builtin': True,
230 'comment': 'contains no interfaces',
231 },
232 {
233 '.id': '*2000010',
234 'name': 'WAN',
235 'dynamic': False,
236 'include': '',
237 'exclude': '',
238 'builtin': False,
239 'comment': 'defconf',
240 },
241 {
242 '.id': '*2000011',
243 'name': 'Foo',
244 'dynamic': False,
245 'include': '',
246 'exclude': '',
247 'builtin': False,
248 'comment': '',
249 },
250 ]
251
252 START_INTERFACE_LIST_OLD_DATA = massage_expected_result_data(START_INTERFACE_LIST, ('interface', 'list'), remove_builtin=True)
253
254 START_INTERFACE_GRE = [
255 {
256 '.id': '*10',
257 'name': 'gre-tunnel3',
258 'mtu': 'auto',
259 'actual-mtu': 65496,
260 'local-address': '0.0.0.0',
261 'remote-address': '192.168.1.1',
262 'dscp': 'inherit',
263 'clamp-tcp-mss': True,
264 'dont-fragment': False,
265 'allow-fast-path': True,
266 'running': True,
267 'disabled': False,
268 },
269 {
270 '.id': '*11',
271 'name': 'gre-tunnel4',
272 'mtu': 'auto',
273 'actual-mtu': 65496,
274 'local-address': '0.0.0.0',
275 'remote-address': '192.168.1.2',
276 'keepalive': '10s,10',
277 'dscp': 'inherit',
278 'clamp-tcp-mss': True,
279 'dont-fragment': False,
280 'allow-fast-path': True,
281 'running': True,
282 'disabled': False,
283 },
284 {
285 '.id': '*12',
286 'name': 'gre-tunnel5',
287 'mtu': 'auto',
288 'actual-mtu': 65496,
289 'local-address': '192.168.0.1',
290 'remote-address': '192.168.1.3',
291 'keepalive': '20s,20',
292 'dscp': 'inherit',
293 'clamp-tcp-mss': True,
294 'dont-fragment': False,
295 'allow-fast-path': True,
296 'running': True,
297 'disabled': False,
298 'comment': 'foo',
299 },
300 ]
301
302 START_INTERFACE_GRE_OLD_DATA = massage_expected_result_data(START_INTERFACE_GRE, ('interface', 'gre'))
303
304
305 class TestRouterosApiModifyModule(ModuleTestCase):
306
307 def setUp(self):
308 super(TestRouterosApiModifyModule, self).setUp()
309 self.module = api_modify
310 self.module.LibRouterosError = FakeLibRouterosError
311 self.module.connect = MagicMock(new=fake_ros_api)
312 self.module.check_has_library = MagicMock()
313 self.patch_create_api = patch(
314 'ansible_collections.community.routeros.plugins.modules.api_modify.create_api',
315 MagicMock(new=fake_ros_api))
316 self.patch_create_api.start()
317 self.patch_get_api_version = patch(
318 'ansible_collections.community.routeros.plugins.modules.api_modify.get_api_version',
319 MagicMock(return_value=FAKE_ROS_VERSION))
320 self.patch_get_api_version.start()
321 self.config_module_args = {
322 'username': 'admin',
323 'password': 'pаss',
324 'hostname': '127.0.0.1',
325 }
326
327 def tearDown(self):
328 self.patch_get_api_version.stop()
329 self.patch_create_api.stop()
330
331 def test_module_fail_when_required_args_missing(self):
332 with self.assertRaises(AnsibleFailJson) as exc:
333 with set_module_args({}):
334 self.module.main()
335
336 result = exc.exception.args[0]
337 self.assertEqual(result['failed'], True)
338
339 def test_invalid_path(self):
340 with self.assertRaises(AnsibleFailJson) as exc:
341 args = self.config_module_args.copy()
342 args.update({
343 'path': 'something invalid',
344 'data': [],
345 })
346 with set_module_args(args):
347 self.module.main()
348
349 result = exc.exception.args[0]
350 self.assertEqual(result['failed'], True)
351 self.assertEqual(result['msg'].startswith('value of path must be one of: '), True)
352
353 def test_invalid_option(self):
354 with self.assertRaises(AnsibleFailJson) as exc:
355 args = self.config_module_args.copy()
356 args.update({
357 'path': 'ip dns static',
358 'data': [{
359 'name': 'baz',
360 'foo': 'bar',
361 }],
362 })
363 with set_module_args(args):
364 self.module.main()
365
366 result = exc.exception.args[0]
367 self.assertEqual(result['failed'], True)
368 self.assertEqual(result['msg'], 'Unknown key "foo" at index 1.')
369
370 def test_invalid_disabled_and_enabled_option(self):
371 with self.assertRaises(AnsibleFailJson) as exc:
372 args = self.config_module_args.copy()
373 args.update({
374 'path': 'ip dns static',
375 'data': [{
376 'name': 'baz',
377 'comment': 'foo',
378 '!comment': None,
379 }],
380 })
381 with set_module_args(args):
382 self.module.main()
383
384 result = exc.exception.args[0]
385 self.assertEqual(result['failed'], True)
386 self.assertEqual(result['msg'], 'Not both "comment" and "!comment" must appear at index 1.')
387
388 def test_invalid_disabled_option(self):
389 with self.assertRaises(AnsibleFailJson) as exc:
390 args = self.config_module_args.copy()
391 args.update({
392 'path': 'ip dns static',
393 'data': [{
394 'name': 'foo',
395 '!disabled': None,
396 }],
397 })
398 with set_module_args(args):
399 self.module.main()
400
401 result = exc.exception.args[0]
402 self.assertEqual(result['failed'], True)
403 self.assertEqual(result['msg'], 'Key "!disabled" must not be disabled (leading "!") at index 1.')
404
405 def test_invalid_disabled_option_value(self):
406 with self.assertRaises(AnsibleFailJson) as exc:
407 args = self.config_module_args.copy()
408 args.update({
409 'path': 'ip dns static',
410 'data': [{
411 'name': 'baz',
412 '!comment': 'foo',
413 }],
414 })
415 with set_module_args(args):
416 self.module.main()
417
418 result = exc.exception.args[0]
419 self.assertEqual(result['failed'], True)
420 self.assertEqual(result['msg'], 'Disabled key "!comment" must not have a value at index 1.')
421
422 def test_invalid_non_disabled_option_value(self):
423 with self.assertRaises(AnsibleFailJson) as exc:
424 args = self.config_module_args.copy()
425 args.update({
426 'path': 'ip dns static',
427 'data': [{
428 'name': None,
429 }],
430 })
431 with set_module_args(args):
432 self.module.main()
433
434 result = exc.exception.args[0]
435 self.assertEqual(result['failed'], True)
436 self.assertEqual(result['msg'], 'Key "name" must not be disabled (value null/~/None) at index 1.')
437
438 def test_invalid_required_missing(self):
439 with self.assertRaises(AnsibleFailJson) as exc:
440 args = self.config_module_args.copy()
441 args.update({
442 'path': 'ip dhcp-server',
443 'data': [{
444 'interface': 'eth0',
445 }],
446 })
447 with set_module_args(args):
448 self.module.main()
449
450 result = exc.exception.args[0]
451 self.assertEqual(result['failed'], True)
452 self.assertEqual(result['msg'], 'Every element in data must contain "name". For example, the element at index #1 does not provide it.')
453
454 def test_invalid_required_one_of_missing(self):
455 with self.assertRaises(AnsibleFailJson) as exc:
456 args = self.config_module_args.copy()
457 args.update({
458 'path': 'ip dns static',
459 'data': [{
460 'address': '192.168.88.1',
461 }],
462 })
463 with set_module_args(args):
464 self.module.main()
465
466 result = exc.exception.args[0]
467 self.assertEqual(result['failed'], True)
468 self.assertEqual(result['msg'], 'Every element in data must contain one of "name", "regexp". For example, the element at index 1 does not provide it.')
469
470 def test_invalid_mutually_exclusive_both(self):
471 with self.assertRaises(AnsibleFailJson) as exc:
472 args = self.config_module_args.copy()
473 args.update({
474 'path': 'ip dns static',
475 'data': [{
476 'name': 'foo',
477 'regexp': 'bar',
478 'address': '192.168.88.1',
479 }],
480 })
481 with set_module_args(args):
482 self.module.main()
483
484 result = exc.exception.args[0]
485 self.assertEqual(result['failed'], True)
486 self.assertEqual(result['msg'], 'Keys "name", "regexp" cannot be used at the same time at index 1.')
487
488 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
489 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC, read_only=True))
490 def test_sync_list_idempotent(self):
491 with self.assertRaises(AnsibleExitJson) as exc:
492 args = self.config_module_args.copy()
493 args.update({
494 'path': 'ip dns static',
495 'data': [
496 {
497 '.id': 'bam', # this should be ignored
498 'comment': 'defconf',
499 'name': 'router',
500 'address': '192.168.88.1',
501 },
502 {
503 'name': 'foo',
504 'address': '192.168.88.2',
505 },
506 {
507 'comment': None,
508 'name': 'router',
509 'text': 'Router Text Entry',
510 },
511 {
512 'name': 'foobar',
513 'type': 'SRV',
514 'srv-port': '123',
515 'srv-priority': 5,
516 'srv-target': '192.168.88.23',
517 },
518 ],
519 })
520 with set_module_args(args):
521 self.module.main()
522
523 result = exc.exception.args[0]
524 self.assertEqual(result['changed'], False)
525 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
526 self.assertEqual(result['new_data'], START_IP_DNS_STATIC_OLD_DATA)
527
528 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
529 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC, read_only=True))
530 def test_sync_list_idempotent_2(self):
531 with self.assertRaises(AnsibleExitJson) as exc:
532 args = self.config_module_args.copy()
533 args.update({
534 'path': 'ip dns static',
535 'data': [
536 {
537 'comment': 'defconf',
538 'name': 'router',
539 'address': '192.168.88.1',
540 },
541 {
542 'name': 'foo',
543 'comment': '',
544 'address': '192.168.88.2',
545 },
546 {
547 'name': 'router',
548 '!comment': None,
549 'text': 'Router Text Entry',
550 },
551 {
552 'name': 'foobar',
553 'type': 'SRV',
554 'srv-port': 123,
555 'srv-priority': '5',
556 'srv-target': '192.168.88.23',
557 'srv-weight': '15',
558 },
559 ],
560 'handle_absent_entries': 'remove',
561 'handle_entries_content': 'remove',
562 })
563 with set_module_args(args):
564 self.module.main()
565
566 result = exc.exception.args[0]
567 self.assertEqual(result['changed'], False)
568 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
569 self.assertEqual(result['new_data'], START_IP_DNS_STATIC_OLD_DATA)
570
571 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
572 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC, read_only=True))
573 def test_sync_list_idempotent_3(self):
574 with self.assertRaises(AnsibleExitJson) as exc:
575 args = self.config_module_args.copy()
576 args.update({
577 'path': 'ip dns static',
578 'data': [
579 {
580 'comment': 'defconf',
581 'name': 'router',
582 'address': '192.168.88.1',
583 },
584 ],
585 })
586 with set_module_args(args):
587 self.module.main()
588
589 result = exc.exception.args[0]
590 self.assertEqual(result['changed'], False)
591 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
592 self.assertEqual(result['new_data'], START_IP_DNS_STATIC_OLD_DATA)
593
594 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
595 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC))
596 def test_sync_list_add(self):
597 with self.assertRaises(AnsibleExitJson) as exc:
598 args = self.config_module_args.copy()
599 args.update({
600 'path': 'ip dns static',
601 'data': [
602 {
603 'comment': 'defconf',
604 'name': 'router',
605 'address': '192.168.88.1',
606 },
607 {
608 'name': 'router',
609 'text': 'Router Text Entry',
610 },
611 {
612 'name': 'router',
613 'text': 'Router Text Entry 2',
614 },
615 {
616 'name': 'foo',
617 'address': '192.168.88.2',
618 },
619 ],
620 })
621 with set_module_args(args):
622 self.module.main()
623
624 result = exc.exception.args[0]
625 self.assertEqual(result['changed'], True)
626 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
627 self.assertEqual(result['new_data'], [
628 {
629 '.id': '*1',
630 'comment': 'defconf',
631 'name': 'router',
632 'address': '192.168.88.1',
633 'ttl': '1d',
634 'disabled': False,
635 'match-subdomain': False,
636 },
637 {
638 '.id': '*A',
639 'name': 'router',
640 'text': 'Router Text Entry',
641 'ttl': '1d',
642 'disabled': False,
643 'match-subdomain': False,
644 },
645 {
646 '.id': '*7',
647 'name': 'foo',
648 'address': '192.168.88.2',
649 'ttl': '1d',
650 'disabled': False,
651 'match-subdomain': False,
652 },
653 {
654 '.id': '*9',
655 'name': 'foobar',
656 'type': 'SRV',
657 'srv-port': '123',
658 'srv-priority': 5,
659 'srv-target': '192.168.88.23',
660 'srv-weight': 15,
661 'ttl': '1d',
662 'disabled': False,
663 'match-subdomain': False,
664 },
665 {
666 '.id': '*NEW1',
667 'name': 'router',
668 'text': 'Router Text Entry 2',
669 'ttl': '1d',
670 'disabled': False,
671 'match-subdomain': False,
672 },
673 ])
674
675 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
676 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC))
677 def test_sync_list_modify_1(self):
678 with self.assertRaises(AnsibleExitJson) as exc:
679 args = self.config_module_args.copy()
680 args.update({
681 'path': 'ip dns static',
682 'data': [
683 {
684 'comment': 'defconf',
685 'name': 'router',
686 'address': '192.168.88.1',
687 },
688 {
689 'name': 'router',
690 'text': 'Router Text Entry 2',
691 },
692 {
693 'name': 'foo',
694 'address': '192.168.88.2',
695 },
696 ],
697 })
698 with set_module_args(args):
699 self.module.main()
700
701 result = exc.exception.args[0]
702 self.assertEqual(result['changed'], True)
703 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
704 self.assertEqual(result['new_data'], [
705 {
706 '.id': '*1',
707 'comment': 'defconf',
708 'name': 'router',
709 'address': '192.168.88.1',
710 'ttl': '1d',
711 'disabled': False,
712 'match-subdomain': False,
713 },
714 {
715 '.id': '*A',
716 'name': 'router',
717 'text': 'Router Text Entry',
718 'ttl': '1d',
719 'disabled': False,
720 'match-subdomain': False,
721 },
722 {
723 '.id': '*7',
724 'name': 'foo',
725 'address': '192.168.88.2',
726 'ttl': '1d',
727 'disabled': False,
728 'match-subdomain': False,
729 },
730 {
731 '.id': '*9',
732 'name': 'foobar',
733 'type': 'SRV',
734 'srv-port': '123',
735 'srv-priority': 5,
736 'srv-target': '192.168.88.23',
737 'srv-weight': 15,
738 'ttl': '1d',
739 'disabled': False,
740 'match-subdomain': False,
741 },
742 {
743 '.id': '*NEW1',
744 'name': 'router',
745 'text': 'Router Text Entry 2',
746 'ttl': '1d',
747 'disabled': False,
748 'match-subdomain': False,
749 },
750 ])
751
752 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
753 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC, read_only=True))
754 def test_sync_list_modify_1_check(self):
755 with self.assertRaises(AnsibleExitJson) as exc:
756 args = self.config_module_args.copy()
757 args.update({
758 'path': 'ip dns static',
759 'data': [
760 {
761 'comment': 'defconf',
762 'name': 'router',
763 'address': '192.168.88.1',
764 },
765 {
766 'name': 'router',
767 'text': 'Router Text Entry 2',
768 },
769 {
770 'name': 'foo',
771 'address': '192.168.88.2',
772 },
773 ],
774 '_ansible_check_mode': True,
775 })
776 with set_module_args(args):
777 self.module.main()
778
779 result = exc.exception.args[0]
780 self.assertEqual(result['changed'], True)
781 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
782 self.assertEqual(result['new_data'], [
783 {
784 '.id': '*1',
785 'comment': 'defconf',
786 'name': 'router',
787 'address': '192.168.88.1',
788 'ttl': '1d',
789 'disabled': False,
790 'match-subdomain': False,
791 },
792 {
793 '.id': '*A',
794 'name': 'router',
795 'text': 'Router Text Entry',
796 'ttl': '1d',
797 'disabled': False,
798 'match-subdomain': False,
799 },
800 {
801 '.id': '*7',
802 'name': 'foo',
803 'address': '192.168.88.2',
804 'ttl': '1d',
805 'disabled': False,
806 'match-subdomain': False,
807 },
808 {
809 '.id': '*9',
810 'name': 'foobar',
811 'type': 'SRV',
812 'srv-port': '123',
813 'srv-priority': 5,
814 'srv-target': '192.168.88.23',
815 'srv-weight': 15,
816 'ttl': '1d',
817 'disabled': False,
818 'match-subdomain': False,
819 },
820 {
821 'name': 'router',
822 'text': 'Router Text Entry 2',
823 },
824 ])
825
826 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
827 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC))
828 def test_sync_list_modify_2(self):
829 with self.assertRaises(AnsibleExitJson) as exc:
830 args = self.config_module_args.copy()
831 args.update({
832 'path': 'ip dns static',
833 'data': [
834 {
835 'comment': '',
836 'name': 'router',
837 'address': '192.168.88.1',
838 },
839 {
840 'name': 'router',
841 'text': 'Router Text Entry 2',
842 },
843 {
844 'name': 'foo',
845 'address': '192.168.88.2',
846 },
847 ],
848 'handle_absent_entries': 'remove',
849 'handle_entries_content': 'remove',
850 })
851 with set_module_args(args):
852 self.module.main()
853
854 result = exc.exception.args[0]
855 self.assertEqual(result['changed'], True)
856 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
857 self.assertEqual(result['new_data'], [
858 {
859 '.id': '*1',
860 'name': 'router',
861 'address': '192.168.88.1',
862 'ttl': '1d',
863 'disabled': False,
864 'match-subdomain': False,
865 },
866 {
867 '.id': '*A',
868 'name': 'router',
869 'text': 'Router Text Entry 2',
870 'ttl': '1d',
871 'disabled': False,
872 'match-subdomain': False,
873 },
874 {
875 '.id': '*7',
876 'name': 'foo',
877 'address': '192.168.88.2',
878 'ttl': '1d',
879 'disabled': False,
880 'match-subdomain': False,
881 },
882 ])
883
884 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
885 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC, read_only=True))
886 def test_sync_list_modify_2_check(self):
887 with self.assertRaises(AnsibleExitJson) as exc:
888 args = self.config_module_args.copy()
889 args.update({
890 'path': 'ip dns static',
891 'data': [
892 {
893 'comment': '',
894 'name': 'router',
895 'address': '192.168.88.1',
896 },
897 {
898 'name': 'router',
899 'text': 'Router Text Entry 2',
900 },
901 {
902 'name': 'foo',
903 'address': '192.168.88.2',
904 },
905 ],
906 'handle_absent_entries': 'remove',
907 'handle_entries_content': 'remove',
908 '_ansible_check_mode': True,
909 })
910 with set_module_args(args):
911 self.module.main()
912
913 result = exc.exception.args[0]
914 self.assertEqual(result['changed'], True)
915 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
916 self.assertEqual(result['new_data'], [
917 {
918 '.id': '*1',
919 'name': 'router',
920 'address': '192.168.88.1',
921 'ttl': '1d',
922 'disabled': False,
923 'match-subdomain': False,
924 },
925 {
926 '.id': '*A',
927 'name': 'router',
928 'text': 'Router Text Entry 2',
929 'ttl': '1d',
930 'disabled': False,
931 'match-subdomain': False,
932 },
933 {
934 '.id': '*7',
935 'name': 'foo',
936 'address': '192.168.88.2',
937 'ttl': '1d',
938 'disabled': False,
939 'match-subdomain': False,
940 },
941 ])
942
943 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
944 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC))
945 def test_sync_list_modify_3(self):
946 with self.assertRaises(AnsibleExitJson) as exc:
947 args = self.config_module_args.copy()
948 args.update({
949 'path': 'ip dns static',
950 'data': [
951 {
952 '!comment': None,
953 'name': 'router',
954 'address': '192.168.88.1',
955 },
956 {
957 'name': 'router',
958 'cname': 'router.com.',
959 },
960 {
961 'name': 'foo',
962 'address': '192.168.88.2',
963 'ttl': '1d',
964 },
965 ],
966 'handle_absent_entries': 'remove',
967 'handle_entries_content': 'remove',
968 })
969 with set_module_args(args):
970 self.module.main()
971
972 result = exc.exception.args[0]
973 self.assertEqual(result['changed'], True)
974 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
975 self.assertEqual(result['new_data'], [
976 {
977 '.id': '*1',
978 'name': 'router',
979 'address': '192.168.88.1',
980 'ttl': '1d',
981 'disabled': False,
982 'match-subdomain': False,
983 },
984 {
985 '.id': '*7',
986 'name': 'foo',
987 'address': '192.168.88.2',
988 'ttl': '1d',
989 'disabled': False,
990 'match-subdomain': False,
991 },
992 {
993 '.id': '*NEW1',
994 'name': 'router',
995 'cname': 'router.com.',
996 'ttl': '1d',
997 'disabled': False,
998 'match-subdomain': False,
999 },
1000 ])
1001
1002 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1003 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC, read_only=True))
1004 def test_sync_list_modify_3_check(self):
1005 with self.assertRaises(AnsibleExitJson) as exc:
1006 args = self.config_module_args.copy()
1007 args.update({
1008 'path': 'ip dns static',
1009 'data': [
1010 {
1011 '!comment': None,
1012 'name': 'router',
1013 'address': '192.168.88.1',
1014 },
1015 {
1016 'name': 'router',
1017 'cname': 'router.com.',
1018 },
1019 {
1020 'name': 'foo',
1021 'address': '192.168.88.2',
1022 'ttl': '1d',
1023 },
1024 ],
1025 'handle_absent_entries': 'remove',
1026 'handle_entries_content': 'remove',
1027 '_ansible_check_mode': True,
1028 })
1029 with set_module_args(args):
1030 self.module.main()
1031
1032 result = exc.exception.args[0]
1033 self.assertEqual(result['changed'], True)
1034 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
1035 self.assertEqual(result['new_data'], [
1036 {
1037 '.id': '*1',
1038 'name': 'router',
1039 'address': '192.168.88.1',
1040 'ttl': '1d',
1041 'disabled': False,
1042 'match-subdomain': False,
1043 },
1044 {
1045 '.id': '*7',
1046 'name': 'foo',
1047 'address': '192.168.88.2',
1048 'ttl': '1d',
1049 'disabled': False,
1050 'match-subdomain': False,
1051 },
1052 {
1053 'name': 'router',
1054 'cname': 'router.com.',
1055 },
1056 ])
1057
1058 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1059 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC))
1060 def test_sync_list_modify_4(self):
1061 with self.assertRaises(AnsibleExitJson) as exc:
1062 args = self.config_module_args.copy()
1063 args.update({
1064 'path': 'ip dns static',
1065 'data': [
1066 {
1067 'name': 'router',
1068 'address': '192.168.88.1',
1069 },
1070 {
1071 'name': 'router',
1072 'comment': 'defconf',
1073 'text': 'Router Text Entry 2',
1074 },
1075 {
1076 'name': 'foo',
1077 'address': '192.168.88.2',
1078 },
1079 ],
1080 'handle_absent_entries': 'remove',
1081 'handle_entries_content': 'remove',
1082 })
1083 with set_module_args(args):
1084 self.module.main()
1085
1086 result = exc.exception.args[0]
1087 self.assertEqual(result['changed'], True)
1088 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
1089 self.assertEqual(result['new_data'], [
1090 {
1091 '.id': '*1',
1092 'name': 'router',
1093 'address': '192.168.88.1',
1094 'ttl': '1d',
1095 'disabled': False,
1096 'match-subdomain': False,
1097 },
1098 {
1099 '.id': '*A',
1100 'comment': 'defconf',
1101 'name': 'router',
1102 'text': 'Router Text Entry 2',
1103 'ttl': '1d',
1104 'disabled': False,
1105 'match-subdomain': False,
1106 },
1107 {
1108 '.id': '*7',
1109 'name': 'foo',
1110 'address': '192.168.88.2',
1111 'ttl': '1d',
1112 'disabled': False,
1113 'match-subdomain': False,
1114 },
1115 ])
1116
1117 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1118 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC, read_only=True))
1119 def test_sync_list_modify_4_check(self):
1120 with self.assertRaises(AnsibleExitJson) as exc:
1121 args = self.config_module_args.copy()
1122 args.update({
1123 'path': 'ip dns static',
1124 'data': [
1125 {
1126 'name': 'router',
1127 'address': '192.168.88.1',
1128 },
1129 {
1130 'name': 'router',
1131 'comment': 'defconf',
1132 'text': 'Router Text Entry 2',
1133 },
1134 {
1135 'name': 'foo',
1136 'address': '192.168.88.2',
1137 },
1138 ],
1139 'handle_absent_entries': 'remove',
1140 'handle_entries_content': 'remove',
1141 '_ansible_check_mode': True,
1142 })
1143 with set_module_args(args):
1144 self.module.main()
1145
1146 result = exc.exception.args[0]
1147 self.assertEqual(result['changed'], True)
1148 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
1149 self.assertEqual(result['new_data'], [
1150 {
1151 '.id': '*1',
1152 'name': 'router',
1153 'address': '192.168.88.1',
1154 'ttl': '1d',
1155 'disabled': False,
1156 'match-subdomain': False,
1157 },
1158 {
1159 '.id': '*A',
1160 'comment': 'defconf',
1161 'name': 'router',
1162 'text': 'Router Text Entry 2',
1163 'ttl': '1d',
1164 'disabled': False,
1165 'match-subdomain': False,
1166 },
1167 {
1168 '.id': '*7',
1169 'name': 'foo',
1170 'address': '192.168.88.2',
1171 'ttl': '1d',
1172 'disabled': False,
1173 'match-subdomain': False,
1174 },
1175 ])
1176
1177 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1178 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC))
1179 def test_sync_list_delete(self):
1180 with self.assertRaises(AnsibleExitJson) as exc:
1181 args = self.config_module_args.copy()
1182 args.update({
1183 'path': 'ip dns static',
1184 'data': [
1185 {
1186 'comment': 'defconf',
1187 'name': 'router',
1188 'address': '192.168.88.1',
1189 },
1190 ],
1191 'handle_absent_entries': 'remove',
1192 'handle_entries_content': 'remove',
1193 })
1194 with set_module_args(args):
1195 self.module.main()
1196
1197 result = exc.exception.args[0]
1198 self.assertEqual(result['changed'], True)
1199 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
1200 self.assertEqual(result['new_data'], [
1201 {
1202 '.id': '*1',
1203 'comment': 'defconf',
1204 'name': 'router',
1205 'address': '192.168.88.1',
1206 'ttl': '1d',
1207 'disabled': False,
1208 'match-subdomain': False,
1209 },
1210 ])
1211
1212 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1213 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC, read_only=True))
1214 def test_sync_list_delete_check(self):
1215 with self.assertRaises(AnsibleExitJson) as exc:
1216 args = self.config_module_args.copy()
1217 args.update({
1218 'path': 'ip dns static',
1219 'data': [
1220 {
1221 'comment': 'defconf',
1222 'name': 'router',
1223 'address': '192.168.88.1',
1224 },
1225 ],
1226 'handle_absent_entries': 'remove',
1227 'handle_entries_content': 'remove',
1228 '_ansible_check_mode': True,
1229 })
1230 with set_module_args(args):
1231 self.module.main()
1232
1233 result = exc.exception.args[0]
1234 self.assertEqual(result['changed'], True)
1235 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
1236 self.assertEqual(result['new_data'], [
1237 {
1238 '.id': '*1',
1239 'comment': 'defconf',
1240 'name': 'router',
1241 'address': '192.168.88.1',
1242 'ttl': '1d',
1243 'disabled': False,
1244 'match-subdomain': False,
1245 },
1246 ])
1247
1248 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1249 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC))
1250 def test_sync_list_reorder(self):
1251 with self.assertRaises(AnsibleExitJson) as exc:
1252 args = self.config_module_args.copy()
1253 args.update({
1254 'path': 'ip dns static',
1255 'data': [
1256 {
1257 'name': 'foo',
1258 'address': '192.168.88.2',
1259 },
1260 {
1261 'name': 'foo',
1262 'text': 'bar',
1263 },
1264 {
1265 'name': 'router',
1266 'text': 'Router Text Entry',
1267 },
1268 {
1269 'comment': 'defconf',
1270 'name': 'router',
1271 'address': '192.168.88.1',
1272 },
1273 ],
1274 'handle_absent_entries': 'remove',
1275 'handle_entries_content': 'remove',
1276 'ensure_order': True,
1277 })
1278 with set_module_args(args):
1279 self.module.main()
1280
1281 result = exc.exception.args[0]
1282 self.assertEqual(result['changed'], True)
1283 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
1284 self.assertEqual(result['new_data'], [
1285 {
1286 '.id': '*7',
1287 'name': 'foo',
1288 'address': '192.168.88.2',
1289 'ttl': '1d',
1290 'disabled': False,
1291 'match-subdomain': False,
1292 },
1293 {
1294 '.id': '*NEW1',
1295 'name': 'foo',
1296 'text': 'bar',
1297 'ttl': '1d',
1298 'disabled': False,
1299 'match-subdomain': False,
1300 },
1301 {
1302 '.id': '*A',
1303 'name': 'router',
1304 'text': 'Router Text Entry',
1305 'ttl': '1d',
1306 'disabled': False,
1307 'match-subdomain': False,
1308 },
1309 {
1310 '.id': '*1',
1311 'comment': 'defconf',
1312 'name': 'router',
1313 'address': '192.168.88.1',
1314 'ttl': '1d',
1315 'disabled': False,
1316 'match-subdomain': False,
1317 },
1318 ])
1319
1320 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1321 new=create_fake_path(('ip', 'dns', 'static'), START_IP_DNS_STATIC, read_only=True))
1322 def test_sync_list_reorder_check(self):
1323 with self.assertRaises(AnsibleExitJson) as exc:
1324 args = self.config_module_args.copy()
1325 args.update({
1326 'path': 'ip dns static',
1327 'data': [
1328 {
1329 'name': 'foo',
1330 'address': '192.168.88.2',
1331 },
1332 {
1333 'name': 'foo',
1334 'text': 'bar',
1335 },
1336 {
1337 'name': 'router',
1338 'text': 'Router Text Entry',
1339 },
1340 {
1341 'comment': 'defconf',
1342 'name': 'router',
1343 'address': '192.168.88.1',
1344 },
1345 ],
1346 'handle_absent_entries': 'remove',
1347 'handle_entries_content': 'remove',
1348 'ensure_order': True,
1349 '_ansible_check_mode': True,
1350 })
1351 with set_module_args(args):
1352 self.module.main()
1353
1354 result = exc.exception.args[0]
1355 self.assertEqual(result['changed'], True)
1356 self.assertEqual(result['old_data'], START_IP_DNS_STATIC_OLD_DATA)
1357 self.assertEqual(result['new_data'], [
1358 {
1359 '.id': '*7',
1360 'name': 'foo',
1361 'address': '192.168.88.2',
1362 'ttl': '1d',
1363 'disabled': False,
1364 'match-subdomain': False,
1365 },
1366 {
1367 'name': 'foo',
1368 'text': 'bar',
1369 },
1370 {
1371 '.id': '*A',
1372 'name': 'router',
1373 'text': 'Router Text Entry',
1374 'ttl': '1d',
1375 'disabled': False,
1376 'match-subdomain': False,
1377 },
1378 {
1379 '.id': '*1',
1380 'comment': 'defconf',
1381 'name': 'router',
1382 'address': '192.168.88.1',
1383 'ttl': '1d',
1384 'disabled': False,
1385 'match-subdomain': False,
1386 },
1387 ])
1388
1389 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1390 new=create_fake_path(('ip', 'settings'), START_IP_SETTINGS, read_only=True))
1391 def test_sync_value_idempotent(self):
1392 with self.assertRaises(AnsibleExitJson) as exc:
1393 args = self.config_module_args.copy()
1394 args.update({
1395 'path': 'ip settings',
1396 'data': [
1397 {
1398 'arp-timeout': '30s',
1399 'icmp-rate-limit': 20,
1400 'icmp-rate-mask': '0x1818',
1401 'ip-forward': True,
1402 'max-neighbor-entries': 8192,
1403 },
1404 ],
1405 })
1406 with set_module_args(args):
1407 self.module.main()
1408
1409 result = exc.exception.args[0]
1410 self.assertEqual(result['changed'], False)
1411 self.assertEqual(result['old_data'], START_IP_SETTINGS_OLD_DATA)
1412 self.assertEqual(result['new_data'], START_IP_SETTINGS_OLD_DATA)
1413
1414 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1415 new=create_fake_path(('ip', 'settings'), START_IP_SETTINGS, read_only=True))
1416 def test_sync_value_idempotent_2(self):
1417 with self.assertRaises(AnsibleExitJson) as exc:
1418 args = self.config_module_args.copy()
1419 args.update({
1420 'path': 'ip settings',
1421 'data': [
1422 {
1423 'accept-redirects': True,
1424 'icmp-rate-limit': 20,
1425 },
1426 ],
1427 'handle_entries_content': 'remove',
1428 })
1429 with set_module_args(args):
1430 self.module.main()
1431
1432 result = exc.exception.args[0]
1433 self.assertEqual(result['changed'], False)
1434 self.assertEqual(result['old_data'], START_IP_SETTINGS_OLD_DATA)
1435 self.assertEqual(result['new_data'], START_IP_SETTINGS_OLD_DATA)
1436
1437 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1438 new=create_fake_path(('ip', 'settings'), START_IP_SETTINGS, read_only=True))
1439 def test_sync_value_idempotent_3(self):
1440 with self.assertRaises(AnsibleExitJson) as exc:
1441 args = self.config_module_args.copy()
1442 args.update({
1443 'path': 'ip settings',
1444 'data': [
1445 {
1446 'accept-redirects': 'yes',
1447 'icmp-rate-limit': '20',
1448 },
1449 ],
1450 'handle_entries_content': 'remove',
1451 })
1452 with set_module_args(args):
1453 self.module.main()
1454
1455 result = exc.exception.args[0]
1456 self.assertEqual(result['changed'], False)
1457 self.assertEqual(result['old_data'], START_IP_SETTINGS_OLD_DATA)
1458 self.assertEqual(result['new_data'], START_IP_SETTINGS_OLD_DATA)
1459
1460 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1461 new=create_fake_path(('ip', 'settings'), START_IP_SETTINGS))
1462 def test_sync_value_modify(self):
1463 with self.assertRaises(AnsibleExitJson) as exc:
1464 args = self.config_module_args.copy()
1465 args.update({
1466 'path': 'ip settings',
1467 'data': [
1468 {
1469 'accept-redirects': True,
1470 'accept-source-route': True,
1471 'max-neighbor-entries': 4096,
1472 },
1473 ],
1474 })
1475 with set_module_args(args):
1476 self.module.main()
1477
1478 result = exc.exception.args[0]
1479 self.assertEqual(result['changed'], True)
1480 self.assertEqual(result['old_data'], START_IP_SETTINGS_OLD_DATA)
1481 self.assertEqual(result['new_data'], [
1482 {
1483 'accept-redirects': True,
1484 'accept-source-route': True,
1485 'allow-fast-path': True,
1486 'arp-timeout': '30s',
1487 'icmp-rate-limit': 20,
1488 'icmp-rate-mask': '0x1818',
1489 'ip-forward': True,
1490 'max-neighbor-entries': 4096,
1491 'route-cache': True,
1492 'rp-filter': False,
1493 'secure-redirects': True,
1494 'send-redirects': True,
1495 'tcp-syncookies': False,
1496 },
1497 ])
1498
1499 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1500 new=create_fake_path(('ip', 'settings'), START_IP_SETTINGS, read_only=True))
1501 def test_sync_value_modify_check(self):
1502 with self.assertRaises(AnsibleExitJson) as exc:
1503 args = self.config_module_args.copy()
1504 args.update({
1505 'path': 'ip settings',
1506 'data': [
1507 {
1508 'accept-redirects': True,
1509 'accept-source-route': True,
1510 'max-neighbor-entries': 4096,
1511 },
1512 ],
1513 '_ansible_check_mode': True,
1514 })
1515 with set_module_args(args):
1516 self.module.main()
1517
1518 result = exc.exception.args[0]
1519 self.assertEqual(result['changed'], True)
1520 self.assertEqual(result['old_data'], START_IP_SETTINGS_OLD_DATA)
1521 self.assertEqual(result['new_data'], [
1522 {
1523 'accept-redirects': True,
1524 'accept-source-route': True,
1525 'allow-fast-path': True,
1526 'arp-timeout': '30s',
1527 'icmp-rate-limit': 20,
1528 'icmp-rate-mask': '0x1818',
1529 'ip-forward': True,
1530 'max-neighbor-entries': 4096,
1531 'route-cache': True,
1532 'rp-filter': False,
1533 'secure-redirects': True,
1534 'send-redirects': True,
1535 'tcp-syncookies': False,
1536 },
1537 ])
1538
1539 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1540 new=create_fake_path(('ip', 'settings'), START_IP_SETTINGS))
1541 def test_sync_value_modify_2(self):
1542 with self.assertRaises(AnsibleExitJson) as exc:
1543 args = self.config_module_args.copy()
1544 args.update({
1545 'path': 'ip settings',
1546 'data': [
1547 {
1548 'accept-redirects': True,
1549 'accept-source-route': True,
1550 'max-neighbor-entries': 4096,
1551 },
1552 ],
1553 'handle_entries_content': 'remove',
1554 })
1555 with set_module_args(args):
1556 self.module.main()
1557
1558 result = exc.exception.args[0]
1559 self.assertEqual(result['changed'], True)
1560 self.assertEqual(result['old_data'], START_IP_SETTINGS_OLD_DATA)
1561 self.assertEqual(result['new_data'], [
1562 {
1563 'accept-redirects': True,
1564 'accept-source-route': True,
1565 'allow-fast-path': True,
1566 'arp-timeout': '30s',
1567 'icmp-rate-limit': 10,
1568 'icmp-rate-mask': '0x1818',
1569 'ip-forward': True,
1570 'max-neighbor-entries': 4096,
1571 'route-cache': True,
1572 'rp-filter': False,
1573 'secure-redirects': True,
1574 'send-redirects': True,
1575 'tcp-syncookies': False,
1576 },
1577 ])
1578
1579 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1580 new=create_fake_path(('ip', 'settings'), START_IP_SETTINGS, read_only=True))
1581 def test_sync_value_modify_2_check(self):
1582 with self.assertRaises(AnsibleExitJson) as exc:
1583 args = self.config_module_args.copy()
1584 args.update({
1585 'path': 'ip settings',
1586 'data': [
1587 {
1588 'accept-redirects': True,
1589 'accept-source-route': True,
1590 'max-neighbor-entries': 4096,
1591 },
1592 ],
1593 'handle_entries_content': 'remove',
1594 '_ansible_check_mode': True,
1595 })
1596 with set_module_args(args):
1597 self.module.main()
1598
1599 result = exc.exception.args[0]
1600 self.assertEqual(result['changed'], True)
1601 self.assertEqual(result['old_data'], START_IP_SETTINGS_OLD_DATA)
1602 self.assertEqual(result['new_data'], [
1603 {
1604 'accept-redirects': True,
1605 'accept-source-route': True,
1606 'allow-fast-path': True,
1607 'arp-timeout': '30s',
1608 'icmp-rate-limit': 10,
1609 'icmp-rate-mask': '0x1818',
1610 'ip-forward': True,
1611 'max-neighbor-entries': 4096,
1612 'route-cache': True,
1613 'rp-filter': False,
1614 'secure-redirects': True,
1615 'send-redirects': True,
1616 'tcp-syncookies': False,
1617 },
1618 ])
1619
1620 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1621 new=create_fake_path(('ip', 'address'), START_IP_ADDRESS, read_only=True))
1622 def test_sync_primary_key_idempotent(self):
1623 with self.assertRaises(AnsibleExitJson) as exc:
1624 args = self.config_module_args.copy()
1625 args.update({
1626 'path': 'ip address',
1627 'data': [
1628 {
1629 'address': '192.168.1.0/24',
1630 'interface': 'LAN',
1631 'comment': '',
1632 },
1633 {
1634 'address': '192.168.88.0/24',
1635 'interface': 'bridge',
1636 '!comment': None,
1637 },
1638 ],
1639 })
1640 with set_module_args(args):
1641 self.module.main()
1642
1643 result = exc.exception.args[0]
1644 self.assertEqual(result['changed'], False)
1645 self.assertEqual(result['old_data'], START_IP_ADDRESS_OLD_DATA)
1646 self.assertEqual(result['new_data'], START_IP_ADDRESS_OLD_DATA)
1647
1648 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1649 new=create_fake_path(('ip', 'address'), START_IP_ADDRESS, read_only=True))
1650 def test_sync_primary_key_idempotent_2(self):
1651 with self.assertRaises(AnsibleExitJson) as exc:
1652 args = self.config_module_args.copy()
1653 args.update({
1654 'path': 'ip address',
1655 'data': [
1656 {
1657 'address': '192.168.88.0/24',
1658 'interface': 'bridge',
1659 },
1660 {
1661 'address': '10.0.0.0/16',
1662 'interface': 'WAN',
1663 'disabled': True,
1664 '!comment': '',
1665 },
1666 {
1667 'address': '192.168.1.0/24',
1668 'interface': 'LAN',
1669 'disabled': False,
1670 'comment': None,
1671 },
1672 ],
1673 'handle_absent_entries': 'remove',
1674 'handle_entries_content': 'remove',
1675 })
1676 with set_module_args(args):
1677 self.module.main()
1678
1679 result = exc.exception.args[0]
1680 self.assertEqual(result['changed'], False)
1681 self.assertEqual(result['old_data'], START_IP_ADDRESS_OLD_DATA)
1682 self.assertEqual(result['new_data'], START_IP_ADDRESS_OLD_DATA)
1683
1684 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1685 new=create_fake_path(('ip', 'address'), START_IP_ADDRESS))
1686 def test_sync_primary_key_cru(self):
1687 with self.assertRaises(AnsibleExitJson) as exc:
1688 args = self.config_module_args.copy()
1689 args.update({
1690 'path': 'ip address',
1691 'data': [
1692 {
1693 'address': '10.10.0.0/16',
1694 'interface': 'WIFI',
1695 },
1696 {
1697 'address': '192.168.1.0/24',
1698 'interface': 'LAN',
1699 'disabled': True,
1700 },
1701 {
1702 'address': '192.168.88.0/24',
1703 'interface': 'bridge',
1704 'comment': 'foo',
1705 },
1706 ],
1707 'handle_absent_entries': 'remove',
1708 'handle_entries_content': 'remove',
1709 })
1710 with set_module_args(args):
1711 self.module.main()
1712
1713 result = exc.exception.args[0]
1714 self.assertEqual(result['changed'], True)
1715 self.assertEqual(result['old_data'], START_IP_ADDRESS_OLD_DATA)
1716 self.assertEqual(result['new_data'], [
1717 {
1718 '.id': '*1',
1719 'comment': 'foo',
1720 'address': '192.168.88.0/24',
1721 'interface': 'bridge',
1722 'disabled': False,
1723 },
1724 {
1725 '.id': '*3',
1726 'address': '192.168.1.0/24',
1727 'interface': 'LAN',
1728 'disabled': True,
1729 },
1730 {
1731 '.id': '*NEW1',
1732 'address': '10.10.0.0/16',
1733 'interface': 'WIFI',
1734 'disabled': False,
1735 },
1736 ])
1737
1738 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1739 new=create_fake_path(('ip', 'address'), START_IP_ADDRESS, read_only=True))
1740 def test_sync_primary_key_cru_check(self):
1741 with self.assertRaises(AnsibleExitJson) as exc:
1742 args = self.config_module_args.copy()
1743 args.update({
1744 'path': 'ip address',
1745 'data': [
1746 {
1747 'address': '10.10.0.0/16',
1748 'interface': 'WIFI',
1749 },
1750 {
1751 'address': '192.168.1.0/24',
1752 'interface': 'LAN',
1753 'disabled': True,
1754 },
1755 {
1756 'address': '192.168.88.0/24',
1757 'interface': 'bridge',
1758 'comment': 'foo',
1759 },
1760 ],
1761 'handle_absent_entries': 'remove',
1762 'handle_entries_content': 'remove',
1763 '_ansible_check_mode': True,
1764 })
1765 with set_module_args(args):
1766 self.module.main()
1767
1768 result = exc.exception.args[0]
1769 self.assertEqual(result['changed'], True)
1770 self.assertEqual(result['old_data'], START_IP_ADDRESS_OLD_DATA)
1771 self.assertEqual(result['new_data'], [
1772 {
1773 '.id': '*1',
1774 'comment': 'foo',
1775 'address': '192.168.88.0/24',
1776 'interface': 'bridge',
1777 'disabled': False,
1778 },
1779 {
1780 '.id': '*3',
1781 'address': '192.168.1.0/24',
1782 'interface': 'LAN',
1783 'disabled': True,
1784 },
1785 {
1786 'address': '10.10.0.0/16',
1787 'interface': 'WIFI',
1788 },
1789 ])
1790
1791 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1792 new=create_fake_path(('ip', 'address'), START_IP_ADDRESS))
1793 def test_sync_primary_key_cru_reorder(self):
1794 with self.assertRaises(AnsibleExitJson) as exc:
1795 args = self.config_module_args.copy()
1796 args.update({
1797 'path': 'ip address',
1798 'data': [
1799 {
1800 'address': '10.10.0.0/16',
1801 'interface': 'WIFI',
1802 },
1803 {
1804 'address': '192.168.1.0/24',
1805 'interface': 'LAN',
1806 'disabled': True,
1807 },
1808 {
1809 'address': '192.168.88.0/24',
1810 'interface': 'bridge',
1811 'comment': 'foo',
1812 },
1813 ],
1814 'handle_absent_entries': 'remove',
1815 'handle_entries_content': 'remove',
1816 'ensure_order': True,
1817 })
1818 with set_module_args(args):
1819 self.module.main()
1820
1821 result = exc.exception.args[0]
1822 self.assertEqual(result['changed'], True)
1823 self.assertEqual(result['old_data'], START_IP_ADDRESS_OLD_DATA)
1824 self.assertEqual(result['new_data'], [
1825 {
1826 '.id': '*NEW1',
1827 'address': '10.10.0.0/16',
1828 'interface': 'WIFI',
1829 'disabled': False,
1830 },
1831 {
1832 '.id': '*3',
1833 'address': '192.168.1.0/24',
1834 'interface': 'LAN',
1835 'disabled': True,
1836 },
1837 {
1838 '.id': '*1',
1839 'comment': 'foo',
1840 'address': '192.168.88.0/24',
1841 'interface': 'bridge',
1842 'disabled': False,
1843 },
1844 ])
1845
1846 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1847 new=create_fake_path(('ip', 'address'), START_IP_ADDRESS, read_only=True))
1848 def test_sync_primary_key_cru_reorder_check(self):
1849 with self.assertRaises(AnsibleExitJson) as exc:
1850 args = self.config_module_args.copy()
1851 args.update({
1852 'path': 'ip address',
1853 'data': [
1854 {
1855 'address': '10.10.0.0/16',
1856 'interface': 'WIFI',
1857 },
1858 {
1859 'address': '192.168.1.0/24',
1860 'interface': 'LAN',
1861 'disabled': True,
1862 },
1863 {
1864 'address': '192.168.88.0/24',
1865 'interface': 'bridge',
1866 'comment': 'foo',
1867 },
1868 ],
1869 'handle_absent_entries': 'remove',
1870 'handle_entries_content': 'remove',
1871 'ensure_order': True,
1872 '_ansible_check_mode': True,
1873 })
1874 with set_module_args(args):
1875 self.module.main()
1876
1877 result = exc.exception.args[0]
1878 self.assertEqual(result['changed'], True)
1879 self.assertEqual(result['old_data'], START_IP_ADDRESS_OLD_DATA)
1880 self.assertEqual(result['new_data'], [
1881 {
1882 'address': '10.10.0.0/16',
1883 'interface': 'WIFI',
1884 },
1885 {
1886 '.id': '*3',
1887 'address': '192.168.1.0/24',
1888 'interface': 'LAN',
1889 'disabled': True,
1890 },
1891 {
1892 '.id': '*1',
1893 'comment': 'foo',
1894 'address': '192.168.88.0/24',
1895 'interface': 'bridge',
1896 'disabled': False,
1897 },
1898 ])
1899
1900 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1901 new=create_fake_path(('ip', 'dhcp-server', 'lease'), START_IP_DHCP_SERVER_LEASE, read_only=True))
1902 def test_absent_value(self):
1903 with self.assertRaises(AnsibleExitJson) as exc:
1904 args = self.config_module_args.copy()
1905 args.update({
1906 'path': 'ip dhcp-server lease',
1907 'data': [
1908 {
1909 'address': '192.168.88.2',
1910 'mac-address': '11:22:33:44:55:66',
1911 'client-id': 'ff:1:2:3:4:5:6:7:8:9:a:b:c:d:e:f:0:1:2',
1912 'server': 'main',
1913 'comment': 'foo',
1914 },
1915 {
1916 'address': '192.168.88.3',
1917 'mac-address': '11:22:33:44:55:77',
1918 'client-id': '1:2:3:4:5:6:7',
1919 'server': 'main',
1920 },
1921 {
1922 'address': '0.0.0.1',
1923 'mac-address': '00:00:00:00:00:01',
1924 'server': 'all',
1925 },
1926 {
1927 'address': '0.0.0.2',
1928 'mac-address': '00:00:00:00:00:02',
1929 'server': 'all',
1930 },
1931 ],
1932 'handle_absent_entries': 'remove',
1933 'handle_entries_content': 'remove',
1934 'ensure_order': True,
1935 })
1936 with set_module_args(args):
1937 self.module.main()
1938
1939 result = exc.exception.args[0]
1940 self.assertEqual(result['changed'], False)
1941 self.assertEqual(result['old_data'], START_IP_DHCP_SERVER_LEASE_OLD_DATA)
1942 self.assertEqual(result['new_data'], START_IP_DHCP_SERVER_LEASE_OLD_DATA)
1943
1944 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1945 new=create_fake_path(('ip', 'dhcp-client'), START_IP_DHCP_CLIENT, read_only=True))
1946 def test_default_remove_combination_idempotent(self):
1947 with self.assertRaises(AnsibleExitJson) as exc:
1948 args = self.config_module_args.copy()
1949 args.update({
1950 'path': 'ip dhcp-client',
1951 'data': [
1952 {
1953 'interface': 'ether1',
1954 },
1955 {
1956 'interface': 'ether2',
1957 'dhcp-options': None,
1958 },
1959 {
1960 'interface': 'ether3',
1961 'dhcp-options': 'hostname',
1962 },
1963 ],
1964 'handle_absent_entries': 'remove',
1965 'handle_entries_content': 'remove',
1966 'ensure_order': True,
1967 })
1968 with set_module_args(args):
1969 self.module.main()
1970
1971 result = exc.exception.args[0]
1972 self.assertEqual(result['changed'], False)
1973 self.assertEqual(result['old_data'], START_IP_DHCP_CLIENT_OLD_DATA)
1974 self.assertEqual(result['new_data'], START_IP_DHCP_CLIENT_OLD_DATA)
1975
1976 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
1977 new=create_fake_path(('ip', 'dhcp-client'), START_IP_DHCP_CLIENT, read_only=True))
1978 def test_default_remove_combination_idempotent_2(self):
1979 with self.assertRaises(AnsibleExitJson) as exc:
1980 args = self.config_module_args.copy()
1981 args.update({
1982 'path': 'ip dhcp-client',
1983 'data': [
1984 {
1985 'interface': 'ether1',
1986 'add-default-route': 'yes',
1987 'default-route-distance': '1',
1988 },
1989 {
1990 'interface': 'ether2',
1991 'dhcp-options': None,
1992 'add-default-route': True,
1993 'default-route-distance': 1,
1994 },
1995 {
1996 'interface': 'ether3',
1997 'dhcp-options': 'hostname',
1998 },
1999 ],
2000 'handle_absent_entries': 'remove',
2001 'handle_entries_content': 'remove',
2002 'ensure_order': True,
2003 })
2004 with set_module_args(args):
2005 self.module.main()
2006
2007 result = exc.exception.args[0]
2008 self.assertEqual(result['changed'], False)
2009 self.assertEqual(result['old_data'], START_IP_DHCP_CLIENT_OLD_DATA)
2010 self.assertEqual(result['new_data'], START_IP_DHCP_CLIENT_OLD_DATA)
2011
2012 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
2013 new=create_fake_path(('ip', 'dhcp-client'), []))
2014 def test_default_remove_combination_create(self):
2015 with self.assertRaises(AnsibleExitJson) as exc:
2016 args = self.config_module_args.copy()
2017 args.update({
2018 'path': 'ip dhcp-client',
2019 'data': [
2020 {
2021 'interface': 'ether1',
2022 },
2023 {
2024 'interface': 'ether2',
2025 'dhcp-options': None,
2026 },
2027 {
2028 'interface': 'ether3',
2029 'dhcp-options': 'hostname',
2030 },
2031 ],
2032 'handle_absent_entries': 'remove',
2033 'handle_entries_content': 'remove',
2034 'ensure_order': True,
2035 })
2036 with set_module_args(args):
2037 self.module.main()
2038
2039 result = exc.exception.args[0]
2040 self.assertEqual(result['changed'], True)
2041 self.assertEqual(result['old_data'], [])
2042 self.assertEqual(result['new_data'], [
2043 {
2044 ".id": "*NEW1",
2045 "add-default-route": True,
2046 "default-route-distance": 1,
2047 "dhcp-options": "hostname,clientid",
2048 "disabled": False,
2049 "interface": "ether1",
2050 "use-peer-dns": True,
2051 "use-peer-ntp": True,
2052 },
2053 {
2054 # "!dhcp-options": None,
2055 ".id": "*NEW2",
2056 "add-default-route": True,
2057 "default-route-distance": 1,
2058 "disabled": False,
2059 "interface": "ether2",
2060 "use-peer-dns": True,
2061 "use-peer-ntp": True,
2062 },
2063 {
2064 ".id": "*NEW3",
2065 "add-default-route": True,
2066 "default-route-distance": 1,
2067 "dhcp-options": "hostname",
2068 "disabled": False,
2069 "interface": "ether3",
2070 "use-peer-dns": True,
2071 "use-peer-ntp": True,
2072 },
2073 ])
2074
2075 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
2076 new=create_fake_path(('interface', 'list'), START_INTERFACE_LIST, read_only=True))
2077 def test_absent_entries_builtin(self):
2078 with self.assertRaises(AnsibleExitJson) as exc:
2079 args = self.config_module_args.copy()
2080 args.update({
2081 'path': 'interface list',
2082 'data': [
2083 {
2084 'name': 'WAN',
2085 'comment': 'defconf',
2086 },
2087 {
2088 'name': 'Foo',
2089 },
2090 ],
2091 'handle_absent_entries': 'remove',
2092 'ensure_order': True,
2093 })
2094 with set_module_args(args):
2095 self.module.main()
2096
2097 result = exc.exception.args[0]
2098 self.assertEqual(result['changed'], False)
2099 self.assertEqual(result['old_data'], START_INTERFACE_LIST_OLD_DATA)
2100 self.assertEqual(result['new_data'], START_INTERFACE_LIST_OLD_DATA)
2101
2102 @patch('ansible_collections.community.routeros.plugins.modules.api_modify.compose_api_path',
2103 new=create_fake_path(('interface', 'gre'), START_INTERFACE_GRE, read_only=True))
2104 def test_idempotent_default_disabled(self):
2105 with self.assertRaises(AnsibleExitJson) as exc:
2106 args = self.config_module_args.copy()
2107 args.update({
2108 'path': 'interface gre',
2109 'data': [
2110 {
2111 'name': 'gre-tunnel3',
2112 'remote-address': '192.168.1.1',
2113 '!keepalive': None,
2114 },
2115 {
2116 'name': 'gre-tunnel4',
2117 'remote-address': '192.168.1.2',
2118 },
2119 {
2120 'name': 'gre-tunnel5',
2121 'local-address': '192.168.0.1',
2122 'remote-address': '192.168.1.3',
2123 'keepalive': '20s,20',
2124 'comment': 'foo',
2125 },
2126 ],
2127 'handle_absent_entries': 'remove',
2128 'ensure_order': True,
2129 })
2130 with set_module_args(args):
2131 self.module.main()
2132
2133 result = exc.exception.args[0]
2134 self.assertEqual(result['changed'], False)
2135 self.assertEqual(result['old_data'], START_INTERFACE_GRE_OLD_DATA)
2136 self.assertEqual(result['new_data'], START_INTERFACE_GRE_OLD_DATA)