Debian Codegraph @

ansible_collections/community/rabbitmq/tests/unit/modules/test_rabbitmq_global_parameter.py

raw
1 # -*- coding: utf-8 -*-
2
3 from __future__ import (absolute_import, division, print_function)
4 __metaclass__ = type
5
6 from ansible_collections.community.rabbitmq.plugins.modules import rabbitmq_global_parameter
7
8 from ansible_collections.community.rabbitmq.tests.unit.compat.mock import patch
9 from ansible_collections.community.rabbitmq.tests.unit.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args
10
11
12 class TestRabbitMQGlobalParameterModule(ModuleTestCase):
13 def setUp(self):
14 super(TestRabbitMQGlobalParameterModule, self).setUp()
15 self.module = rabbitmq_global_parameter
16
17 def tearDown(self):
18 super(TestRabbitMQGlobalParameterModule, self).tearDown()
19
20 def _assert(self, exc, attribute, expected_value, msg=''):
21 value = exc.message[attribute] if hasattr(exc, attribute) else exc.args[0][attribute]
22 assert value == expected_value, msg
23
24 def test_without_required_parameters(self):
25 """Failure must occur when all parameters are missing."""
26 with self.assertRaises(AnsibleFailJson):
27 with set_module_args({}):
28 self.module.main()
29
30 @patch('ansible.module_utils.basic.AnsibleModule.get_bin_path')
31 @patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_global_parameter.RabbitMqGlobalParameter._exec')
32 def test_read_without_initial_global_parameters(self, _exec, get_bin_path):
33 """Test that the code to read the global parameters does not fail anymore for RabbitMQ 3.7.x."""
34 with set_module_args({
35 'name': 'cluster_name',
36 'state': 'absent',
37 }):
38 get_bin_path.return_value = '/rabbitmqctl'
39
40 # command list_global_parameters returns:
41 # - RabbitMQ 3.6.x: ''
42 # - RabbitMQ 3.7.x: '\n'
43 # - RabbitMQ 3.8.x: 'name\tvalue\n' table header
44 for out in '', '\n', 'name\tvalue\n':
45 _exec.return_value = out.splitlines()
46 try:
47 self.module.main()
48 except AnsibleExitJson as e:
49 self._assert(e, 'changed', False)
50 self._assert(e, 'state', 'absent')
51
52 @patch('ansible.module_utils.basic.AnsibleModule.get_bin_path')
53 @patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_global_parameter.RabbitMqGlobalParameter._exec')
54 def test_remove_global_parameter(self, _exec, get_bin_path):
55 """Test removal of global parameters."""
56 with set_module_args({
57 'name': 'cluster_name',
58 'state': 'absent',
59 }):
60 get_bin_path.return_value = '/rabbitmqctl'
61
62 # command list_global_parameters returns:
63 # - RabbitMQ 3.6.x: ''
64 # - RabbitMQ 3.7.x: '\n'
65 # - RabbitMQ 3.8.x: 'name\tvalue\n' table header
66 for out in 'cluster_name\t"rabbitmq-test"', 'cluster_name\t"rabbitmq-test"\n', 'name\tvalue\ncluster_name\t"rabbitmq-test"\n':
67 _exec.return_value = out.splitlines()
68 try:
69 self.module.main()
70 except AnsibleExitJson as e:
71 self._assert(e, 'changed', True)
72 self._assert(e, 'state', 'absent')
73
74 @patch('ansible.module_utils.basic.AnsibleModule.get_bin_path')
75 @patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_global_parameter.RabbitMqGlobalParameter._exec')
76 def test_set_global_parameter(self, _exec, get_bin_path):
77 """Test setting of global parameters."""
78 with set_module_args({
79 'name': 'cluster_name',
80 'value': '"rabbitmq-test"',
81 'state': 'present',
82 }):
83 get_bin_path.return_value = '/rabbitmqctl'
84
85 versions = ['3.6', '3.7', '3.8']
86 for version_num in versions:
87 def side_effect(args, check_rc=True):
88 if 'list_global_parameters' in args:
89 if version_num == '3.6':
90 return 'other_param\t"other_value"\ncluster_name\t"another_name"'.splitlines()
91 elif version_num == '3.7':
92 return 'other_param\t"other_value"\ncluster_name\t"another_name"\n'.splitlines()
93 else:
94 return 'name\tvalue\nother_param\t"other_value"\ncluster_name\t"another_name"\n'.splitlines()
95 elif 'clear_global_parameter' in args or 'set_global_parameter' in args:
96 return ''.splitlines()
97 _exec.side_effect = side_effect
98 try:
99 self.module.main()
100 except AnsibleExitJson as e:
101 self._assert(e, 'changed', True)
102 self._assert(e, 'state', 'present')
103 self._assert(e, 'value', 'rabbitmq-test')
104
105 @patch('ansible.module_utils.basic.AnsibleModule.get_bin_path')
106 @patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_global_parameter.RabbitMqGlobalParameter._exec')
107 def test_set_no_change_global_parameter(self, _exec, get_bin_path):
108 """Test that there is no change when setting the same global parameter."""
109 with set_module_args({
110 'name': 'cluster_name',
111 'value': '"rabbitmq-test"',
112 'state': 'present',
113 }):
114 get_bin_path.return_value = '/rabbitmqctl'
115
116 def side_effect(args, check_rc=True):
117 if 'list_global_parameters' in args:
118 return 'other_param\t"other_value"\ncluster_name\t"rabbitmq-test"'.splitlines()
119 elif 'clear_global_parameter' in args or 'set_global_parameter' in args:
120 return ''.splitlines()
121 _exec.side_effect = side_effect
122 try:
123 self.module.main()
124 except AnsibleExitJson as e:
125 self._assert(e, 'changed', False)
126 self._assert(e, 'state', 'present')
127 self._assert(e, 'value', 'rabbitmq-test')