Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / test.py @ c608d6e9

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.storage.test import Storage
42
from kamaki.clients.pithos.test import Pithos
43

    
44

    
45
class SilentEvent(TestCase):
46

    
47
    can_finish = -1
48

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

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

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

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

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

    
75

    
76
#  TestCase auxiliary methods
77

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

    
87

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

    
93

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

    
105

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

    
114

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