~gnuoy/charms/trusty/glance/add-nrpe-checks

« back to all changes in this revision

Viewing changes to unit_tests/test_glance_utils.py

  • Committer: James Page
  • Date: 2013-10-22 23:15:28 UTC
  • mto: This revision was merged to the branch mainline in revision 40.
  • Revision ID: james.page@canonical.com-20131022231528-jz838l5devz7rt39
Add missing files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from mock import patch, call, MagicMock
 
2
 
 
3
from collections import OrderedDict
 
4
 
 
5
import glance_utils as utils
 
6
 
 
7
from test_utils import (
 
8
    CharmTestCase,
 
9
)
 
10
 
 
11
TO_PATCH = [
 
12
    'config',
 
13
    'log',
 
14
    'ceph_create_pool',
 
15
    'ceph_pool_exists',
 
16
    'relation_ids',
 
17
    'get_os_codename_package',
 
18
    'get_os_codename_install_source',
 
19
    'configure_installation_source',
 
20
    'eligible_leader',
 
21
    'templating',
 
22
    'apt_update',
 
23
    'apt_install',
 
24
    'mkdir'
 
25
]
 
26
 
 
27
 
 
28
class TestGlanceUtils(CharmTestCase):
 
29
    def setUp(self):
 
30
        super(TestGlanceUtils, self).setUp(utils, TO_PATCH)
 
31
        self.config.side_effect = self.test_config.get_all
 
32
 
 
33
    @patch('subprocess.check_call')
 
34
    def test_migrate_database(self, check_call):
 
35
        '''It migrates database with cinder-manage'''
 
36
        utils.migrate_database()
 
37
        check_call.assert_called_with(['glance-manage', 'db_sync'])
 
38
 
 
39
    def test_ensure_ceph_pool(self):
 
40
        self.ceph_pool_exists.return_value = False
 
41
        utils.ensure_ceph_pool(service='glance', replicas=3)
 
42
        self.ceph_create_pool.assert_called_with(service='glance',
 
43
                                                 name='glance',
 
44
                                                 replicas=3)
 
45
 
 
46
    def test_ensure_ceph_pool_already_exists(self):
 
47
        self.ceph_pool_exists.return_value = True
 
48
        utils.ensure_ceph_pool(service='glance', replicas=3)
 
49
        self.assertFalse(self.ceph_create_pool.called)
 
50
 
 
51
    @patch('os.path.exists')
 
52
    def test_register_configs_apache(self, exists):
 
53
        exists.return_value = False
 
54
        self.get_os_codename_package.return_value = 'grizzly'
 
55
        self.relation_ids.return_value = False
 
56
        configs = utils.register_configs()
 
57
        calls = []
 
58
        for conf in [utils.GLANCE_REGISTRY_CONF,
 
59
                     utils.GLANCE_API_CONF,
 
60
                     utils.GLANCE_API_PASTE_INI,
 
61
                     utils.GLANCE_REGISTRY_PASTE_INI,
 
62
                     utils.HAPROXY_CONF,
 
63
                     utils.HTTPS_APACHE_CONF]:
 
64
            calls.append(
 
65
                call(conf,
 
66
                     utils.CONFIG_FILES[conf]['hook_contexts'])
 
67
            )
 
68
        configs.register.assert_has_calls(calls, any_order=True)
 
69
 
 
70
    @patch('os.path.exists')
 
71
    def test_register_configs_apache24(self, exists):
 
72
        exists.return_value = True
 
73
        self.get_os_codename_package.return_value = 'grizzly'
 
74
        self.relation_ids.return_value = False
 
75
        configs = utils.register_configs()
 
76
        calls = []
 
77
        for conf in [utils.GLANCE_REGISTRY_CONF,
 
78
                     utils.GLANCE_API_CONF,
 
79
                     utils.GLANCE_API_PASTE_INI,
 
80
                     utils.GLANCE_REGISTRY_PASTE_INI,
 
81
                     utils.HAPROXY_CONF,
 
82
                     utils.HTTPS_APACHE_24_CONF]:
 
83
            calls.append(
 
84
                call(conf,
 
85
                     utils.CONFIG_FILES[conf]['hook_contexts'])
 
86
            )
 
87
        configs.register.assert_has_calls(calls, any_order=True)
 
88
 
 
89
    @patch('os.path.exists')
 
90
    def test_register_configs_ceph(self, exists):
 
91
        exists.return_value = False
 
92
        self.get_os_codename_package.return_value = 'grizzly'
 
93
        self.relation_ids.return_value = ['ceph:0']
 
94
        configs = utils.register_configs()
 
95
        calls = []
 
96
        for conf in [utils.GLANCE_REGISTRY_CONF,
 
97
                     utils.GLANCE_API_CONF,
 
98
                     utils.GLANCE_API_PASTE_INI,
 
99
                     utils.GLANCE_REGISTRY_PASTE_INI,
 
100
                     utils.HAPROXY_CONF,
 
101
                     utils.HTTPS_APACHE_CONF,
 
102
                     utils.CEPH_CONF]:
 
103
            calls.append(
 
104
                call(conf,
 
105
                     utils.CONFIG_FILES[conf]['hook_contexts'])
 
106
            )
 
107
        configs.register.assert_has_calls(calls, any_order=True)
 
108
        self.mkdir.assert_called_with('/etc/ceph')
 
109
 
 
110
    def test_restart_map(self):
 
111
        ex_map = OrderedDict([
 
112
            (utils.GLANCE_REGISTRY_CONF, ['glance-registry']),
 
113
            (utils.GLANCE_API_CONF, ['glance-api']),
 
114
            (utils.GLANCE_API_PASTE_INI, ['glance-api']),
 
115
            (utils.GLANCE_REGISTRY_PASTE_INI, ['glance-registry']),
 
116
            (utils.CEPH_CONF, ['glance-api', 'glance-registry']),
 
117
            (utils.HAPROXY_CONF, ['haproxy']),
 
118
            (utils.HTTPS_APACHE_CONF, ['apache2']),
 
119
            (utils.HTTPS_APACHE_24_CONF, ['apache2'])
 
120
        ])
 
121
        self.assertEquals(ex_map, utils.restart_map())
 
122
 
 
123
    @patch.object(utils, 'migrate_database')
 
124
    def test_openstack_upgrade_leader(self, migrate):
 
125
        self.config.side_effect = None
 
126
        self.config.return_value = 'cloud:precise-havana'
 
127
        self.eligible_leader.return_value = True
 
128
        self.get_os_codename_install_source.return_value = 'havana'
 
129
        configs = MagicMock()
 
130
        utils.do_openstack_upgrade(configs)
 
131
        self.assertTrue(configs.write_all.called)
 
132
        configs.set_release.assert_called_with(openstack_release='havana')
 
133
        self.assertTrue(migrate.called)
 
134
 
 
135
    @patch.object(utils, 'migrate_database')
 
136
    def test_openstack_upgrade_not_leader(self, migrate):
 
137
        self.config.side_effect = None
 
138
        self.config.return_value = 'cloud:precise-havana'
 
139
        self.eligible_leader.return_value = False
 
140
        self.get_os_codename_install_source.return_value = 'havana'
 
141
        configs = MagicMock()
 
142
        utils.do_openstack_upgrade(configs)
 
143
        self.assertTrue(configs.write_all.called)
 
144
        configs.set_release.assert_called_with(openstack_release='havana')
 
145
        self.assertFalse(migrate.called)