Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / test.py @ 85898ca4

History | View | Annotate | Download (3.7 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.cyclades.test import Cyclades
40
from kamaki.clients.image.test import Image
41
from kamaki.clients.pithos.test import Pithos
42

    
43

    
44
class SilentEvent(TestCase):
45

    
46
    can_finish = -1
47

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

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

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

    
65
        for t in threads:
66
            t.start()
67

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

    
74

    
75
#  TestCase auxiliary methods
76

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

    
86

    
87
def _add_value(foo, value):
88
    def wrap(self):
89
        return foo(self, value)
90
    return wrap
91

    
92

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

    
104

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

    
113

    
114
if __name__ == '__main__':
115
    from sys import argv
116
    main(argv)