11
def write_file(path, content, *args, **kwargs):
12
with open(path, 'w') as f:
17
@mock.patch.object(utils, 'log', lambda *args, **kwargs: None)
18
@mock.patch.object(utils, 'write_file', write_file)
19
class UtilsTestCase(unittest.TestCase):
22
self.tmpdir = tempfile.mkdtemp()
23
utils.COROSYNC_CONF = os.path.join(self.tmpdir, 'corosync.conf')
26
shutil.rmtree(self.tmpdir)
28
@mock.patch.object(utils, 'relation_get')
29
@mock.patch.object(utils, 'related_units')
30
@mock.patch.object(utils, 'relation_ids')
31
@mock.patch.object(utils, 'get_network_address')
32
@mock.patch.object(utils, 'config')
33
def check_debug(self, enabled, mock_config, get_network_address,
34
relation_ids, related_units, relation_get):
35
cfg = {'debug': enabled,
37
'corosync_transport': 'udpu',
38
'corosync_mcastaddr': 'corosync_mcastaddr'}
43
mock_config.side_effect = c
44
get_network_address.return_value = "127.0.0.1"
45
relation_ids.return_value = ['foo:1']
46
related_units.return_value = ['unit-machine-0']
47
relation_get.return_value = 'iface'
49
utils.get_ha_nodes = mock.MagicMock()
50
conf = utils.get_corosync_conf()
51
self.assertEqual(conf['debug'], enabled)
53
self.assertTrue(utils.emit_corosync_conf())
55
with open(utils.COROSYNC_CONF) as fd:
58
pattern = 'debug: on\n'
60
pattern = 'debug: off\n'
62
matches = re.findall(pattern, content, re.M)
63
self.assertEqual(len(matches), 2, str(matches))
65
def test_debug_on(self):
66
self.check_debug(True)
68
def test_debug_off(self):
69
self.check_debug(False)
71
@mock.patch.object(utils, 'config')
72
def test_get_transport(self, mock_config):
73
mock_config.return_value = 'udp'
74
self.assertEqual('udp', utils.get_transport())
76
mock_config.return_value = 'udpu'
77
self.assertEqual('udpu', utils.get_transport())
79
mock_config.return_value = 'hafu'
80
self.assertRaises(ValueError, utils.get_transport)