/
test_rabbitmq_context.py
268 lines (220 loc) · 10.8 KB
/
test_rabbitmq_context.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Copyright 2016 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import rabbitmq_context
import mock
import unittest
import tempfile
class TestRabbitMQSSLContext(unittest.TestCase):
@mock.patch("rabbitmq_context.config")
@mock.patch("rabbitmq_context.close_port")
@mock.patch("rabbitmq_context.ssl_utils.reconfigure_client_ssl")
@mock.patch("rabbitmq_context.ssl_utils.get_ssl_mode")
def test_context_ssl_off(self, get_ssl_mode, reconfig_ssl, close_port,
config):
get_ssl_mode.return_value = ("off", "off")
self.assertEqual(rabbitmq_context.RabbitMQSSLContext().__call__(), {
"ssl_mode": "off"
})
self.assertTrue(close_port.called)
self.assertTrue(reconfig_ssl.called)
@mock.patch("rabbitmq_context.open_port")
@mock.patch("rabbitmq_context.os.chmod")
@mock.patch("rabbitmq_context.os.chown")
@mock.patch("rabbitmq_context.os.path.exists")
@mock.patch("rabbitmq_context.pwd.getpwnam")
@mock.patch("rabbitmq_context.grp.getgrnam")
@mock.patch("rabbitmq_context.config")
@mock.patch("rabbitmq_context.close_port")
@mock.patch("rabbitmq_context.ssl_utils.reconfigure_client_ssl")
@mock.patch("rabbitmq_context.ssl_utils.get_ssl_mode")
def test_context_ssl_on(self, get_ssl_mode, reconfig_ssl, close_port,
config, gr, pw, exists, chown, chmod, open_port):
exists.return_value = True
get_ssl_mode.return_value = ("on", "on")
def config_get(n):
return None
config.side_effect = config_get
def pw(name):
class Uid(object):
pw_uid = 1
gr_gid = 100
return Uid()
pw.side_effect = pw
gr.side_effect = pw
m = mock.mock_open()
with mock.patch('rabbitmq_context.open', m, create=True):
self.assertEqual(
rabbitmq_context.RabbitMQSSLContext().__call__(), {
"ssl_port": None,
"ssl_cert_file": "/etc/rabbitmq/rabbit-server-cert.pem",
"ssl_key_file": '/etc/rabbitmq/rabbit-server-privkey.pem',
"ssl_client": False,
"ssl_ca_file": "",
"ssl_only": False,
"ssl_mode": "on",
})
self.assertTrue(reconfig_ssl.called)
self.assertTrue(open_port.called)
class TestRabbitMQClusterContext(unittest.TestCase):
@mock.patch.object(rabbitmq_context, 'cmp_pkgrevno')
@mock.patch("rabbitmq_context.config")
def test_context_ssl_off(self, config, mock_cmp_pkgrevno):
config_data = {'cluster-partition-handling': 'ignore',
'connection-backlog': 200,
'queue-master-locator': 'client-local'}
config.side_effect = config_data.get
mock_cmp_pkgrevno.return_value = 0
self.assertEqual(
rabbitmq_context.RabbitMQClusterContext().__call__(), {
'cluster_partition_handling': "ignore",
'connection_backlog': 200,
'queue_master_locator': 'client-local',
})
config.assert_has_calls([mock.call("cluster-partition-handling"),
mock.call("connection-backlog")],
mock.call('queue-master-locator'))
@mock.patch.object(rabbitmq_context, 'cmp_pkgrevno')
@mock.patch("rabbitmq_context.config")
def test_queue_master_locator_min_masters(self, config, mock_cmp_pkgrevno):
config_data = {'cluster-partition-handling': 'ignore',
'connection-backlog': 200,
'queue-master-locator': 'min-masters'}
config.side_effect = config_data.get
mock_cmp_pkgrevno.return_value = 0
self.assertEqual(
rabbitmq_context.RabbitMQClusterContext().__call__(), {
'cluster_partition_handling': "ignore",
'connection_backlog': 200,
'queue_master_locator': 'min-masters',
})
config.assert_has_calls([mock.call("cluster-partition-handling"),
mock.call("connection-backlog")],
mock.call('queue-master-locator'))
@mock.patch.object(rabbitmq_context, 'cmp_pkgrevno')
@mock.patch("rabbitmq_context.config")
def test_rabbit_server_3pt6(self, config, mock_cmp_pkgrevno):
config_data = {'cluster-partition-handling': 'ignore',
'queue-master-locator': 'min-masters',
'connection-backlog': 200}
config.side_effect = config_data.get
mock_cmp_pkgrevno.return_value = -1
self.assertEqual(
rabbitmq_context.RabbitMQClusterContext().__call__(), {
'cluster_partition_handling': "ignore",
'connection_backlog': 200,
})
config.assert_has_calls([mock.call("cluster-partition-handling"),
mock.call("connection-backlog")])
assert mock.call('queue-master-locator') not in config.mock_calls
class TestRabbitMQEnvContext(unittest.TestCase):
@mock.patch.object(rabbitmq_context.psutil, 'NUM_CPUS', 2)
@mock.patch.object(rabbitmq_context, 'relation_ids', lambda *args: [])
@mock.patch.object(rabbitmq_context, 'service_name')
@mock.patch.object(rabbitmq_context, 'config')
def test_rabbitmqenv(self, mock_config, mock_service_name):
config = {}
def fake_config(key):
return config.get(key)
mock_service_name.return_value = 'svc_foo'
mock_config.side_effect = fake_config
with tempfile.NamedTemporaryFile() as tmpfile:
with mock.patch('rabbitmq_context.ENV_CONF', tmpfile.name):
config['prefer-ipv6'] = True
config['erl-vm-io-thread-multiplier'] = 36
ctxt = rabbitmq_context.RabbitMQEnvContext()()
self.assertEqual(ctxt['settings'],
{'RABBITMQ_SERVER_START_ARGS':
"'-proto_dist inet6_tcp'",
'RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS':
"'+A 72'"})
@mock.patch.object(rabbitmq_context.psutil, 'NUM_CPUS', 2)
@mock.patch.object(rabbitmq_context, 'relation_ids')
@mock.patch.object(rabbitmq_context, 'service_name')
@mock.patch.object(rabbitmq_context, 'config')
def test_rabbitmqenv_legacy_ha_support(self, mock_config,
mock_service_name,
mock_relation_ids):
config = {}
def fake_config(key):
return config.get(key)
def fake_relation_ids(key):
if 'ha':
return ['ha:1']
mock_relation_ids.side_effect = fake_relation_ids
mock_service_name.return_value = 'svc_foo'
mock_config.side_effect = fake_config
with tempfile.NamedTemporaryFile() as tmpfile:
with mock.patch('rabbitmq_context.ENV_CONF', tmpfile.name):
ctxt = rabbitmq_context.RabbitMQEnvContext()()
self.assertEqual(ctxt['settings'],
{'RABBITMQ_NODENAME': 'svc_foo@localhost',
'RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS':
"'+A 48'"})
mock_relation_ids.side_effect = lambda key: []
with tempfile.NamedTemporaryFile() as tmpfile:
with mock.patch('rabbitmq_context.ENV_CONF', tmpfile.name):
ctxt = rabbitmq_context.RabbitMQEnvContext()()
self.assertEqual(ctxt['settings'],
{'RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS':
"'+A 48'"})
@mock.patch.object(rabbitmq_context.psutil, 'NUM_CPUS', 2)
@mock.patch.object(rabbitmq_context, 'relation_ids')
@mock.patch.object(rabbitmq_context, 'service_name')
@mock.patch.object(rabbitmq_context, 'config')
def test_rabbitmqenv_existing_nodename(self, mock_config,
mock_service_name,
mock_relation_ids):
def fake_relation_ids(key):
if 'ha':
return ['ha:1']
mock_relation_ids.side_effect = fake_relation_ids
mock_service_name.return_value = 'svc_foo'
mock_config.return_value = None
with tempfile.NamedTemporaryFile() as tmpfile:
with mock.patch('rabbitmq_context.ENV_CONF', tmpfile.name):
with open(tmpfile.name, 'w') as fd:
fd.write("RABBITMQ_NODENAME = blah@localhost")
ctxt = rabbitmq_context.RabbitMQEnvContext()()
self.assertEqual(ctxt['settings'],
{'RABBITMQ_NODENAME': 'blah@localhost',
'RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS':
"'+A 48'"})
@mock.patch.object(rabbitmq_context, 'relation_ids', lambda *args: [])
@mock.patch.object(rabbitmq_context.psutil, 'NUM_CPUS', 128)
@mock.patch.object(rabbitmq_context, 'service_name')
@mock.patch.object(rabbitmq_context, 'config')
def test_rabbitmqenv_in_container(self, mock_config, mock_service_name):
mock_service_name.return_value = 'svc_foo'
config = {}
def fake_config(key):
return config.get(key)
mock_config.side_effect = fake_config
with mock.patch.object(rabbitmq_context, 'is_container') as \
mock_is_ctnr:
mock_is_ctnr.return_value = True
ctxt = rabbitmq_context.RabbitMQEnvContext()()
self.assertEqual(ctxt['settings'],
{'RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS':
"'+A 48'"})
config['erl-vm-io-thread-multiplier'] = 24
ctxt = rabbitmq_context.RabbitMQEnvContext()()
self.assertEqual(ctxt['settings'],
{'RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS':
"'+A 3072'"})
del config['erl-vm-io-thread-multiplier']
mock_is_ctnr.return_value = False
ctxt = rabbitmq_context.RabbitMQEnvContext()()
self.assertEqual(ctxt['settings'],
{'RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS':
"'+A 3072'"})