~gandelman-a/charms/precise/nova-compute/unused_func

« back to all changes in this revision

Viewing changes to unit_tests/test_nova_compute_utils.py

  • Committer: James Page
  • Date: 2013-10-15 12:04:13 UTC
  • mfrom: (46.1.83 nova-compute)
  • Revision ID: james.page@canonical.com-20131015120413-grclbw2ot5gbgp5r
Update of all Havana / Saucy / python-redux work:

* Full python rewrite using new OpenStack charm-helpers.

* Test coverage

* Havana support

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from mock import patch, MagicMock, call
 
2
 
 
3
from test_utils import CharmTestCase, patch_open
 
4
 
 
5
 
 
6
import nova_compute_utils as utils
 
7
 
 
8
TO_PATCH = [
 
9
    'config',
 
10
    'os_release',
 
11
    'log',
 
12
    'neutron_plugin_attribute',
 
13
    'related_units',
 
14
    'relation_ids',
 
15
    'relation_get',
 
16
]
 
17
 
 
18
OVS_PKGS = [
 
19
    'quantum-plugin-openvswitch-agent',
 
20
    'openvswitch-datapath-dkms',
 
21
]
 
22
 
 
23
 
 
24
class NovaComputeUtilsTests(CharmTestCase):
 
25
    def setUp(self):
 
26
        super(NovaComputeUtilsTests, self).setUp(utils, TO_PATCH)
 
27
        self.config.side_effect = self.test_config.get
 
28
 
 
29
    @patch.object(utils, 'network_manager')
 
30
    def test_determine_packages_nova_network(self, net_man):
 
31
        net_man.return_value = 'flatdhcpmanager'
 
32
        self.relation_ids.return_value = []
 
33
        result = utils.determine_packages()
 
34
        ex = utils.BASE_PACKAGES + [
 
35
            'nova-api',
 
36
            'nova-network',
 
37
            'nova-compute-kvm'
 
38
        ]
 
39
        self.assertEquals(ex, result)
 
40
 
 
41
    @patch.object(utils, 'neutron_plugin')
 
42
    @patch.object(utils, 'network_manager')
 
43
    def test_determine_packages_quantum(self, net_man, n_plugin):
 
44
        self.neutron_plugin_attribute.return_value = OVS_PKGS
 
45
        net_man.return_value = 'quantum'
 
46
        n_plugin.return_value = 'ovs'
 
47
        self.relation_ids.return_value = []
 
48
        result = utils.determine_packages()
 
49
        ex = utils.BASE_PACKAGES + OVS_PKGS + ['nova-compute-kvm']
 
50
        self.assertEquals(ex, result)
 
51
 
 
52
    @patch.object(utils, 'neutron_plugin')
 
53
    @patch.object(utils, 'network_manager')
 
54
    def test_determine_packages_quantum_ceph(self, net_man, n_plugin):
 
55
        self.neutron_plugin_attribute.return_value = OVS_PKGS
 
56
        net_man.return_value = 'quantum'
 
57
        n_plugin.return_value = 'ovs'
 
58
        self.relation_ids.return_value = ['ceph:0']
 
59
        result = utils.determine_packages()
 
60
        ex = (utils.BASE_PACKAGES + OVS_PKGS +
 
61
              ['ceph-common', 'nova-compute-kvm'])
 
62
        self.assertEquals(ex, result)
 
63
 
 
64
    @patch.object(utils, 'network_manager')
 
65
    def test_resource_map_nova_network_no_multihost(self, net_man):
 
66
        self.skipTest('skipped until contexts are properly mocked')
 
67
        self.test_config.set('multi-host', 'no')
 
68
        net_man.return_value = 'FlatDHCPManager'
 
69
        result = utils.resource_map()
 
70
        ex = {
 
71
            '/etc/default/libvirt-bin': {
 
72
                'contexts': [],
 
73
                'services': ['libvirt-bin']
 
74
            },
 
75
            '/etc/libvirt/qemu.conf': {
 
76
                'contexts': [],
 
77
                'services': ['libvirt-bin']
 
78
            },
 
79
            '/etc/nova/nova-compute.conf': {
 
80
                'contexts': [],
 
81
                'services': ['nova-compute']
 
82
            },
 
83
            '/etc/nova/nova.conf': {
 
84
                'contexts': [],
 
85
                'services': ['nova-compute']
 
86
            },
 
87
        }
 
88
        self.assertEquals(ex, result)
 
89
 
 
90
    @patch.object(utils, 'network_manager')
 
91
    def test_resource_map_nova_network(self, net_man):
 
92
 
 
93
        self.skipTest('skipped until contexts are properly mocked')
 
94
        net_man.return_value = 'FlatDHCPManager'
 
95
        result = utils.resource_map()
 
96
        ex = {
 
97
            '/etc/default/libvirt-bin': {
 
98
                'contexts': [], 'services': ['libvirt-bin']
 
99
            },
 
100
            '/etc/libvirt/qemu.conf': {
 
101
                'contexts': [],
 
102
                'services': ['libvirt-bin']
 
103
            },
 
104
            '/etc/nova/nova-compute.conf': {
 
105
                'contexts': [],
 
106
                'services': ['nova-compute']
 
107
            },
 
108
            '/etc/nova/nova.conf': {
 
109
                'contexts': [],
 
110
                'services': ['nova-compute', 'nova-api', 'nova-network']
 
111
            }
 
112
        }
 
113
        self.assertEquals(ex, result)
 
114
 
 
115
    @patch.object(utils, 'neutron_plugin')
 
116
    @patch.object(utils, 'network_manager')
 
117
    def test_resource_map_quantum_ovs(self, net_man, _plugin):
 
118
        self.skipTest('skipped until contexts are properly mocked.')
 
119
        net_man.return_value = 'Quantum'
 
120
        _plugin.return_value = 'ovs'
 
121
        result = utils.resource_map()
 
122
        ex = {
 
123
            '/etc/default/libvirt-bin': {
 
124
                'contexts': [],
 
125
                'services': ['libvirt-bin']
 
126
            },
 
127
            '/etc/libvirt/qemu.conf': {
 
128
                'contexts': [],
 
129
                'services': ['libvirt-bin']
 
130
            },
 
131
            '/etc/nova/nova-compute.conf': {
 
132
                'contexts': [],
 
133
                'services': ['nova-compute']
 
134
            },
 
135
            '/etc/nova/nova.conf': {
 
136
                'contexts': [],
 
137
                'services': ['nova-compute']
 
138
            },
 
139
            '/etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini': {
 
140
                'contexts': [],
 
141
                'services': ['quantum-plugin-openvswitch-agent']
 
142
            },
 
143
            '/etc/quantum/quantum.conf': {
 
144
                'contexts': [],
 
145
                'services': ['quantum-plugin-openvswitch-agent']}
 
146
            }
 
147
 
 
148
        self.assertEquals(ex, result)
 
149
 
 
150
    def fake_user(self, username='foo'):
 
151
        user = MagicMock()
 
152
        user.pw_dir = '/home/' + username
 
153
        return user
 
154
 
 
155
    @patch('__builtin__.open')
 
156
    @patch('pwd.getpwnam')
 
157
    def test_public_ssh_key_not_found(self, getpwnam, _open):
 
158
        _open.side_effect = Exception
 
159
        getpwnam.return_value = self.fake_user('foo')
 
160
        self.assertEquals(None, utils.public_ssh_key())
 
161
 
 
162
    @patch('pwd.getpwnam')
 
163
    def test_public_ssh_key(self, getpwnam):
 
164
        getpwnam.return_value = self.fake_user('foo')
 
165
        with patch_open() as (_open, _file):
 
166
            _file.read.return_value = 'mypubkey'
 
167
            result = utils.public_ssh_key('foo')
 
168
        self.assertEquals(result, 'mypubkey')
 
169
 
 
170
    def test_import_authorized_keys_missing_data(self):
 
171
        self.relation_get.return_value = None
 
172
        with patch_open() as (_open, _file):
 
173
            utils.import_authorized_keys(user='foo')
 
174
            self.assertFalse(_open.called)
 
175
 
 
176
    @patch('pwd.getpwnam')
 
177
    def test_import_authorized_keys(self, getpwnam):
 
178
        getpwnam.return_value = self.fake_user('foo')
 
179
        self.relation_get.side_effect = [
 
180
            'Zm9vX2tleQo=',  # relation_get('known_hosts')
 
181
            'Zm9vX2hvc3QK',  # relation_get('authorized_keys')
 
182
        ]
 
183
 
 
184
        ex_open = [
 
185
            call('/home/foo/.ssh/authorized_keys', 'wb'),
 
186
            call('/home/foo/.ssh/known_hosts', 'wb')
 
187
        ]
 
188
        ex_write = [
 
189
            call('foo_host\n'),
 
190
            call('foo_key\n'),
 
191
        ]
 
192
 
 
193
        with patch_open() as (_open, _file):
 
194
            utils.import_authorized_keys(user='foo')
 
195
            self.assertEquals(ex_open, _open.call_args_list)
 
196
            self.assertEquals(ex_write, _file.write.call_args_list)
 
197
 
 
198
    @patch('subprocess.check_call')
 
199
    def test_import_keystone_cert_missing_data(self, check_call):
 
200
        self.relation_get.return_value = None
 
201
        with patch_open() as (_open, _file):
 
202
            utils.import_keystone_ca_cert()
 
203
            self.assertFalse(_open.called)
 
204
        self.assertFalse(check_call.called)
 
205
 
 
206
    @patch.object(utils, 'check_call')
 
207
    def test_import_keystone_cert(self, check_call):
 
208
        self.relation_get.return_value = 'Zm9vX2NlcnQK'
 
209
        with patch_open() as (_open, _file):
 
210
            utils.import_keystone_ca_cert()
 
211
            _open.assert_called_with(utils.CA_CERT_PATH, 'wb')
 
212
            _file.write.assert_called_with('foo_cert\n')
 
213
        check_call.assert_called_with(['update-ca-certificates'])
 
214
 
 
215
    @patch('charmhelpers.contrib.openstack.templating.OSConfigRenderer')
 
216
    @patch.object(utils, 'quantum_enabled')
 
217
    @patch.object(utils, 'resource_map')
 
218
    def test_register_configs(self, resource_map, quantum, renderer):
 
219
        quantum.return_value = False
 
220
        self.os_release.return_value = 'havana'
 
221
        fake_renderer = MagicMock()
 
222
        fake_renderer.register = MagicMock()
 
223
        renderer.return_value = fake_renderer
 
224
        ctxt1 = MagicMock()
 
225
        ctxt2 = MagicMock()
 
226
        rsc_map = {
 
227
            '/etc/nova/nova.conf': {
 
228
                'services': ['nova-compute'],
 
229
                'contexts': [ctxt1],
 
230
            },
 
231
            '/etc/nova/nova-compute.conf': {
 
232
                'services': ['nova-compute'],
 
233
                'contexts': [ctxt2],
 
234
            },
 
235
        }
 
236
        resource_map.return_value = rsc_map
 
237
        utils.register_configs()
 
238
        renderer.assert_called_with(
 
239
            openstack_release='havana', templates_dir='templates/')
 
240
        ex_reg = [
 
241
            call('/etc/nova/nova-compute.conf', [ctxt2]),
 
242
            call('/etc/nova/nova.conf', [ctxt1])
 
243
        ]
 
244
        self.assertEquals(fake_renderer.register.call_args_list, ex_reg)