Debian Codegraph @

ansible_collections/community/routeros/tests/unit/plugins/modules/test_api_info.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, Key, fake_ros_api,
14 )
15 from ansible_collections.community.routeros.plugins.modules import api_info
16
17
18 class TestRouterosApiInfoModule(ModuleTestCase):
19
20 def setUp(self):
21 super(TestRouterosApiInfoModule, self).setUp()
22 self.module = api_info
23 self.module.LibRouterosError = FakeLibRouterosError
24 self.module.connect = MagicMock(new=fake_ros_api)
25 self.module.check_has_library = MagicMock()
26 self.patch_create_api = patch('ansible_collections.community.routeros.plugins.modules.api_info.create_api', MagicMock(new=fake_ros_api))
27 self.patch_create_api.start()
28 self.patch_get_api_version = patch(
29 'ansible_collections.community.routeros.plugins.modules.api_info.get_api_version',
30 MagicMock(return_value=FAKE_ROS_VERSION))
31 self.patch_get_api_version.start()
32 self.module.Key = MagicMock(new=Key)
33 self.config_module_args = {
34 'username': 'admin',
35 'password': 'pаss',
36 'hostname': '127.0.0.1',
37 }
38
39 def tearDown(self):
40 self.patch_get_api_version.stop()
41 self.patch_create_api.stop()
42
43 def test_module_fail_when_required_args_missing(self):
44 with self.assertRaises(AnsibleFailJson) as exc:
45 with set_module_args({}):
46 self.module.main()
47
48 result = exc.exception.args[0]
49 self.assertEqual(result['failed'], True)
50
51 def test_invalid_path(self):
52 with self.assertRaises(AnsibleFailJson) as exc:
53 args = self.config_module_args.copy()
54 args.update({
55 'path': 'something invalid'
56 })
57 with set_module_args(args):
58 self.module.main()
59
60 result = exc.exception.args[0]
61 self.assertEqual(result['failed'], True)
62 self.assertEqual(result['msg'].startswith('value of path must be one of: '), True)
63
64 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
65 def test_empty_result(self, mock_compose_api_path):
66 mock_compose_api_path.return_value = []
67 with self.assertRaises(AnsibleExitJson) as exc:
68 args = self.config_module_args.copy()
69 args.update({
70 'path': 'ip dns static'
71 })
72 with set_module_args(args):
73 self.module.main()
74
75 result = exc.exception.args[0]
76 self.assertEqual(result['changed'], False)
77 self.assertEqual(result['result'], [])
78
79 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
80 def test_regular_result(self, mock_compose_api_path):
81 mock_compose_api_path.return_value = [
82 {
83 'called-format': 'mac:ssid',
84 'interim-update': 'enabled',
85 'mac-caching': 'disabled',
86 'mac-format': 'XX:XX:XX:XX:XX:XX',
87 'mac-mode': 'as-username',
88 'foo': 'bar',
89 '.id': '*1',
90 },
91 ]
92 with self.assertRaises(AnsibleExitJson) as exc:
93 args = self.config_module_args.copy()
94 args.update({
95 'path': 'caps-man aaa',
96 })
97 with set_module_args(args):
98 self.module.main()
99
100 result = exc.exception.args[0]
101 self.assertEqual(result['changed'], False)
102 self.assertEqual(result['result'], [{
103 'interim-update': 'enabled',
104 '.id': '*1',
105 }])
106
107 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
108 def test_result_with_defaults(self, mock_compose_api_path):
109 mock_compose_api_path.return_value = [
110 {
111 'called-format': 'mac:ssid',
112 'interim-update': 'enabled',
113 'mac-caching': 'disabled',
114 'mac-format': 'XX:XX:XX:XX:XX:XX',
115 'mac-mode': 'as-username',
116 'foo': 'bar',
117 '.id': '*1',
118 },
119 ]
120 with self.assertRaises(AnsibleExitJson) as exc:
121 args = self.config_module_args.copy()
122 args.update({
123 'path': 'caps-man aaa',
124 'hide_defaults': False,
125 })
126 with set_module_args(args):
127 self.module.main()
128
129 result = exc.exception.args[0]
130 self.assertEqual(result['changed'], False)
131 self.assertEqual(result['result'], [{
132 'called-format': 'mac:ssid',
133 'interim-update': 'enabled',
134 'mac-caching': 'disabled',
135 'mac-format': 'XX:XX:XX:XX:XX:XX',
136 'mac-mode': 'as-username',
137 '.id': '*1',
138 }])
139
140 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
141 def test_full_result(self, mock_compose_api_path):
142 mock_compose_api_path.return_value = [
143 {
144 'called-format': 'mac:ssid',
145 'interim-update': 'enabled',
146 'mac-caching': 'disabled',
147 'mac-format': 'XX:XX:XX:XX:XX:XX',
148 'mac-mode': 'as-username',
149 'foo': 'bar',
150 '.id': '*1',
151 },
152 ]
153 with self.assertRaises(AnsibleExitJson) as exc:
154 args = self.config_module_args.copy()
155 args.update({
156 'path': 'caps-man aaa',
157 'unfiltered': True,
158 })
159 with set_module_args(args):
160 self.module.main()
161
162 result = exc.exception.args[0]
163 self.assertEqual(result['changed'], False)
164 self.assertEqual(result['result'], [{
165 'interim-update': 'enabled',
166 'foo': 'bar',
167 '.id': '*1',
168 }])
169
170 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
171 def test_disabled_exclamation(self, mock_compose_api_path):
172 mock_compose_api_path.return_value = [
173 {
174 'chain': 'input',
175 'in-interface-list': 'LAN',
176 '.id': '*1',
177 'dynamic': False,
178 },
179 {
180 'chain': 'forward',
181 'action': 'drop',
182 'in-interface': 'sfp1',
183 '.id': '*2',
184 'dynamic': True,
185 },
186 ]
187 with self.assertRaises(AnsibleExitJson) as exc:
188 args = self.config_module_args.copy()
189 args.update({
190 'path': 'ip firewall filter',
191 'handle_disabled': 'exclamation',
192 })
193 with set_module_args(args):
194 self.module.main()
195
196 result = exc.exception.args[0]
197 self.assertEqual(result['changed'], False)
198 self.assertEqual(result['result'], [{
199 'chain': 'input',
200 'in-interface-list': 'LAN',
201 '!action': None,
202 '!address-list': None,
203 '!address-list-timeout': None,
204 '!comment': None,
205 '!connection-bytes': None,
206 '!connection-limit': None,
207 '!connection-mark': None,
208 '!connection-nat-state': None,
209 '!connection-rate': None,
210 '!connection-state': None,
211 '!connection-type': None,
212 '!content': None,
213 '!disabled': None,
214 '!dscp': None,
215 '!dst-address': None,
216 '!dst-address-list': None,
217 '!dst-address-type': None,
218 '!dst-limit': None,
219 '!dst-port': None,
220 '!fragment': None,
221 '!hotspot': None,
222 '!hw-offload': None,
223 '!icmp-options': None,
224 '!in-bridge-port': None,
225 '!in-bridge-port-list': None,
226 '!in-interface': None,
227 '!ingress-priority': None,
228 '!ipsec-policy': None,
229 '!ipv4-options': None,
230 '!jump-target': None,
231 '!layer7-protocol': None,
232 '!limit': None,
233 '!log': None,
234 '!log-prefix': None,
235 '!nth': None,
236 '!out-bridge-port': None,
237 '!out-bridge-port-list': None,
238 '!out-interface': None,
239 '!out-interface-list': None,
240 '!p2p': None,
241 '!packet-mark': None,
242 '!packet-size': None,
243 '!per-connection-classifier': None,
244 '!port': None,
245 '!priority': None,
246 '!protocol': None,
247 '!psd': None,
248 '!random': None,
249 '!realm': None,
250 '!reject-with': None,
251 '!routing-mark': None,
252 '!routing-table': None,
253 '!src-address': None,
254 '!src-address-list': None,
255 '!src-address-type': None,
256 '!src-mac-address': None,
257 '!src-port': None,
258 '!tcp-flags': None,
259 '!tcp-mss': None,
260 '!time': None,
261 '!tls-host': None,
262 '!ttl': None,
263 '.id': '*1',
264 }])
265
266 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
267 def test_disabled_null_value(self, mock_compose_api_path):
268 mock_compose_api_path.return_value = [
269 {
270 'chain': 'input',
271 'in-interface-list': 'LAN',
272 '.id': '*1',
273 'dynamic': False,
274 },
275 ]
276 with self.assertRaises(AnsibleExitJson) as exc:
277 args = self.config_module_args.copy()
278 args.update({
279 'path': 'ip firewall filter',
280 'handle_disabled': 'null-value',
281 })
282 with set_module_args(args):
283 self.module.main()
284
285 result = exc.exception.args[0]
286 self.assertEqual(result['changed'], False)
287 self.assertEqual(result['result'], [{
288 'chain': 'input',
289 'in-interface-list': 'LAN',
290 'action': None,
291 'address-list': None,
292 'address-list-timeout': None,
293 'comment': None,
294 'connection-bytes': None,
295 'connection-limit': None,
296 'connection-mark': None,
297 'connection-nat-state': None,
298 'connection-rate': None,
299 'connection-state': None,
300 'connection-type': None,
301 'content': None,
302 'disabled': None,
303 'dscp': None,
304 'dst-address': None,
305 'dst-address-list': None,
306 'dst-address-type': None,
307 'dst-limit': None,
308 'dst-port': None,
309 'fragment': None,
310 'hotspot': None,
311 'hw-offload': None,
312 'icmp-options': None,
313 'in-bridge-port': None,
314 'in-bridge-port-list': None,
315 'in-interface': None,
316 'ingress-priority': None,
317 'ipsec-policy': None,
318 'ipv4-options': None,
319 'jump-target': None,
320 'layer7-protocol': None,
321 'limit': None,
322 'log': None,
323 'log-prefix': None,
324 'nth': None,
325 'out-bridge-port': None,
326 'out-bridge-port-list': None,
327 'out-interface': None,
328 'out-interface-list': None,
329 'p2p': None,
330 'packet-mark': None,
331 'packet-size': None,
332 'per-connection-classifier': None,
333 'port': None,
334 'priority': None,
335 'protocol': None,
336 'psd': None,
337 'random': None,
338 'realm': None,
339 'reject-with': None,
340 'routing-mark': None,
341 'routing-table': None,
342 'src-address': None,
343 'src-address-list': None,
344 'src-address-type': None,
345 'src-mac-address': None,
346 'src-port': None,
347 'tcp-flags': None,
348 'tcp-mss': None,
349 'time': None,
350 'tls-host': None,
351 'ttl': None,
352 '.id': '*1',
353 }])
354
355 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
356 def test_disabled_omit(self, mock_compose_api_path):
357 mock_compose_api_path.return_value = [
358 {
359 'chain': 'input',
360 'in-interface-list': 'LAN',
361 '.id': '*1',
362 'dynamic': False,
363 },
364 ]
365 with self.assertRaises(AnsibleExitJson) as exc:
366 args = self.config_module_args.copy()
367 args.update({
368 'path': 'ip firewall filter',
369 'handle_disabled': 'omit',
370 })
371 with set_module_args(args):
372 self.module.main()
373
374 result = exc.exception.args[0]
375 self.assertEqual(result['changed'], False)
376 self.assertEqual(result['result'], [{
377 'chain': 'input',
378 'in-interface-list': 'LAN',
379 '.id': '*1',
380 }])
381
382 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
383 def test_dynamic(self, mock_compose_api_path):
384 mock_compose_api_path.return_value = [
385 {
386 'chain': 'input',
387 'in-interface-list': 'LAN',
388 'dynamic': False,
389 '.id': '*1',
390 },
391 {
392 'chain': 'forward',
393 'action': 'drop',
394 'in-interface': 'sfp1',
395 '.id': '*2',
396 'dynamic': True,
397 },
398 ]
399 with self.assertRaises(AnsibleExitJson) as exc:
400 args = self.config_module_args.copy()
401 args.update({
402 'path': 'ip firewall filter',
403 'handle_disabled': 'omit',
404 'include_dynamic': True,
405 })
406 with set_module_args(args):
407 self.module.main()
408
409 result = exc.exception.args[0]
410 self.assertEqual(result['changed'], False)
411 self.assertEqual(result['result'], [
412 {
413 'chain': 'input',
414 'in-interface-list': 'LAN',
415 '.id': '*1',
416 'dynamic': False,
417 },
418 {
419 'chain': 'forward',
420 'action': 'drop',
421 'in-interface': 'sfp1',
422 '.id': '*2',
423 'dynamic': True,
424 },
425 ])
426
427 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
428 def test_builtin_exclude(self, mock_compose_api_path):
429 mock_compose_api_path.return_value = [
430 {
431 '.id': '*2000000',
432 'name': 'all',
433 'dynamic': False,
434 'include': '',
435 'exclude': '',
436 'builtin': True,
437 'comment': 'contains all interfaces',
438 },
439 {
440 '.id': '*2000001',
441 'name': 'none',
442 'dynamic': False,
443 'include': '',
444 'exclude': '',
445 'builtin': True,
446 'comment': 'contains no interfaces',
447 },
448 {
449 '.id': '*2000010',
450 'name': 'WAN',
451 'dynamic': False,
452 'include': '',
453 'exclude': '',
454 'builtin': False,
455 'comment': 'defconf',
456 },
457 ]
458 with self.assertRaises(AnsibleExitJson) as exc:
459 args = self.config_module_args.copy()
460 args.update({
461 'path': 'interface list',
462 'handle_disabled': 'omit',
463 })
464 with set_module_args(args):
465 self.module.main()
466
467 result = exc.exception.args[0]
468 self.assertEqual(result['changed'], False)
469 self.assertEqual(result['result'], [
470 {
471 '.id': '*2000010',
472 'name': 'WAN',
473 'comment': 'defconf',
474 },
475 ])
476
477 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
478 def test_builtin_include(self, mock_compose_api_path):
479 mock_compose_api_path.return_value = [
480 {
481 '.id': '*2000000',
482 'name': 'all',
483 'dynamic': False,
484 'include': '',
485 'exclude': '',
486 'builtin': True,
487 'comment': 'contains all interfaces',
488 },
489 {
490 '.id': '*2000001',
491 'name': 'none',
492 'dynamic': False,
493 'include': '',
494 'exclude': '',
495 'builtin': True,
496 'comment': 'contains no interfaces',
497 },
498 {
499 '.id': '*2000010',
500 'name': 'WAN',
501 'dynamic': False,
502 'include': '',
503 'exclude': '',
504 'builtin': False,
505 'comment': 'defconf',
506 },
507 ]
508 with self.assertRaises(AnsibleExitJson) as exc:
509 args = self.config_module_args.copy()
510 args.update({
511 'path': 'interface list',
512 'handle_disabled': 'omit',
513 'include_builtin': True,
514 })
515 with set_module_args(args):
516 self.module.main()
517
518 result = exc.exception.args[0]
519 self.assertEqual(result['changed'], False)
520 self.assertEqual(result['result'], [
521 {
522 '.id': '*2000000',
523 'name': 'all',
524 'builtin': True,
525 'comment': 'contains all interfaces',
526 },
527 {
528 '.id': '*2000001',
529 'name': 'none',
530 'builtin': True,
531 'comment': 'contains no interfaces',
532 },
533 {
534 '.id': '*2000010',
535 'name': 'WAN',
536 'builtin': False,
537 'comment': 'defconf',
538 },
539 ])
540
541 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
542 def test_absent(self, mock_compose_api_path):
543 mock_compose_api_path.return_value = [
544 {
545 '.id': '*1',
546 'address': '192.168.88.2',
547 'mac-address': '11:22:33:44:55:66',
548 'client-id': 'ff:1:2:3:4:5:6:7:8:9:a:b:c:d:e:f:0:1:2',
549 'address-lists': '',
550 'server': 'main',
551 'dhcp-option': '',
552 'status': 'waiting',
553 'last-seen': 'never',
554 'radius': False,
555 'dynamic': False,
556 'blocked': False,
557 'disabled': False,
558 'comment': 'foo',
559 },
560 {
561 '.id': '*2',
562 'address': '192.168.88.3',
563 'mac-address': '11:22:33:44:55:77',
564 'client-id': '1:2:3:4:5:6:7',
565 'address-lists': '',
566 'server': 'main',
567 'dhcp-option': '',
568 'status': 'bound',
569 'expires-after': '3d7m8s',
570 'last-seen': '1m52s',
571 'active-address': '192.168.88.14',
572 'active-mac-address': '11:22:33:44:55:76',
573 'active-client-id': '1:2:3:4:5:6:7',
574 'active-server': 'main',
575 'host-name': 'bar',
576 'radius': False,
577 'dynamic': False,
578 'blocked': False,
579 'disabled': False,
580 },
581 {
582 '.id': '*3',
583 'address': '0.0.0.1',
584 'mac-address': '00:00:00:00:00:01',
585 'address-lists': '',
586 'dhcp-option': '',
587 'status': 'waiting',
588 'last-seen': 'never',
589 'radius': False,
590 'dynamic': False,
591 'blocked': False,
592 'disabled': False,
593 },
594 ]
595 with self.assertRaises(AnsibleExitJson) as exc:
596 args = self.config_module_args.copy()
597 args.update({
598 'path': 'ip dhcp-server lease',
599 'handle_disabled': 'omit',
600 })
601 with set_module_args(args):
602 self.module.main()
603
604 result = exc.exception.args[0]
605 self.assertEqual(result['changed'], False)
606 self.assertEqual(result['result'], [
607 {
608 '.id': '*1',
609 'address': '192.168.88.2',
610 'mac-address': '11:22:33:44:55:66',
611 'client-id': 'ff:1:2:3:4:5:6:7:8:9:a:b:c:d:e:f:0:1:2',
612 'server': 'main',
613 'comment': 'foo',
614 },
615 {
616 '.id': '*2',
617 'address': '192.168.88.3',
618 'mac-address': '11:22:33:44:55:77',
619 'client-id': '1:2:3:4:5:6:7',
620 'server': 'main',
621 },
622 {
623 '.id': '*3',
624 'address': '0.0.0.1',
625 'mac-address': '00:00:00:00:00:01',
626 'server': 'all',
627 },
628 ])
629
630 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
631 def test_default_disable_1(self, mock_compose_api_path):
632 mock_compose_api_path.return_value = [
633 {
634 '.id': '*10',
635 'name': 'gre-tunnel3',
636 'mtu': 'auto',
637 'actual-mtu': 65496,
638 'local-address': '0.0.0.0',
639 'remote-address': '192.168.1.1',
640 'dscp': 'inherit',
641 'clamp-tcp-mss': True,
642 'dont-fragment': False,
643 'allow-fast-path': True,
644 'running': True,
645 'disabled': False,
646 },
647 {
648 '.id': '*11',
649 'name': 'gre-tunnel4',
650 'mtu': 'auto',
651 'actual-mtu': 65496,
652 'local-address': '0.0.0.0',
653 'remote-address': '192.168.1.2',
654 'keepalive': '10s,10',
655 'dscp': 'inherit',
656 'clamp-tcp-mss': True,
657 'dont-fragment': False,
658 'allow-fast-path': True,
659 'running': True,
660 'disabled': False,
661 },
662 {
663 '.id': '*12',
664 'name': 'gre-tunnel5',
665 'mtu': 'auto',
666 'actual-mtu': 65496,
667 'local-address': '192.168.0.1',
668 'remote-address': '192.168.1.3',
669 'keepalive': '20s,20',
670 'dscp': 'inherit',
671 'clamp-tcp-mss': True,
672 'dont-fragment': False,
673 'allow-fast-path': True,
674 'running': True,
675 'disabled': False,
676 'comment': 'foo',
677 },
678 ]
679 with self.assertRaises(AnsibleExitJson) as exc:
680 args = self.config_module_args.copy()
681 args.update({
682 'path': 'interface gre',
683 })
684 with set_module_args(args):
685 self.module.main()
686
687 result = exc.exception.args[0]
688 self.assertEqual(result['changed'], False)
689 self.assertEqual(result['result'], [
690 {
691 '.id': '*10',
692 'name': 'gre-tunnel3',
693 'remote-address': '192.168.1.1',
694 '!comment': None,
695 '!ipsec-secret': None,
696 '!keepalive': None,
697 },
698 {
699 '.id': '*11',
700 'name': 'gre-tunnel4',
701 'remote-address': '192.168.1.2',
702 '!comment': None,
703 '!ipsec-secret': None,
704 },
705 {
706 '.id': '*12',
707 'name': 'gre-tunnel5',
708 'local-address': '192.168.0.1',
709 'remote-address': '192.168.1.3',
710 'keepalive': '20s,20',
711 'comment': 'foo',
712 '!ipsec-secret': None,
713 },
714 ])
715
716 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
717 def test_default_disable_2(self, mock_compose_api_path):
718 mock_compose_api_path.return_value = [
719 {
720 '.id': '*10',
721 'name': 'gre-tunnel3',
722 'mtu': 'auto',
723 'actual-mtu': 65496,
724 'local-address': '0.0.0.0',
725 'remote-address': '192.168.1.1',
726 'dscp': 'inherit',
727 'clamp-tcp-mss': True,
728 'dont-fragment': False,
729 'allow-fast-path': True,
730 'running': True,
731 'disabled': False,
732 },
733 {
734 '.id': '*11',
735 'name': 'gre-tunnel4',
736 'mtu': 'auto',
737 'actual-mtu': 65496,
738 'local-address': '0.0.0.0',
739 'remote-address': '192.168.1.2',
740 'keepalive': '10s,10',
741 'dscp': 'inherit',
742 'clamp-tcp-mss': True,
743 'dont-fragment': False,
744 'allow-fast-path': True,
745 'running': True,
746 'disabled': False,
747 },
748 {
749 '.id': '*12',
750 'name': 'gre-tunnel5',
751 'mtu': 'auto',
752 'actual-mtu': 65496,
753 'local-address': '192.168.0.1',
754 'remote-address': '192.168.1.3',
755 'keepalive': '20s,20',
756 'dscp': 'inherit',
757 'clamp-tcp-mss': True,
758 'dont-fragment': False,
759 'allow-fast-path': True,
760 'running': True,
761 'disabled': False,
762 'comment': 'foo',
763 },
764 ]
765 with self.assertRaises(AnsibleExitJson) as exc:
766 args = self.config_module_args.copy()
767 args.update({
768 'path': 'interface gre',
769 'handle_disabled': 'omit',
770 'hide_defaults': False,
771 })
772 with set_module_args(args):
773 self.module.main()
774
775 result = exc.exception.args[0]
776 self.assertEqual(result['changed'], False)
777 self.assertEqual(result['result'], [
778 {
779 '.id': '*10',
780 'name': 'gre-tunnel3',
781 'mtu': 'auto',
782 'local-address': '0.0.0.0',
783 'remote-address': '192.168.1.1',
784 'dscp': 'inherit',
785 'clamp-tcp-mss': True,
786 'dont-fragment': False,
787 'allow-fast-path': True,
788 'disabled': False,
789 },
790 {
791 '.id': '*11',
792 'name': 'gre-tunnel4',
793 'mtu': 'auto',
794 'local-address': '0.0.0.0',
795 'remote-address': '192.168.1.2',
796 'keepalive': '10s,10',
797 'dscp': 'inherit',
798 'clamp-tcp-mss': True,
799 'dont-fragment': False,
800 'allow-fast-path': True,
801 'disabled': False,
802 },
803 {
804 '.id': '*12',
805 'name': 'gre-tunnel5',
806 'mtu': 'auto',
807 'local-address': '192.168.0.1',
808 'remote-address': '192.168.1.3',
809 'keepalive': '20s,20',
810 'dscp': 'inherit',
811 'clamp-tcp-mss': True,
812 'dont-fragment': False,
813 'allow-fast-path': True,
814 'disabled': False,
815 'comment': 'foo',
816 },
817 ])
818
819 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
820 def test_restrict_1(self, mock_compose_api_path):
821 mock_compose_api_path.return_value = [
822 {
823 'chain': 'input',
824 'in-interface-list': 'LAN',
825 'dynamic': False,
826 '.id': '*1',
827 },
828 {
829 'chain': 'forward',
830 'action': 'drop',
831 'in-interface': 'sfp1',
832 '.id': '*2',
833 'dynamic': False,
834 },
835 ]
836 with self.assertRaises(AnsibleExitJson) as exc:
837 args = self.config_module_args.copy()
838 args.update({
839 'path': 'ip firewall filter',
840 'handle_disabled': 'omit',
841 'restrict': [],
842 })
843 with set_module_args(args):
844 self.module.main()
845
846 result = exc.exception.args[0]
847 self.assertEqual(result['changed'], False)
848 self.assertEqual(result['result'], [
849 {
850 'chain': 'input',
851 'in-interface-list': 'LAN',
852 '.id': '*1',
853 },
854 {
855 'chain': 'forward',
856 'action': 'drop',
857 'in-interface': 'sfp1',
858 '.id': '*2',
859 },
860 ])
861
862 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
863 def test_restrict_2(self, mock_compose_api_path):
864 mock_compose_api_path.return_value = [
865 {
866 'chain': 'input',
867 'in-interface-list': 'LAN',
868 'dynamic': False,
869 '.id': '*1',
870 },
871 {
872 'chain': 'forward',
873 'action': 'drop',
874 'in-interface': 'sfp1',
875 '.id': '*2',
876 'dynamic': False,
877 },
878 {
879 'chain': 'input',
880 'action': 'drop',
881 'dynamic': False,
882 '.id': '*3',
883 },
884 ]
885 with self.assertRaises(AnsibleExitJson) as exc:
886 args = self.config_module_args.copy()
887 args.update({
888 'path': 'ip firewall filter',
889 'handle_disabled': 'omit',
890 'restrict': [{
891 'field': 'chain',
892 'values': ['forward'],
893 }],
894 })
895 with set_module_args(args):
896 self.module.main()
897
898 result = exc.exception.args[0]
899 self.assertEqual(result['changed'], False)
900 self.assertEqual(result['result'], [
901 {
902 'chain': 'forward',
903 'action': 'drop',
904 'in-interface': 'sfp1',
905 '.id': '*2',
906 },
907 ])
908
909 @patch('ansible_collections.community.routeros.plugins.modules.api_info.compose_api_path')
910 def test_restrict_3(self, mock_compose_api_path):
911 mock_compose_api_path.return_value = [
912 {
913 'chain': 'input',
914 'in-interface-list': 'LAN',
915 'dynamic': False,
916 '.id': '*1',
917 },
918 {
919 'chain': 'forward',
920 'action': 'drop',
921 'in-interface': 'sfp1',
922 '.id': '*2',
923 'dynamic': False,
924 },
925 {
926 'chain': 'input',
927 'action': 'drop',
928 'dynamic': False,
929 '.id': '*3',
930 },
931 {
932 'chain': 'input',
933 'action': 'drop',
934 'comment': 'Foo',
935 'dynamic': False,
936 '.id': '*4',
937 },
938 {
939 'chain': 'input',
940 'action': 'drop',
941 'comment': 'Bar',
942 'dynamic': False,
943 '.id': '*5',
944 },
945 ]
946 with self.assertRaises(AnsibleExitJson) as exc:
947 args = self.config_module_args.copy()
948 args.update({
949 'path': 'ip firewall filter',
950 'handle_disabled': 'omit',
951 'restrict': [
952 {
953 'field': 'chain',
954 'values': ['input', 'foobar'],
955 },
956 {
957 'field': 'action',
958 'values': ['drop', 42],
959 },
960 {
961 'field': 'comment',
962 'values': [None, 'Foo'],
963 },
964 ],
965 })
966 with set_module_args(args):
967 self.module.main()
968
969 result = exc.exception.args[0]
970 self.assertEqual(result['changed'], False)
971 self.assertEqual(result['result'], [
972 {
973 'chain': 'input',
974 'action': 'drop',
975 '.id': '*3',
976 },
977 {
978 'chain': 'input',
979 'action': 'drop',
980 'comment': 'Foo',
981 '.id': '*4',
982 },
983 ])