root / kamaki / clients / livetest / __init__.py @ cedde35d
History | View | Annotate | Download (7 kB)
1 | 6d192774 | Stavros Sachtouris | # Copyright 2012-2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 6d192774 | Stavros Sachtouris | #
|
3 | 6d192774 | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 6d192774 | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 6d192774 | Stavros Sachtouris | # conditions are met:
|
6 | 6d192774 | Stavros Sachtouris | #
|
7 | 6d192774 | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 6d192774 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 6d192774 | Stavros Sachtouris | # disclaimer.
|
10 | 6d192774 | Stavros Sachtouris | #
|
11 | 6d192774 | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 6d192774 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 6d192774 | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 6d192774 | Stavros Sachtouris | # provided with the distribution.
|
15 | 6d192774 | Stavros Sachtouris | #
|
16 | 6d192774 | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 6d192774 | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 6d192774 | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 6d192774 | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 6d192774 | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 6d192774 | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 6d192774 | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 6d192774 | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 6d192774 | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 6d192774 | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 6d192774 | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 6d192774 | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 6d192774 | Stavros Sachtouris | #
|
29 | 6d192774 | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 6d192774 | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 6d192774 | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 6d192774 | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | 6d192774 | Stavros Sachtouris | |
34 | 5207c784 | Stavros Sachtouris | import inspect |
35 | 5207c784 | Stavros Sachtouris | from unittest import TestCase, TestSuite, TextTestRunner |
36 | 1f0370c9 | Stavros Sachtouris | from argparse import ArgumentParser |
37 | 54d7c02a | Stavros Sachtouris | from sys import stdout |
38 | 1f0370c9 | Stavros Sachtouris | |
39 | 6d192774 | Stavros Sachtouris | from kamaki.cli.config import Config |
40 | 02846a75 | Stavros Sachtouris | from kamaki.cli.utils import spiner |
41 | 6d192774 | Stavros Sachtouris | |
42 | 6d192774 | Stavros Sachtouris | |
43 | 6d192774 | Stavros Sachtouris | def _add_value(foo, value): |
44 | 6d192774 | Stavros Sachtouris | def wrap(self): |
45 | 6d192774 | Stavros Sachtouris | return foo(self, value) |
46 | 6d192774 | Stavros Sachtouris | return wrap
|
47 | 6d192774 | Stavros Sachtouris | |
48 | 5207c784 | Stavros Sachtouris | |
49 | 1f0370c9 | Stavros Sachtouris | class Generic(TestCase): |
50 | 6d192774 | Stavros Sachtouris | |
51 | 02846a75 | Stavros Sachtouris | _waits = [] |
52 | 6d192774 | Stavros Sachtouris | _cnf = None
|
53 | 1f0370c9 | Stavros Sachtouris | _grp = None
|
54 | 1f0370c9 | Stavros Sachtouris | _fetched = {} |
55 | 6d192774 | Stavros Sachtouris | |
56 | d1f78278 | Stavros Sachtouris | def __init__(self, specific=None, config=None, group=None): |
57 | 1f0370c9 | Stavros Sachtouris | super(Generic, self).__init__(specific) |
58 | d1f78278 | Stavros Sachtouris | self._cnf = config or Config() |
59 | 1f0370c9 | Stavros Sachtouris | self._grp = group
|
60 | 02846a75 | Stavros Sachtouris | self._waits.append(0.71828) |
61 | 02846a75 | Stavros Sachtouris | for i in range(10): |
62 | 02846a75 | Stavros Sachtouris | self._waits.append(self._waits[-1] * 2.71828) |
63 | 1f0370c9 | Stavros Sachtouris | |
64 | 1f0370c9 | Stavros Sachtouris | def __getitem__(self, key): |
65 | 1f0370c9 | Stavros Sachtouris | key = self._key(key)
|
66 | 1f0370c9 | Stavros Sachtouris | try:
|
67 | f5f2dc53 | Stavros Sachtouris | r = self._fetched[key]
|
68 | f5f2dc53 | Stavros Sachtouris | return r
|
69 | 1f0370c9 | Stavros Sachtouris | return self._fetched[key] |
70 | 1f0370c9 | Stavros Sachtouris | except KeyError: |
71 | f5f2dc53 | Stavros Sachtouris | r = self._get_from_cnf(key)
|
72 | f5f2dc53 | Stavros Sachtouris | return r
|
73 | 1f0370c9 | Stavros Sachtouris | return self._get_from_cnf(key) |
74 | 1f0370c9 | Stavros Sachtouris | |
75 | 1f0370c9 | Stavros Sachtouris | def _key(self, key): |
76 | 1f0370c9 | Stavros Sachtouris | return ('', key) if isinstance(key, str)\ |
77 | 1f0370c9 | Stavros Sachtouris | else ('', key[0]) if len(key) == 1\ |
78 | 1f0370c9 | Stavros Sachtouris | else key
|
79 | 1f0370c9 | Stavros Sachtouris | |
80 | 1f0370c9 | Stavros Sachtouris | def _get_from_cnf(self, key): |
81 | 420598d0 | Stavros Sachtouris | val = 0
|
82 | 420598d0 | Stavros Sachtouris | if key[0]: |
83 | f5f2dc53 | Stavros Sachtouris | keystr = '%s_%s' % key
|
84 | f5f2dc53 | Stavros Sachtouris | val = self._cnf.get('livetest', keystr) or self._cnf.get(*key) |
85 | 420598d0 | Stavros Sachtouris | if not val: |
86 | f5f2dc53 | Stavros Sachtouris | val = self._cnf.get('livetest', key[1]) or self._cnf.get( |
87 | a2ba4204 | Stavros Sachtouris | 'global',
|
88 | a2ba4204 | Stavros Sachtouris | key[1])
|
89 | 1f0370c9 | Stavros Sachtouris | self._fetched[key] = val
|
90 | 1f0370c9 | Stavros Sachtouris | return val
|
91 | 1f0370c9 | Stavros Sachtouris | |
92 | b482315a | Stavros Sachtouris | def _safe_progress_bar(self, msg): |
93 | b482315a | Stavros Sachtouris | """Try to get a progress bar, but do not raise errors"""
|
94 | b482315a | Stavros Sachtouris | try:
|
95 | b4a7aa1f | Stavros Sachtouris | from progress.bar import ShadyBar |
96 | 02846a75 | Stavros Sachtouris | wait_bar = ShadyBar(msg) |
97 | 02846a75 | Stavros Sachtouris | |
98 | 02846a75 | Stavros Sachtouris | def wait_gen(n): |
99 | 02846a75 | Stavros Sachtouris | for i in wait_bar.iter(range(int(n))): |
100 | 02846a75 | Stavros Sachtouris | yield
|
101 | 02846a75 | Stavros Sachtouris | yield
|
102 | 02846a75 | Stavros Sachtouris | wait_cb = wait_gen |
103 | b482315a | Stavros Sachtouris | except Exception: |
104 | 02846a75 | Stavros Sachtouris | stdout.write('%s:' % msg)
|
105 | 02846a75 | Stavros Sachtouris | (wait_bar, wait_cb) = (None, spiner)
|
106 | 02846a75 | Stavros Sachtouris | return (wait_bar, wait_cb)
|
107 | b482315a | Stavros Sachtouris | |
108 | b482315a | Stavros Sachtouris | def _safe_progress_bar_finish(self, progress_bar): |
109 | b482315a | Stavros Sachtouris | try:
|
110 | b482315a | Stavros Sachtouris | progress_bar.finish() |
111 | b482315a | Stavros Sachtouris | except Exception: |
112 | b58c1078 | Stavros Sachtouris | print('\b DONE')
|
113 | 02846a75 | Stavros Sachtouris | |
114 | 02846a75 | Stavros Sachtouris | def do_with_progress_bar(self, action, msg, items): |
115 | 02846a75 | Stavros Sachtouris | if not items: |
116 | 02846a75 | Stavros Sachtouris | print('%s: DONE' % msg)
|
117 | 02846a75 | Stavros Sachtouris | return
|
118 | 02846a75 | Stavros Sachtouris | (action_bar, action_cb) = self._safe_progress_bar(msg)
|
119 | 02846a75 | Stavros Sachtouris | action_gen = action_cb(len(items))
|
120 | d1f78278 | Stavros Sachtouris | try:
|
121 | d1f78278 | Stavros Sachtouris | action_gen.next() |
122 | d1f78278 | Stavros Sachtouris | except Exception: |
123 | d1f78278 | Stavros Sachtouris | pass
|
124 | 02846a75 | Stavros Sachtouris | for item in items: |
125 | 02846a75 | Stavros Sachtouris | action(item) |
126 | 02846a75 | Stavros Sachtouris | action_gen.next() |
127 | 02846a75 | Stavros Sachtouris | self._safe_progress_bar_finish(action_bar)
|
128 | b482315a | Stavros Sachtouris | |
129 | 114e19da | Stavros Sachtouris | def assert_dicts_are_equal(self, d1, d2): |
130 | 114e19da | Stavros Sachtouris | self.assertTrue(set(d1) == set(d2)) |
131 | 114e19da | Stavros Sachtouris | for k, v in d1.items(): |
132 | 114e19da | Stavros Sachtouris | if isinstance(v, dict): |
133 | 114e19da | Stavros Sachtouris | self.assert_dicts_are_equal(d1[k], d2[k])
|
134 | 114e19da | Stavros Sachtouris | else:
|
135 | 114e19da | Stavros Sachtouris | self.assertEqual(v, d2[k])
|
136 | 114e19da | Stavros Sachtouris | |
137 | b482315a | Stavros Sachtouris | def assert_dicts_are_deeply_equal(self, d1, d2): |
138 | c088076b | Stavros Sachtouris | st1 = set([unicode(k) for k in d1]) |
139 | c088076b | Stavros Sachtouris | st2 = set([unicode(k) for k in d2]) |
140 | 24ff0a35 | Stavros Sachtouris | diff1 = st1.difference(st2) |
141 | 24ff0a35 | Stavros Sachtouris | diff2 = st2.difference(st1) |
142 | 24ff0a35 | Stavros Sachtouris | self.assertTrue(
|
143 | 24ff0a35 | Stavros Sachtouris | not (diff1 or diff2), |
144 | 24ff0a35 | Stavros Sachtouris | 'Key differences:\n\td1-d2=%s\n\td2-d1=%s' % (diff1, diff2))
|
145 | b482315a | Stavros Sachtouris | for k, v in d1.items(): |
146 | b482315a | Stavros Sachtouris | if isinstance(v, dict): |
147 | b482315a | Stavros Sachtouris | self.assert_dicts_are_deeply_equal(v, d2[k])
|
148 | b482315a | Stavros Sachtouris | else:
|
149 | b482315a | Stavros Sachtouris | self.assertEqual(unicode(v), unicode(d2[k])) |
150 | b482315a | Stavros Sachtouris | |
151 | 54d7c02a | Stavros Sachtouris | def test_000(self): |
152 | 5207c784 | Stavros Sachtouris | print('')
|
153 | 54d7c02a | Stavros Sachtouris | methods = [method for method in inspect.getmembers( |
154 | 54d7c02a | Stavros Sachtouris | self,
|
155 | 24ff0a35 | Stavros Sachtouris | predicate=inspect.ismethod) if method[0].startswith('_test_')] |
156 | d2e1b032 | Stavros Sachtouris | failures = 0
|
157 | 54d7c02a | Stavros Sachtouris | for method in methods: |
158 | a3bca46c | Stavros Sachtouris | stdout.write('Test %s ' % method[0][6:]) |
159 | a3bca46c | Stavros Sachtouris | stdout.flush() |
160 | 54d7c02a | Stavros Sachtouris | try:
|
161 | 54d7c02a | Stavros Sachtouris | method[1]()
|
162 | 54d7c02a | Stavros Sachtouris | print(' ...ok')
|
163 | 54d7c02a | Stavros Sachtouris | except AssertionError: |
164 | 54d7c02a | Stavros Sachtouris | print(' FAIL: %s (%s)' % (method[0], method[1])) |
165 | d2e1b032 | Stavros Sachtouris | failures += 1
|
166 | d2e1b032 | Stavros Sachtouris | if failures:
|
167 | d2e1b032 | Stavros Sachtouris | raise AssertionError('%s failures' % failures) |
168 | 54d7c02a | Stavros Sachtouris | |
169 | 1f0370c9 | Stavros Sachtouris | |
170 | 1f0370c9 | Stavros Sachtouris | def init_parser(): |
171 | 1f0370c9 | Stavros Sachtouris | parser = ArgumentParser(add_help=False)
|
172 | 24ff0a35 | Stavros Sachtouris | parser.add_argument( |
173 | 24ff0a35 | Stavros Sachtouris | '-h', '--help', |
174 | 1f0370c9 | Stavros Sachtouris | dest='help',
|
175 | 1f0370c9 | Stavros Sachtouris | action='store_true',
|
176 | 1f0370c9 | Stavros Sachtouris | default=False,
|
177 | 1f0370c9 | Stavros Sachtouris | help="Show this help message and exit")
|
178 | 1f0370c9 | Stavros Sachtouris | return parser
|
179 | 1f0370c9 | Stavros Sachtouris | |
180 | 1f0370c9 | Stavros Sachtouris | |
181 | e7e7dbbd | Stavros Sachtouris | def main(argv, config=None): |
182 | 1f0370c9 | Stavros Sachtouris | suiteFew = TestSuite() |
183 | 1f0370c9 | Stavros Sachtouris | if len(argv) == 0 or argv[0] == 'pithos': |
184 | f5f2dc53 | Stavros Sachtouris | from kamaki.clients.livetest.pithos import Pithos |
185 | a3bca46c | Stavros Sachtouris | test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000') |
186 | a3bca46c | Stavros Sachtouris | suiteFew.addTest(Pithos(test_method, config)) |
187 | 02846a75 | Stavros Sachtouris | if len(argv) == 0 or argv[0] == 'cyclades': |
188 | f5f2dc53 | Stavros Sachtouris | from kamaki.clients.livetest.cyclades import Cyclades |
189 | 02846a75 | Stavros Sachtouris | test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000') |
190 | d1f78278 | Stavros Sachtouris | suiteFew.addTest(Cyclades(test_method, config)) |
191 | 1f0370c9 | Stavros Sachtouris | if len(argv) == 0 or argv[0] == 'image': |
192 | f5f2dc53 | Stavros Sachtouris | from kamaki.clients.livetest.image import Image |
193 | 54d7c02a | Stavros Sachtouris | test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000') |
194 | d1f78278 | Stavros Sachtouris | suiteFew.addTest(Image(test_method, config)) |
195 | 1f0370c9 | Stavros Sachtouris | if len(argv) == 0 or argv[0] == 'astakos': |
196 | f5f2dc53 | Stavros Sachtouris | from kamaki.clients.livetest.astakos import Astakos |
197 | 5207c784 | Stavros Sachtouris | test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000') |
198 | 5207c784 | Stavros Sachtouris | suiteFew.addTest(Astakos(test_method, config)) |
199 | 1f0370c9 | Stavros Sachtouris | |
200 | 1f0370c9 | Stavros Sachtouris | TextTestRunner(verbosity=2).run(suiteFew)
|
201 | 1f0370c9 | Stavros Sachtouris | |
202 | 1f0370c9 | Stavros Sachtouris | if __name__ == '__main__': |
203 | 1f0370c9 | Stavros Sachtouris | parser = init_parser() |
204 | 1f0370c9 | Stavros Sachtouris | args, argv = parser.parse_known_args() |
205 | 1f0370c9 | Stavros Sachtouris | if len(argv) > 2 or getattr(args, 'help') or len(argv) < 1: |
206 | f5f2dc53 | Stavros Sachtouris | raise Exception('\tusage: livetest <group> [command]') |
207 | 1f0370c9 | Stavros Sachtouris | main(argv) |