Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / test.py @ f4de4c91

History | View | Annotate | Download (6.5 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
from json import loads
38

    
39
from kamaki.clients.connection.test import (
40
    KamakiConnection,
41
    KamakiHTTPConnection,
42
    KamakiResponse,
43
    KamakiHTTPResponse)
44
from kamaki.clients.utils.test import Utils
45
from kamaki.clients.astakos.test import Astakos
46
from kamaki.clients.compute.test import Compute, ComputeRest
47
from kamaki.clients.cyclades.test import Cyclades, CycladesRest
48
from kamaki.clients.image.test import Image
49
from kamaki.clients.storage.test import Storage
50
from kamaki.clients.pithos.test import Pithos, PithosRest
51

    
52

    
53
class ClientError(TestCase):
54

    
55
    def test___init__(self):
56
        from kamaki.clients import ClientError
57
        for msg, status, details, exp_msg, exp_status, exp_details in (
58
                ('some msg', 42, 0.28, 0, 0, 0),
59
                ('some msg', 'fail', [], 0, 0, 0),
60
                ('some msg', 42, 'details on error', 0, 0, 0),
61
                (
62
                    '404 {"ExampleError":'
63
                    ' {"message": "a msg", "code": 42, "details": "dets"}}',
64
                    404,
65
                    0,
66
                    '404 ExampleError (a msg)\n',
67
                    42,
68
                    ['dets']),
69
                (
70
                    '404 {"ExampleError":'
71
                    ' {"message": "a msg", "code": 42}}',
72
                    404,
73
                    'details on error',
74
                    '404 ExampleError (a msg)\n',
75
                    42,
76
                    0),
77
                (
78
                    '404 {"ExampleError":'
79
                    ' {"details": "Explain your error"}}',
80
                    404,
81
                    'details on error',
82
                    '404 ExampleError',
83
                    0,
84
                    ['details on error', 'Explain your error']),
85
                ('some msg\n', -10, ['details', 'on', 'error'], 0, 0, 0)):
86
            ce = ClientError(msg, status, details)
87
            exp_msg = exp_msg or (msg if msg.endswith('\n') else msg + '\n')
88
            exp_status = exp_status or status
89
            exp_details = exp_details or details
90
            self.assertEqual('%s' % ce, exp_msg)
91
            self.assertEqual(
92
                exp_status if isinstance(exp_status, int) else 0,
93
                ce.status)
94
            self.assertEqual(exp_details, ce.details)
95

    
96

    
97
class SilentEvent(TestCase):
98

    
99
    def thread_content(self, methodid):
100
        wait = 0.1
101
        self.can_finish = -1
102
        while self.can_finish < methodid and wait < 4:
103
            sleep(wait)
104
            wait = 2 * wait
105
        self._value = methodid
106
        self.assertTrue(wait < 4)
107

    
108
    def setUp(self):
109
        from kamaki.clients import SilentEvent
110
        self.SE = SilentEvent
111

    
112
    def test_threads(self):
113
        threads = []
114
        for i in range(4):
115
            threads.append(self.SE(self.thread_content, i))
116

    
117
        for t in threads:
118
            t.start()
119

    
120
        for i in range(4):
121
            self.assertTrue(threads[i].is_alive())
122
            self.can_finish = i
123
            threads[i].join()
124
            self.assertFalse(threads[i].is_alive())
125

    
126

    
127
#  TestCase auxiliary methods
128

    
129
def runTestCase(cls, test_name, args=[], failure_collector=[]):
130
    """
131
    :param cls: (TestCase) a set of Tests
132

133
    :param test_name: (str)
134

135
    :param args: (list) these are prefixed with test_ and used as params when
136
        instantiating cls
137

138
    :param failure_collector: (list) collects info of test failures
139

140
    :returns: (int) total # of run tests
141
    """
142
    suite = TestSuite()
143
    if args:
144
        suite.addTest(cls('_'.join(['test'] + args)))
145
    else:
146
        suite.addTest(makeSuite(cls))
147
    print('* Test * %s *' % test_name)
148
    r = TextTestRunner(verbosity=2).run(suite)
149
    failure_collector += r.failures
150
    return r.testsRun
151

    
152

    
153
def _add_value(foo, value):
154
    def wrap(self):
155
        return foo(self, value)
156
    return wrap
157

    
158

    
159
def get_test_classes(module=__import__(__name__), name=''):
160
    module_stack = [module]
161
    while module_stack:
162
        module = module_stack[-1]
163
        module_stack = module_stack[:-1]
164
        for objname, obj in getmembers(module):
165
            if (objname == name or not name):
166
                if isclass(obj) and objname != 'TestCase' and (
167
                        issubclass(obj, TestCase)):
168
                    yield (obj, objname)
169

    
170

    
171
def main(argv):
172
    found = False
173
    failure_collector = list()
174
    num_of_tests = 0
175
    for cls, name in get_test_classes(name=argv[1] if len(argv) > 1 else ''):
176
        found = True
177
        num_of_tests += runTestCase(cls, name, argv[2:], failure_collector)
178
    if not found:
179
        print('Test "%s" not found' % ' '.join(argv[1:]))
180
    else:
181
        for i, failure in enumerate(failure_collector):
182
            print('Failure %s: ' % (i + 1))
183
            for field in failure:
184
                print('\t%s' % field)
185
        print('\nTotal tests run: %s' % num_of_tests)
186
        print('Total failures: %s' % len(failure_collector))
187

    
188

    
189
if __name__ == '__main__':
190
    from sys import argv
191
    main(argv)