-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_cli.py
345 lines (298 loc) · 11 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# encoding: utf-8
import datetime
import logging
import os
import os.path
from StringIO import StringIO
import sys
import tempfile
from nose.tools import (assert_raises, eq_ as eq, ok_ as ok, assert_in,
assert_not_in, assert_not_equal as neq, assert_false as nok)
from pylons import config
from paste.script.command import run
import ckan.lib.cli as cli
import ckan.lib.jobs as jobs
import ckan.tests.helpers as helpers
log = logging.getLogger(__name__)
def paster(*args, **kwargs):
'''
Call a paster command.
All arguments are parsed and passed on to the command. The
``--config`` option is automatically appended.
By default, an ``AssertionError`` is raised if the command exits
with a non-zero return code or if anything is written to STDERR.
Pass ``fail_on_error=False`` to disable this behavior.
Example::
code, stdout, stderr = paster(u'jobs', u'list')
assert u'My Job Title' in stdout
code, stdout, stderr = paster(u'jobs', u'foobar',
fail_on_error=False)
assert code == 1
assert u'Unknown command' in stderr
Any ``SystemExit`` raised by the command is swallowed.
:returns: A tuple containing the return code, the content of
STDOUT, and the content of STDERR.
'''
fail_on_error = kwargs.pop(u'fail_on_error', True)
args = list(args) + [u'--config=' + config[u'__file__']]
sys.stdout, sys.stderr = StringIO(u''), StringIO(u'')
code = 0
try:
run(args)
except SystemExit as e:
code = e.code
finally:
stdout, stderr = sys.stdout.getvalue(), sys.stderr.getvalue()
sys.stdout, sys.stderr = sys.__stdout__, sys.__stderr__
if code != 0 and fail_on_error:
raise AssertionError(u'Paster command exited with non-zero '
+ u'return code {}: {}'.format(code, stderr))
if stderr.strip() and fail_on_error:
raise AssertionError(u'Paster command wrote to STDERR: {}'.format(
stderr))
return code, stdout, stderr
class TestUserAdd(object):
'''Tests for UserCmd.add'''
@classmethod
def setup_class(cls):
cls.user_cmd = cli.UserCmd('user-command')
def setup(self):
helpers.reset_db()
def test_cli_user_add_valid_args(self):
'''Command shouldn't raise SystemExit when valid args are provided.'''
self.user_cmd.args = ['add', 'berty', 'password=password123',
'fullname=Berty Guffball',
'email=berty@example.com']
try:
self.user_cmd.add()
except SystemExit:
assert False, "SystemExit exception shouldn't be raised"
def test_cli_user_add_no_args(self):
'''Command with no args raises SystemExit.'''
self.user_cmd.args = ['add', ]
assert_raises(SystemExit, self.user_cmd.add)
def test_cli_user_add_no_fullname(self):
'''Command shouldn't raise SystemExit when fullname arg not present.'''
self.user_cmd.args = ['add', 'berty', 'password=password123',
'email=berty@example.com']
try:
self.user_cmd.add()
except SystemExit:
assert False, "SystemExit exception shouldn't be raised"
def test_cli_user_add_unicode_fullname_unicode_decode_error(self):
'''
Command shouldn't raise UnicodeDecodeError when fullname contains
characters outside of the ascii characterset.
'''
self.user_cmd.args = ['add', 'berty', 'password=password123',
'fullname=Harold Müffintøp',
'email=berty@example.com']
try:
self.user_cmd.add()
except UnicodeDecodeError:
assert False, "UnicodeDecodeError exception shouldn't be raised"
def test_cli_user_add_unicode_fullname_system_exit(self):
'''
Command shouldn't raise SystemExit when fullname contains
characters outside of the ascii characterset.
'''
self.user_cmd.args = ['add', 'berty', 'password=password123',
'fullname=Harold Müffintøp',
'email=berty@example.com']
try:
self.user_cmd.add()
except SystemExit:
assert False, "SystemExit exception shouldn't be raised"
class TestJobsUnknown(helpers.RQTestBase):
'''
Test unknown sub-command for ``paster jobs``.
'''
def test_unknown_command(self):
'''
Test error handling for unknown ``paster jobs`` sub-command.
'''
code, stdout, stderr = paster(u'jobs', u'does-not-exist',
fail_on_error=False)
neq(code, 0)
assert_in(u'Unknown command', stderr)
class TestJobsList(helpers.RQTestBase):
'''
Tests for ``paster jobs list``.
'''
def test_list_default_queue(self):
'''
Test output of ``jobs list`` for default queue.
'''
job = self.enqueue()
stdout = paster(u'jobs', u'list')[1]
fields = stdout.split()
eq(len(fields), 3)
dt = datetime.datetime.strptime(fields[0], u'%Y-%m-%dT%H:%M:%S')
now = datetime.datetime.utcnow()
ok(abs((now - dt).total_seconds()) < 10)
eq(fields[1], job.id)
eq(fields[2], jobs.DEFAULT_QUEUE_NAME)
def test_list_other_queue(self):
'''
Test output of ``jobs.list`` for non-default queue.
'''
job = self.enqueue(queue=u'my_queue')
stdout = paster(u'jobs', u'list')[1]
fields = stdout.split()
eq(len(fields), 3)
eq(fields[2], u'my_queue')
def test_list_title(self):
'''
Test title output of ``jobs list``.
'''
job = self.enqueue(title=u'My_Title')
stdout = paster(u'jobs', u'list')[1]
fields = stdout.split()
eq(len(fields), 4)
eq(fields[3], u'"My_Title"')
def test_list_filter(self):
'''
Test filtering by queues for ``jobs list``.
'''
job1 = self.enqueue(queue=u'q1')
job2 = self.enqueue(queue=u'q2')
job3 = self.enqueue(queue=u'q3')
stdout = paster(u'jobs', u'list', u'q1', u'q2')[1]
assert_in(u'q1', stdout)
assert_in(u'q2', stdout)
assert_not_in(u'q3', stdout)
class TestJobShow(helpers.RQTestBase):
'''
Tests for ``paster jobs show``.
'''
def test_show_existing(self):
'''
Test ``jobs show`` for an existing job.
'''
job = self.enqueue(queue=u'my_queue', title=u'My Title')
stdout = paster(u'jobs', u'show', job.id)[1]
assert_in(job.id, stdout)
assert_in(jobs.remove_queue_name_prefix(job.origin), stdout)
def test_show_missing_id(self):
'''
Test ``jobs show`` with a missing ID.
'''
code, stdout, stderr = paster(u'jobs', u'show', fail_on_error=False)
neq(code, 0)
ok(stderr)
class TestJobsCancel(helpers.RQTestBase):
'''
Tests for ``paster jobs cancel``.
'''
def test_cancel_existing(self):
'''
Test ``jobs cancel`` for an existing job.
'''
job1 = self.enqueue()
job2 = self.enqueue()
stdout = paster(u'jobs', u'cancel', job1.id)[1]
all_jobs = self.all_jobs()
eq(len(all_jobs), 1)
eq(all_jobs[0].id, job2.id)
assert_in(job1.id, stdout)
def test_cancel_not_existing(self):
'''
Test ``jobs cancel`` for a not existing job.
'''
code, stdout, stderr = paster(u'jobs', u'cancel', u'does-not-exist',
fail_on_error=False)
neq(code, 0)
assert_in(u'does-not-exist', stderr)
def test_cancel_missing_id(self):
'''
Test ``jobs cancel`` with a missing ID.
'''
code, stdout, stderr = paster(u'jobs', u'cancel', fail_on_error=False)
neq(code, 0)
ok(stderr)
class TestJobsClear(helpers.RQTestBase):
'''
Tests for ``paster jobs clear``.
'''
def test_clear_all_queues(self):
'''
Test clearing all queues via ``jobs clear``.
'''
self.enqueue()
self.enqueue()
self.enqueue(queue=u'q1')
self.enqueue(queue=u'q2')
stdout = paster(u'jobs', u'clear')[1]
assert_in(jobs.DEFAULT_QUEUE_NAME, stdout)
assert_in(u'q1', stdout)
assert_in(u'q2', stdout)
eq(self.all_jobs(), [])
def test_clear_specific_queues(self):
'''
Test clearing specific queues via ``jobs clear``.
'''
job1 = self.enqueue()
job2 = self.enqueue(queue=u'q1')
self.enqueue(queue=u'q2')
self.enqueue(queue=u'q2')
self.enqueue(queue=u'q3')
stdout = paster(u'jobs', u'clear', u'q2', u'q3')[1]
assert_in(u'q2', stdout)
assert_in(u'q3', stdout)
assert_not_in(jobs.DEFAULT_QUEUE_NAME, stdout)
assert_not_in(u'q1', stdout)
all_jobs = self.all_jobs()
eq(set(all_jobs), {job1, job2})
class TestJobsTest(helpers.RQTestBase):
'''
Tests for ``paster jobs test``.
'''
def test_test_default_queue(self):
'''
Test ``jobs test`` for the default queue.
'''
stdout = paster(u'jobs', u'test')[1]
all_jobs = self.all_jobs()
eq(len(all_jobs), 1)
eq(jobs.remove_queue_name_prefix(all_jobs[0].origin),
jobs.DEFAULT_QUEUE_NAME)
def test_test_specific_queues(self):
'''
Test ``jobs test`` for specific queues.
'''
stdout = paster(u'jobs', u'test', u'q1', u'q2')[1]
all_jobs = self.all_jobs()
eq(len(all_jobs), 2)
eq({jobs.remove_queue_name_prefix(j.origin) for j in all_jobs},
{u'q1', u'q2'})
class TestJobsWorker(helpers.RQTestBase):
'''
Tests for ``paster jobs worker``.
'''
# All tests of ``jobs worker`` must use the ``--burst`` option to
# make sure that the worker exits.
def test_worker_default_queue(self):
'''
Test ``jobs worker`` with the default queue.
'''
with tempfile.NamedTemporaryFile(delete=False) as f:
self.enqueue(os.remove, args=[f.name])
paster(u'jobs', u'worker', u'--burst')
all_jobs = self.all_jobs()
eq(all_jobs, [])
nok(os.path.isfile(f.name))
def test_worker_specific_queues(self):
'''
Test ``jobs worker`` with specific queues.
'''
with tempfile.NamedTemporaryFile(delete=False) as f:
with tempfile.NamedTemporaryFile(delete=False) as g:
job1 = self.enqueue()
job2 = self.enqueue(queue=u'q2')
self.enqueue(os.remove, args=[f.name], queue=u'q3')
self.enqueue(os.remove, args=[g.name], queue=u'q4')
paster(u'jobs', u'worker', u'--burst', u'q3', u'q4')
all_jobs = self.all_jobs()
eq(set(all_jobs), {job1, job2})
nok(os.path.isfile(f.name))
nok(os.path.isfile(g.name))