4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
17
from datetime import datetime
18
from nova.api.openstack.compute.contrib import services
19
from nova import context
21
from nova import exception
22
from nova.openstack.common import timeutils
24
from nova.tests.api.openstack import fakes
27
fake_services_list = [{'binary': 'nova-scheduler',
29
'availability_zone': 'nova',
32
'updated_at': datetime(2012, 10, 29, 13, 42, 2),
33
'created_at': datetime(2012, 9, 18, 2, 46, 27)},
34
{'binary': 'nova-compute',
36
'availability_zone': 'nova',
39
'updated_at': datetime(2012, 10, 29, 13, 42, 5),
40
'created_at': datetime(2012, 9, 18, 2, 46, 27)},
41
{'binary': 'nova-scheduler',
43
'availability_zone': 'nova',
46
'updated_at': datetime(2012, 9, 19, 6, 55, 34),
47
'created_at': datetime(2012, 9, 18, 2, 46, 28)},
48
{'binary': 'nova-compute',
50
'availability_zone': 'nova',
53
'updated_at': datetime(2012, 9, 18, 8, 3, 38),
54
'created_at': datetime(2012, 9, 18, 2, 46, 28)},
58
class FakeRequest(object):
59
environ = {"nova.context": context.get_admin_context()}
63
class FakeRequestWithSevice(object):
64
environ = {"nova.context": context.get_admin_context()}
65
GET = {"service": "nova-compute"}
68
class FakeRequestWithHost(object):
69
environ = {"nova.context": context.get_admin_context()}
70
GET = {"host": "host1"}
73
class FakeRequestWithHostService(object):
74
environ = {"nova.context": context.get_admin_context()}
75
GET = {"host": "host1", "service": "nova-compute"}
78
def fake_servcie_get_all(context):
79
return fake_services_list
82
def fake_service_get_by_host_binary(context, host, binary):
83
for service in fake_services_list:
84
if service['host'] == host and service['binary'] == binary:
89
def fake_service_get_by_id(value):
90
for service in fake_services_list:
91
if service['id'] == value:
96
def fake_service_update(context, service_id, values):
97
service = fake_service_get_by_id(service_id)
99
raise exception.ServiceNotFound(service_id=service_id)
101
{'host': 'host1', 'service': 'nova-compute',
102
'disabled': values['disabled']}
106
return datetime(2012, 10, 29, 13, 42, 11)
109
class ServicesTest(test.TestCase):
112
super(ServicesTest, self).setUp()
114
self.stubs.Set(db, "service_get_all", fake_servcie_get_all)
115
self.stubs.Set(timeutils, "utcnow", fake_utcnow)
116
self.stubs.Set(db, "service_get_by_args",
117
fake_service_get_by_host_binary)
118
self.stubs.Set(db, "service_update", fake_service_update)
120
self.context = context.get_admin_context()
121
self.controller = services.ServiceController()
124
super(ServicesTest, self).tearDown()
126
def test_services_list(self):
128
res_dict = self.controller.index(req)
130
response = {'services': [{'binary': 'nova-scheduler',
131
'host': 'host1', 'zone': 'nova',
132
'status': 'disabled', 'state': 'up',
133
'updated_at': datetime(2012, 10, 29, 13, 42, 2)},
134
{'binary': 'nova-compute',
135
'host': 'host1', 'zone': 'nova',
136
'status': 'disabled', 'state': 'up',
137
'updated_at': datetime(2012, 10, 29, 13, 42, 5)},
138
{'binary': 'nova-scheduler', 'host': 'host2',
140
'status': 'enabled', 'state': 'down',
141
'updated_at': datetime(2012, 9, 19, 6, 55, 34)},
142
{'binary': 'nova-compute', 'host': 'host2',
144
'status': 'disabled', 'state': 'down',
145
'updated_at': datetime(2012, 9, 18, 8, 3, 38)}]}
146
self.assertEqual(res_dict, response)
148
def test_services_list_with_host(self):
149
req = FakeRequestWithHost()
150
res_dict = self.controller.index(req)
152
response = {'services': [{'binary': 'nova-scheduler', 'host': 'host1',
154
'status': 'disabled', 'state': 'up',
155
'updated_at': datetime(2012, 10, 29, 13, 42, 2)},
156
{'binary': 'nova-compute', 'host': 'host1',
158
'status': 'disabled', 'state': 'up',
159
'updated_at': datetime(2012, 10, 29, 13, 42, 5)}]}
160
self.assertEqual(res_dict, response)
162
def test_services_list_with_service(self):
163
req = FakeRequestWithSevice()
164
res_dict = self.controller.index(req)
166
response = {'services': [{'binary': 'nova-compute', 'host': 'host1',
168
'status': 'disabled', 'state': 'up',
169
'updated_at': datetime(2012, 10, 29, 13, 42, 5)},
170
{'binary': 'nova-compute', 'host': 'host2',
172
'status': 'disabled', 'state': 'down',
173
'updated_at': datetime(2012, 9, 18, 8, 3, 38)}]}
174
self.assertEqual(res_dict, response)
176
def test_services_list_with_host_service(self):
177
req = FakeRequestWithHostService()
178
res_dict = self.controller.index(req)
180
response = {'services': [{'binary': 'nova-compute', 'host': 'host1',
182
'status': 'disabled', 'state': 'up',
183
'updated_at': datetime(2012, 10, 29, 13, 42, 5)}]}
184
self.assertEqual(res_dict, response)
186
def test_services_enable(self):
187
body = {'host': 'host1', 'service': 'nova-compute'}
188
req = fakes.HTTPRequest.blank('/v2/fake/os-services/enable')
189
res_dict = self.controller.update(req, "enable", body)
191
self.assertEqual(res_dict['disabled'], False)
193
def test_services_disable(self):
194
req = fakes.HTTPRequest.blank('/v2/fake/os-services/disable')
195
body = {'host': 'host1', 'service': 'nova-compute'}
196
res_dict = self.controller.update(req, "disable", body)
198
self.assertEqual(res_dict['disabled'], True)