root / kamaki / clients / test.py @ 27058e48
History | View | Annotate | Download (4.8 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 |
from unittest import makeSuite, TestSuite, TextTestRunner, TestCase |
35 |
from time import sleep |
36 |
from inspect import getmembers, isclass |
37 |
|
38 |
from kamaki.clients.connection.test import ( |
39 |
KamakiConnection, |
40 |
KamakiHTTPConnection, |
41 |
KamakiResponse, |
42 |
KamakiHTTPResponse) |
43 |
from kamaki.clients.utils.test import Utils |
44 |
from kamaki.clients.astakos.test import Astakos |
45 |
from kamaki.clients.compute.test import Compute, ComputeRest |
46 |
from kamaki.clients.cyclades.test import Cyclades, CycladesRest |
47 |
from kamaki.clients.image.test import Image |
48 |
from kamaki.clients.storage.test import Storage |
49 |
from kamaki.clients.pithos.test import Pithos, PithosRest |
50 |
|
51 |
|
52 |
class SilentEvent(TestCase): |
53 |
|
54 |
can_finish = -1
|
55 |
|
56 |
def thread_content(self, methodid): |
57 |
wait = 0.1
|
58 |
while self.can_finish < methodid and wait < 4: |
59 |
sleep(wait) |
60 |
wait = 2 * wait
|
61 |
self._value = methodid
|
62 |
self.assertTrue(wait < 4) |
63 |
|
64 |
def setUp(self): |
65 |
from kamaki.clients import SilentEvent |
66 |
self.SE = SilentEvent
|
67 |
|
68 |
def test_threads(self): |
69 |
threads = [] |
70 |
for i in range(4): |
71 |
threads.append(self.SE(self.thread_content, i)) |
72 |
|
73 |
for t in threads: |
74 |
t.start() |
75 |
|
76 |
for i in range(4): |
77 |
self.assertTrue(threads[i].is_alive())
|
78 |
self.can_finish = i
|
79 |
threads[i].join() |
80 |
self.assertFalse(threads[i].is_alive())
|
81 |
|
82 |
|
83 |
# TestCase auxiliary methods
|
84 |
|
85 |
def runTestCase(cls, test_name, args=[], failure_collector=[]): |
86 |
"""
|
87 |
:param cls: (TestCase) a set of Tests
|
88 |
|
89 |
:param test_name: (str)
|
90 |
|
91 |
:param args: (list) these are prefixed with test_ and used as params when
|
92 |
instantiating cls
|
93 |
|
94 |
:param failure_collector: (list) collects info of test failures
|
95 |
|
96 |
:returns: (int) total # of run tests
|
97 |
"""
|
98 |
suite = TestSuite() |
99 |
if args:
|
100 |
suite.addTest(cls('_'.join(['test'] + args))) |
101 |
else:
|
102 |
suite.addTest(makeSuite(cls)) |
103 |
print('* Test * %s *' % test_name)
|
104 |
r = TextTestRunner(verbosity=2).run(suite)
|
105 |
failure_collector += r.failures |
106 |
return r.testsRun
|
107 |
|
108 |
|
109 |
def _add_value(foo, value): |
110 |
def wrap(self): |
111 |
return foo(self, value) |
112 |
return wrap
|
113 |
|
114 |
|
115 |
def get_test_classes(module=__import__(__name__), name=''): |
116 |
module_stack = [module] |
117 |
while module_stack:
|
118 |
module = module_stack[-1]
|
119 |
module_stack = module_stack[:-1]
|
120 |
for objname, obj in getmembers(module): |
121 |
if (objname == name or not name): |
122 |
if isclass(obj) and objname != 'TestCase' and ( |
123 |
issubclass(obj, TestCase)):
|
124 |
yield (obj, objname)
|
125 |
|
126 |
|
127 |
def main(argv): |
128 |
found = False
|
129 |
failure_collector = list()
|
130 |
num_of_tests = 0
|
131 |
for cls, name in get_test_classes(name=argv[1] if len(argv) > 1 else ''): |
132 |
found = True
|
133 |
num_of_tests += runTestCase(cls, name, argv[2:], failure_collector)
|
134 |
if not found: |
135 |
print('Test "%s" not found' % ' '.join(argv[1:])) |
136 |
else:
|
137 |
for i, failure in enumerate(failure_collector): |
138 |
print('Failure %s: ' % (i + 1)) |
139 |
for field in failure: |
140 |
print('\t%s' % field)
|
141 |
print('\nTotal tests run: %s' % num_of_tests)
|
142 |
print('Total failures: %s' % len(failure_collector)) |
143 |
|
144 |
|
145 |
if __name__ == '__main__': |
146 |
from sys import argv |
147 |
main(argv) |