~1chb1n/charms/trusty/cinder/next.normalize-makefile-test-deps

« back to all changes in this revision

Viewing changes to unit_tests/test_cluster_hooks.py

  • Committer: Adam Gandelman
  • Date: 2013-10-17 21:48:08 UTC
  • Revision ID: adamg@canonical.com-20131017214808-k52pya40bowxzg4i
Merging python-redux and havana work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from mock import MagicMock, patch, call
 
3
 
 
4
import cinder_utils as utils
 
5
 
 
6
# Need to do some early patching to get the module loaded.
 
7
#_restart_map = utils.restart_map
 
8
_register_configs = utils.register_configs
 
9
_service_enabled = utils.service_enabled
 
10
utils.register_configs = MagicMock()
 
11
utils.service_enabled = MagicMock()
 
12
 
 
13
import cinder_hooks as hooks
 
14
 
 
15
# Unpatch it now that its loaded.
 
16
utils.register_configs = _register_configs
 
17
utils.service_enabled = _service_enabled
 
18
 
 
19
from test_utils import (
 
20
    CharmTestCase,
 
21
    RESTART_MAP,
 
22
)
 
23
 
 
24
TO_PATCH = [
 
25
    # cinder_utils
 
26
    'clean_storage',
 
27
    'determine_packages',
 
28
    'ensure_block_device',
 
29
    'ensure_ceph_keyring',
 
30
    'ensure_ceph_pool',
 
31
    'juju_log',
 
32
    'lsb_release',
 
33
    'migrate_database',
 
34
    'prepare_lvm_storage',
 
35
    'register_configs',
 
36
    'service_enabled',
 
37
    'set_ceph_env_variables',
 
38
    'CONFIGS',
 
39
    'CLUSTER_RES',
 
40
    # charmhelpers.core.hookenv
 
41
    'config',
 
42
    'relation_set',
 
43
    'service_name',
 
44
    'unit_get',
 
45
    # charmhelpers.core.host
 
46
    'apt_install',
 
47
    'apt_update',
 
48
    # charmhelpers.contrib.openstack.openstack_utils
 
49
    'configure_installation_source',
 
50
    # charmhelpers.contrib.hahelpers.cluster_utils
 
51
    'eligible_leader',
 
52
    'get_hacluster_config',
 
53
]
 
54
 
 
55
 
 
56
class TestClusterHooks(CharmTestCase):
 
57
    def setUp(self):
 
58
        super(TestClusterHooks, self).setUp(hooks, TO_PATCH)
 
59
        self.config.side_effect = self.test_config.get_all
 
60
 
 
61
    @patch('charmhelpers.core.host.service')
 
62
    @patch('charmhelpers.core.host.file_hash')
 
63
    def test_cluster_hook(self, file_hash, service):
 
64
        '''Ensure API restart before haproxy on cluster changed'''
 
65
        # set first hash lookup on all files
 
66
        side_effects = []
 
67
        # set first hash lookup on all configs in restart_on_change
 
68
        [side_effects.append('foo') for f in RESTART_MAP.keys()]
 
69
        # set second hash lookup on all configs in restart_on_change
 
70
        [side_effects.append('bar') for f in RESTART_MAP.keys()]
 
71
        file_hash.side_effect = side_effects
 
72
        hooks.hooks.execute(['hooks/cluster-relation-changed'])
 
73
        ex = [
 
74
            call('restart', 'cinder-api'),
 
75
            call('restart', 'cinder-volume'),
 
76
            call('restart', 'cinder-scheduler'),
 
77
            call('restart', 'haproxy'),
 
78
            call('restart', 'apache2')]
 
79
        self.assertEquals(ex, service.call_args_list)
 
80
 
 
81
    def test_ha_joined_complete_config(self):
 
82
        '''Ensure hacluster subordinate receives all relevant config'''
 
83
        conf = {
 
84
            'ha-bindiface': 'eth100',
 
85
            'ha-mcastport': '37373',
 
86
            'vip': '192.168.25.163',
 
87
            'vip_iface': 'eth101',
 
88
            'vip_cidr': '19',
 
89
        }
 
90
        self.get_hacluster_config.return_value = conf
 
91
        hooks.hooks.execute(['hooks/ha-relation-joined'])
 
92
        ex_args = {
 
93
            'corosync_mcastport': '37373',
 
94
            'init_services': {'res_cinder_haproxy': 'haproxy'},
 
95
            'resource_params': {
 
96
                'res_cinder_vip':
 
97
                'params ip="192.168.25.163" cidr_netmask="19" nic="eth101"',
 
98
                'res_cinder_haproxy': 'op monitor interval="5s"'
 
99
            },
 
100
            'corosync_bindiface': 'eth100',
 
101
            'clones': {'cl_cinder_haproxy': 'res_cinder_haproxy'},
 
102
            'resources': {
 
103
                'res_cinder_vip': 'ocf:heartbeat:IPaddr2',
 
104
                'res_cinder_haproxy': 'lsb:haproxy'
 
105
            }
 
106
        }
 
107
        self.relation_set.assert_called_with(**ex_args)