~1chb1n/charms/trusty/hacluster/next.1601-test-update2

« back to all changes in this revision

Viewing changes to unit_tests/test_hacluster_utils.py

  • Committer: Billy Olsen
  • Date: 2015-05-01 12:43:29 UTC
  • mfrom: (43.2.8 hacluster.cleanup)
  • Revision ID: billy.olsen@canonical.com-20150501124329-aj6vf5rw9ce95n47
[hopem,r=wolsen]

Refactor and clean-up the hacluster charm.
This makes the code format and layout more consistent with
the rest of the openstack charms.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import mock
 
2
import os
 
3
import re
 
4
import shutil
 
5
import tempfile
 
6
import unittest
 
7
 
 
8
import utils
 
9
 
 
10
 
 
11
def write_file(path, content, *args, **kwargs):
 
12
    with open(path, 'w') as f:
 
13
        f.write(content)
 
14
        f.flush()
 
15
 
 
16
 
 
17
@mock.patch.object(utils, 'log', lambda *args, **kwargs: None)
 
18
@mock.patch.object(utils, 'write_file', write_file)
 
19
class UtilsTestCase(unittest.TestCase):
 
20
 
 
21
    def setUp(self):
 
22
        self.tmpdir = tempfile.mkdtemp()
 
23
        utils.COROSYNC_CONF = os.path.join(self.tmpdir, 'corosync.conf')
 
24
 
 
25
    def tearDown(self):
 
26
        shutil.rmtree(self.tmpdir)
 
27
 
 
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,
 
36
               'prefer-ipv6': False,
 
37
               'corosync_transport': 'udpu',
 
38
               'corosync_mcastaddr': 'corosync_mcastaddr'}
 
39
 
 
40
        def c(k):
 
41
            return cfg.get(k)
 
42
 
 
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'
 
48
 
 
49
        utils.get_ha_nodes = mock.MagicMock()
 
50
        conf = utils.get_corosync_conf()
 
51
        self.assertEqual(conf['debug'], enabled)
 
52
 
 
53
        self.assertTrue(utils.emit_corosync_conf())
 
54
 
 
55
        with open(utils.COROSYNC_CONF) as fd:
 
56
            content = fd.read()
 
57
            if enabled:
 
58
                pattern = 'debug: on\n'
 
59
            else:
 
60
                pattern = 'debug: off\n'
 
61
 
 
62
            matches = re.findall(pattern, content, re.M)
 
63
            self.assertEqual(len(matches), 2, str(matches))
 
64
 
 
65
    def test_debug_on(self):
 
66
        self.check_debug(True)
 
67
 
 
68
    def test_debug_off(self):
 
69
        self.check_debug(False)
 
70
 
 
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())
 
75
 
 
76
        mock_config.return_value = 'udpu'
 
77
        self.assertEqual('udpu', utils.get_transport())
 
78
 
 
79
        mock_config.return_value = 'hafu'
 
80
        self.assertRaises(ValueError, utils.get_transport)