summaryrefslogtreecommitdiff
path: root/watcher_tempest_plugin/tests/api/admin/base.py
diff options
context:
space:
mode:
Diffstat (limited to 'watcher_tempest_plugin/tests/api/admin/base.py')
-rw-r--r--watcher_tempest_plugin/tests/api/admin/base.py259
1 files changed, 0 insertions, 259 deletions
diff --git a/watcher_tempest_plugin/tests/api/admin/base.py b/watcher_tempest_plugin/tests/api/admin/base.py
deleted file mode 100644
index 63cdb83..0000000
--- a/watcher_tempest_plugin/tests/api/admin/base.py
+++ /dev/null
@@ -1,259 +0,0 @@
1# -*- encoding: utf-8 -*-
2# Copyright (c) 2016 b<>com
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain 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,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import functools
18
19from tempest.lib.common.utils import data_utils
20from tempest.lib.common.utils import test_utils
21from tempest import test
22
23from watcher_tempest_plugin import infra_optim_clients as clients
24
25
26class BaseInfraOptimTest(test.BaseTestCase):
27 """Base class for Infrastructure Optimization API tests."""
28
29 # States where the object is waiting for some event to perform a transition
30 IDLE_STATES = ('RECOMMENDED',
31 'FAILED',
32 'SUCCEEDED',
33 'CANCELLED',
34 'SUSPENDED')
35 # States where the object can only be DELETED (end of its life-cycle)
36 FINISHED_STATES = ('FAILED',
37 'SUCCEEDED',
38 'CANCELLED',
39 'SUPERSEDED')
40
41 @classmethod
42 def setup_credentials(cls):
43 super(BaseInfraOptimTest, cls).setup_credentials()
44 cls.mgr = clients.AdminManager()
45
46 @classmethod
47 def setup_clients(cls):
48 super(BaseInfraOptimTest, cls).setup_clients()
49 cls.client = cls.mgr.io_client
50
51 @classmethod
52 def resource_setup(cls):
53 super(BaseInfraOptimTest, cls).resource_setup()
54
55 # Set of all created audit templates UUIDs
56 cls.created_audit_templates = set()
57 # Set of all created audit UUIDs
58 cls.created_audits = set()
59 # Set of all created audit UUIDs. We use it to build the list of
60 # action plans to delete (including potential orphan one(s))
61 cls.created_action_plans_audit_uuids = set()
62
63 @classmethod
64 def resource_cleanup(cls):
65 """Ensure that all created objects get destroyed."""
66 try:
67 action_plans_to_be_deleted = set()
68 # Phase 1: Make sure all objects are in an idle state
69 for audit_uuid in cls.created_audits:
70 test_utils.call_until_true(
71 func=functools.partial(
72 cls.is_audit_idle, audit_uuid),
73 duration=30,
74 sleep_for=.5
75 )
76
77 for audit_uuid in cls.created_action_plans_audit_uuids:
78 _, action_plans = cls.client.list_action_plans(
79 audit_uuid=audit_uuid)
80 action_plans_to_be_deleted.update(
81 ap['uuid'] for ap in action_plans['action_plans'])
82
83 for action_plan in action_plans['action_plans']:
84 try:
85 test_utils.call_until_true(
86 func=functools.partial(
87 cls.is_action_plan_idle, action_plan['uuid']),
88 duration=30,
89 sleep_for=.5
90 )
91 except Exception:
92 action_plans_to_be_deleted.remove(
93 action_plan['uuid'])
94
95 # Phase 2: Delete them all
96 for action_plan_uuid in action_plans_to_be_deleted:
97 cls.delete_action_plan(action_plan_uuid)
98
99 for audit_uuid in cls.created_audits.copy():
100 cls.delete_audit(audit_uuid)
101
102 for audit_template_uuid in cls.created_audit_templates.copy():
103 cls.delete_audit_template(audit_template_uuid)
104
105 finally:
106 super(BaseInfraOptimTest, cls).resource_cleanup()
107
108 def validate_self_link(self, resource, uuid, link):
109 """Check whether the given self link formatted correctly."""
110 expected_link = "{base}/{pref}/{res}/{uuid}".format(
111 base=self.client.base_url,
112 pref=self.client.URI_PREFIX,
113 res=resource,
114 uuid=uuid
115 )
116 self.assertEqual(expected_link, link)
117
118 def assert_expected(self, expected, actual,
119 keys=('created_at', 'updated_at', 'deleted_at')):
120 # Check if not expected keys/values exists in actual response body
121 for key, value in expected.items():
122 if key not in keys:
123 self.assertIn(key, actual)
124 self.assertEqual(value, actual[key])
125
126 # ### AUDIT TEMPLATES ### #
127
128 @classmethod
129 def create_audit_template(cls, goal, name=None, description=None,
130 strategy=None, scope=None):
131 """Wrapper utility for creating a test audit template
132
133 :param goal: Goal UUID or name related to the audit template.
134 :param name: The name of the audit template. Default: My Audit Template
135 :param description: The description of the audit template.
136 :param strategy: Strategy UUID or name related to the audit template.
137 :param scope: Scope that will be applied on all derived audits.
138 :return: A tuple with The HTTP response and its body
139 """
140 description = description or data_utils.rand_name(
141 'test-audit_template')
142 resp, body = cls.client.create_audit_template(
143 name=name, description=description,
144 goal=goal, strategy=strategy, scope=scope)
145
146 cls.created_audit_templates.add(body['uuid'])
147
148 return resp, body
149
150 @classmethod
151 def delete_audit_template(cls, uuid):
152 """Deletes a audit_template having the specified UUID
153
154 :param uuid: The unique identifier of the audit template
155 :return: Server response
156 """
157 resp, _ = cls.client.delete_audit_template(uuid)
158
159 if uuid in cls.created_audit_templates:
160 cls.created_audit_templates.remove(uuid)
161
162 return resp
163
164 # ### AUDITS ### #
165
166 @classmethod
167 def create_audit(cls, audit_template_uuid, audit_type='ONESHOT',
168 state=None, interval=None, parameters=None):
169 """Wrapper utility for creating a test audit
170
171 :param audit_template_uuid: Audit Template UUID this audit will use
172 :param audit_type: Audit type (either ONESHOT or CONTINUOUS)
173 :param state: Audit state (str)
174 :param interval: Audit interval in seconds or cron syntax (str)
175 :param parameters: list of execution parameters
176 :return: A tuple with The HTTP response and its body
177 """
178 resp, body = cls.client.create_audit(
179 audit_template_uuid=audit_template_uuid, audit_type=audit_type,
180 state=state, interval=interval, parameters=parameters)
181
182 cls.created_audits.add(body['uuid'])
183 cls.created_action_plans_audit_uuids.add(body['uuid'])
184
185 return resp, body
186
187 @classmethod
188 def delete_audit(cls, audit_uuid):
189 """Deletes an audit having the specified UUID
190
191 :param audit_uuid: The unique identifier of the audit.
192 :return: the HTTP response
193 """
194 resp, _ = cls.client.delete_audit(audit_uuid)
195
196 if audit_uuid in cls.created_audits:
197 cls.created_audits.remove(audit_uuid)
198
199 return resp
200
201 @classmethod
202 def has_audit_succeeded(cls, audit_uuid):
203 _, audit = cls.client.show_audit(audit_uuid)
204 return audit.get('state') == 'SUCCEEDED'
205
206 @classmethod
207 def has_audit_finished(cls, audit_uuid):
208 _, audit = cls.client.show_audit(audit_uuid)
209 return audit.get('state') in cls.FINISHED_STATES
210
211 @classmethod
212 def is_audit_idle(cls, audit_uuid):
213 _, audit = cls.client.show_audit(audit_uuid)
214 return audit.get('state') in cls.IDLE_STATES
215
216 # ### ACTION PLANS ### #
217
218 @classmethod
219 def create_action_plan(cls, audit_template_uuid, **audit_kwargs):
220 """Wrapper utility for creating a test action plan
221
222 :param audit_template_uuid: Audit template UUID to use
223 :param audit_kwargs: Dict of audit properties to set
224 :return: The action plan as dict
225 """
226 _, audit = cls.create_audit(audit_template_uuid, **audit_kwargs)
227 audit_uuid = audit['uuid']
228
229 assert test_utils.call_until_true(
230 func=functools.partial(cls.has_audit_finished, audit_uuid),
231 duration=30,
232 sleep_for=.5
233 )
234
235 _, action_plans = cls.client.list_action_plans(audit_uuid=audit_uuid)
236 if len(action_plans['action_plans']) == 0:
237 return
238
239 return action_plans['action_plans'][0]
240
241 @classmethod
242 def delete_action_plan(cls, action_plan_uuid):
243 """Deletes an action plan having the specified UUID
244
245 :param action_plan_uuid: The unique identifier of the action plan.
246 :return: the HTTP response
247 """
248 resp, _ = cls.client.delete_action_plan(action_plan_uuid)
249
250 if action_plan_uuid in cls.created_action_plans_audit_uuids:
251 cls.created_action_plans_audit_uuids.remove(action_plan_uuid)
252
253 return resp
254
255 @classmethod
256 def is_action_plan_idle(cls, action_plan_uuid):
257 """This guard makes sure your action plan is not running"""
258 _, action_plan = cls.client.show_action_plan(action_plan_uuid)
259 return action_plan.get('state') in cls.IDLE_STATES