Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.8 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
        failures = 0
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
                failures += 1
97
        if failures:
98
            raise AssertionError('%s failures' % failures)
99

    
100

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

    
110

    
111
def main(argv):
112

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

    
138
    TextTestRunner(verbosity=2).run(suiteFew)
139

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