~0x44/nova/bug838466

« back to all changes in this revision

Viewing changes to nova/tests/service_unittest.py

  • Committer: Eric Day
  • Date: 2010-10-21 18:49:51 UTC
  • mto: This revision was merged to the branch mainline in revision 377.
  • Revision ID: eday@oddments.org-20101021184951-x0vs3s8y7mc0aeyy
PEP8 and pylint cleanup. There should be no functional changes here, just style changes to get violations down.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
import mox
24
24
 
 
25
from twisted.application.app import startApplication
 
26
 
25
27
from nova import context
26
 
from nova import db
27
28
from nova import exception
28
29
from nova import flags
29
30
from nova import rpc
30
31
from nova import test
31
32
from nova import service
32
33
from nova import manager
33
 
from nova import wsgi
34
 
from nova.compute import manager as compute_manager
35
34
 
36
 
flags.DEFINE_string("fake_manager", "nova.tests.test_service.FakeManager",
 
35
FLAGS = flags.FLAGS
 
36
flags.DEFINE_string("fake_manager", "nova.tests.service_unittest.FakeManager",
37
37
                    "Manager for testing")
38
38
 
39
39
 
48
48
        return 'service'
49
49
 
50
50
 
51
 
class ServiceManagerTestCase(test.TestCase):
 
51
class ServiceManagerTestCase(test.BaseTestCase):
52
52
    """Test cases for Services"""
53
53
 
 
54
    def test_attribute_error_for_no_manager(self):
 
55
        serv = service.Service('test',
 
56
                               'test',
 
57
                               'test',
 
58
                               'nova.tests.service_unittest.FakeManager')
 
59
        self.assertRaises(AttributeError, getattr, serv, 'test_method')
 
60
 
54
61
    def test_message_gets_to_manager(self):
55
62
        serv = service.Service('test',
56
63
                               'test',
57
64
                               'test',
58
 
                               'nova.tests.test_service.FakeManager')
59
 
        serv.start()
 
65
                               'nova.tests.service_unittest.FakeManager')
 
66
        serv.startService()
60
67
        self.assertEqual(serv.test_method(), 'manager')
61
68
 
62
69
    def test_override_manager_method(self):
63
70
        serv = ExtendedService('test',
64
71
                               'test',
65
72
                               'test',
66
 
                               'nova.tests.test_service.FakeManager')
67
 
        serv.start()
 
73
                               'nova.tests.service_unittest.FakeManager')
 
74
        serv.startService()
68
75
        self.assertEqual(serv.test_method(), 'service')
69
76
 
70
77
 
71
 
class ServiceFlagsTestCase(test.TestCase):
72
 
    def test_service_enabled_on_create_based_on_flag(self):
73
 
        self.flags(enable_new_services=True)
74
 
        host = 'foo'
75
 
        binary = 'nova-fake'
76
 
        app = service.Service.create(host=host, binary=binary)
77
 
        app.start()
78
 
        app.stop()
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'])
82
 
 
83
 
    def test_service_disabled_on_create_based_on_flag(self):
84
 
        self.flags(enable_new_services=False)
85
 
        host = 'foo'
86
 
        binary = 'nova-fake'
87
 
        app = service.Service.create(host=host, binary=binary)
88
 
        app.start()
89
 
        app.stop()
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'])
93
 
 
94
 
 
95
 
class ServiceTestCase(test.TestCase):
 
78
class ServiceTestCase(test.BaseTestCase):
96
79
    """Test cases for Services"""
97
80
 
98
81
    def setUp(self):
99
82
        super(ServiceTestCase, self).setUp()
100
83
        self.mox.StubOutWithMock(service, 'db')
 
84
        self.context = context.get_admin_context()
101
85
 
102
86
    def test_create(self):
103
87
        host = 'foo'
106
90
 
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)
110
 
 
 
93
        app = service.Service.create(host=host, binary=binary)
 
94
 
 
95
        self.mox.StubOutWithMock(rpc,
 
96
                                 'AdapterConsumer',
 
97
                                 use_mock_anything=True)
 
98
        self.mox.StubOutWithMock(
 
99
                service.task, 'LoopingCall', use_mock_anything=True)
 
100
        rpc.AdapterConsumer(connection=mox.IgnoreArg(),
 
101
                            topic=topic,
 
102
                            proxy=mox.IsA(service.Service)).AndReturn(
 
103
                                    rpc.AdapterConsumer)
 
104
 
 
105
        rpc.AdapterConsumer(connection=mox.IgnoreArg(),
 
106
                            topic='%s.%s' % (topic, host),
 
107
                            proxy=mox.IsA(service.Service)).AndReturn(
 
108
                                    rpc.AdapterConsumer)
 
109
 
 
110
        rpc.AdapterConsumer.attach_to_twisted()
 
111
        rpc.AdapterConsumer.attach_to_twisted()
 
112
 
 
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(),
 
118
                                       now=mox.IgnoreArg())
 
119
        service.task.LoopingCall(mox.IgnoreArg()).AndReturn(
 
120
                        service.task.LoopingCall)
 
121
        service.task.LoopingCall.start(interval=mox.IgnoreArg(),
 
122
                                       now=mox.IgnoreArg())
 
123
 
 
124
        service_create = {'host': host,
 
125
                          'binary': binary,
 
126
                          'topic': topic,
 
127
                          'report_count': 0}
 
128
        service_ref = {'host': host,
 
129
                       'binary': binary,
 
130
                       'report_count': 0,
 
131
                       'id': 1}
 
132
 
 
133
        service.db.service_get_by_args(mox.IgnoreArg(),
 
134
                                       host,
 
135
                                       binary).AndRaise(exception.NotFound())
 
136
        service.db.service_create(mox.IgnoreArg(),
 
137
                                  service_create).AndReturn(service_ref)
 
138
        self.mox.ReplayAll()
 
139
 
 
140
        startApplication(app, False)
111
141
        self.assert_(app)
112
142
 
 
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):
 
148
        host = 'foo'
 
149
        binary = 'bar'
 
150
        service_ref = {'host': host,
 
151
                       'binary': binary,
 
152
                       'report_count': 0,
 
153
                       'id': 1}
 
154
        service.db.__getattr__('report_state')
 
155
        service.db.service_get_by_args(self.context,
 
156
                                       host,
 
157
                                       binary).AndReturn(service_ref)
 
158
        service.db.service_update(self.context, service_ref['id'],
 
159
                                  mox.ContainsKeyValue('report_count', 1))
 
160
 
 
161
        self.mox.ReplayAll()
 
162
        s = service.Service()
 
163
        rv = yield s.report_state(host, binary)
 
164
 
 
165
    def test_report_state_no_service(self):
 
166
        host = 'foo'
 
167
        binary = 'bar'
 
168
        service_create = {'host': host,
 
169
                          'binary': binary,
 
170
                          'report_count': 0}
 
171
        service_ref = {'host': host,
 
172
                       'binary': binary,
 
173
                       'report_count': 0,
 
174
                       'id': 1}
 
175
 
 
176
        service.db.__getattr__('report_state')
 
177
        service.db.service_get_by_args(self.context,
 
178
                                      host,
 
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))
 
185
 
 
186
        self.mox.ReplayAll()
 
187
        s = service.Service()
 
188
        rv = yield s.report_state(host, binary)
 
189
 
113
190
    def test_report_state_newly_disconnected(self):
114
191
        host = 'foo'
115
192
        binary = 'bar'
116
 
        topic = 'test'
117
 
        service_create = {'host': host,
118
 
                          'binary': binary,
119
 
                          'topic': topic,
120
 
                          'report_count': 0,
121
 
                          'availability_zone': 'nova'}
122
193
        service_ref = {'host': host,
123
 
                          'binary': binary,
124
 
                          'topic': topic,
125
 
                          'report_count': 0,
126
 
                          'availability_zone': 'nova',
127
 
                          'id': 1}
 
194
                       'binary': binary,
 
195
                       'report_count': 0,
 
196
                       'id': 1}
128
197
 
129
 
        service.db.service_get_by_args(mox.IgnoreArg(),
130
 
                                      host,
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,
 
200
                                       host,
 
201
                                       binary).AndRaise(Exception())
136
202
 
137
203
        self.mox.ReplayAll()
138
 
        serv = service.Service(host,
139
 
                               binary,
140
 
                               topic,
141
 
                               'nova.tests.test_service.FakeManager')
142
 
        serv.start()
143
 
        serv.report_state()
144
 
        self.assert_(serv.model_disconnected)
 
204
        s = service.Service()
 
205
        rv = yield s.report_state(host, binary)
 
206
 
 
207
        self.assert_(s.model_disconnected)
145
208
 
146
209
    def test_report_state_newly_connected(self):
147
210
        host = 'foo'
148
211
        binary = 'bar'
149
 
        topic = 'test'
150
 
        service_create = {'host': host,
151
 
                          'binary': binary,
152
 
                          'topic': topic,
153
 
                          'report_count': 0,
154
 
                          'availability_zone': 'nova'}
155
212
        service_ref = {'host': host,
156
 
                          'binary': binary,
157
 
                          'topic': topic,
158
 
                          'report_count': 0,
159
 
                          'availability_zone': 'nova',
160
 
                          'id': 1}
 
213
                       'binary': binary,
 
214
                       'report_count': 0,
 
215
                       'id': 1}
161
216
 
162
 
        service.db.service_get_by_args(mox.IgnoreArg(),
163
 
                                      host,
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,
 
219
                                       host,
 
220
                                       binary).AndReturn(service_ref)
 
221
        service.db.service_update(self.context, service_ref['id'],
170
222
                                  mox.ContainsKeyValue('report_count', 1))
171
223
 
172
224
        self.mox.ReplayAll()
173
 
        serv = service.Service(host,
174
 
                               binary,
175
 
                               topic,
176
 
                               'nova.tests.test_service.FakeManager')
177
 
        serv.start()
178
 
        serv.model_disconnected = True
179
 
        serv.report_state()
180
 
 
181
 
        self.assert_(not serv.model_disconnected)
182
 
 
183
 
 
184
 
class TestWSGIService(test.TestCase):
185
 
 
186
 
    def setUp(self):
187
 
        super(TestWSGIService, self).setUp()
188
 
        self.stubs.Set(wsgi.Loader, "load_app", mox.MockAnything())
189
 
 
190
 
    def test_service_random_port(self):
191
 
        test_service = service.WSGIService("test_service")
192
 
        self.assertEquals(0, test_service.port)
193
 
        test_service.start()
194
 
        self.assertNotEqual(0, test_service.port)
195
 
        test_service.stop()
196
 
 
197
 
 
198
 
class TestLauncher(test.TestCase):
199
 
 
200
 
    def setUp(self):
201
 
        super(TestLauncher, self).setUp()
202
 
        self.stubs.Set(wsgi.Loader, "load_app", mox.MockAnything())
203
 
        self.service = service.WSGIService("test_service")
204
 
 
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)
210
 
        launcher.stop()
 
225
        s = service.Service()
 
226
        s.model_disconnected = True
 
227
        rv = yield s.report_state(host, binary)
 
228
 
 
229
        self.assert_(not s.model_disconnected)
 
230