1
from mock import MagicMock
2
from copy import deepcopy
3
from tests.test_utils import CharmTestCase
5
import hooks.nova_compute_context as context
8
'get_os_codename_package',
10
'filter_installed_packages',
20
'network_manager': 'quantum',
21
'quantum_auth_strategy': 'keystone',
22
'keystone_host': 'keystone_host',
24
'quantum_url': 'http://quantum_url',
25
'service_tenant': 'admin',
26
'service_username': 'admin',
27
'service_password': 'openstack',
28
'quantum_security_groups': 'yes',
29
'quantum_plugin': 'ovs',
32
# Context for an OVS plugin contains at least the following. Other bits
33
# (driver names) are dependent on OS release.
34
BASE_QUANTUM_OVS_PLUGIN_CONTEXT = {
35
'core_plugin': 'quantum.plugins.openvswitch.ovs_quantum_plugin.'\
37
'enable_tunneling': True,
38
'libvirt_use_virtio_for_bridges': True,
39
'local_ip': '10.0.0.1',
40
'nova_firewall_driver': 'nova.virt.firewall.NoopFirewallDriver',
41
'ovs_firewall_driver': 'quantum.agent.linux.iptables_firewall.'\
42
'OVSHybridIptablesFirewallDriver',
43
'tenant_network_type': 'gre',
44
'tunnel_id_ranges': '1:1000',
45
'quantum_plugin': 'ovs',
46
'quantum_security_groups': True,
49
def fake_log(msg, level=None):
50
level = level or 'INFO'
51
print '[juju test log (%s)] %s' % (level, msg)
53
class NovaComputeContextTests(CharmTestCase):
55
super(NovaComputeContextTests, self).setUp(context, TO_PATCH)
56
self.relation_get.side_effect = self.test_relation.get
57
self.config.side_effect = self.test_config.get
58
self.log.side_effect = fake_log
60
def test_cloud_compute_context_no_relation(self):
61
self.relation_ids.return_value = []
62
cloud_compute = context.CloudComputeContext()
63
self.assertEquals({}, cloud_compute())
65
def test_cloud_compute_volume_context_cinder(self):
66
self.relation_ids.return_value = 'cloud-compute:0'
67
cloud_compute = context.CloudComputeContext()
69
self.test_relation.set({'volume_service': 'cinder'})
70
result = cloud_compute()
72
'volume_service_config': {
73
'volume_api_class': 'nova.volume.cinder.API'
76
self.assertEquals(ex_ctxt, result)
78
def test_cloud_compute_volume_context_nova_vol(self):
79
self.relation_ids.return_value = 'cloud-compute:0'
80
cloud_compute = context.CloudComputeContext()
81
self.get_os_codename_package.return_value = 'essex'
82
self.test_relation.set({'volume_service': 'nova-volume'})
83
result = cloud_compute()
85
'volume_service_config': {
86
'volume_api_class': 'nova.volume.api.API'
89
self.assertEquals(ex_ctxt, result)
92
def test_cloud_compute_volume_context_nova_vol_unsupported(self):
93
self.relation_ids.return_value = 'cloud-compute:0'
94
cloud_compute = context.CloudComputeContext()
95
# n-vol doesn't exist in grizzly
96
self.get_os_codename_package.return_value = 'grizzly'
97
self.test_relation.set({'volume_service': 'nova-volume'})
98
result = cloud_compute()
99
self.assertEquals({}, result)
101
def test_cloud_compute_flatdhcp_context(self):
102
self.test_relation.set({
103
'network_manager': 'FlatDHCPManager',
104
'ec2_host': 'novaapihost'})
105
cloud_compute = context.CloudComputeContext()
107
'network_manager_config': {
108
'network_manager': 'nova.network.manager.FlatDHCPManager',
109
'ec2_dmz_host': 'novaapihost',
110
'flat_interface': 'eth1'
113
self.assertEquals(ex_ctxt, cloud_compute())
115
def test_cloud_compute_quantum_context(self):
116
self.test_relation.set(QUANTUM_CONTEXT)
117
cloud_compute = context.CloudComputeContext()
118
ex_ctxt = { 'network_manager_config': {
120
'keystone_host': 'keystone_host',
121
'network_api_class': 'nova.network.quantumv2.api.API',
122
'quantum_admin_auth_url': 'http://keystone_host:5000/v2.0',
123
'quantum_admin_password': 'openstack',
124
'quantum_admin_tenant_name': 'admin',
125
'quantum_admin_username': 'admin',
126
'quantum_auth_strategy': 'keystone',
127
'quantum_plugin': 'ovs',
128
'quantum_security_groups': 'yes',
129
'quantum_url': 'http://quantum_url'
132
self.assertEquals(ex_ctxt, cloud_compute())
133
self._save_flag_file.assert_called_with(
134
path='/etc/nova/nm.conf', data='quantum')
136
def test_quantum_plugin_context_no_setting(self):
137
qplugin = context.QuantumPluginContext()
138
self.assertEquals({}, qplugin())
140
def _test_qplugin_context(self, os_release):
141
self.get_os_codename_package.return_value = os_release
142
self.unit_private_ip.return_value = '10.0.0.1'
143
self.test_relation.set(
144
{'quantum_plugin': 'ovs', 'quantum_security_groups': 'yes'})
145
qplugin = context.QuantumPluginContext()
146
qplugin._ensure_packages = MagicMock()
149
def test_quantum_plugin_context_ovs_folsom(self):
150
ex_ctxt = deepcopy(BASE_QUANTUM_OVS_PLUGIN_CONTEXT)
151
ex_ctxt['libvirt_vif_driver'] = ('nova.virt.libvirt.vif.'
152
'LibvirtHybridOVSBridgeDriver')
153
self.assertEquals(ex_ctxt, self._test_qplugin_context('folsom'))
154
self._save_flag_file.assert_called_with(
155
path='/etc/nova/quantum_plugin.conf', data='ovs')
157
def test_quantum_plugin_context_ovs_grizzly_and_beyond(self):
158
ex_ctxt = deepcopy(BASE_QUANTUM_OVS_PLUGIN_CONTEXT)
159
ex_ctxt['libvirt_vif_driver'] = ('nova.virt.libvirt.vif.'
160
'LibvirtGenericVIFDriver')
161
self.assertEquals(ex_ctxt, self._test_qplugin_context('grizzly'))
162
self._save_flag_file.assert_called_with(
163
path='/etc/nova/quantum_plugin.conf', data='ovs')
165
def test_libvirt_bin_context_no_migration(self):
166
self.test_config.set('enable-live-migration', 'false')
167
libvirt = context.NovaComputeLibvirtContext()
168
self.assertEquals({'libvirtd_opts': '-d'}, libvirt())
170
def test_libvirt_bin_context_migration_tcp_listen(self):
171
self.test_config.set('enable-live-migration', 'true')
172
libvirt = context.NovaComputeLibvirtContext()
173
self.assertEquals({'libvirtd_opts': '-d -l'}, libvirt())