3618: now can use a list of sort columns if there are multiple models involved
[arvados.git] / services / api / test / functional / arvados / v1 / jobs_controller_test.rb
1 require 'test_helper'
2 require 'helpers/git_test_helper'
3
4 class Arvados::V1::JobsControllerTest < ActionController::TestCase
5
6   include GitTestHelper
7
8   test "submit a job" do
9     authorize_with :active
10     post :create, job: {
11       script: "hash",
12       script_version: "master",
13       repository: "foo",
14       script_parameters: {}
15     }
16     assert_response :success
17     assert_not_nil assigns(:object)
18     new_job = JSON.parse(@response.body)
19     assert_not_nil new_job['uuid']
20     assert_not_nil new_job['script_version'].match(/^[0-9a-f]{40}$/)
21     assert_equal 0, new_job['priority']
22   end
23
24   test "normalize output and log uuids when creating job" do
25     authorize_with :active
26     post :create, job: {
27       script: "hash",
28       script_version: "master",
29       script_parameters: {},
30       repository: "foo",
31       started_at: Time.now,
32       finished_at: Time.now,
33       running: false,
34       success: true,
35       output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
36       log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
37     }
38     assert_response :success
39     assert_not_nil assigns(:object)
40     new_job = assigns(:object)
41     assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['log']
42     assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['output']
43     version = new_job['script_version']
44
45     # Make sure version doesn't get mangled by normalize
46     assert_not_nil version.match(/^[0-9a-f]{40}$/)
47     assert_equal 'master', json_response['supplied_script_version']
48   end
49
50   test "normalize output and log uuids when updating job" do
51     authorize_with :active
52
53     foobar_job = jobs(:foobar)
54
55     new_output = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
56     new_log = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
57     put :update, {
58       id: foobar_job['uuid'],
59       job: {
60         output: new_output,
61         log: new_log
62       }
63     }
64
65     updated_job = json_response
66     assert_not_equal foobar_job['log'], updated_job['log']
67     assert_not_equal new_log, updated_job['log']  # normalized during update
68     assert_equal new_log[0,new_log.rindex('+')], updated_job['log']
69     assert_not_equal foobar_job['output'], updated_job['output']
70     assert_not_equal new_output, updated_job['output']  # normalized during update
71     assert_equal new_output[0,new_output.rindex('+')], updated_job['output']
72   end
73
74   test "cancel a running job" do
75     # We need to verify that "cancel" creates a trigger file, so first
76     # let's make sure there is no stale trigger file.
77     begin
78       File.unlink(Rails.configuration.crunch_refresh_trigger)
79     rescue Errno::ENOENT
80     end
81
82     authorize_with :active
83     put :update, {
84       id: jobs(:running).uuid,
85       job: {
86         cancelled_at: 4.day.ago
87       }
88     }
89     assert_response :success
90     assert_not_nil assigns(:object)
91     job = JSON.parse(@response.body)
92     assert_not_nil job['uuid']
93     assert_not_nil job['cancelled_at']
94     assert_not_nil job['cancelled_by_user_uuid']
95     assert_not_nil job['cancelled_by_client_uuid']
96     assert_equal(true, Time.parse(job['cancelled_at']) > 1.minute.ago,
97                  'server should correct bogus cancelled_at ' +
98                  job['cancelled_at'])
99     assert_equal(true,
100                  File.exists?(Rails.configuration.crunch_refresh_trigger),
101                  'trigger file should be created when job is cancelled')
102   end
103
104   test "cancelling a cancelled jobs stays cancelled" do
105     # We need to verify that "cancel" creates a trigger file, so first
106     # let's make sure there is no stale trigger file.
107     begin
108       File.unlink(Rails.configuration.crunch_refresh_trigger)
109     rescue Errno::ENOENT
110     end
111
112     authorize_with :active
113     put :update, {
114       id: jobs(:running_cancelled).uuid,
115       job: {
116         cancelled_at: nil
117       }
118     }
119     assert_response 422
120   end
121
122   ['abc.py', 'hash.py'].each do |script|
123     test "update job script attribute to #{script} without failing script_version check" do
124       authorize_with :admin
125       put :update, {
126         id: jobs(:uses_nonexistent_script_version).uuid,
127         job: {
128           script: script
129         }
130       }
131       assert_response :success
132       resp = assigns(:object)
133       assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
134     end
135   end
136
137   test "search jobs by uuid with >= query" do
138     authorize_with :active
139     get :index, {
140       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
141     }
142     assert_response :success
143     found = assigns(:objects).collect(&:uuid)
144     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
145     assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
146   end
147
148   test "search jobs by uuid with <= query" do
149     authorize_with :active
150     get :index, {
151       filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
152     }
153     assert_response :success
154     found = assigns(:objects).collect(&:uuid)
155     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
156     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
157   end
158
159   test "search jobs by uuid with >= and <= query" do
160     authorize_with :active
161     get :index, {
162       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
163               ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
164     }
165     assert_response :success
166     found = assigns(:objects).collect(&:uuid)
167     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
168   end
169
170   test "search jobs by uuid with < query" do
171     authorize_with :active
172     get :index, {
173       filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
174     }
175     assert_response :success
176     found = assigns(:objects).collect(&:uuid)
177     assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
178     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
179   end
180
181   test "search jobs by uuid with like query" do
182     authorize_with :active
183     get :index, {
184       filters: [['uuid', 'like', '%hmckwoma9pl%']]
185     }
186     assert_response :success
187     found = assigns(:objects).collect(&:uuid)
188     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
189   end
190
191   test "search jobs by uuid with 'in' query" do
192     authorize_with :active
193     get :index, {
194       filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
195                                 'zzzzz-8i9sb-pshmckwoma9plh7']]]
196     }
197     assert_response :success
198     found = assigns(:objects).collect(&:uuid)
199     assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
200                               'zzzzz-8i9sb-pshmckwoma9plh7']
201   end
202
203   test "search jobs by uuid with 'not in' query" do
204     exclude_uuids = [jobs(:running).uuid,
205                      jobs(:running_cancelled).uuid]
206     authorize_with :active
207     get :index, {
208       filters: [['uuid', 'not in', exclude_uuids]]
209     }
210     assert_response :success
211     found = assigns(:objects).collect(&:uuid)
212     assert_not_empty found, "'not in' query returned nothing"
213     assert_empty(found & exclude_uuids,
214                  "'not in' query returned uuids I asked not to get")
215   end
216
217   ['=', '!='].each do |operator|
218     [['uuid', 'zzzzz-8i9sb-pshmckwoma9plh7'],
219      ['output', nil]].each do |attr, operand|
220       test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
221         authorize_with :active
222         get :index, {
223           filters: [[attr, operator, operand]]
224         }
225         assert_response :success
226         values = assigns(:objects).collect { |x| x.send(attr) }
227         assert_not_empty values, "query should return non-empty result"
228         if operator == '='
229           assert_empty values - [operand], "query results do not satisfy query"
230         else
231           assert_empty values & [operand], "query results do not satisfy query"
232         end
233       end
234     end
235   end
236
237   test "search jobs by started_at with < query" do
238     authorize_with :active
239     get :index, {
240       filters: [['started_at', '<', Time.now.to_s]]
241     }
242     assert_response :success
243     found = assigns(:objects).collect(&:uuid)
244     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
245   end
246
247   test "search jobs by started_at with > query" do
248     authorize_with :active
249     get :index, {
250       filters: [['started_at', '>', Time.now.to_s]]
251     }
252     assert_response :success
253     assert_equal 0, assigns(:objects).count
254   end
255
256   test "search jobs by started_at with >= query on metric date" do
257     authorize_with :active
258     get :index, {
259       filters: [['started_at', '>=', '2014-01-01']]
260     }
261     assert_response :success
262     found = assigns(:objects).collect(&:uuid)
263     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
264   end
265
266   test "search jobs by started_at with >= query on metric date and time" do
267     authorize_with :active
268     get :index, {
269       filters: [['started_at', '>=', '2014-01-01 01:23:45']]
270     }
271     assert_response :success
272     found = assigns(:objects).collect(&:uuid)
273     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
274   end
275
276   test "search jobs with 'any' operator" do
277     authorize_with :active
278     get :index, {
279       where: { any: ['contains', 'pshmckw'] }
280     }
281     assert_response :success
282     found = assigns(:objects).collect(&:uuid)
283     assert_equal 0, found.index('zzzzz-8i9sb-pshmckwoma9plh7')
284     assert_equal 1, found.count
285   end
286
287   test "search jobs by nonexistent column with < query" do
288     authorize_with :active
289     get :index, {
290       filters: [['is_borked', '<', 'fizzbuzz']]
291     }
292     assert_response 422
293   end
294
295   test "finish a job" do
296     authorize_with :active
297     put :update, {
298       id: jobs(:nearly_finished_job).uuid,
299       job: {
300         output: '551392cc37a317abf865b95f66f4ef94+101',
301         log: '9215de2a951a721f5f156bc08cf63ad7+93',
302         tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
303         success: true,
304         running: false,
305         finished_at: Time.now.to_s
306       }
307     }
308     assert_response :success
309   end
310
311   [:spectator, :admin].each_with_index do |which_token, i|
312     test "get job queue as #{which_token} user" do
313       authorize_with which_token
314       get :queue
315       assert_response :success
316       assert_equal i, assigns(:objects).count
317     end
318   end
319
320   test "get job queue as with a = filter" do
321     authorize_with :admin
322     get :queue, { filters: [['script','=','foo']] }
323     assert_response :success
324     assert_equal ['foo'], assigns(:objects).collect(&:script).uniq
325     assert_equal 0, assigns(:objects)[0].queue_position
326   end
327
328   test "get job queue as with a != filter" do
329     authorize_with :admin
330     get :queue, { filters: [['script','!=','foo']] }
331     assert_response :success
332     assert_equal 0, assigns(:objects).count
333   end
334
335   [:spectator, :admin].each do |which_token|
336     test "get queue_size as #{which_token} user" do
337       authorize_with which_token
338       get :queue_size
339       assert_response :success
340       assert_equal 1, JSON.parse(@response.body)["queue_size"]
341     end
342   end
343
344   test "job includes assigned nodes" do
345     authorize_with :active
346     get :show, {id: jobs(:nearly_finished_job).uuid}
347     assert_response :success
348     assert_equal([nodes(:busy).uuid], json_response["node_uuids"])
349   end
350
351   test "job lock success" do
352     authorize_with :active
353     post :lock, {id: jobs(:queued).uuid}
354     assert_response :success
355     job = Job.where(uuid: jobs(:queued).uuid).first
356     assert_equal "Running", job.state
357   end
358
359   test "job lock conflict" do
360     authorize_with :active
361     post :lock, {id: jobs(:running).uuid}
362     assert_response 403 # forbidden
363   end
364 end