summaryrefslogtreecommitdiff
path: root/glance/tests/functional/v2/test_metadef_resourcetypes.py
blob: b1769747afc92344b2dd17b353822e971366bbe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from oslo_serialization import jsonutils
import requests
from six.moves import http_client as http

from glance.tests.functional.v2 import metadef_base


class TestMetadefResourceTypes(metadef_base.MetadefFunctionalTestBase):

    def setUp(self):
        super(TestMetadefResourceTypes, self).setUp()
        self.cleanup()
        self.api_server.deployment_flavor = 'noauth'
        self.start_servers(**self.__dict__.copy())

    def _url(self, path):
        return 'http://127.0.0.1:%d%s' % (self.api_port, path)

    def _headers(self, custom_headers=None):
        base_headers = {
            'X-Identity-Status': 'Confirmed',
            'X-Auth-Token': '932c5c84-02ac-4fe5-a9ba-620af0e2bb96',
            'X-User-Id': 'f9a41d13-0c13-47e9-bee2-ce4e8bfe958e',
            'X-Tenant-Id': self.tenant1,
            'X-Roles': 'admin',
        }
        base_headers.update(custom_headers or {})
        return base_headers

    def test_metadef_resource_types_lifecycle(self):
        # Namespace should not exist
        path = self._url('/v2/metadefs/namespaces/MyNamespace')
        response = requests.get(path, headers=self._headers())
        self.assertEqual(http.NOT_FOUND, response.status_code)

        # Create a namespace
        path = self._url('/v2/metadefs/namespaces')
        headers = self._headers({'content-type': 'application/json'})
        namespace_name = 'MyNamespace'
        data = jsonutils.dumps({
            "namespace": namespace_name,
            "display_name": "My User Friendly Namespace",
            "description": "My description",
            "visibility": "public",
            "protected": False,
            "owner": "The Test Owner"
        })
        response = requests.post(path, headers=headers, data=data)
        self.assertEqual(http.CREATED, response.status_code)

        # Resource type should not exist
        path = self._url('/v2/metadefs/namespaces/%s/resource_types' %
                         (namespace_name))
        response = requests.get(path, headers=self._headers())
        metadef_resource_type = jsonutils.loads(response.text)
        self.assertEqual(
            0, len(metadef_resource_type['resource_type_associations']))

        # Create a resource type
        path = self._url('/v2/metadefs/namespaces/MyNamespace/resource_types')
        headers = self._headers({'content-type': 'application/json'})
        metadef_resource_type_name = "resource_type1"
        data = jsonutils.dumps(
            {
                "name": "resource_type1",
                "prefix": "hw_",
                "properties_target": "image",
            }
        )
        response = requests.post(path, headers=headers, data=data)
        self.assertEqual(http.CREATED, response.status_code)

        # Attempt to insert a duplicate
        response = requests.post(path, headers=headers, data=data)
        self.assertEqual(http.CONFLICT, response.status_code)

        # Get the metadef resource type created above
        path = self._url('/v2/metadefs/namespaces/%s/resource_types' %
                         (namespace_name))
        response = requests.get(path,
                                headers=self._headers())
        self.assertEqual(http.OK, response.status_code)
        metadef_resource_type = jsonutils.loads(response.text)
        self.assertEqual(
            "resource_type1",
            metadef_resource_type['resource_type_associations'][0]['name'])

        # Returned resource type should match the created resource type
        resource_type = jsonutils.loads(response.text)
        checked_keys = set([
            u'name',
            u'prefix',
            u'properties_target',
            u'created_at',
            u'updated_at'
        ])
        self.assertEqual(
            set(resource_type['resource_type_associations'][0].keys()),
            checked_keys)
        expected_metadef_resource_types = {
            "name": metadef_resource_type_name,
            "prefix": "hw_",
            "properties_target": "image",
        }

        # Simple key values
        checked_values = set([
            u'name',
            u'prefix',
            u'properties_target',
        ])
        for key, value in expected_metadef_resource_types.items():
            if(key in checked_values):
                self.assertEqual(
                    resource_type['resource_type_associations'][0][key],
                    value, key)

        # Deassociate of metadef resource type resource_type1
        path = self._url('/v2/metadefs/namespaces/%s/resource_types/%s' %
                         (namespace_name, metadef_resource_type_name))
        response = requests.delete(path, headers=self._headers())
        self.assertEqual(http.NO_CONTENT, response.status_code)

        # resource_type1 should not exist
        path = self._url('/v2/metadefs/namespaces/%s/resource_types' %
                         (namespace_name))
        response = requests.get(path, headers=self._headers())
        metadef_resource_type = jsonutils.loads(response.text)
        self.assertEqual(
            0, len(metadef_resource_type['resource_type_associations']))

    def _create_resource_type(self, namespaces):
        resource_types = []
        for namespace in namespaces:
            headers = self._headers({'X-Tenant-Id': namespace['owner']})
            data = {
                "name": "resource_type_of_%s" % (namespace['namespace']),
                "prefix": "hw_",
                "properties_target": "image"
            }
            path = self._url('/v2/metadefs/namespaces/%s/resource_types' %
                             (namespace['namespace']))
            response = requests.post(path, headers=headers, json=data)
            self.assertEqual(http.CREATED, response.status_code)
            rs_type = response.json()
            resource_type = dict()
            resource_type[namespace['namespace']] = rs_type['name']
            resource_types.append(resource_type)

        return resource_types

    def test_role_base_metadef_resource_types_lifecycle(self):
        # Create public and private namespaces for tenant1 and tenant2
        path = self._url('/v2/metadefs/namespaces')
        headers = self._headers({'content-type': 'application/json'})
        tenant1_namespaces = []
        tenant2_namespaces = []
        for tenant in [self.tenant1, self.tenant2]:
            headers['X-Tenant-Id'] = tenant
            for visibility in ['public', 'private']:
                namespace_data = {
                    "namespace": "%s_%s_namespace" % (tenant, visibility),
                    "display_name": "My User Friendly Namespace",
                    "description": "My description",
                    "visibility": visibility,
                    "owner": tenant
                }
                namespace = self.create_namespace(path, headers,
                                                  namespace_data)
                self.assertNamespacesEqual(namespace, namespace_data)
                if tenant == self.tenant1:
                    tenant1_namespaces.append(namespace)
                else:
                    tenant2_namespaces.append(namespace)

        # Create a resource type for each namespace created above
        tenant1_resource_types = self._create_resource_type(
            tenant1_namespaces)
        tenant2_resource_types = self._create_resource_type(
            tenant2_namespaces)

        def _check_resource_type_access(namespaces, tenant):
            headers = self._headers({'X-Tenant-Id': tenant,
                                     'X-Roles': 'reader,member'})
            for namespace in namespaces:
                path = self._url('/v2/metadefs/namespaces/%s/resource_types' %
                                 (namespace['namespace']))
                response = requests.get(path, headers=headers)
                if namespace['visibility'] == 'public':
                    self.assertEqual(http.OK, response.status_code)
                else:
                    self.assertEqual(http.NOT_FOUND, response.status_code)

        def _check_resource_types(tenant, total_rs_types):
            # Resource types are visible across tenants for all users
            path = self._url('/v2/metadefs/resource_types')
            headers = self._headers({'X-Tenant-Id': tenant,
                                     'X-Roles': 'reader,member'})
            response = requests.get(path, headers=headers)
            self.assertEqual(http.OK, response.status_code)
            metadef_resource_type = response.json()

            # The resource types list count should be same as the total
            # resource types created across the tenants.
            self.assertEqual(
                sorted(x['name']
                       for x in metadef_resource_type['resource_types']),
                sorted(value for x in total_rs_types
                       for key, value in x.items()))

        # Check Tenant 1 can access resource types of all public namespace
        # and cannot access resource type of private namespace of Tenant 2
        _check_resource_type_access(tenant2_namespaces, self.tenant1)

        # Check Tenant 2 can access public namespace and cannot access private
        # namespace of Tenant 1
        _check_resource_type_access(tenant1_namespaces, self.tenant2)

        # List all resource type irrespective of namespace & tenant are
        # accessible non admin roles
        total_resource_types = tenant1_resource_types + tenant2_resource_types
        _check_resource_types(self.tenant1, total_resource_types)
        _check_resource_types(self.tenant2, total_resource_types)

        # Disassociate resource type should not be allowed to non admin role
        for resource_type in total_resource_types:
            for namespace, rs_type in resource_type.items():
                path = \
                    self._url('/v2/metadefs/namespaces/%s/resource_types/%s' %
                              (namespace, rs_type))
                response = requests.delete(
                    path, headers=self._headers({
                        'X-Roles': 'reader,member',
                        'X-Tenant-Id': namespace.split('_')[0]
                    }))
                self.assertEqual(http.FORBIDDEN, response.status_code)

        # Disassociate of all metadef resource types
        headers = self._headers()
        for resource_type in total_resource_types:
            for namespace, rs_type in resource_type.items():
                path = \
                    self._url('/v2/metadefs/namespaces/%s/resource_types/%s' %
                              (namespace, rs_type))
                response = requests.delete(path, headers=headers)
                self.assertEqual(http.NO_CONTENT, response.status_code)

                # Disassociated resource type should not be exist
                # When the specified resource type is not associated with given
                # namespace then it returns empty list in response instead of
                # raising not found error
                path = self._url(
                    '/v2/metadefs/namespaces/%s/resource_types' % namespace)
                response = requests.get(path, headers=headers)
                metadef_resource_type = response.json()
                self.assertEqual(
                    [], metadef_resource_type['resource_type_associations'])