Revision 1f0370c9
b/kamaki/cli/commands/test_cli.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A.command |
33 | 33 |
|
34 |
from kamaki.cli.commands import errors |
|
34 | 35 |
from kamaki.cli import command |
35 | 36 |
from kamaki.cli.commands import _command_init |
36 | 37 |
from kamaki.cli.command_tree import CommandTree |
37 | 38 |
from kamaki.clients import tests |
39 |
from kamaki.cli.errors import raiseCLIError |
|
38 | 40 |
|
39 | 41 |
test_cmds = CommandTree('test', 'Unitest clients') |
40 | 42 |
_commands = [test_cmds] |
... | ... | |
54 | 56 |
class test_error(_test_init): |
55 | 57 |
"""Create an error message with optional message""" |
56 | 58 |
|
57 |
def main(self, errmsg='', importance=0, index=0):
|
|
58 |
from kamaki.cli.errors import raiseCLIError
|
|
59 |
@errors.generic.all
|
|
60 |
def _run(self, errmsg='', importance=0, index=0):
|
|
59 | 61 |
l = [1, 2] |
60 | 62 |
try: |
61 | 63 |
l[int(index)] |
... | ... | |
63 | 65 |
raiseCLIError(err, errmsg, importance) |
64 | 66 |
raiseCLIError(None, errmsg, importance) |
65 | 67 |
|
68 |
def main(self, errmsg='', importance=0, index=0): |
|
69 |
self._run(errmsg, importance, index) |
|
70 |
|
|
66 | 71 |
|
67 | 72 |
@command(test_cmds) |
68 | 73 |
class test_args(_test_init): |
69 | 74 |
"""Test how arguments are treated by kamaki""" |
70 | 75 |
|
71 |
def main(self, *args): |
|
76 |
@errors.generic.all |
|
77 |
def _run(self, *args): |
|
72 | 78 |
print(args) |
73 | 79 |
|
80 |
def main(self, *args): |
|
81 |
self._run(args) |
|
82 |
|
|
74 | 83 |
|
75 | 84 |
@command(test_cmds) |
76 | 85 |
class test_all(_test_init): |
77 | 86 |
"""test all clients""" |
78 | 87 |
|
79 |
def main(self): |
|
88 |
@errors.generic.all |
|
89 |
def _run(self): |
|
80 | 90 |
for client in ('pithos', 'cyclades', 'image', 'astakos'): |
81 | 91 |
super(self.__class__, self).main(client) |
82 | 92 |
|
93 |
def main(self): |
|
94 |
self._run() |
|
95 |
|
|
83 | 96 |
|
84 | 97 |
@command(test_cmds) |
85 | 98 |
class test_pithos(_test_init): |
86 | 99 |
""" test Pithos client""" |
87 | 100 |
|
88 |
def main(self, method=None): |
|
101 |
@errors.generic.all |
|
102 |
def _run(self, method=None): |
|
89 | 103 |
super(self.__class__, self).main('pithos', method) |
90 | 104 |
|
105 |
def main(self, method=None): |
|
106 |
self._run(method) |
|
107 |
|
|
91 | 108 |
|
92 | 109 |
@command(test_cmds) |
93 | 110 |
class test_cyclades(_test_init): |
94 | 111 |
""" test Cyclades client""" |
95 | 112 |
|
96 |
def main(self, method=None): |
|
113 |
@errors.generic.all |
|
114 |
def _run(self, method=None): |
|
97 | 115 |
super(self.__class__, self).main('cyclades', method) |
98 | 116 |
|
117 |
def main(self, method=None): |
|
118 |
self._run(method) |
|
119 |
|
|
99 | 120 |
|
100 | 121 |
@command(test_cmds) |
101 | 122 |
class test_image(_test_init): |
102 | 123 |
""" test Image client""" |
103 | 124 |
|
104 |
def main(self, method=None): |
|
125 |
@errors.generic.all |
|
126 |
def _run(self, method=None): |
|
105 | 127 |
super(self.__class__, self).main('image', method) |
106 | 128 |
|
129 |
def main(self, method=None): |
|
130 |
self._run(method) |
|
131 |
|
|
107 | 132 |
|
108 | 133 |
@command(test_cmds) |
109 | 134 |
class test_astakos(_test_init): |
110 | 135 |
""" test Astakos client""" |
111 | 136 |
|
112 |
def main(self, method=None): |
|
137 |
@errors.generic.all |
|
138 |
def _run(self, method=None): |
|
113 | 139 |
super(self.__class__, self).main('astakos', method) |
114 | 140 |
|
141 |
def main(self, method=None): |
|
142 |
self._run(method) |
|
143 |
|
|
115 | 144 |
|
116 | 145 |
@command(test_cmds) |
117 | 146 |
class test_prints(_test_init): |
... | ... | |
129 | 158 |
'long key of size 75 characters is used to' +\ |
130 | 159 |
' check the effects on total result': l1} |
131 | 160 |
|
132 |
def main(self): |
|
161 |
@errors.generic.all |
|
162 |
def _run(self): |
|
133 | 163 |
from kamaki.cli.utils import print_dict, print_list, print_items |
134 | 164 |
print('Test simple dict:\n- - -') |
135 | 165 |
print_dict(self.d1) |
... | ... | |
176 | 206 |
print('\nTest print_items with lists- - -') |
177 | 207 |
print_items([['i00', 'i01', 'i02'], [self.l2, 'i11', self.d1], 3]) |
178 | 208 |
print('- - -') |
209 |
|
|
210 |
def main(self): |
|
211 |
self._run() |
b/kamaki/clients/tests/__init__.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
from unittest import TestCase, TestSuite, makeSuite, TextTestRunner |
|
35 |
from argparse import ArgumentParser |
|
36 |
|
|
34 | 37 |
from kamaki.cli.config import Config |
35 | 38 |
|
36 | 39 |
|
... | ... | |
40 | 43 |
return wrap |
41 | 44 |
|
42 | 45 |
|
43 |
class configuration(object):
|
|
46 |
class Generic(TestCase):
|
|
44 | 47 |
|
45 | 48 |
_cnf = None |
46 |
_client = '' |
|
49 |
_grp = None |
|
50 |
_fetched = {} |
|
47 | 51 |
|
48 |
def __init__(self, client='', config_file=None): |
|
52 |
def __init__(self, specific=None, config_file=None, group=None): |
|
53 |
super(Generic, self).__init__(specific) |
|
49 | 54 |
self._cnf = Config(config_file) |
50 |
self._client = client |
|
51 |
|
|
52 |
@property |
|
53 |
def generic_property(self, value): |
|
54 |
""" |
|
55 |
:param client: (str) if given, try to get test_client.* and then |
|
56 |
client.* |
|
57 |
:returns: (str) priorities: test_client, client, test, global |
|
58 |
""" |
|
59 |
if not hasattr(self, '_%s' % value): |
|
60 |
_acnt = self._cnf.get('test', '%s_%s' % (self._client, value))\ |
|
61 |
or self._cnf.get(self._client, value)\ |
|
62 |
or self._cnf.get('test', value)\ |
|
63 |
or self._cnf.get('global', value) |
|
64 |
if _acnt: |
|
65 |
setattr(self, '_%s' % value, _acnt) |
|
66 |
return getattr(self, '_%s' % value, None) |
|
67 |
|
|
68 |
for foo in ('url', 'token'): |
|
69 |
setattr(self, foo, _add_value(generic_property, foo)) |
|
55 |
self._grp = group |
|
56 |
|
|
57 |
def __getitem__(self, key): |
|
58 |
key = self._key(key) |
|
59 |
try: |
|
60 |
return self._fetched[key] |
|
61 |
except KeyError: |
|
62 |
return self._get_from_cnf(key) |
|
63 |
|
|
64 |
def _key(self, key): |
|
65 |
return ('', key) if isinstance(key, str)\ |
|
66 |
else ('', key[0]) if len(key) == 1\ |
|
67 |
else key |
|
68 |
|
|
69 |
def _get_from_cnf(self, key): |
|
70 |
val = (self._cnf.get('test', '%s_%s' % key) if key[0] else None)\ |
|
71 |
or self._cnf.get(*key)\ |
|
72 |
or self._cnf.get('test', key[1])\ |
|
73 |
or self._cnf.get('global', key[1]) |
|
74 |
self._fetched[key] = val |
|
75 |
return val |
|
76 |
|
|
77 |
|
|
78 |
def init_parser(): |
|
79 |
parser = ArgumentParser(add_help=False) |
|
80 |
parser.add_argument('-h', '--help', |
|
81 |
dest='help', |
|
82 |
action='store_true', |
|
83 |
default=False, |
|
84 |
help="Show this help message and exit") |
|
85 |
return parser |
|
86 |
|
|
87 |
|
|
88 |
def main(argv): |
|
89 |
|
|
90 |
suiteFew = TestSuite() |
|
91 |
""" |
|
92 |
if len(argv) == 0 or argv[0] == 'pithos': |
|
93 |
if len(argv) == 1: |
|
94 |
suiteFew.addTest(unittest.makeSuite(testPithos)) |
|
95 |
else: |
|
96 |
suiteFew.addTest(testPithos('test_' + argv[1])) |
|
97 |
if len(argv) == 0 or argv[0] == 'cyclades': |
|
98 |
if len(argv) == 1: |
|
99 |
#suiteFew.addTest(unittest.makeSuite(testCyclades)) |
|
100 |
suiteFew.addTest(testCyclades('test_000')) |
|
101 |
else: |
|
102 |
suiteFew.addTest(testCyclades('test_' + argv[1])) |
|
103 |
if len(argv) == 0 or argv[0] == 'image': |
|
104 |
if len(argv) == 1: |
|
105 |
suiteFew.addTest(unittest.makeSuite(testImage)) |
|
106 |
else: |
|
107 |
suiteFew.addTest(testImage('test_' + argv[1])) |
|
108 |
""" |
|
109 |
if len(argv) == 0 or argv[0] == 'astakos': |
|
110 |
from kamaki.clients.tests.astakos import Astakos |
|
111 |
if len(argv) == 1: |
|
112 |
suiteFew.addTest(makeSuite(Astakos)) |
|
113 |
else: |
|
114 |
suiteFew.addTest(Astakos('test_' + argv[1])) |
|
115 |
|
|
116 |
TextTestRunner(verbosity=2).run(suiteFew) |
|
117 |
|
|
118 |
if __name__ == '__main__': |
|
119 |
parser = init_parser() |
|
120 |
args, argv = parser.parse_known_args() |
|
121 |
if len(argv) > 2 or getattr(args, 'help') or len(argv) < 1: |
|
122 |
raise Exception('\tusage: tests.py <group> [command]') |
|
123 |
main(argv) |
b/kamaki/clients/tests/astakos.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
from unittest import TestCase |
|
35 |
|
|
36 |
from kamaki.clients.tests import configuration |
|
34 |
from kamaki.clients import tests |
|
37 | 35 |
from kamaki.clients.astakos import AstakosClient as astakos |
38 | 36 |
|
39 | 37 |
|
40 |
class testAstakos(TestCase):
|
|
38 |
class Astakos(tests.Generic):
|
|
41 | 39 |
def setUp(self): |
42 |
self.cnf = configuration('astakos') |
|
43 |
self.client = astakos(self.cnf.url, self.cnf.token) |
|
40 |
self.client = astakos( |
|
41 |
self['astakos', 'url'], |
|
42 |
self['astakos', 'token']) |
|
44 | 43 |
|
45 | 44 |
def tearDown(self): |
46 | 45 |
pass |
47 | 46 |
|
48 | 47 |
def test_authenticate(self): |
49 | 48 |
r = self.client.authenticate() |
50 |
for term in ('username', |
|
49 |
for term in ( |
|
50 |
'username', |
|
51 | 51 |
'auth_token_expires', |
52 | 52 |
'auth_token', |
53 | 53 |
'auth_token_created', |
Also available in: Unified diff