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

« back to all changes in this revision

Viewing changes to cinder/tests/api/contrib/test_extended_snapshot_attributes.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
# All Rights Reserved.
 
3
#
 
4
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
5
#    not use this file except in compliance with the License. You may obtain
 
6
#    a copy of the License at
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
#    License for the specific language governing permissions and limitations
 
14
#    under the License.
 
15
 
 
16
from lxml import etree
 
17
import webob
 
18
 
 
19
from cinder.api.contrib import extended_snapshot_attributes
 
20
from cinder import exception
 
21
from cinder import flags
 
22
from cinder.openstack.common import jsonutils
 
23
from cinder import test
 
24
from cinder.tests.api.openstack import fakes
 
25
from cinder import volume
 
26
 
 
27
 
 
28
FLAGS = flags.FLAGS
 
29
 
 
30
 
 
31
UUID1 = '00000000-0000-0000-0000-000000000001'
 
32
UUID2 = '00000000-0000-0000-0000-000000000002'
 
33
 
 
34
 
 
35
def _get_default_snapshot_param():
 
36
    return {
 
37
        'id': UUID1,
 
38
        'volume_id': 12,
 
39
        'status': 'available',
 
40
        'volume_size': 100,
 
41
        'created_at': None,
 
42
        'display_name': 'Default name',
 
43
        'display_description': 'Default description',
 
44
        'project_id': 'fake',
 
45
        'progress': '0%'
 
46
        }
 
47
 
 
48
 
 
49
def fake_snapshot_get(self, context, snapshot_id):
 
50
    param = _get_default_snapshot_param()
 
51
    return param
 
52
 
 
53
 
 
54
def fake_snapshot_get_all(self, context, search_opts=None):
 
55
    param = _get_default_snapshot_param()
 
56
    return [param]
 
57
 
 
58
 
 
59
class ExtendedSnapshotAttributesTest(test.TestCase):
 
60
    content_type = 'application/json'
 
61
    prefix = 'os-extended-snapshot-attributes:'
 
62
 
 
63
    def setUp(self):
 
64
        super(ExtendedSnapshotAttributesTest, self).setUp()
 
65
        self.stubs.Set(volume.api.API, 'get_snapshot', fake_snapshot_get)
 
66
        self.stubs.Set(volume.api.API, 'get_all_snapshots',
 
67
                       fake_snapshot_get_all)
 
68
 
 
69
    def _make_request(self, url):
 
70
        req = webob.Request.blank(url)
 
71
        req.headers['Accept'] = self.content_type
 
72
        res = req.get_response(fakes.wsgi_app())
 
73
        return res
 
74
 
 
75
    def _get_snapshot(self, body):
 
76
        return jsonutils.loads(body).get('snapshot')
 
77
 
 
78
    def _get_snapshots(self, body):
 
79
        return jsonutils.loads(body).get('snapshots')
 
80
 
 
81
    def assertSnapshotAttributes(self, snapshot, project_id, progress):
 
82
        self.assertEqual(snapshot.get('%sproject_id' % self.prefix),
 
83
                                      project_id)
 
84
        self.assertEqual(snapshot.get('%sprogress' % self.prefix), progress)
 
85
 
 
86
    def test_show(self):
 
87
        url = '/v1/fake/snapshots/%s' % UUID2
 
88
        res = self._make_request(url)
 
89
 
 
90
        self.assertEqual(res.status_int, 200)
 
91
        self.assertSnapshotAttributes(self._get_snapshot(res.body),
 
92
                                project_id='fake',
 
93
                                progress='0%')
 
94
 
 
95
    def test_detail(self):
 
96
        url = '/v1/fake/snapshots/detail'
 
97
        res = self._make_request(url)
 
98
 
 
99
        self.assertEqual(res.status_int, 200)
 
100
        for i, snapshot in enumerate(self._get_snapshots(res.body)):
 
101
            self.assertSnapshotAttributes(snapshot,
 
102
                                    project_id='fake',
 
103
                                    progress='0%')
 
104
 
 
105
    def test_no_instance_passthrough_404(self):
 
106
 
 
107
        def fake_snapshot_get(*args, **kwargs):
 
108
            raise exception.InstanceNotFound()
 
109
 
 
110
        self.stubs.Set(volume.api.API, 'get_snapshot', fake_snapshot_get)
 
111
        url = '/v1/fake/snapshots/70f6db34-de8d-4fbd-aafb-4065bdfa6115'
 
112
        res = self._make_request(url)
 
113
 
 
114
        self.assertEqual(res.status_int, 404)
 
115
 
 
116
 
 
117
class ExtendedSnapshotAttributesXmlTest(ExtendedSnapshotAttributesTest):
 
118
    content_type = 'application/xml'
 
119
    ext = extended_snapshot_attributes
 
120
    prefix = '{%s}' % ext.Extended_snapshot_attributes.namespace
 
121
 
 
122
    def _get_snapshot(self, body):
 
123
        return etree.XML(body)
 
124
 
 
125
    def _get_snapshots(self, body):
 
126
        return etree.XML(body).getchildren()