Debian Codegraph @

keystoneclient/tests/unit/v3/test_endpoint_policy.py

raw symbols SCIP
1 # Copyright 2014 IBM Corp.
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 uuid
16
17 from keystoneclient.tests.unit.v3 import test_endpoint_filter
18 from keystoneclient.tests.unit.v3 import utils
19
20
21 class EndpointPolicyTests(utils.ClientTestCase,
22 test_endpoint_filter.EndpointTestUtils):
23 """Test policy-endpoint associations (a.k.a. EndpointPolicy Extension)."""
24
25 def setUp(self):
26 super(EndpointPolicyTests, self).setUp()
27 self.manager = self.client.endpoint_policy
28
29 def new_policy_ref(self, **kwargs):
30 kwargs.setdefault('id', uuid.uuid4().hex)
31 kwargs.setdefault('type', uuid.uuid4().hex)
32 kwargs.setdefault('blob', uuid.uuid4().hex)
33 return kwargs
34
35 def new_region_ref(self, **kwargs):
36 kwargs = self.new_ref(**kwargs)
37 return kwargs
38
39 def new_service_ref(self, **kwargs):
40 kwargs = self.new_ref(**kwargs)
41 kwargs.setdefault('name', uuid.uuid4().hex)
42 kwargs.setdefault('type', uuid.uuid4().hex)
43 return kwargs
44
45 def _crud_policy_association_for_endpoint_via_id(
46 self, http_action, manager_action):
47 policy_id = uuid.uuid4().hex
48 endpoint_id = uuid.uuid4().hex
49
50 self.stub_url(http_action,
51 ['policies', policy_id, self.manager.OS_EP_POLICY_EXT,
52 'endpoints', endpoint_id],
53 status_code=204)
54 manager_action(policy=policy_id, endpoint=endpoint_id)
55
56 def _crud_policy_association_for_endpoint_via_obj(
57 self, http_action, manager_action):
58 policy_ref = self.new_policy_ref()
59 endpoint_ref = self.new_endpoint_ref()
60 policy = self.client.policies.resource_class(
61 self.client.policies, policy_ref, loaded=True)
62 endpoint = self.client.endpoints.resource_class(
63 self.client.endpoints, endpoint_ref, loaded=True)
64
65 self.stub_url(http_action,
66 ['policies', policy_ref['id'],
67 self.manager.OS_EP_POLICY_EXT,
68 'endpoints', endpoint_ref['id']],
69 status_code=204)
70 manager_action(policy=policy, endpoint=endpoint)
71
72 def test_create_policy_association_for_endpoint_via_id(self):
73 self._crud_policy_association_for_endpoint_via_id(
74 'PUT', self.manager.create_policy_association_for_endpoint)
75
76 def test_create_policy_association_for_endpoint_via_obj(self):
77 self._crud_policy_association_for_endpoint_via_obj(
78 'PUT', self.manager.create_policy_association_for_endpoint)
79
80 def test_check_policy_association_for_endpoint_via_id(self):
81 self._crud_policy_association_for_endpoint_via_id(
82 'HEAD', self.manager.check_policy_association_for_endpoint)
83
84 def test_check_policy_association_for_endpoint_via_obj(self):
85 self._crud_policy_association_for_endpoint_via_obj(
86 'HEAD', self.manager.check_policy_association_for_endpoint)
87
88 def test_delete_policy_association_for_endpoint_via_id(self):
89 self._crud_policy_association_for_endpoint_via_id(
90 'DELETE', self.manager.delete_policy_association_for_endpoint)
91
92 def test_delete_policy_association_for_endpoint_via_obj(self):
93 self._crud_policy_association_for_endpoint_via_obj(
94 'DELETE', self.manager.delete_policy_association_for_endpoint)
95
96 def _crud_policy_association_for_service_via_id(
97 self, http_action, manager_action):
98 policy_id = uuid.uuid4().hex
99 service_id = uuid.uuid4().hex
100
101 self.stub_url(http_action,
102 ['policies', policy_id, self.manager.OS_EP_POLICY_EXT,
103 'services', service_id],
104 status_code=204)
105 manager_action(policy=policy_id, service=service_id)
106
107 def _crud_policy_association_for_service_via_obj(
108 self, http_action, manager_action):
109 policy_ref = self.new_policy_ref()
110 service_ref = self.new_service_ref()
111 policy = self.client.policies.resource_class(
112 self.client.policies, policy_ref, loaded=True)
113 service = self.client.services.resource_class(
114 self.client.services, service_ref, loaded=True)
115
116 self.stub_url(http_action,
117 ['policies', policy_ref['id'],
118 self.manager.OS_EP_POLICY_EXT,
119 'services', service_ref['id']],
120 status_code=204)
121 manager_action(policy=policy, service=service)
122
123 def test_create_policy_association_for_service_via_id(self):
124 self._crud_policy_association_for_service_via_id(
125 'PUT', self.manager.create_policy_association_for_service)
126
127 def test_create_policy_association_for_service_via_obj(self):
128 self._crud_policy_association_for_service_via_obj(
129 'PUT', self.manager.create_policy_association_for_service)
130
131 def test_check_policy_association_for_service_via_id(self):
132 self._crud_policy_association_for_service_via_id(
133 'HEAD', self.manager.check_policy_association_for_service)
134
135 def test_check_policy_association_for_service_via_obj(self):
136 self._crud_policy_association_for_service_via_obj(
137 'HEAD', self.manager.check_policy_association_for_service)
138
139 def test_delete_policy_association_for_service_via_id(self):
140 self._crud_policy_association_for_service_via_id(
141 'DELETE', self.manager.delete_policy_association_for_service)
142
143 def test_delete_policy_association_for_service_via_obj(self):
144 self._crud_policy_association_for_service_via_obj(
145 'DELETE', self.manager.delete_policy_association_for_service)
146
147 def _crud_policy_association_for_region_and_service_via_id(
148 self, http_action, manager_action):
149 policy_id = uuid.uuid4().hex
150 region_id = uuid.uuid4().hex
151 service_id = uuid.uuid4().hex
152
153 self.stub_url(http_action,
154 ['policies', policy_id, self.manager.OS_EP_POLICY_EXT,
155 'services', service_id, 'regions', region_id],
156 status_code=204)
157 manager_action(policy=policy_id, region=region_id, service=service_id)
158
159 def _crud_policy_association_for_region_and_service_via_obj(
160 self, http_action, manager_action):
161 policy_ref = self.new_policy_ref()
162 region_ref = self.new_region_ref()
163 service_ref = self.new_service_ref()
164 policy = self.client.policies.resource_class(
165 self.client.policies, policy_ref, loaded=True)
166 region = self.client.regions.resource_class(
167 self.client.regions, region_ref, loaded=True)
168 service = self.client.services.resource_class(
169 self.client.services, service_ref, loaded=True)
170
171 self.stub_url(http_action,
172 ['policies', policy_ref['id'],
173 self.manager.OS_EP_POLICY_EXT,
174 'services', service_ref['id'],
175 'regions', region_ref['id']],
176 status_code=204)
177 manager_action(policy=policy, region=region, service=service)
178
179 def test_create_policy_association_for_region_and_service_via_id(self):
180 self._crud_policy_association_for_region_and_service_via_id(
181 'PUT',
182 self.manager.create_policy_association_for_region_and_service)
183
184 def test_create_policy_association_for_region_and_service_via_obj(self):
185 self._crud_policy_association_for_region_and_service_via_obj(
186 'PUT',
187 self.manager.create_policy_association_for_region_and_service)
188
189 def test_check_policy_association_for_region_and_service_via_id(self):
190 self._crud_policy_association_for_region_and_service_via_id(
191 'HEAD',
192 self.manager.check_policy_association_for_region_and_service)
193
194 def test_check_policy_association_for_region_and_service_via_obj(self):
195 self._crud_policy_association_for_region_and_service_via_obj(
196 'HEAD',
197 self.manager.check_policy_association_for_region_and_service)
198
199 def test_delete_policy_association_for_region_and_service_via_id(self):
200 self._crud_policy_association_for_region_and_service_via_id(
201 'DELETE',
202 self.manager.delete_policy_association_for_region_and_service)
203
204 def test_delete_policy_association_for_region_and_service_via_obj(self):
205 self._crud_policy_association_for_region_and_service_via_obj(
206 'DELETE',
207 self.manager.delete_policy_association_for_region_and_service)
208
209 def test_get_policy_for_endpoint(self):
210 endpoint_id = uuid.uuid4().hex
211 expected_policy = self.new_policy_ref()
212
213 self.stub_url('GET',
214 ['endpoints', endpoint_id, self.manager.OS_EP_POLICY_EXT,
215 'policy'],
216 json={'policy': expected_policy},
217 status_code=200)
218
219 policy_resp = self.manager.get_policy_for_endpoint(
220 endpoint=endpoint_id)
221
222 self.assertEqual(expected_policy['id'], policy_resp.id)
223 self.assertEqual(expected_policy['blob'], policy_resp.blob)
224 self.assertEqual(expected_policy['type'], policy_resp.type)
225
226 def test_list_endpoints_for_policy(self):
227 policy_id = uuid.uuid4().hex
228 endpoints = {'endpoints': [self.new_endpoint_ref(),
229 self.new_endpoint_ref()]}
230 self.stub_url('GET',
231 ['policies', policy_id, self.manager.OS_EP_POLICY_EXT,
232 'endpoints'],
233 json=endpoints,
234 status_code=200)
235
236 endpoints_resp = self.manager.list_endpoints_for_policy(
237 policy=policy_id)
238
239 expected_endpoint_ids = [
240 endpoint['id'] for endpoint in endpoints['endpoints']]
241 actual_endpoint_ids = [endpoint.id for endpoint in endpoints_resp]
242 self.assertEqual(expected_endpoint_ids, actual_endpoint_ids)