Debian Codegraph @

glanceclient/tests/unit/v2/base.py

raw symbols SCIP
1 # Copyright 2016 NTT DATA
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 testtools
16
17
18 class BaseController(testtools.TestCase):
19 def __init__(self, api, schema_api, controller_class):
20 self.controller = controller_class(api, schema_api)
21
22 def _assertRequestId(self, obj):
23 self.assertIsNotNone(getattr(obj, 'request_ids', None))
24 self.assertEqual(['req-1234'], obj.request_ids)
25
26 def list(self, *args, **kwargs):
27 gen_obj = self.controller.list(*args, **kwargs)
28 # For generator cases the request_ids property will be an empty list
29 # until the underlying generator is invoked at-least once.
30 resources = list(gen_obj)
31 if len(resources) > 0:
32 self._assertRequestId(gen_obj)
33 else:
34 # If list is empty that means geneator object has raised
35 # StopIteration for first iteration and will not contain the
36 # request_id in it.
37 self.assertEqual([], gen_obj.request_ids)
38
39 return resources
40
41 def get_associated_image_tasks(self, *args, **kwargs):
42 resource = self.controller.get_associated_image_tasks(
43 *args, **kwargs)
44
45 self._assertRequestId(resource)
46 return resource
47
48 def get(self, *args, **kwargs):
49 resource = self.controller.get(*args, **kwargs)
50
51 self._assertRequestId(resource)
52 return resource
53
54 def create(self, *args, **kwargs):
55 resource = self.controller.create(*args, **kwargs)
56 self._assertRequestId(resource)
57 return resource
58
59 def create_multiple(self, *args, **kwargs):
60 tags = self.controller.create_multiple(*args, **kwargs)
61 actual = [tag.name for tag in tags]
62 self._assertRequestId(tags)
63 return actual
64
65 def update(self, *args, **properties):
66 resource = self.controller.update(*args, **properties)
67 self._assertRequestId(resource)
68 return resource
69
70 def delete(self, *args):
71 resp = self.controller.delete(*args)
72 self._assertRequestId(resp)
73
74 def delete_all(self, *args):
75 resp = self.controller.delete_all(*args)
76 self._assertRequestId(resp)
77
78 def deactivate(self, *args):
79 resp = self.controller.deactivate(*args)
80 self._assertRequestId(resp)
81
82 def reactivate(self, *args):
83 resp = self.controller.reactivate(*args)
84 self._assertRequestId(resp)
85
86 def upload(self, *args, **kwargs):
87 resp = self.controller.upload(*args, **kwargs)
88 self._assertRequestId(resp)
89
90 def stage(self, *args, **kwargs):
91 resp = self.controller.stage(*args, **kwargs)
92 self._assertRequestId(resp)
93
94 def data(self, *args, **kwargs):
95 body = self.controller.data(*args, **kwargs)
96 self._assertRequestId(body)
97 return body
98
99 def delete_locations(self, *args):
100 resp = self.controller.delete_locations(*args)
101 self._assertRequestId(resp)
102
103 def add_location(self, *args, **kwargs):
104 resp = self.controller.add_location(*args, **kwargs)
105 self._assertRequestId(resp)
106
107 def update_location(self, *args, **kwargs):
108 resp = self.controller.update_location(*args, **kwargs)
109 self._assertRequestId(resp)
110
111 def associate(self, *args, **kwargs):
112 resource_types = self.controller.associate(*args, **kwargs)
113 self._assertRequestId(resource_types)
114 return resource_types
115
116 def deassociate(self, *args):
117 resp = self.controller.deassociate(*args)
118 self._assertRequestId(resp)
119
120 def image_import(self, *args, **kwargs):
121 resp = self.controller.image_import(*args, **kwargs)
122 self._assertRequestId(resp)
123
124 def add_image_location(self, *args):
125 resp = self.controller.add_image_location(*args)
126 self._assertRequestId(resp)
127
128 def get_image_locations(self, *args):
129 resource = self.controller.get_image_locations(*args)
130 self._assertRequestId(resource)
131 return resource
132
133
134 class BaseResourceTypeController(BaseController):
135 def __init__(self, api, schema_api, controller_class):
136 super(BaseResourceTypeController, self).__init__(api, schema_api,
137 controller_class)
138
139 def get(self, *args, **kwargs):
140 resource_types = self.controller.get(*args)
141 names = [rt.name for rt in resource_types]
142 self._assertRequestId(resource_types)
143 return names