Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / tests / __init__.py @ 80f0d425

History | View | Annotate | Download (5 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

    
39
from kamaki.cli.config import Config
40

    
41

    
42
def _add_value(foo, value):
43
    def wrap(self):
44
        return foo(self, value)
45
    return wrap
46

    
47

    
48
class Generic(TestCase):
49

    
50
    _cnf = None
51
    _grp = None
52
    _fetched = {}
53

    
54
    def __init__(self, specific=None, config_file=None, group=None):
55
        super(Generic, self).__init__(specific)
56
        self._cnf = Config(config_file)
57
        self._grp = group
58

    
59
    def __getitem__(self, key):
60
        key = self._key(key)
61
        try:
62
            return self._fetched[key]
63
        except KeyError:
64
            return self._get_from_cnf(key)
65

    
66
    def _key(self, key):
67
        return ('', key) if isinstance(key, str)\
68
            else ('', key[0]) if len(key) == 1\
69
            else key
70

    
71
    def _get_from_cnf(self, key):
72
        val = 0
73
        if key[0]:
74
            val = self._cnf.get('test', '%s_%s' % key)\
75
                or self._cnf.get(*key)
76
        if not val:
77
            val = self._cnf.get('test', key[1])\
78
                or self._cnf.get('global', key[1])
79
        self._fetched[key] = val
80
        return val
81

    
82
    def test_000(self):
83
        import inspect
84
        methods = [method for method in inspect.getmembers(
85
            self,
86
            predicate=inspect.ismethod)\
87
            if method[0].startswith('_test_')]
88
        exceptions = {}
89
        for method in methods:
90
            stdout.write('Test %s' % method[0][6:])
91
            try:
92
                method[1]()
93
                print(' ...ok')
94
            except AssertionError:
95
                print('  FAIL: %s (%s)' % (method[0], method[1]))
96
                exceptions[method[0]] = extract_stack()
97
        for m, e in exceptions.items():
98
            print('==================\nFAIL: %s\n------------------' % m[6:])
99
            for err in e[-30:]:
100
                print('%s %s %s %s' % err)
101
        if exceptions:
102
            raise AssertionError('(#of fails: %s)' % len(exceptions))
103

    
104

    
105
def init_parser():
106
    parser = ArgumentParser(add_help=False)
107
    parser.add_argument('-h', '--help',
108
        dest='help',
109
        action='store_true',
110
        default=False,
111
        help="Show this help message and exit")
112
    return parser
113

    
114

    
115
def main(argv):
116

    
117
    suiteFew = TestSuite()
118
    """
119
    if len(argv) == 0 or argv[0] == 'pithos':
120
        if len(argv) == 1:
121
            suiteFew.addTest(unittest.makeSuite(testPithos))
122
        else:
123
            suiteFew.addTest(testPithos('test_' + argv[1]))
124
    if len(argv) == 0 or argv[0] == 'cyclades':
125
        if len(argv) == 1:
126
            #suiteFew.addTest(unittest.makeSuite(testCyclades))
127
            suiteFew.addTest(testCyclades('test_000'))
128
        else:
129
            suiteFew.addTest(testCyclades('test_' + argv[1]))
130
    """
131
    if len(argv) == 0 or argv[0] == 'image':
132
        from kamaki.clients.tests.image import Image
133
        test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000')
134
        suiteFew.addTest(Image(test_method))
135
    if len(argv) == 0 or argv[0] == 'astakos':
136
        from kamaki.clients.tests.astakos import Astakos
137
        if len(argv) == 1:
138
            suiteFew.addTest(makeSuite(Astakos))
139
        else:
140
            suiteFew.addTest(Astakos('test_' + argv[1]))
141

    
142
    TextTestRunner(verbosity=2).run(suiteFew)
143

    
144
if __name__ == '__main__':
145
    parser = init_parser()
146
    args, argv = parser.parse_known_args()
147
    if len(argv) > 2 or getattr(args, 'help') or len(argv) < 1:
148
        raise Exception('\tusage: tests.py <group> [command]')
149
    main(argv)