Use json module to dump json in set_ha_mode
Use json module to dump json in set_ha_mode rather than trying to generate json using string interpolation. This fixes a bug when using the 'nodes' mode which was generating invalid json. The new function test is taken from Id7ef45b7001d26ede3fd61f97626b5e9e8b81196 Change-Id: Ieb49036389221f6fbf2db93fbe4aebe6e986ea21 Co-Authored-By: Trent Lloyd <trent.lloyd@canonical.com>
This commit is contained in:
parent
c72d401192
commit
32bce11f0f
|
@ -450,19 +450,25 @@ def set_ha_mode(vhost, mode, params=None, sync_mode='automatic'):
|
|||
return
|
||||
|
||||
if mode == 'all':
|
||||
value = '{"ha-mode": "all", "ha-sync-mode": "%s"}' % sync_mode
|
||||
definition = {
|
||||
"ha-mode": "all",
|
||||
"ha-sync-mode": sync_mode}
|
||||
elif mode == 'exactly':
|
||||
value = '{"ha-mode":"exactly","ha-params":%s,"ha-sync-mode":"%s"}' \
|
||||
% (params, sync_mode)
|
||||
definition = {
|
||||
"ha-mode": "exactly",
|
||||
"ha-params": params,
|
||||
"ha-sync-mode": sync_mode}
|
||||
elif mode == 'nodes':
|
||||
value = '{"ha-mode":"nodes","ha-params":[%s]},"ha-sync-mode": "%s"' % (
|
||||
",".join(params), sync_mode)
|
||||
definition = {
|
||||
"ha-mode": "nodes",
|
||||
"ha-params": params,
|
||||
"ha-sync-mode": sync_mode}
|
||||
else:
|
||||
raise RabbitmqError(("Unknown mode '%s', known modes: "
|
||||
"all, exactly, nodes"))
|
||||
|
||||
log("Setting HA policy to vhost '%s'" % vhost, level='INFO')
|
||||
set_policy(vhost, 'HA', r'^(?!amq\.).*', value)
|
||||
set_policy(vhost, 'HA', r'^(?!amq\.).*', json.dumps(definition))
|
||||
|
||||
|
||||
def clear_ha_mode(vhost, name='HA', force=False):
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
import collections
|
||||
from functools import wraps
|
||||
import json
|
||||
import mock
|
||||
import os
|
||||
import sys
|
||||
|
@ -1411,3 +1412,44 @@ class UtilsTests(CharmTestCase):
|
|||
self.assertTrue(rabbit_utils.rabbit_supports_json())
|
||||
mock_cmp_pkgrevno.return_value = -1
|
||||
self.assertFalse(rabbit_utils.rabbit_supports_json())
|
||||
|
||||
@mock.patch('rabbit_utils.caching_cmp_pkgrevno')
|
||||
@mock.patch('rabbit_utils.set_policy')
|
||||
@mock.patch('rabbit_utils.config')
|
||||
def test_set_ha_mode(self,
|
||||
mock_config,
|
||||
mock_set_policy,
|
||||
mock_caching_cmp_pkgrevno):
|
||||
"""Testing set_ha_mode"""
|
||||
mock_config.side_effect = self.test_config
|
||||
mock_caching_cmp_pkgrevno.return_value = 1
|
||||
|
||||
expected_policy = {
|
||||
'all': {
|
||||
'ha-mode': 'all',
|
||||
'ha-sync-mode': 'automatic',
|
||||
},
|
||||
'exactly': {
|
||||
'ha-mode': 'exactly',
|
||||
'ha-sync-mode': 'automatic',
|
||||
'ha-params': 2,
|
||||
},
|
||||
'nodes': {
|
||||
'ha-mode': 'nodes',
|
||||
'ha-sync-mode': 'automatic',
|
||||
'ha-params': ["rabbit@nodeA", "rabbit@nodeB"]
|
||||
},
|
||||
}
|
||||
for mode, policy in expected_policy.items():
|
||||
rabbit_utils.set_ha_mode('test_vhost', mode,
|
||||
params=policy.get('ha-params'))
|
||||
|
||||
mock_set_policy.assert_called_once()
|
||||
|
||||
self.assertEqual(mock_set_policy.call_args.args[0:3],
|
||||
('test_vhost', 'HA', r'^(?!amq\.).*',))
|
||||
|
||||
generated_policy = json.loads(mock_set_policy.call_args.args[3])
|
||||
self.assertEqual(generated_policy, policy)
|
||||
|
||||
mock_set_policy.reset_mock()
|
||||
|
|
Loading…
Reference in New Issue