root / kamaki / clients / test / __init__.py @ 03493855
History | View | Annotate | Download (3.6 kB)
1 |
# Copyright 2012-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 |
|
37 |
from kamaki.clients.test.astakos import Astakos |
38 |
from kamaki.clients.test.cyclades import Cyclades |
39 |
from kamaki.clients.test.image import Image |
40 |
#from kamaki.clients.test.pithos import Pithos
|
41 |
|
42 |
|
43 |
def _add_value(foo, value): |
44 |
def wrap(self): |
45 |
return foo(self, value) |
46 |
return wrap
|
47 |
|
48 |
|
49 |
def get_test_classes(module=__import__(__name__), name=''): |
50 |
from inspect import getmembers, isclass |
51 |
for objname, obj in getmembers(module): |
52 |
from unittest import TestCase |
53 |
if (objname == name or not name) and isclass(obj) and ( |
54 |
issubclass(obj, TestCase)):
|
55 |
yield (obj, objname)
|
56 |
|
57 |
|
58 |
class SilentEvent(TestCase): |
59 |
|
60 |
can_finish = -1
|
61 |
|
62 |
def thread_content(self, methodid): |
63 |
wait = 0.1
|
64 |
while self.can_finish < methodid and wait < 4: |
65 |
sleep(wait) |
66 |
wait = 2 * wait
|
67 |
self._value = methodid
|
68 |
self.assertTrue(wait < 4) |
69 |
|
70 |
def setUp(self): |
71 |
from kamaki.clients import SilentEvent |
72 |
self.SE = SilentEvent
|
73 |
|
74 |
def test_threads(self): |
75 |
threads = [] |
76 |
for i in range(4): |
77 |
threads.append(self.SE(self.thread_content, i)) |
78 |
|
79 |
for t in threads: |
80 |
t.start() |
81 |
|
82 |
for i in range(4): |
83 |
self.assertTrue(threads[i].is_alive())
|
84 |
self.can_finish = i
|
85 |
threads[i].join() |
86 |
self.assertFalse(threads[i].is_alive())
|
87 |
|
88 |
|
89 |
def main(argv): |
90 |
found = False
|
91 |
for cls, name in get_test_classes(name=argv[1] if len(argv) > 1 else ''): |
92 |
found = True
|
93 |
args = argv[2:]
|
94 |
suite = TestSuite() |
95 |
if args:
|
96 |
try:
|
97 |
suite.addTest(cls('_'.join(['test'] + args))) |
98 |
except ValueError: |
99 |
print('Test %s not found in %s suite' % (' '.join(args), name)) |
100 |
continue
|
101 |
else:
|
102 |
suite.addTest(makeSuite(cls)) |
103 |
print('Test %s' % name)
|
104 |
TextTestRunner(verbosity=2).run(suite)
|
105 |
if not found: |
106 |
print('Test "%s" not found' % ' '.join(argv[1:])) |
107 |
|
108 |
|
109 |
if __name__ == '__main__': |
110 |
from sys import argv |
111 |
main(argv) |