Add info in container-not-found error
[kamaki] / kamaki / cli / commands / test_cli.py
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.command
33
34 from kamaki.cli import command
35 from kamaki.cli.commands import _command_init
36 from kamaki.cli.command_tree import CommandTree
37 from kamaki.clients import tests
38
39 test_cmds = CommandTree('test', 'Unitest clients')
40 _commands = [test_cmds]
41
42
43 #print('Command Terms: ', get_cmd_terms())
44
45
46 class _test_init(_command_init):
47
48     def main(self, client, method=None):
49         tests.cnf = self.config
50         if method:
51             tests.main([client, method])
52         else:
53             tests.main([client])
54
55
56 @command(test_cmds)
57 class test_error(_test_init):
58     """Create an error message with optional message"""
59
60     def main(self, errmsg='', importance=0, index=0):
61         from kamaki.cli.errors import raiseCLIError
62         l = [1, 2]
63         try:
64             l[int(index)]
65         except Exception as err:
66             raiseCLIError(err, errmsg, importance)
67         raiseCLIError(None, errmsg, importance)
68
69
70 @command(test_cmds)
71 class test_args(_test_init):
72     """Test how arguments are treated by kamaki"""
73
74     def main(self, *args):
75         print(args)
76
77
78 @command(test_cmds)
79 class test_all(_test_init):
80     """test all clients"""
81
82     def main(self):
83         for client in ('pithos', 'cyclades', 'image', 'astakos'):
84             super(self.__class__, self).main(client)
85
86
87 @command(test_cmds)
88 class test_pithos(_test_init):
89     """ test Pithos client"""
90
91     def main(self, method=None):
92         super(self.__class__, self).main('pithos', method)
93
94
95 @command(test_cmds)
96 class test_cyclades(_test_init):
97     """ test Cyclades client"""
98
99     def main(self, method=None):
100         super(self.__class__, self).main('cyclades', method)
101
102
103 @command(test_cmds)
104 class test_image(_test_init):
105     """ test Image client"""
106
107     def main(self, method=None):
108         super(self.__class__, self).main('image', method)
109
110
111 @command(test_cmds)
112 class test_astakos(_test_init):
113     """ test Astakos client"""
114
115     def main(self, method=None):
116         super(self.__class__, self).main('astakos', method)
117
118
119 @command(test_cmds)
120 class test_prints(_test_init):
121     """ user-test print methods for lists and dicts"""
122
123     d1 = {'key0a': 'val0a', 'key0b': 'val0b', 'key0c': 'val0c'}
124
125     l1 = [1, 'string', '3', 'many (2 or 3) numbers and strings combined', 5]
126
127     d2 = {'key1a': 'val0a', 'key1b': d1, 'key1c': l1}
128
129     l2 = [d2, l1, d1]
130
131     d3 = {'dict 1': d1, 'dict 2': d2, 'list2': l2,
132         'long key of size 75 characters is used to' +\
133         ' check the effects on total result': l1}
134
135     def main(self):
136         from kamaki.cli.utils import print_dict, print_list
137         print('Test simple dict:\n- - -')
138         print_dict(self.d1)
139         print('- - -\n')
140         print('\nTest simple list:\n- - -')
141         print_list(self.l1)
142         print('- - -\n')
143         print('\nTest 2-level dict:\n- - -')
144         print_dict(self.d2)
145         print('- - -\n')
146         print('\nTest non-trivial list:\n- - -')
147         print_list(self.l2)
148         print('- - -')
149         print('\nTest extreme dict:\n- - -')
150         print_dict(self.d3)
151         print('- - -\n')