Debian Codegraph @

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

raw
1 # Copyright (c) 2016 Red Hat Inc.
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 import json
10
11 from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch
12 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args
13
14 from ansible_collections.community.routeros.plugins.modules import command
15 from .routeros_module import TestRouterosModule, load_fixture
16
17
18 class TestRouterosCommandModule(TestRouterosModule):
19
20 module = command
21
22 def setUp(self):
23 super(TestRouterosCommandModule, self).setUp()
24
25 self.mock_run_commands = patch('ansible_collections.community.routeros.plugins.modules.command.run_commands')
26 self.run_commands = self.mock_run_commands.start()
27
28 def tearDown(self):
29 super(TestRouterosCommandModule, self).tearDown()
30 self.mock_run_commands.stop()
31
32 def load_fixtures(self, commands=None):
33
34 def load_from_file(*args, **kwargs):
35 module, commands = args
36 output = list()
37
38 for item in commands:
39 try:
40 obj = json.loads(item)
41 command = obj
42 except ValueError:
43 command = item
44 filename = str(command).replace(' ', '_').replace('/', '')
45 output.append(load_fixture(filename))
46 return output
47
48 self.run_commands.side_effect = load_from_file
49
50 def test_command_simple(self):
51 with set_module_args(dict(commands=['/system resource print'])):
52 result = self.execute_module(changed=True)
53 self.assertEqual(len(result['stdout']), 1)
54 self.assertTrue('platform: "MikroTik"' in result['stdout'][0])
55
56 def test_command_multiple(self):
57 with set_module_args(dict(commands=['/system resource print', '/system resource print'])):
58 result = self.execute_module(changed=True)
59 self.assertEqual(len(result['stdout']), 2)
60 self.assertTrue('platform: "MikroTik"' in result['stdout'][0])
61
62 def test_command_wait_for(self):
63 wait_for = 'result[0] contains "MikroTik"'
64 with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for)):
65 self.execute_module(changed=True)
66
67 def test_command_wait_for_fails(self):
68 wait_for = 'result[0] contains "test string"'
69 with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for)):
70 self.execute_module(failed=True)
71 self.assertEqual(self.run_commands.call_count, 10)
72
73 def test_command_retries(self):
74 wait_for = 'result[0] contains "test string"'
75 with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, retries=2)):
76 self.execute_module(failed=True)
77 self.assertEqual(self.run_commands.call_count, 2)
78
79 def test_command_match_any(self):
80 wait_for = ['result[0] contains "MikroTik"',
81 'result[0] contains "test string"']
82 with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, match='any')):
83 self.execute_module(changed=True)
84
85 def test_command_match_all(self):
86 wait_for = ['result[0] contains "MikroTik"',
87 'result[0] contains "RB1100"']
88 with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, match='all')):
89 self.execute_module(changed=True)
90
91 def test_command_match_all_failure(self):
92 wait_for = ['result[0] contains "MikroTik"',
93 'result[0] contains "test string"']
94 commands = ['/system resource print', '/system resource print']
95 with set_module_args(dict(commands=commands, wait_for=wait_for, match='all')):
96 self.execute_module(failed=True)
97
98 def test_command_wait_for_2(self):
99 wait_for = 'result[0] contains "wireless"'
100 with set_module_args(dict(commands=['/system package print'], wait_for=wait_for)):
101 self.execute_module(changed=True)