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

« back to all changes in this revision

Viewing changes to cinder/tests/api/openstack/volume/contrib/test_volume_tenant_attribute.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-11-23 08:39:28 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20121123083928-xvzet603cjfj9p1t
Tags: 2013.1~g1-0ubuntu1
* New upstream release.
* debian/patches/avoid_setuptools_git_dependency.patch:
  Avoid git installation. (LP: #1075948) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#   Copyright 2012 OpenStack LLC.
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
 
import json
16
 
import datetime
17
 
 
18
 
from lxml import etree
19
 
import webob
20
 
 
21
 
from cinder import context
22
 
from cinder import test
23
 
from cinder import utils
24
 
from cinder import volume
25
 
from cinder.tests.api.openstack import fakes
26
 
 
27
 
PROJECT_ID = '88fd1da4-f464-4a87-9ce5-26f2f40743b9'
28
 
 
29
 
 
30
 
def fake_volume_get(*args, **kwargs):
31
 
    return {
32
 
        'id': 'fake',
33
 
        'host': 'host001',
34
 
        'status': 'available',
35
 
        'size': 5,
36
 
        'availability_zone': 'somewhere',
37
 
        'created_at': datetime.datetime.now(),
38
 
        'attach_status': None,
39
 
        'display_name': 'anothervolume',
40
 
        'display_description': 'Just another volume!',
41
 
        'volume_type_id': None,
42
 
        'snapshot_id': None,
43
 
        'project_id': PROJECT_ID,
44
 
    }
45
 
 
46
 
 
47
 
def fake_volume_get_all(*args, **kwargs):
48
 
    return [fake_volume_get()]
49
 
 
50
 
 
51
 
def app():
52
 
    # no auth, just let environ['cinder.context'] pass through
53
 
    api = fakes.volume.APIRouter()
54
 
    mapper = fakes.urlmap.URLMap()
55
 
    mapper['/v1'] = api
56
 
    return mapper
57
 
 
58
 
 
59
 
class VolumeTenantAttributeTest(test.TestCase):
60
 
 
61
 
    def setUp(self):
62
 
        super(VolumeTenantAttributeTest, self).setUp()
63
 
        self.stubs.Set(volume.API, 'get', fake_volume_get)
64
 
        self.stubs.Set(volume.API, 'get_all', fake_volume_get_all)
65
 
        self.UUID = utils.gen_uuid()
66
 
 
67
 
    def test_get_volume_allowed(self):
68
 
        ctx = context.RequestContext('admin', 'fake', True)
69
 
        req = webob.Request.blank('/v1/fake/volumes/%s' % self.UUID)
70
 
        req.method = 'GET'
71
 
        req.environ['cinder.context'] = ctx
72
 
        res = req.get_response(app())
73
 
        vol = json.loads(res.body)['volume']
74
 
        self.assertEqual(vol['os-vol-tenant-attr:tenant_id'], PROJECT_ID)
75
 
 
76
 
    def test_get_volume_unallowed(self):
77
 
        ctx = context.RequestContext('non-admin', 'fake', False)
78
 
        req = webob.Request.blank('/v1/fake/volumes/%s' % self.UUID)
79
 
        req.method = 'GET'
80
 
        req.environ['cinder.context'] = ctx
81
 
        res = req.get_response(app())
82
 
        vol = json.loads(res.body)['volume']
83
 
        self.assertFalse('os-vol-tenant-attr:tenant_id' in vol)
84
 
 
85
 
    def test_list_detail_volumes_allowed(self):
86
 
        ctx = context.RequestContext('admin', 'fake', True)
87
 
        req = webob.Request.blank('/v1/fake/volumes/detail')
88
 
        req.method = 'GET'
89
 
        req.environ['cinder.context'] = ctx
90
 
        res = req.get_response(app())
91
 
        vol = json.loads(res.body)['volumes']
92
 
        self.assertEqual(vol[0]['os-vol-tenant-attr:tenant_id'], PROJECT_ID)
93
 
 
94
 
    def test_list_detail_volumes_unallowed(self):
95
 
        ctx = context.RequestContext('non-admin', 'fake', False)
96
 
        req = webob.Request.blank('/v1/fake/volumes/detail')
97
 
        req.method = 'GET'
98
 
        req.environ['cinder.context'] = ctx
99
 
        res = req.get_response(app())
100
 
        vol = json.loads(res.body)['volumes']
101
 
        self.assertFalse('os-vol-tenant-attr:tenant_id' in vol[0])
102
 
 
103
 
    def test_list_simple_volumes_no_tenant_id(self):
104
 
        ctx = context.RequestContext('admin', 'fake', True)
105
 
        req = webob.Request.blank('/v1/fake/volumes')
106
 
        req.method = 'GET'
107
 
        req.environ['cinder.context'] = ctx
108
 
        res = req.get_response(app())
109
 
        vol = json.loads(res.body)['volumes']
110
 
        self.assertFalse('os-vol-tenant-attr:tenant_id' in vol[0])
111
 
 
112
 
    def test_get_volume_xml(self):
113
 
        ctx = context.RequestContext('admin', 'fake', True)
114
 
        req = webob.Request.blank('/v1/fake/volumes/%s' % self.UUID)
115
 
        req.method = 'GET'
116
 
        req.accept = 'application/xml'
117
 
        req.environ['cinder.context'] = ctx
118
 
        res = req.get_response(app())
119
 
        vol = etree.XML(res.body)
120
 
        tenant_key = ('{http://docs.openstack.org/volume/ext/'
121
 
                    'volume_tenant_attribute/api/v1}tenant_id')
122
 
        self.assertEqual(vol.get(tenant_key), PROJECT_ID)
123
 
 
124
 
    def test_list_volumes_detail_xml(self):
125
 
        ctx = context.RequestContext('admin', 'fake', True)
126
 
        req = webob.Request.blank('/v1/fake/volumes/detail')
127
 
        req.method = 'GET'
128
 
        req.accept = 'application/xml'
129
 
        req.environ['cinder.context'] = ctx
130
 
        res = req.get_response(app())
131
 
        vol = list(etree.XML(res.body))[0]
132
 
        tenant_key = ('{http://docs.openstack.org/volume/ext/'
133
 
                       'volume_tenant_attribute/api/v1}tenant_id')
134
 
        self.assertEqual(vol.get(tenant_key), PROJECT_ID)