1
# Copyright 2012 IBM Corp.
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
15
"""Tests for the conductor service"""
17
from nova.compute import instance_types
18
from nova.compute import vm_states
19
from nova import conductor
20
from nova.conductor import api as conductor_api
21
from nova.conductor import manager as conductor_manager
22
from nova.conductor import rpcapi as conductor_rpcapi
23
from nova import context
25
from nova import notifications
29
FAKE_IMAGE_REF = 'fake-image-ref'
32
class BaseTestCase(test.TestCase):
34
super(BaseTestCase, self).setUp()
36
self.project_id = 'fake'
37
self.context = context.RequestContext(self.user_id,
40
def _create_fake_instance(self, params=None, type_name='m1.tiny'):
45
inst['vm_state'] = vm_states.ACTIVE
46
inst['image_ref'] = FAKE_IMAGE_REF
47
inst['reservation_id'] = 'r-fakeres'
48
inst['launch_time'] = '10'
49
inst['user_id'] = self.user_id
50
inst['project_id'] = self.project_id
51
inst['host'] = 'fake_host'
52
type_id = instance_types.get_instance_type_by_name(type_name)['id']
53
inst['instance_type_id'] = type_id
54
inst['ami_launch_index'] = 0
58
inst['ephemeral_gb'] = 0
59
inst['architecture'] = 'x86_64'
60
inst['os_type'] = 'Linux'
62
return db.instance_create(self.context, inst)
65
class ConductorTestCase(BaseTestCase):
66
"""Conductor Manager Tests"""
68
super(ConductorTestCase, self).setUp()
69
self.conductor = conductor_manager.ConductorManager()
72
def _do_update(self, instance_uuid, **updates):
73
return self.conductor.instance_update(self.context, instance_uuid,
76
def test_instance_update(self):
77
instance = self._create_fake_instance()
78
new_inst = self._do_update(instance['uuid'],
79
vm_state=vm_states.STOPPED)
80
instance = db.instance_get_by_uuid(self.context, instance['uuid'])
81
self.assertEqual(instance['vm_state'], vm_states.STOPPED)
82
self.assertEqual(new_inst['vm_state'], instance['vm_state'])
84
def test_instance_update_invalid_key(self):
85
# NOTE(danms): the real DB API call ignores invalid keys
87
self.assertRaises(KeyError,
88
self._do_update, 'any-uuid', foobar=1)
91
class ConductorRPCAPITestCase(ConductorTestCase):
92
"""Conductor RPC API Tests"""
94
super(ConductorRPCAPITestCase, self).setUp()
95
self.conductor_service = self.start_service(
96
'conductor', manager='nova.conductor.manager.ConductorManager')
97
self.conductor = conductor_rpcapi.ConductorAPI()
100
class ConductorLocalAPITestCase(ConductorTestCase):
101
"""Conductor LocalAPI Tests"""
103
super(ConductorLocalAPITestCase, self).setUp()
104
self.conductor = conductor_api.LocalAPI()
107
def _do_update(self, instance_uuid, **updates):
108
# NOTE(danms): the public API takes actual keyword arguments,
109
# so override the base class here to make the call correctly
110
return self.conductor.instance_update(self.context, instance_uuid,
114
class ConductorAPITestCase(ConductorLocalAPITestCase):
115
"""Conductor API Tests"""
117
super(ConductorAPITestCase, self).setUp()
118
self.conductor_service = self.start_service(
119
'conductor', manager='nova.conductor.manager.ConductorManager')
120
self.conductor = conductor_api.API()
124
class ConductorImportTest(test.TestCase):
125
def test_import_conductor_local(self):
126
self.flags(use_local=True, group='conductor')
127
self.assertTrue(isinstance(conductor.API(),
128
conductor_api.LocalAPI))
130
def test_import_conductor_rpc(self):
131
self.flags(use_local=False, group='conductor')
132
self.assertTrue(isinstance(conductor.API(),