51
class ServiceManagerTestCase(test.TestCase):
51
class ServiceManagerTestCase(test.BaseTestCase):
52
52
"""Test cases for Services"""
54
def test_attribute_error_for_no_manager(self):
55
serv = service.Service('test',
58
'nova.tests.service_unittest.FakeManager')
59
self.assertRaises(AttributeError, getattr, serv, 'test_method')
54
61
def test_message_gets_to_manager(self):
55
62
serv = service.Service('test',
58
'nova.tests.test_service.FakeManager')
65
'nova.tests.service_unittest.FakeManager')
60
67
self.assertEqual(serv.test_method(), 'manager')
62
69
def test_override_manager_method(self):
63
70
serv = ExtendedService('test',
66
'nova.tests.test_service.FakeManager')
73
'nova.tests.service_unittest.FakeManager')
68
75
self.assertEqual(serv.test_method(), 'service')
71
class ServiceFlagsTestCase(test.TestCase):
72
def test_service_enabled_on_create_based_on_flag(self):
73
self.flags(enable_new_services=True)
76
app = service.Service.create(host=host, binary=binary)
79
ref = db.service_get(context.get_admin_context(), app.service_id)
80
db.service_destroy(context.get_admin_context(), app.service_id)
81
self.assert_(not ref['disabled'])
83
def test_service_disabled_on_create_based_on_flag(self):
84
self.flags(enable_new_services=False)
87
app = service.Service.create(host=host, binary=binary)
90
ref = db.service_get(context.get_admin_context(), app.service_id)
91
db.service_destroy(context.get_admin_context(), app.service_id)
92
self.assert_(ref['disabled'])
95
class ServiceTestCase(test.TestCase):
78
class ServiceTestCase(test.BaseTestCase):
96
79
"""Test cases for Services"""
99
82
super(ServiceTestCase, self).setUp()
100
83
self.mox.StubOutWithMock(service, 'db')
84
self.context = context.get_admin_context()
102
86
def test_create(self):
107
91
# NOTE(vish): Create was moved out of mox replay to make sure that
108
92
# the looping calls are created in StartService.
109
app = service.Service.create(host=host, binary=binary, topic=topic)
93
app = service.Service.create(host=host, binary=binary)
95
self.mox.StubOutWithMock(rpc,
97
use_mock_anything=True)
98
self.mox.StubOutWithMock(
99
service.task, 'LoopingCall', use_mock_anything=True)
100
rpc.AdapterConsumer(connection=mox.IgnoreArg(),
102
proxy=mox.IsA(service.Service)).AndReturn(
105
rpc.AdapterConsumer(connection=mox.IgnoreArg(),
106
topic='%s.%s' % (topic, host),
107
proxy=mox.IsA(service.Service)).AndReturn(
110
rpc.AdapterConsumer.attach_to_twisted()
111
rpc.AdapterConsumer.attach_to_twisted()
113
# Stub out looping call a bit needlessly since we don't have an easy
114
# way to cancel it (yet) when the tests finishes
115
service.task.LoopingCall(mox.IgnoreArg()).AndReturn(
116
service.task.LoopingCall)
117
service.task.LoopingCall.start(interval=mox.IgnoreArg(),
119
service.task.LoopingCall(mox.IgnoreArg()).AndReturn(
120
service.task.LoopingCall)
121
service.task.LoopingCall.start(interval=mox.IgnoreArg(),
124
service_create = {'host': host,
128
service_ref = {'host': host,
133
service.db.service_get_by_args(mox.IgnoreArg(),
135
binary).AndRaise(exception.NotFound())
136
service.db.service_create(mox.IgnoreArg(),
137
service_create).AndReturn(service_ref)
140
startApplication(app, False)
111
141
self.assert_(app)
143
# We're testing sort of weird behavior in how report_state decides
144
# whether it is disconnected, it looks for a variable on itself called
145
# 'model_disconnected' and report_state doesn't really do much so this
146
# these are mostly just for coverage
147
def test_report_state(self):
150
service_ref = {'host': host,
154
service.db.__getattr__('report_state')
155
service.db.service_get_by_args(self.context,
157
binary).AndReturn(service_ref)
158
service.db.service_update(self.context, service_ref['id'],
159
mox.ContainsKeyValue('report_count', 1))
162
s = service.Service()
163
rv = yield s.report_state(host, binary)
165
def test_report_state_no_service(self):
168
service_create = {'host': host,
171
service_ref = {'host': host,
176
service.db.__getattr__('report_state')
177
service.db.service_get_by_args(self.context,
179
binary).AndRaise(exception.NotFound())
180
service.db.service_create(self.context,
181
service_create).AndReturn(service_ref)
182
service.db.service_get(self.context, service_ref['id']).AndReturn(service_ref)
183
service.db.service_update(self.context, service_ref['id'],
184
mox.ContainsKeyValue('report_count', 1))
187
s = service.Service()
188
rv = yield s.report_state(host, binary)
113
190
def test_report_state_newly_disconnected(self):
117
service_create = {'host': host,
121
'availability_zone': 'nova'}
122
193
service_ref = {'host': host,
126
'availability_zone': 'nova',
129
service.db.service_get_by_args(mox.IgnoreArg(),
131
binary).AndRaise(exception.NotFound())
132
service.db.service_create(mox.IgnoreArg(),
133
service_create).AndReturn(service_ref)
134
service.db.service_get(mox.IgnoreArg(),
135
mox.IgnoreArg()).AndRaise(Exception())
198
service.db.__getattr__('report_state')
199
service.db.service_get_by_args(self.context,
201
binary).AndRaise(Exception())
137
203
self.mox.ReplayAll()
138
serv = service.Service(host,
141
'nova.tests.test_service.FakeManager')
144
self.assert_(serv.model_disconnected)
204
s = service.Service()
205
rv = yield s.report_state(host, binary)
207
self.assert_(s.model_disconnected)
146
209
def test_report_state_newly_connected(self):
150
service_create = {'host': host,
154
'availability_zone': 'nova'}
155
212
service_ref = {'host': host,
159
'availability_zone': 'nova',
162
service.db.service_get_by_args(mox.IgnoreArg(),
164
binary).AndRaise(exception.NotFound())
165
service.db.service_create(mox.IgnoreArg(),
166
service_create).AndReturn(service_ref)
167
service.db.service_get(mox.IgnoreArg(),
168
service_ref['id']).AndReturn(service_ref)
169
service.db.service_update(mox.IgnoreArg(), service_ref['id'],
217
service.db.__getattr__('report_state')
218
service.db.service_get_by_args(self.context,
220
binary).AndReturn(service_ref)
221
service.db.service_update(self.context, service_ref['id'],
170
222
mox.ContainsKeyValue('report_count', 1))
172
224
self.mox.ReplayAll()
173
serv = service.Service(host,
176
'nova.tests.test_service.FakeManager')
178
serv.model_disconnected = True
181
self.assert_(not serv.model_disconnected)
184
class TestWSGIService(test.TestCase):
187
super(TestWSGIService, self).setUp()
188
self.stubs.Set(wsgi.Loader, "load_app", mox.MockAnything())
190
def test_service_random_port(self):
191
test_service = service.WSGIService("test_service")
192
self.assertEquals(0, test_service.port)
194
self.assertNotEqual(0, test_service.port)
198
class TestLauncher(test.TestCase):
201
super(TestLauncher, self).setUp()
202
self.stubs.Set(wsgi.Loader, "load_app", mox.MockAnything())
203
self.service = service.WSGIService("test_service")
205
def test_launch_app(self):
206
self.assertEquals(0, self.service.port)
207
launcher = service.Launcher()
208
launcher.launch_server(self.service)
209
self.assertEquals(0, self.service.port)
225
s = service.Service()
226
s.model_disconnected = True
227
rv = yield s.report_state(host, binary)
229
self.assert_(not s.model_disconnected)