~ubuntu-cloud-archive/ubuntu/precise/nova/trunk

« back to all changes in this revision

Viewing changes to nova/tests/api/openstack/compute/contrib/test_flavor_disabled.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Soren Hansen
  • Date: 2012-09-07 17:49:53 UTC
  • mfrom: (1.1.61)
  • Revision ID: package-import@ubuntu.com-20120907174953-oapuvix1jxm830he
Tags: 2012.2~rc1~20120907.15996-0ubuntu1
[ Chuck Short ]
* New upstream release.
* debian/nova-common.postinst: Drop nova_sudoers permission changing
  since we do it in the debian/rules. (LP: #995285)

[ Soren Hansen ]
* Update debian/watch to account for symbolically named tarballs and
  use newer URL.
* Fix Launchpad URLs in debian/watch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Nebula, Inc.
 
2
#
 
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
 
6
#
 
7
#         http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
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
 
13
#    under the License.
 
14
 
 
15
from lxml import etree
 
16
import webob
 
17
 
 
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
 
22
from nova import test
 
23
from nova.tests.api.openstack import fakes
 
24
 
 
25
 
 
26
FLAGS = flags.FLAGS
 
27
 
 
28
 
 
29
FAKE_FLAVORS = {
 
30
    'flavor 1': {
 
31
        "flavorid": '1',
 
32
        "name": 'flavor 1',
 
33
        "memory_mb": '256',
 
34
        "root_gb": '10',
 
35
        "disabled": False,
 
36
    },
 
37
    'flavor 2': {
 
38
        "flavorid": '2',
 
39
        "name": 'flavor 2',
 
40
        "memory_mb": '512',
 
41
        "root_gb": '20',
 
42
        "disabled": True,
 
43
    },
 
44
}
 
45
 
 
46
 
 
47
def fake_instance_type_get_by_flavor_id(flavorid):
 
48
    return FAKE_FLAVORS['flavor %s' % flavorid]
 
49
 
 
50
 
 
51
def fake_instance_type_get_all(*args, **kwargs):
 
52
    return FAKE_FLAVORS
 
53
 
 
54
 
 
55
class FlavorDisabledTest(test.TestCase):
 
56
    content_type = 'application/json'
 
57
    prefix = '%s:' % flavor_disabled.Flavor_disabled.alias
 
58
 
 
59
    def setUp(self):
 
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)
 
70
 
 
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())
 
75
        return res
 
76
 
 
77
    def _get_flavor(self, body):
 
78
        return jsonutils.loads(body).get('flavor')
 
79
 
 
80
    def _get_flavors(self, body):
 
81
        return jsonutils.loads(body).get('flavors')
 
82
 
 
83
    def assertFlavorDisabled(self, flavor, disabled):
 
84
        self.assertEqual(str(flavor.get('%sdisabled' % self.prefix)), disabled)
 
85
 
 
86
    def test_show(self):
 
87
        url = '/v2/fake/flavors/1'
 
88
        res = self._make_request(url)
 
89
 
 
90
        self.assertEqual(res.status_int, 200)
 
91
        self.assertFlavorDisabled(self._get_flavor(res.body), 'False')
 
92
 
 
93
    def test_detail(self):
 
94
        url = '/v2/fake/flavors/detail'
 
95
        res = self._make_request(url)
 
96
 
 
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')
 
101
 
 
102
 
 
103
class FlavorDisabledXmlTest(FlavorDisabledTest):
 
104
    content_type = 'application/xml'
 
105
    prefix = '{%s}' % flavor_disabled.Flavor_disabled.namespace
 
106
 
 
107
    def _get_flavor(self, body):
 
108
        return etree.XML(body)
 
109
 
 
110
    def _get_flavors(self, body):
 
111
        return etree.XML(body).getchildren()