root / kamaki / clients / tests / __init__.py @ d1f78278
History | View | Annotate | Download (6.4 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 | 1f0370c9 | Stavros Sachtouris | from unittest import TestCase, TestSuite, makeSuite, TextTestRunner |
35 | 1f0370c9 | Stavros Sachtouris | from argparse import ArgumentParser |
36 | 54d7c02a | Stavros Sachtouris | from sys import stdout |
37 | b482315a | Stavros Sachtouris | from progress.bar import ShadyBar |
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 | 1f0370c9 | Stavros Sachtouris | class Generic(TestCase): |
49 | 6d192774 | Stavros Sachtouris | |
50 | 02846a75 | Stavros Sachtouris | _waits = [] |
51 | 6d192774 | Stavros Sachtouris | _cnf = None
|
52 | 1f0370c9 | Stavros Sachtouris | _grp = None
|
53 | 1f0370c9 | Stavros Sachtouris | _fetched = {} |
54 | 6d192774 | Stavros Sachtouris | |
55 | d1f78278 | Stavros Sachtouris | def __init__(self, specific=None, config=None, group=None): |
56 | 1f0370c9 | Stavros Sachtouris | super(Generic, self).__init__(specific) |
57 | d1f78278 | Stavros Sachtouris | self._cnf = config or Config() |
58 | 1f0370c9 | Stavros Sachtouris | self._grp = group
|
59 | 02846a75 | Stavros Sachtouris | self._waits.append(0.71828) |
60 | 02846a75 | Stavros Sachtouris | for i in range(10): |
61 | 02846a75 | Stavros Sachtouris | self._waits.append(self._waits[-1] * 2.71828) |
62 | 1f0370c9 | Stavros Sachtouris | |
63 | 1f0370c9 | Stavros Sachtouris | def __getitem__(self, key): |
64 | 1f0370c9 | Stavros Sachtouris | key = self._key(key)
|
65 | 1f0370c9 | Stavros Sachtouris | try:
|
66 | 1f0370c9 | Stavros Sachtouris | return self._fetched[key] |
67 | 1f0370c9 | Stavros Sachtouris | except KeyError: |
68 | 1f0370c9 | Stavros Sachtouris | return self._get_from_cnf(key) |
69 | 1f0370c9 | Stavros Sachtouris | |
70 | 1f0370c9 | Stavros Sachtouris | def _key(self, key): |
71 | 1f0370c9 | Stavros Sachtouris | return ('', key) if isinstance(key, str)\ |
72 | 1f0370c9 | Stavros Sachtouris | else ('', key[0]) if len(key) == 1\ |
73 | 1f0370c9 | Stavros Sachtouris | else key
|
74 | 1f0370c9 | Stavros Sachtouris | |
75 | 1f0370c9 | Stavros Sachtouris | def _get_from_cnf(self, key): |
76 | 420598d0 | Stavros Sachtouris | val = 0
|
77 | 420598d0 | Stavros Sachtouris | if key[0]: |
78 | 420598d0 | Stavros Sachtouris | val = self._cnf.get('test', '%s_%s' % key)\ |
79 | 420598d0 | Stavros Sachtouris | or self._cnf.get(*key) |
80 | 420598d0 | Stavros Sachtouris | if not val: |
81 | 420598d0 | Stavros Sachtouris | val = self._cnf.get('test', key[1])\ |
82 | 420598d0 | Stavros Sachtouris | or self._cnf.get('global', key[1]) |
83 | 1f0370c9 | Stavros Sachtouris | self._fetched[key] = val
|
84 | 1f0370c9 | Stavros Sachtouris | return val
|
85 | 1f0370c9 | Stavros Sachtouris | |
86 | b482315a | Stavros Sachtouris | def _safe_progress_bar(self, msg): |
87 | b482315a | Stavros Sachtouris | """Try to get a progress bar, but do not raise errors"""
|
88 | b482315a | Stavros Sachtouris | try:
|
89 | 02846a75 | Stavros Sachtouris | wait_bar = ShadyBar(msg) |
90 | 02846a75 | Stavros Sachtouris | |
91 | 02846a75 | Stavros Sachtouris | def wait_gen(n): |
92 | 02846a75 | Stavros Sachtouris | for i in wait_bar.iter(range(int(n))): |
93 | 02846a75 | Stavros Sachtouris | yield
|
94 | 02846a75 | Stavros Sachtouris | yield
|
95 | 02846a75 | Stavros Sachtouris | wait_cb = wait_gen |
96 | b482315a | Stavros Sachtouris | except Exception: |
97 | 02846a75 | Stavros Sachtouris | stdout.write('%s:' % msg)
|
98 | 02846a75 | Stavros Sachtouris | (wait_bar, wait_cb) = (None, spiner)
|
99 | 02846a75 | Stavros Sachtouris | return (wait_bar, wait_cb)
|
100 | b482315a | Stavros Sachtouris | |
101 | b482315a | Stavros Sachtouris | def _safe_progress_bar_finish(self, progress_bar): |
102 | b482315a | Stavros Sachtouris | try:
|
103 | b482315a | Stavros Sachtouris | progress_bar.finish() |
104 | b482315a | Stavros Sachtouris | except Exception: |
105 | 02846a75 | Stavros Sachtouris | print(' DONE')
|
106 | 02846a75 | Stavros Sachtouris | |
107 | 02846a75 | Stavros Sachtouris | def do_with_progress_bar(self, action, msg, items): |
108 | 02846a75 | Stavros Sachtouris | if not items: |
109 | 02846a75 | Stavros Sachtouris | print('%s: DONE' % msg)
|
110 | 02846a75 | Stavros Sachtouris | return
|
111 | 02846a75 | Stavros Sachtouris | (action_bar, action_cb) = self._safe_progress_bar(msg)
|
112 | 02846a75 | Stavros Sachtouris | action_gen = action_cb(len(items))
|
113 | d1f78278 | Stavros Sachtouris | try:
|
114 | d1f78278 | Stavros Sachtouris | action_gen.next() |
115 | d1f78278 | Stavros Sachtouris | except Exception: |
116 | d1f78278 | Stavros Sachtouris | pass
|
117 | 02846a75 | Stavros Sachtouris | for item in items: |
118 | 02846a75 | Stavros Sachtouris | action(item) |
119 | 02846a75 | Stavros Sachtouris | action_gen.next() |
120 | 02846a75 | Stavros Sachtouris | self._safe_progress_bar_finish(action_bar)
|
121 | b482315a | Stavros Sachtouris | |
122 | b482315a | Stavros Sachtouris | def assert_dicts_are_deeply_equal(self, d1, d2): |
123 | b482315a | Stavros Sachtouris | for k, v in d1.items(): |
124 | b482315a | Stavros Sachtouris | self.assertTrue(k in d2) |
125 | b482315a | Stavros Sachtouris | if isinstance(v, dict): |
126 | b482315a | Stavros Sachtouris | self.assert_dicts_are_deeply_equal(v, d2[k])
|
127 | b482315a | Stavros Sachtouris | else:
|
128 | b482315a | Stavros Sachtouris | self.assertEqual(unicode(v), unicode(d2[k])) |
129 | b482315a | Stavros Sachtouris | |
130 | 54d7c02a | Stavros Sachtouris | def test_000(self): |
131 | 54d7c02a | Stavros Sachtouris | import inspect |
132 | 54d7c02a | Stavros Sachtouris | methods = [method for method in inspect.getmembers( |
133 | 54d7c02a | Stavros Sachtouris | self,
|
134 | 54d7c02a | Stavros Sachtouris | predicate=inspect.ismethod)\ |
135 | 54d7c02a | Stavros Sachtouris | if method[0].startswith('_test_')] |
136 | d2e1b032 | Stavros Sachtouris | failures = 0
|
137 | 54d7c02a | Stavros Sachtouris | for method in methods: |
138 | 54d7c02a | Stavros Sachtouris | stdout.write('Test %s' % method[0][6:]) |
139 | 54d7c02a | Stavros Sachtouris | try:
|
140 | 54d7c02a | Stavros Sachtouris | method[1]()
|
141 | 54d7c02a | Stavros Sachtouris | print(' ...ok')
|
142 | 54d7c02a | Stavros Sachtouris | except AssertionError: |
143 | 54d7c02a | Stavros Sachtouris | print(' FAIL: %s (%s)' % (method[0], method[1])) |
144 | d2e1b032 | Stavros Sachtouris | failures += 1
|
145 | d2e1b032 | Stavros Sachtouris | if failures:
|
146 | d2e1b032 | Stavros Sachtouris | raise AssertionError('%s failures' % failures) |
147 | 54d7c02a | Stavros Sachtouris | |
148 | 1f0370c9 | Stavros Sachtouris | |
149 | 1f0370c9 | Stavros Sachtouris | def init_parser(): |
150 | 1f0370c9 | Stavros Sachtouris | parser = ArgumentParser(add_help=False)
|
151 | 1f0370c9 | Stavros Sachtouris | parser.add_argument('-h', '--help', |
152 | 1f0370c9 | Stavros Sachtouris | dest='help',
|
153 | 1f0370c9 | Stavros Sachtouris | action='store_true',
|
154 | 1f0370c9 | Stavros Sachtouris | default=False,
|
155 | 1f0370c9 | Stavros Sachtouris | help="Show this help message and exit")
|
156 | 1f0370c9 | Stavros Sachtouris | return parser
|
157 | 1f0370c9 | Stavros Sachtouris | |
158 | 1f0370c9 | Stavros Sachtouris | |
159 | 1f0370c9 | Stavros Sachtouris | def main(argv): |
160 | d1f78278 | Stavros Sachtouris | _main(argv, config=None)
|
161 | d1f78278 | Stavros Sachtouris | |
162 | 1f0370c9 | Stavros Sachtouris | |
163 | d1f78278 | Stavros Sachtouris | def _main(argv, config=None): |
164 | 1f0370c9 | Stavros Sachtouris | suiteFew = TestSuite() |
165 | 1f0370c9 | Stavros Sachtouris | """
|
166 | 1f0370c9 | Stavros Sachtouris | if len(argv) == 0 or argv[0] == 'pithos':
|
167 | 1f0370c9 | Stavros Sachtouris | if len(argv) == 1:
|
168 | 1f0370c9 | Stavros Sachtouris | suiteFew.addTest(unittest.makeSuite(testPithos))
|
169 | 1f0370c9 | Stavros Sachtouris | else:
|
170 | 1f0370c9 | Stavros Sachtouris | suiteFew.addTest(testPithos('test_' + argv[1]))
|
171 | 420598d0 | Stavros Sachtouris | """
|
172 | 02846a75 | Stavros Sachtouris | if len(argv) == 0 or argv[0] == 'cyclades': |
173 | 02846a75 | Stavros Sachtouris | from kamaki.clients.tests.cyclades import Cyclades |
174 | 02846a75 | Stavros Sachtouris | test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000') |
175 | d1f78278 | Stavros Sachtouris | suiteFew.addTest(Cyclades(test_method, config)) |
176 | 1f0370c9 | Stavros Sachtouris | if len(argv) == 0 or argv[0] == 'image': |
177 | 420598d0 | Stavros Sachtouris | from kamaki.clients.tests.image import Image |
178 | 54d7c02a | Stavros Sachtouris | test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000') |
179 | d1f78278 | Stavros Sachtouris | suiteFew.addTest(Image(test_method, config)) |
180 | 1f0370c9 | Stavros Sachtouris | if len(argv) == 0 or argv[0] == 'astakos': |
181 | 1f0370c9 | Stavros Sachtouris | from kamaki.clients.tests.astakos import Astakos |
182 | 1f0370c9 | Stavros Sachtouris | if len(argv) == 1: |
183 | d1f78278 | Stavros Sachtouris | suiteFew.addTest(makeSuite(Astakos, config)) |
184 | 1f0370c9 | Stavros Sachtouris | else:
|
185 | d1f78278 | Stavros Sachtouris | suiteFew.addTest(Astakos('test_' + argv[1], config)) |
186 | 1f0370c9 | Stavros Sachtouris | |
187 | 1f0370c9 | Stavros Sachtouris | TextTestRunner(verbosity=2).run(suiteFew)
|
188 | 1f0370c9 | Stavros Sachtouris | |
189 | 1f0370c9 | Stavros Sachtouris | if __name__ == '__main__': |
190 | 1f0370c9 | Stavros Sachtouris | parser = init_parser() |
191 | 1f0370c9 | Stavros Sachtouris | args, argv = parser.parse_known_args() |
192 | 1f0370c9 | Stavros Sachtouris | if len(argv) > 2 or getattr(args, 'help') or len(argv) < 1: |
193 | 1f0370c9 | Stavros Sachtouris | raise Exception('\tusage: tests.py <group> [command]') |
194 | 1f0370c9 | Stavros Sachtouris | main(argv) |