1 # Copyright 2012-2013 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
35 from unittest import TestCase, TestSuite, TextTestRunner
36 from argparse import ArgumentParser
37 from sys import stdout
39 from kamaki.cli.config import Config
40 from kamaki.cli.utils import spiner
43 def _add_value(foo, value):
45 return foo(self, value)
49 class Generic(TestCase):
56 def __init__(self, specific=None, config=None, group=None):
57 super(Generic, self).__init__(specific)
58 self._cnf = config or Config()
60 self._waits.append(0.71828)
62 self._waits.append(self._waits[-1] * 2.71828)
64 def __getitem__(self, key):
67 r = self._fetched[key]
69 return self._fetched[key]
71 r = self._get_from_cnf(key)
73 return self._get_from_cnf(key)
76 return ('', key) if isinstance(key, str)\
77 else ('', key[0]) if len(key) == 1\
80 def _get_from_cnf(self, key):
83 keystr = '%s_%s' % key
84 val = self._cnf.get('livetest', keystr) or self._cnf.get(*key)
86 val = self._cnf.get('livetest', key[1]) or self._cnf.get(
89 self._fetched[key] = val
92 def _safe_progress_bar(self, msg):
93 """Try to get a progress bar, but do not raise errors"""
95 from progress.bar import ShadyBar
96 wait_bar = ShadyBar(msg)
99 for i in wait_bar.iter(range(int(n))):
104 stdout.write('%s:' % msg)
105 (wait_bar, wait_cb) = (None, spiner)
106 return (wait_bar, wait_cb)
108 def _safe_progress_bar_finish(self, progress_bar):
110 progress_bar.finish()
114 def do_with_progress_bar(self, action, msg, items):
116 print('%s: DONE' % msg)
118 (action_bar, action_cb) = self._safe_progress_bar(msg)
119 action_gen = action_cb(len(items))
127 self._safe_progress_bar_finish(action_bar)
129 def assert_dicts_are_deeply_equal(self, d1, d2):
130 (st1, st2) = (set(d1.keys()), set(d2.keys()))
131 diff1 = st1.difference(st2)
132 diff2 = st2.difference(st1)
134 not (diff1 or diff2),
135 'Key differences:\n\td1-d2=%s\n\td2-d1=%s' % (diff1, diff2))
136 for k, v in d1.items():
137 if isinstance(v, dict):
138 self.assert_dicts_are_deeply_equal(v, d2[k])
140 self.assertEqual(unicode(v), unicode(d2[k]))
144 methods = [method for method in inspect.getmembers(
146 predicate=inspect.ismethod) if method[0].startswith('_test_')]
148 for method in methods:
149 stdout.write('Test %s ' % method[0][6:])
154 except AssertionError:
155 print(' FAIL: %s (%s)' % (method[0], method[1]))
158 raise AssertionError('%s failures' % failures)
162 parser = ArgumentParser(add_help=False)
168 help="Show this help message and exit")
173 _main(argv, config=None)
176 def _main(argv, config=None):
177 suiteFew = TestSuite()
178 if len(argv) == 0 or argv[0] == 'pithos':
179 from kamaki.clients.livetest.pithos import Pithos
180 test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000')
181 suiteFew.addTest(Pithos(test_method, config))
182 if len(argv) == 0 or argv[0] == 'cyclades':
183 from kamaki.clients.livetest.cyclades import Cyclades
184 test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000')
185 suiteFew.addTest(Cyclades(test_method, config))
186 if len(argv) == 0 or argv[0] == 'image':
187 from kamaki.clients.livetest.image import Image
188 test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000')
189 suiteFew.addTest(Image(test_method, config))
190 if len(argv) == 0 or argv[0] == 'astakos':
191 from kamaki.clients.livetest.astakos import Astakos
192 test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000')
193 suiteFew.addTest(Astakos(test_method, config))
195 TextTestRunner(verbosity=2).run(suiteFew)
197 if __name__ == '__main__':
198 parser = init_parser()
199 args, argv = parser.parse_known_args()
200 if len(argv) > 2 or getattr(args, 'help') or len(argv) < 1:
201 raise Exception('\tusage: livetest <group> [command]')