Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / tests / __init__.py @ db77d79e

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