1
# Copyright 2012 Nebula, Inc.
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
from lxml import etree
18
from nova.compute import instance_types
19
from nova import flags
20
from nova.openstack.common import jsonutils
22
from nova.tests.api.openstack import fakes
46
def fake_instance_type_get_by_flavor_id(flavorid):
47
return FAKE_FLAVORS['flavor %s' % flavorid]
50
def fake_instance_type_get_all(*args, **kwargs):
54
class FlavorSwapTest(test.TestCase):
55
content_type = 'application/json'
59
super(FlavorSwapTest, self).setUp()
60
ext = ('nova.api.openstack.compute.contrib'
61
'.flavor_swap.Flavor_swap')
62
self.flags(osapi_compute_extension=[ext])
63
fakes.stub_out_nw_api(self.stubs)
64
self.stubs.Set(instance_types, "get_all_types",
65
fake_instance_type_get_all)
66
self.stubs.Set(instance_types,
67
"get_instance_type_by_flavor_id",
68
fake_instance_type_get_by_flavor_id)
70
def _make_request(self, url):
71
req = webob.Request.blank(url)
72
req.headers['Accept'] = self.content_type
73
res = req.get_response(fakes.wsgi_app())
76
def _get_flavor(self, body):
77
return jsonutils.loads(body).get('flavor')
79
def _get_flavors(self, body):
80
return jsonutils.loads(body).get('flavors')
82
def assertFlavorSwap(self, flavor, swap):
83
self.assertEqual(str(flavor.get('%sswap' % self.prefix)), swap)
86
url = '/v2/fake/flavors/1'
87
res = self._make_request(url)
89
self.assertEqual(res.status_int, 200)
90
self.assertFlavorSwap(self._get_flavor(res.body), '512')
92
def test_detail(self):
93
url = '/v2/fake/flavors/detail'
94
res = self._make_request(url)
96
self.assertEqual(res.status_int, 200)
97
flavors = self._get_flavors(res.body)
98
self.assertFlavorSwap(flavors[0], '512')
99
self.assertFlavorSwap(flavors[1], '')
102
class FlavorSwapXmlTest(FlavorSwapTest):
103
content_type = 'application/xml'
105
def _get_flavor(self, body):
106
return etree.XML(body)
108
def _get_flavors(self, body):
109
return etree.XML(body).getchildren()