1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
22
from nova import context
23
from nova import flags
25
from nova.compute import api
26
from nova.tests.api.openstack import fakes
37
STOP = datetime.datetime.utcnow()
38
START = STOP - datetime.timedelta(hours=HOURS)
41
def fake_instance_type_get(self, context, instance_type_id):
45
'memory_mb': MEMORY_MB,
50
def get_fake_db_instance(start, end, instance_id, tenant_id):
51
return {'id': instance_id,
53
'project_id': tenant_id,
54
'user_id': 'fakeuser',
55
'display_name': 'name',
56
'state_description': 'state',
57
'instance_type_id': 1,
62
def fake_instance_get_active_by_window(self, context, begin, end, project_id):
63
return [get_fake_db_instance(START,
66
"faketenant_%s" % (x / SERVERS))
67
for x in xrange(TENANTS * SERVERS)]
70
class SimpleTenantUsageTest(test.TestCase):
72
super(SimpleTenantUsageTest, self).setUp()
73
self.stubs.Set(api.API, "get_instance_type",
74
fake_instance_type_get)
75
self.stubs.Set(api.API, "get_active_by_window",
76
fake_instance_get_active_by_window)
77
self.admin_context = context.RequestContext('fakeadmin_0',
80
self.user_context = context.RequestContext('fakeadmin_0',
83
self.alt_user_context = context.RequestContext('fakeadmin_0',
86
FLAGS.allow_admin_api = True
88
def test_verify_index(self):
89
req = webob.Request.blank(
90
'/v1.1/123/os-simple-tenant-usage?start=%s&end=%s' %
91
(START.isoformat(), STOP.isoformat()))
93
req.headers["content-type"] = "application/json"
95
res = req.get_response(fakes.wsgi_app(
96
fake_auth_context=self.admin_context))
98
self.assertEqual(res.status_int, 200)
99
res_dict = json.loads(res.body)
100
usages = res_dict['tenant_usages']
101
from nova import log as logging
103
for i in xrange(TENANTS):
104
self.assertEqual(int(usages[i]['total_hours']),
106
self.assertEqual(int(usages[i]['total_local_gb_usage']),
107
SERVERS * LOCAL_GB * HOURS)
108
self.assertEqual(int(usages[i]['total_memory_mb_usage']),
109
SERVERS * MEMORY_MB * HOURS)
110
self.assertEqual(int(usages[i]['total_vcpus_usage']),
111
SERVERS * VCPUS * HOURS)
112
self.assertFalse(usages[i].get('server_usages'))
114
def test_verify_detailed_index(self):
115
req = webob.Request.blank(
116
'/v1.1/123/os-simple-tenant-usage?'
117
'detailed=1&start=%s&end=%s' %
118
(START.isoformat(), STOP.isoformat()))
120
req.headers["content-type"] = "application/json"
122
res = req.get_response(fakes.wsgi_app(
123
fake_auth_context=self.admin_context))
124
self.assertEqual(res.status_int, 200)
125
res_dict = json.loads(res.body)
126
usages = res_dict['tenant_usages']
127
for i in xrange(TENANTS):
128
servers = usages[i]['server_usages']
129
for j in xrange(SERVERS):
130
self.assertEqual(int(servers[j]['hours']), HOURS)
132
def test_verify_index_fails_for_nonadmin(self):
133
req = webob.Request.blank(
134
'/v1.1/123/os-simple-tenant-usage?'
135
'detailed=1&start=%s&end=%s' %
136
(START.isoformat(), STOP.isoformat()))
138
req.headers["content-type"] = "application/json"
140
res = req.get_response(fakes.wsgi_app())
141
self.assertEqual(res.status_int, 403)
143
def test_verify_show(self):
144
req = webob.Request.blank(
145
'/v1.1/faketenant_0/os-simple-tenant-usage/'
146
'faketenant_0?start=%s&end=%s' %
147
(START.isoformat(), STOP.isoformat()))
149
req.headers["content-type"] = "application/json"
151
res = req.get_response(fakes.wsgi_app(
152
fake_auth_context=self.user_context))
153
self.assertEqual(res.status_int, 200)
154
res_dict = json.loads(res.body)
156
usage = res_dict['tenant_usage']
157
servers = usage['server_usages']
158
self.assertEqual(len(usage['server_usages']), SERVERS)
159
for j in xrange(SERVERS):
160
self.assertEqual(int(servers[j]['hours']), HOURS)
162
def test_verify_show_cant_view_other_tenant(self):
163
req = webob.Request.blank(
164
'/v1.1/faketenant_1/os-simple-tenant-usage/'
165
'faketenant_0?start=%s&end=%s' %
166
(START.isoformat(), STOP.isoformat()))
168
req.headers["content-type"] = "application/json"
170
res = req.get_response(fakes.wsgi_app(
171
fake_auth_context=self.alt_user_context))
172
self.assertEqual(res.status_int, 403)