1
# Copyright 2011 OpenStack Foundation
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
16
from lxml import etree
19
from nova.api.openstack.compute.contrib import extended_availability_zone
20
from nova import availability_zones
21
from nova import compute
22
from nova import exception
23
from nova.openstack.common import jsonutils
25
from nova.tests.api.openstack import fakes
27
UUID1 = '00000000-0000-0000-0000-000000000001'
28
UUID2 = '00000000-0000-0000-0000-000000000002'
29
UUID3 = '00000000-0000-0000-0000-000000000003'
32
def fake_compute_get(*args, **kwargs):
33
inst = fakes.stub_instance(1, uuid=UUID3, host="get-host")
37
def fake_compute_get_all(*args, **kwargs):
38
inst1 = fakes.stub_instance(1, uuid=UUID1, host="all-host")
39
inst2 = fakes.stub_instance(2, uuid=UUID2, host="all-host")
43
def fake_get_host_availability_zone(context, host):
47
class ExtendedServerAttributesTest(test.TestCase):
48
content_type = 'application/json'
52
super(ExtendedServerAttributesTest, self).setUp()
53
fakes.stub_out_nw_api(self.stubs)
54
self.stubs.Set(compute.api.API, 'get', fake_compute_get)
55
self.stubs.Set(compute.api.API, 'get_all', fake_compute_get_all)
56
self.stubs.Set(availability_zones, 'get_host_availability_zone',
57
fake_get_host_availability_zone)
60
osapi_compute_extension=[
61
'nova.api.openstack.compute.contrib.select_extensions'],
62
osapi_compute_ext_list=['Extended_availability_zone'])
64
def _make_request(self, url):
65
req = webob.Request.blank(url)
66
req.headers['Accept'] = self.content_type
67
res = req.get_response(fakes.wsgi_app(init_only=('servers',)))
70
def _get_server(self, body):
71
return jsonutils.loads(body).get('server')
73
def _get_servers(self, body):
74
return jsonutils.loads(body).get('servers')
76
def assertServerAttributes(self, server, az):
77
self.assertEqual(server.get('%savailability_zone' % self.prefix),
81
url = '/v2/fake/servers/%s' % UUID3
82
res = self._make_request(url)
84
self.assertEqual(res.status_int, 200)
85
self.assertServerAttributes(self._get_server(res.body), 'get-host')
87
def test_detail(self):
88
url = '/v2/fake/servers/detail'
89
res = self._make_request(url)
91
self.assertEqual(res.status_int, 200)
92
for i, server in enumerate(self._get_servers(res.body)):
93
self.assertServerAttributes(server, 'all-host')
95
def test_no_instance_passthrough_404(self):
97
def fake_compute_get(*args, **kwargs):
98
raise exception.InstanceNotFound(instance_id='fake')
100
self.stubs.Set(compute.api.API, 'get', fake_compute_get)
101
url = '/v2/fake/servers/70f6db34-de8d-4fbd-aafb-4065bdfa6115'
102
res = self._make_request(url)
104
self.assertEqual(res.status_int, 404)
107
class ExtendedServerAttributesXmlTest(ExtendedServerAttributesTest):
108
content_type = 'application/xml'
109
prefix = '{%s}' % extended_availability_zone.\
110
Extended_availability_zone.namespace
112
def _get_server(self, body):
113
return etree.XML(body)
115
def _get_servers(self, body):
116
return etree.XML(body).getchildren()