Revision 99e4d887

b/kamaki/clients/compute/test.py
108 108
    status_code = 200
109 109

  
110 110

  
111
def print_iterations(old, new):
112
    if new:
113
        if new % 1000:
114
            return old
115
        stdout.write('\b' * len('%s' % old))
116
        stdout.write('%s' % new)
117
    else:
118
        stdout.write('# of loops:  ')
119
    stdout.flush()
120
    return new
121

  
122

  
111 123
class ComputeRestClient(TestCase):
112 124

  
113 125
    """Set up a ComputesRest thorough test"""
......
129 141
    def _test_get(self, service, params, get, set_param):
130 142
        method = getattr(self.client, '%s_get' % service)
131 143
        param_args = [({}, {k: k}, {k: v[1]}) for k, v in params.items()]
132
        num_of_its, i = '', 0
133
        stdout.write('# of iterations: ')
144
        num_of_its = 0
134 145
        for i, args in enumerate(product(
135 146
                ('', '%s_id' % service),
136 147
                (None, False, True),
......
156 167
            actual = set_param.mock_calls[- len(param_calls):]
157 168
            self.assertEqual(sorted(actual), sorted(param_calls))
158 169

  
159
            if not i % 1000:
160
                stdout.write('\b' * len(num_of_its))
161
                num_of_its = '%s' % i
162
                stdout.write(num_of_its)
163
                stdout.flush()
164
        print ('\b' * len(num_of_its)) + ('%s' % i)
170
            num_of_its = print_iterations(num_of_its, i)
171
        print ('\b' * len('%s' % num_of_its)) + ('%s' % i)
165 172

  
166 173
    @patch('%s.set_param' % rest_pkg)
167 174
    @patch('%s.get' % rest_pkg, return_value=FR())
168 175
    def _test_srv_cmd_get(self, srv, cmd, params, get, set_param):
169 176
        method = getattr(self.client, '%s_%s_get' % (srv, cmd))
170 177
        param_args = [({}, {k: k}, {k: v[1]}) for k, v in params.items()]
171
        num_of_its, i = '', 0
172
        stdout.write('# of iterations: ')
178
        num_of_its = 0
173 179
        for i, args in enumerate(product(
174 180
                ('some_server_id', 'other_server_id'),
175 181
                (None, 'xtra_id'),
......
193 199
            actual = set_param.mock_calls[- len(param_calls):]
194 200
            self.assertEqual(sorted(actual), sorted(param_calls))
195 201

  
196
            if not i % 1000:
197
                stdout.write('\b' * len(num_of_its))
198
                num_of_its = '%s' % i
199
                stdout.write(num_of_its)
200
                stdout.flush()
201
        print ('\b' * len(num_of_its)) + ('%s' % i)
202
            num_of_its = print_iterations(num_of_its, i)
203
        print ('\b' * len('%s' % num_of_its)) + ('%s' % i)
204

  
205
    @patch('%s.delete' % rest_pkg, return_value=FR())
206
    def _test_delete(self, srv, cmd, delete):
207
        method = getattr(
208
            self.client, '%s_%sdelete' % (srv, ('%s_' % cmd) if cmd else ''))
209
        cmd_params = ('some_cmd_value', 'some_other_value') if cmd else ()
210
        num_of_its = 0
211
        for i, args in enumerate(product(
212
                ('%s_id' % srv, 'some_value'),
213
                (204, 208),
214
                ({}, {'k': 'v'}),
215
                *cmd_params)):
216
            (srv_id, success, kwargs) = args[:3]
217
            kwargs['success'] = success
218
            cmd_value = args[-1] if cmd else ''
219
            method_args = (srv_id, cmd_value) if cmd else (srv_id, )
220
            method(*method_args, **kwargs)
221
            srv_str = '/%s/%s' % (srv, srv_id)
222
            cmd_str = ('/%s/%s' % (cmd, cmd_value)) if cmd else ''
223
            self.assertEqual(
224
                delete.mock_calls[-1],
225
                call('%s%s' % (srv_str, cmd_str), **kwargs))
226
            num_of_its = print_iterations(num_of_its, i)
227
        print ('\b' * len('%s' % num_of_its)) + ('%s' % i)
202 228

  
203 229
    def test_servers_get(self):
204 230
        params = dict(
......
242 268
    def test_images_metadata_get(self):
243 269
        self._test_srv_cmd_get('images', 'metadata', {})
244 270

  
245
    @patch('%s.delete' % rest_pkg, return_value=FR())
246
    def _test_delete(self, srv, cmd, delete):
247
        method = getattr(
248
            self.client, '%s_%sdelete' % (srv, ('%s_' % cmd) if cmd else ''))
249
        cmd_params = ('some_cmd_value', 'some_other_value') if cmd else ()
250
        for args in product(
251
                ('%s_id' % srv, 'some_value'),
252
                (204, 208),
253
                ({}, {'k': 'v'}),
254
                *cmd_params):
255
            (srv_id, success, kwargs) = args[:3]
256
            kwargs['success'] = success
257
            cmd_value = args[-1] if cmd else ''
258
            method_args = (srv_id, cmd_value) if cmd else (srv_id)
259
            method(*method_args, **kwargs)
260
            srv_str = '/%s/%s' % (srv, srv_id)
261
            cmd_str = ('/%s/%s' % (cmd, cmd_value)) if cmd else ''
262
            self.assertEqual(
263
                delete.mock_calls[-1],
264
                call('%s%s' % (srv_str, cmd_str), **kwargs))
265

  
266 271
    def test_servers_delete(self):
267 272
        self._test_delete('servers', None)
268 273

  

Also available in: Unified diff