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.api.openstack.compute.contrib import flavor_disabled
19
from nova.compute import instance_types
20
from nova import flags
21
from nova.openstack.common import jsonutils
23
from nova.tests.api.openstack import fakes
47
def fake_instance_type_get_by_flavor_id(flavorid):
48
return FAKE_FLAVORS['flavor %s' % flavorid]
51
def fake_instance_type_get_all(*args, **kwargs):
55
class FlavorDisabledTest(test.TestCase):
56
content_type = 'application/json'
57
prefix = '%s:' % flavor_disabled.Flavor_disabled.alias
60
super(FlavorDisabledTest, self).setUp()
61
ext = ('nova.api.openstack.compute.contrib'
62
'.flavor_disabled.Flavor_disabled')
63
self.flags(osapi_compute_extension=[ext])
64
fakes.stub_out_nw_api(self.stubs)
65
self.stubs.Set(instance_types, "get_all_types",
66
fake_instance_type_get_all)
67
self.stubs.Set(instance_types,
68
"get_instance_type_by_flavor_id",
69
fake_instance_type_get_by_flavor_id)
71
def _make_request(self, url):
72
req = webob.Request.blank(url)
73
req.headers['Accept'] = self.content_type
74
res = req.get_response(fakes.wsgi_app())
77
def _get_flavor(self, body):
78
return jsonutils.loads(body).get('flavor')
80
def _get_flavors(self, body):
81
return jsonutils.loads(body).get('flavors')
83
def assertFlavorDisabled(self, flavor, disabled):
84
self.assertEqual(str(flavor.get('%sdisabled' % self.prefix)), disabled)
87
url = '/v2/fake/flavors/1'
88
res = self._make_request(url)
90
self.assertEqual(res.status_int, 200)
91
self.assertFlavorDisabled(self._get_flavor(res.body), 'False')
93
def test_detail(self):
94
url = '/v2/fake/flavors/detail'
95
res = self._make_request(url)
97
self.assertEqual(res.status_int, 200)
98
flavors = self._get_flavors(res.body)
99
self.assertFlavorDisabled(flavors[0], 'False')
100
self.assertFlavorDisabled(flavors[1], 'True')
103
class FlavorDisabledXmlTest(FlavorDisabledTest):
104
content_type = 'application/xml'
105
prefix = '{%s}' % flavor_disabled.Flavor_disabled.namespace
107
def _get_flavor(self, body):
108
return etree.XML(body)
110
def _get_flavors(self, body):
111
return etree.XML(body).getchildren()