root / snf-tools / synnefo_tools / burnin / common.py @ 79a5c431
History | View | Annotate | Download (11 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
"""
|
35 |
Common utils for burnin tests
|
36 |
|
37 |
"""
|
38 |
|
39 |
import sys |
40 |
import datetime |
41 |
import traceback |
42 |
# Use backported unittest functionality if Python < 2.7
|
43 |
try:
|
44 |
import unittest2 as unittest |
45 |
except ImportError: |
46 |
if sys.version_info < (2, 7): |
47 |
raise Exception("The unittest2 package is required for Python < 2.7") |
48 |
import unittest |
49 |
|
50 |
from kamaki.clients.astakos import AstakosClient |
51 |
from kamaki.clients.compute import ComputeClient |
52 |
from kamaki.clients.pithos import PithosClient |
53 |
|
54 |
from synnefo_tools.burnin.logger import Log |
55 |
|
56 |
|
57 |
# --------------------------------------------------------------------
|
58 |
# Global variables
|
59 |
logger = None # Invalid constant name. pylint: disable-msg=C0103 |
60 |
SNF_TEST_PREFIX = "snf-test-"
|
61 |
CONNECTION_RETRY_LIMIT = 2
|
62 |
|
63 |
|
64 |
# --------------------------------------------------------------------
|
65 |
# BurninTestResult class
|
66 |
class BurninTestResult(unittest.TestResult): |
67 |
"""Modify the TextTestResult class"""
|
68 |
def __init__(self): |
69 |
super(BurninTestResult, self).__init__() |
70 |
|
71 |
# Test parameters
|
72 |
self.failfast = True |
73 |
|
74 |
def startTest(self, test): # noqa |
75 |
"""Called when the test case test is about to be run"""
|
76 |
super(BurninTestResult, self).startTest(test) |
77 |
logger.log(test.__class__.__name__, test.shortDescription()) |
78 |
|
79 |
# Method could be a function. pylint: disable-msg=R0201
|
80 |
def _test_failed(self, test, err): |
81 |
"""Test failed"""
|
82 |
# Get class name
|
83 |
if test.__class__.__name__ == "_ErrorHolder": |
84 |
class_name = test.id().split('.')[-1].rstrip(')') |
85 |
else:
|
86 |
class_name = test.__class__.__name__ |
87 |
err_msg = str(test) + "... failed (%s)." |
88 |
timestamp = datetime.datetime.strftime( |
89 |
datetime.datetime.now(), "%a %b %d %Y %H:%M:%S")
|
90 |
logger.error(class_name, err_msg, timestamp) |
91 |
(err_type, err_value, err_trace) = err |
92 |
trcback = traceback.format_exception(err_type, err_value, err_trace) |
93 |
logger.info(class_name, trcback) |
94 |
|
95 |
def addError(self, test, err): # noqa |
96 |
"""Called when the test case test raises an unexpected exception"""
|
97 |
super(BurninTestResult, self).addError(test, err) |
98 |
self._test_failed(test, err)
|
99 |
|
100 |
def addFailure(self, test, err): # noqa |
101 |
"""Called when the test case test signals a failure"""
|
102 |
super(BurninTestResult, self).addFailure(test, err) |
103 |
self._test_failed(test, err)
|
104 |
|
105 |
|
106 |
# --------------------------------------------------------------------
|
107 |
# BurninTests class
|
108 |
# Too few public methods (0/2). pylint: disable-msg=R0903
|
109 |
class Clients(object): |
110 |
"""Our kamaki clients"""
|
111 |
auth_url = None
|
112 |
token = None
|
113 |
# Astakos
|
114 |
astakos = None
|
115 |
retry = CONNECTION_RETRY_LIMIT |
116 |
# Compute
|
117 |
compute = None
|
118 |
compute_url = None
|
119 |
# Cyclades
|
120 |
cyclades = None
|
121 |
# Pithos
|
122 |
pithos = None
|
123 |
pithos_url = None
|
124 |
|
125 |
|
126 |
# Too many public methods (45/20). pylint: disable-msg=R0904
|
127 |
class BurninTests(unittest.TestCase): |
128 |
"""Common class that all burnin tests should implement"""
|
129 |
clients = Clients() |
130 |
run_id = None
|
131 |
use_ipv6 = None
|
132 |
action_timeout = None
|
133 |
action_warning = None
|
134 |
query_interval = None
|
135 |
|
136 |
@classmethod
|
137 |
def setUpClass(cls): # noqa |
138 |
"""Initialize BurninTests"""
|
139 |
cls.suite_name = cls.__name__ |
140 |
logger.testsuite_start(cls.suite_name) |
141 |
|
142 |
# Set test parameters
|
143 |
cls.longMessage = True
|
144 |
|
145 |
def _setattr(self, attr, value): |
146 |
"""Used by tests to set an attribute to TestCase
|
147 |
|
148 |
Since each instance of the TestCase will only be used to run a single
|
149 |
test method (a new fixture is created for each test) the attributes can
|
150 |
not be saved in the class instance. Instead the class itself should be
|
151 |
used.
|
152 |
|
153 |
"""
|
154 |
setattr(self.__class__, attr, value) |
155 |
|
156 |
def test_000_clients_setup(self): |
157 |
"""Initializing astakos/cyclades/pithos clients"""
|
158 |
# Update class attributes
|
159 |
self.info("Astakos auth url is %s", self.clients.auth_url) |
160 |
self.clients.astakos = AstakosClient(
|
161 |
self.clients.auth_url, self.clients.token) |
162 |
self.clients.astakos.CONNECTION_RETRY_LIMIT = self.clients.retry |
163 |
|
164 |
self.clients.compute_url = \
|
165 |
self.clients.astakos.get_service_endpoints('compute')['publicURL'] |
166 |
self.info("Cyclades url is %s", self.clients.compute_url) |
167 |
self.clients.compute = ComputeClient(
|
168 |
self.clients.compute_url, self.clients.token) |
169 |
self.clients.compute.CONNECTION_RETRY_LIMIT = self.clients.retry |
170 |
|
171 |
self.clients.pithos_url = self.clients.astakos.\ |
172 |
get_service_endpoints('object-store')['publicURL'] |
173 |
self.info("Pithos url is %s", self.clients.pithos_url) |
174 |
self.clients.pithos = PithosClient(
|
175 |
self.clients.pithos_url, self.clients.token) |
176 |
self.clients.pithos.CONNECTION_RETRY_LIMIT = self.clients.retry |
177 |
|
178 |
# ----------------------------------
|
179 |
# Loggers helper functions
|
180 |
def log(self, msg, *args): |
181 |
"""Pass the section value to logger"""
|
182 |
logger.log(self.suite_name, msg, *args)
|
183 |
|
184 |
def info(self, msg, *args): |
185 |
"""Pass the section value to logger"""
|
186 |
logger.info(self.suite_name, msg, *args)
|
187 |
|
188 |
def debug(self, msg, *args): |
189 |
"""Pass the section value to logger"""
|
190 |
logger.debug(self.suite_name, msg, *args)
|
191 |
|
192 |
def warning(self, msg, *args): |
193 |
"""Pass the section value to logger"""
|
194 |
logger.warning(self.suite_name, msg, *args)
|
195 |
|
196 |
def error(self, msg, *args): |
197 |
"""Pass the section value to logger"""
|
198 |
logger.error(self.suite_name, msg, *args)
|
199 |
|
200 |
# ----------------------------------
|
201 |
# Helper functions that every testsuite may need
|
202 |
def _get_uuid(self): |
203 |
"""Get our uuid"""
|
204 |
authenticate = self.clients.astakos.authenticate()
|
205 |
uuid = authenticate['access']['user']['id'] |
206 |
self.info("User's uuid is %s", uuid) |
207 |
return uuid
|
208 |
|
209 |
def _get_username(self): |
210 |
"""Get our User Name"""
|
211 |
authenticate = self.clients.astakos.authenticate()
|
212 |
username = authenticate['access']['user']['name'] |
213 |
self.info("User's name is %s", username) |
214 |
return username
|
215 |
|
216 |
def _get_list_of_flavors(self, detail=False): |
217 |
"""Get (detailed) list of flavors"""
|
218 |
if detail:
|
219 |
self.info("Getting detailed list of flavors") |
220 |
else:
|
221 |
self.info("Getting simple list of flavors") |
222 |
flavors = self.clients.compute.list_flavors(detail=detail)
|
223 |
return flavors
|
224 |
|
225 |
def _set_pithos_account(self, account): |
226 |
"""Set the pithos account"""
|
227 |
assert account, "No pithos account was given" |
228 |
|
229 |
self.info("Setting pithos account to %s", account) |
230 |
self.clients.pithos.account = account
|
231 |
|
232 |
def _get_list_of_containers(self, account=None): |
233 |
"""Get list of containers"""
|
234 |
if account is not None: |
235 |
self._set_pithos_account(account)
|
236 |
self.info("Getting list of containers") |
237 |
return self.clients.pithos.list_containers() |
238 |
|
239 |
def _create_pithos_container(self, container): |
240 |
"""Create a pithos container
|
241 |
|
242 |
If the container exists, nothing will happen
|
243 |
|
244 |
"""
|
245 |
assert container, "No pithos container was given" |
246 |
|
247 |
self.info("Creating pithos container %s", container) |
248 |
self.clients.pithos.container = container
|
249 |
self.clients.pithos.container_put()
|
250 |
|
251 |
|
252 |
# --------------------------------------------------------------------
|
253 |
# Initialize Burnin
|
254 |
def initialize(opts, testsuites): |
255 |
"""Initalize burnin
|
256 |
|
257 |
Initialize our logger and burnin state
|
258 |
|
259 |
"""
|
260 |
# Initialize logger
|
261 |
global logger # Using global statement. pylint: disable-msg=C0103,W0603 |
262 |
logger = Log(opts.log_folder, verbose=opts.verbose, |
263 |
use_colors=opts.use_colors, in_parallel=False,
|
264 |
quiet=opts.quiet) |
265 |
|
266 |
# Initialize clients
|
267 |
Clients.auth_url = opts.auth_url |
268 |
Clients.token = opts.token |
269 |
|
270 |
# Pass the rest options to BurninTests
|
271 |
BurninTests.use_ipv6 = opts.use_ipv6 |
272 |
BurninTests.action_timeout = opts.action_timeout |
273 |
BurninTests.action_warning = opts.action_warning |
274 |
BurninTests.query_interval = opts.query_interval |
275 |
BurninTests.run_id = SNF_TEST_PREFIX + \ |
276 |
datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S")
|
277 |
|
278 |
# Choose tests to run
|
279 |
if opts.tests != "all": |
280 |
testsuites = opts.tests |
281 |
if opts.exclude_tests is not None: |
282 |
testsuites = [tsuite for tsuite in testsuites |
283 |
if tsuite not in opts.exclude_tests] |
284 |
|
285 |
return testsuites
|
286 |
|
287 |
|
288 |
# --------------------------------------------------------------------
|
289 |
# Run Burnin
|
290 |
def run(testsuites, failfast=False, final_report=False): |
291 |
"""Run burnin testsuites"""
|
292 |
global logger # Using global. pylint: disable-msg=C0103,W0603,W0602 |
293 |
|
294 |
success = True
|
295 |
for tcase in testsuites: |
296 |
tsuite = unittest.TestLoader().loadTestsFromTestCase(tcase) |
297 |
results = tsuite.run(BurninTestResult()) |
298 |
|
299 |
was_success = was_successful(tcase.__name__, results.wasSuccessful()) |
300 |
success = success and was_success
|
301 |
if failfast and not success: |
302 |
break
|
303 |
|
304 |
# Are we going to print final report?
|
305 |
if final_report:
|
306 |
logger.print_logfile_to_stdout() |
307 |
# Clean up our logger
|
308 |
del(logger)
|
309 |
|
310 |
# Return
|
311 |
return 0 if success else 1 |
312 |
|
313 |
|
314 |
# --------------------------------------------------------------------
|
315 |
# Helper functions
|
316 |
def was_successful(tsuite, success): |
317 |
"""Handle whether a testsuite was succesful or not"""
|
318 |
if success:
|
319 |
logger.testsuite_success(tsuite) |
320 |
return True |
321 |
else:
|
322 |
logger.testsuite_failure(tsuite) |
323 |
return False |