Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / test.py @ 6695edc9

History | View | Annotate | Download (3.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.astakos.test import Astakos
39
from kamaki.clients.compute.test import Compute, ComputeRestApi
40
from kamaki.clients.cyclades.test import Cyclades
41
from kamaki.clients.image.test import Image
42
from kamaki.clients.storage.test import Storage
43
from kamaki.clients.pithos.test import Pithos
44

    
45

    
46
class SilentEvent(TestCase):
47

    
48
    can_finish = -1
49

    
50
    def thread_content(self, methodid):
51
        wait = 0.1
52
        while self.can_finish < methodid and wait < 4:
53
            sleep(wait)
54
            wait = 2 * wait
55
        self._value = methodid
56
        self.assertTrue(wait < 4)
57

    
58
    def setUp(self):
59
        from kamaki.clients import SilentEvent
60
        self.SE = SilentEvent
61

    
62
    def test_threads(self):
63
        threads = []
64
        for i in range(4):
65
            threads.append(self.SE(self.thread_content, i))
66

    
67
        for t in threads:
68
            t.start()
69

    
70
        for i in range(4):
71
            self.assertTrue(threads[i].is_alive())
72
            self.can_finish = i
73
            threads[i].join()
74
            self.assertFalse(threads[i].is_alive())
75

    
76

    
77
#  TestCase auxiliary methods
78

    
79
def runTestCase(cls, test_name, args=[]):
80
    suite = TestSuite()
81
    if args:
82
        suite.addTest(cls('_'.join(['test'] + args)))
83
    else:
84
        suite.addTest(makeSuite(cls))
85
    print('* Test * %s *' % test_name)
86
    TextTestRunner(verbosity=2).run(suite)
87

    
88

    
89
def _add_value(foo, value):
90
    def wrap(self):
91
        return foo(self, value)
92
    return wrap
93

    
94

    
95
def get_test_classes(module=__import__(__name__), name=''):
96
    module_stack = [module]
97
    while module_stack:
98
        module = module_stack[-1]
99
        module_stack = module_stack[:-1]
100
        for objname, obj in getmembers(module):
101
            if (objname == name or not name):
102
                if isclass(obj) and objname != 'TestCase' and (
103
                        issubclass(obj, TestCase)):
104
                    yield (obj, objname)
105

    
106

    
107
def main(argv):
108
    found = False
109
    for cls, name in get_test_classes(name=argv[1] if len(argv) > 1 else ''):
110
        found = True
111
        runTestCase(cls, name, argv[2:])
112
    if not found:
113
        print('Test "%s" not found' % ' '.join(argv[1:]))
114

    
115

    
116
if __name__ == '__main__':
117
    from sys import argv
118
    main(argv)