Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / test_cli.py @ d1f78278

History | View | Annotate | Download (6.6 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.command
33

    
34
from kamaki.cli.commands import errors
35
from kamaki.cli import command
36
from kamaki.cli.commands import _command_init
37
from kamaki.cli.command_tree import CommandTree
38
from kamaki.clients import tests
39
from kamaki.cli.errors import raiseCLIError
40

    
41
test_cmds = CommandTree('test', 'Unitest clients')
42
_commands = [test_cmds]
43

    
44

    
45
class _test_init(_command_init):
46

    
47
    def main(self, client, method=None):
48
        if method:
49
            tests.main([client, method], config=self.config)
50
        else:
51
            tests.main([client], config=self.config)
52

    
53

    
54
@command(test_cmds)
55
class test_error(_test_init):
56
    """Create an error message with optional message"""
57

    
58
    @errors.generic.all
59
    def _run(self, errmsg='', importance=0, index=0):
60
        l = [1, 2]
61
        try:
62
            l[int(index)]
63
        except Exception as err:
64
            raiseCLIError(err, errmsg, importance)
65
        raiseCLIError(None, errmsg, importance)
66

    
67
    def main(self, errmsg='', importance=0, index=0):
68
        self._run(errmsg, importance, index)
69

    
70

    
71
@command(test_cmds)
72
class test_args(_test_init):
73
    """Test how arguments are treated by kamaki"""
74

    
75
    @errors.generic.all
76
    def _run(self, *args):
77
        print(args)
78

    
79
    def main(self, *args):
80
        self._run(args)
81

    
82

    
83
@command(test_cmds)
84
class test_all(_test_init):
85
    """test all clients"""
86

    
87
    @errors.generic.all
88
    def _run(self):
89
        for client in ('pithos', 'cyclades', 'image', 'astakos'):
90
            super(self.__class__, self).main(client)
91

    
92
    def main(self):
93
        self._run()
94

    
95

    
96
@command(test_cmds)
97
class test_pithos(_test_init):
98
    """ test Pithos client"""
99

    
100
    @errors.generic.all
101
    def _run(self, method=None):
102
        super(self.__class__, self).main('pithos', method)
103

    
104
    def main(self, method=None):
105
        self._run(method)
106

    
107

    
108
@command(test_cmds)
109
class test_cyclades(_test_init):
110
    """ test Cyclades client"""
111

    
112
    @errors.generic.all
113
    def _run(self, method=None):
114
        super(self.__class__, self).main('cyclades', method)
115

    
116
    def main(self, method=None):
117
        self._run(method)
118

    
119

    
120
@command(test_cmds)
121
class test_image(_test_init):
122
    """ test Image client"""
123

    
124
    @errors.generic.all
125
    def _run(self, method=None):
126
        super(self.__class__, self).main('image', method)
127

    
128
    def main(self, method=None):
129
        self._run(method)
130

    
131

    
132
@command(test_cmds)
133
class test_astakos(_test_init):
134
    """ test Astakos client"""
135

    
136
    @errors.generic.all
137
    def _run(self, method=None):
138
        super(self.__class__, self).main('astakos', method)
139

    
140
    def main(self, method=None):
141
        self._run(method)
142

    
143

    
144
@command(test_cmds)
145
class test_prints(_test_init):
146
    """ user-test print methods for lists and dicts"""
147

    
148
    d1 = {'key0a': 'val0a', 'key0b': 'val0b', 'key0c': 'val0c'}
149

    
150
    l1 = [1, 'string', '3', 'many (2 or 3) numbers and strings combined', 5]
151

    
152
    d2 = {'id': 'val0a', 'key0b': d1, 'title': l1}
153

    
154
    l2 = [d2, l1, d1]
155

    
156
    d3 = {'dict 1': d1, 'dict 2': d2, 'list2': l2,
157
        'long key of size 75 characters is used to' +\
158
        ' check the effects on total result': l1}
159

    
160
    @errors.generic.all
161
    def _run(self):
162
        from kamaki.cli.utils import print_dict, print_list, print_items
163
        print('Test simple dict:\n- - -')
164
        print_dict(self.d1)
165
        print('- - -\n')
166
        print('\nTest simple list:\n- - -')
167
        print_list(self.l1)
168
        print('- - -\n')
169
        print('\nTest 2-level dict:\n- - -')
170
        print_dict(self.d2)
171
        print('- - -\n')
172
        print('\nTest non-trivial list:\n- - -')
173
        print_list(self.l2)
174
        print('- - -')
175
        print('\nTest extreme dict:\n- - -')
176
        print_dict(self.d3)
177
        print('- - -\n')
178
        print('Test simple enumerated dict:\n- - -')
179
        print_dict(self.d1, with_enumeration=True)
180
        print('- - -\n')
181
        print('\nTest simple enumerated list:\n- - -')
182
        print_list(self.l1, with_enumeration=True)
183
        print('- - -\n')
184
        print('Test non-trivial deep-enumerated dict:\n- - -')
185
        print_dict(self.d2, with_enumeration=True, recursive_enumeration=True)
186
        print('- - -\n')
187
        print('\nTest non-trivial enumerated list:\n- - -')
188
        print_list(self.l2, with_enumeration=True)
189
        print('- - -\n')
190
        print('\nTest print_items with id:\n- - -')
191
        print_items([{'id': '42', 'title': 'lalakis 1', 'content': self.d1},
192
            {'id': '142', 'title': 'lalakis 2', 'content': self.d2}])
193
        print('- - -')
194
        print('\nTest print_items with id and enumeration:\n- - -')
195
        print_items([{'id': '42', 'title': 'lalakis 1', 'content': self.d1},
196
            {'id': '142', 'title': 'lalakis 2', 'content': self.d2}],
197
            with_enumeration=True)
198
        print('- - -')
199
        print('\nTest print_items with id, title and redundancy:\n- - -')
200
        print_items([{'id': '42', 'title': 'lalakis 1', 'content': self.d1},
201
            {'id': '142', 'title': 'lalakis 2', 'content': self.d2}],
202
            title=('id', 'title'),
203
            with_redundancy=True)
204
        print('- - -')
205
        print('\nTest print_items with lists- - -')
206
        print_items([['i00', 'i01', 'i02'], [self.l2, 'i11', self.d1], 3])
207
        print('- - -')
208

    
209
    def main(self):
210
        self._run()