root / kamaki / clients / tests / __init__.py @ b482315a
History | View | Annotate | Download (5.7 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 TestCase, TestSuite, makeSuite, TextTestRunner |
35 |
from argparse import ArgumentParser |
36 |
from sys import stdout |
37 |
from traceback import extract_stack, print_stack |
38 |
from progress.bar import ShadyBar |
39 |
|
40 |
from kamaki.cli.config import Config |
41 |
|
42 |
|
43 |
def _add_value(foo, value): |
44 |
def wrap(self): |
45 |
return foo(self, value) |
46 |
return wrap
|
47 |
|
48 |
|
49 |
class Generic(TestCase): |
50 |
|
51 |
_cnf = None
|
52 |
_grp = None
|
53 |
_fetched = {} |
54 |
|
55 |
def __init__(self, specific=None, config_file=None, group=None): |
56 |
super(Generic, self).__init__(specific) |
57 |
self._cnf = Config(config_file)
|
58 |
self._grp = group
|
59 |
|
60 |
def __getitem__(self, key): |
61 |
key = self._key(key)
|
62 |
try:
|
63 |
return self._fetched[key] |
64 |
except KeyError: |
65 |
return self._get_from_cnf(key) |
66 |
|
67 |
def _key(self, key): |
68 |
return ('', key) if isinstance(key, str)\ |
69 |
else ('', key[0]) if len(key) == 1\ |
70 |
else key
|
71 |
|
72 |
def _get_from_cnf(self, key): |
73 |
val = 0
|
74 |
if key[0]: |
75 |
val = self._cnf.get('test', '%s_%s' % key)\ |
76 |
or self._cnf.get(*key) |
77 |
if not val: |
78 |
val = self._cnf.get('test', key[1])\ |
79 |
or self._cnf.get('global', key[1]) |
80 |
self._fetched[key] = val
|
81 |
return val
|
82 |
|
83 |
def _safe_progress_bar(self, msg): |
84 |
"""Try to get a progress bar, but do not raise errors"""
|
85 |
try:
|
86 |
progress_bar = ShadyBar() |
87 |
gen = progress_bar.get_generator(msg) |
88 |
except Exception: |
89 |
return (None, None) |
90 |
return (progress_bar, gen)
|
91 |
|
92 |
def _safe_progress_bar_finish(self, progress_bar): |
93 |
try:
|
94 |
progress_bar.finish() |
95 |
except Exception: |
96 |
pass
|
97 |
|
98 |
def do_with_progress_bar(self, action, msg, list, *args, **kwargs): |
99 |
(action_bar, action_cb) = self._safe_progress_bar('') |
100 |
|
101 |
def assert_dicts_are_deeply_equal(self, d1, d2): |
102 |
for k, v in d1.items(): |
103 |
self.assertTrue(k in d2) |
104 |
if isinstance(v, dict): |
105 |
self.assert_dicts_are_deeply_equal(v, d2[k])
|
106 |
else:
|
107 |
self.assertEqual(unicode(v), unicode(d2[k])) |
108 |
|
109 |
def test_000(self): |
110 |
import inspect |
111 |
methods = [method for method in inspect.getmembers( |
112 |
self,
|
113 |
predicate=inspect.ismethod)\ |
114 |
if method[0].startswith('_test_')] |
115 |
failures = 0
|
116 |
for method in methods: |
117 |
stdout.write('Test %s' % method[0][6:]) |
118 |
try:
|
119 |
method[1]()
|
120 |
print(' ...ok')
|
121 |
except AssertionError: |
122 |
print(' FAIL: %s (%s)' % (method[0], method[1])) |
123 |
failures += 1
|
124 |
if failures:
|
125 |
raise AssertionError('%s failures' % failures) |
126 |
|
127 |
|
128 |
def init_parser(): |
129 |
parser = ArgumentParser(add_help=False)
|
130 |
parser.add_argument('-h', '--help', |
131 |
dest='help',
|
132 |
action='store_true',
|
133 |
default=False,
|
134 |
help="Show this help message and exit")
|
135 |
return parser
|
136 |
|
137 |
|
138 |
def main(argv): |
139 |
|
140 |
suiteFew = TestSuite() |
141 |
"""
|
142 |
if len(argv) == 0 or argv[0] == 'pithos':
|
143 |
if len(argv) == 1:
|
144 |
suiteFew.addTest(unittest.makeSuite(testPithos))
|
145 |
else:
|
146 |
suiteFew.addTest(testPithos('test_' + argv[1]))
|
147 |
if len(argv) == 0 or argv[0] == 'cyclades':
|
148 |
if len(argv) == 1:
|
149 |
#suiteFew.addTest(unittest.makeSuite(testCyclades))
|
150 |
suiteFew.addTest(testCyclades('test_000'))
|
151 |
else:
|
152 |
suiteFew.addTest(testCyclades('test_' + argv[1]))
|
153 |
"""
|
154 |
if len(argv) == 0 or argv[0] == 'image': |
155 |
from kamaki.clients.tests.image import Image |
156 |
test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000') |
157 |
suiteFew.addTest(Image(test_method)) |
158 |
if len(argv) == 0 or argv[0] == 'astakos': |
159 |
from kamaki.clients.tests.astakos import Astakos |
160 |
if len(argv) == 1: |
161 |
suiteFew.addTest(makeSuite(Astakos)) |
162 |
else:
|
163 |
suiteFew.addTest(Astakos('test_' + argv[1])) |
164 |
|
165 |
TextTestRunner(verbosity=2).run(suiteFew)
|
166 |
|
167 |
if __name__ == '__main__': |
168 |
parser = init_parser() |
169 |
args, argv = parser.parse_known_args() |
170 |
if len(argv) > 2 or getattr(args, 'help') or len(argv) < 1: |
171 |
raise Exception('\tusage: tests.py <group> [command]') |
172 |
main(argv) |