8784: Fix test for latest firefox.
[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: "active/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: "active/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.exist?(Rails.configuration.crunch_refresh_trigger),
101                  'trigger file should be created when job is cancelled')
102   end
103
104   [
105    [:put, :update, {job:{cancelled_at: Time.now}}, :success],
106    [:put, :update, {job:{cancelled_at: nil}}, :unprocessable_entity],
107    [:put, :update, {job:{state: 'Cancelled'}}, :success],
108    [:put, :update, {job:{state: 'Queued'}}, :unprocessable_entity],
109    [:put, :update, {job:{state: 'Running'}}, :unprocessable_entity],
110    [:put, :update, {job:{state: 'Failed'}}, :unprocessable_entity],
111    [:put, :update, {job:{state: 'Complete'}}, :unprocessable_entity],
112    [:post, :cancel, {}, :success],
113   ].each do |http_method, action, params, expected_response|
114     test "cancelled job stays cancelled after #{[http_method, action, params].inspect}" do
115       # We need to verify that "cancel" creates a trigger file, so first
116       # let's make sure there is no stale trigger file.
117       begin
118         File.unlink(Rails.configuration.crunch_refresh_trigger)
119       rescue Errno::ENOENT
120       end
121
122       authorize_with :active
123       self.send http_method, action, { id: jobs(:cancelled).uuid }.merge(params)
124       assert_response expected_response
125       if expected_response == :success
126         job = json_response
127         assert_not_nil job['cancelled_at'], 'job cancelled again using #{attribute}=#{value} did not have cancelled_at value'
128         assert_equal job['state'], 'Cancelled', 'cancelled again job state changed when updated using using #{attribute}=#{value}'
129       end
130       # Verify database record still says Cancelled
131       assert_equal 'Cancelled', Job.find(jobs(:cancelled).id).state, 'job was un-cancelled'
132     end
133   end
134
135   test "cancelled job updated to any other state change results in error" do
136     # We need to verify that "cancel" creates a trigger file, so first
137     # let's make sure there is no stale trigger file.
138     begin
139       File.unlink(Rails.configuration.crunch_refresh_trigger)
140     rescue Errno::ENOENT
141     end
142
143     authorize_with :active
144     put :update, {
145       id: jobs(:running_cancelled).uuid,
146       job: {
147         cancelled_at: nil
148       }
149     }
150     assert_response 422
151   end
152
153   ['abc.py', 'hash.py'].each do |script|
154     test "update job script attribute to #{script} without failing script_version check" do
155       authorize_with :admin
156       put :update, {
157         id: jobs(:uses_nonexistent_script_version).uuid,
158         job: {
159           script: script
160         }
161       }
162       assert_response :success
163       resp = assigns(:object)
164       assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
165     end
166   end
167
168   test "search jobs by uuid with >= query" do
169     authorize_with :active
170     get :index, {
171       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
172     }
173     assert_response :success
174     found = assigns(:objects).collect(&:uuid)
175     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
176     assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
177   end
178
179   test "search jobs by uuid with <= query" do
180     authorize_with :active
181     get :index, {
182       filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
183     }
184     assert_response :success
185     found = assigns(:objects).collect(&:uuid)
186     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
187     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
188   end
189
190   test "search jobs by uuid with >= and <= query" do
191     authorize_with :active
192     get :index, {
193       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
194               ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
195     }
196     assert_response :success
197     found = assigns(:objects).collect(&:uuid)
198     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
199   end
200
201   test "search jobs by uuid with < query" do
202     authorize_with :active
203     get :index, {
204       filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
205     }
206     assert_response :success
207     found = assigns(:objects).collect(&:uuid)
208     assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
209     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
210   end
211
212   test "search jobs by uuid with like query" do
213     authorize_with :active
214     get :index, {
215       filters: [['uuid', 'like', '%hmckwoma9pl%']]
216     }
217     assert_response :success
218     found = assigns(:objects).collect(&:uuid)
219     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
220   end
221
222   test "search jobs by uuid with 'in' query" do
223     authorize_with :active
224     get :index, {
225       filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
226                                 'zzzzz-8i9sb-pshmckwoma9plh7']]]
227     }
228     assert_response :success
229     found = assigns(:objects).collect(&:uuid)
230     assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
231                               'zzzzz-8i9sb-pshmckwoma9plh7']
232   end
233
234   test "search jobs by uuid with 'not in' query" do
235     exclude_uuids = [jobs(:running).uuid,
236                      jobs(:running_cancelled).uuid]
237     authorize_with :active
238     get :index, {
239       filters: [['uuid', 'not in', exclude_uuids]]
240     }
241     assert_response :success
242     found = assigns(:objects).collect(&:uuid)
243     assert_not_empty found, "'not in' query returned nothing"
244     assert_empty(found & exclude_uuids,
245                  "'not in' query returned uuids I asked not to get")
246   end
247
248   ['=', '!='].each do |operator|
249     [['uuid', 'zzzzz-8i9sb-pshmckwoma9plh7'],
250      ['output', nil]].each do |attr, operand|
251       test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
252         authorize_with :active
253         get :index, {
254           filters: [[attr, operator, operand]]
255         }
256         assert_response :success
257         values = assigns(:objects).collect { |x| x.send(attr) }
258         assert_not_empty values, "query should return non-empty result"
259         if operator == '='
260           assert_empty values - [operand], "query results do not satisfy query"
261         else
262           assert_empty values & [operand], "query results do not satisfy query"
263         end
264       end
265     end
266   end
267
268   test "search jobs by started_at with < query" do
269     authorize_with :active
270     get :index, {
271       filters: [['started_at', '<', Time.now.to_s]]
272     }
273     assert_response :success
274     found = assigns(:objects).collect(&:uuid)
275     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
276   end
277
278   test "search jobs by started_at with > query" do
279     authorize_with :active
280     get :index, {
281       filters: [['started_at', '>', Time.now.to_s]]
282     }
283     assert_response :success
284     assert_equal 0, assigns(:objects).count
285   end
286
287   test "search jobs by started_at with >= query on metric date" do
288     authorize_with :active
289     get :index, {
290       filters: [['started_at', '>=', '2014-01-01']]
291     }
292     assert_response :success
293     found = assigns(:objects).collect(&:uuid)
294     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
295   end
296
297   test "search jobs by started_at with >= query on metric date and time" do
298     authorize_with :active
299     get :index, {
300       filters: [['started_at', '>=', '2014-01-01 01:23:45']]
301     }
302     assert_response :success
303     found = assigns(:objects).collect(&:uuid)
304     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
305   end
306
307   test "search jobs with 'any' operator" do
308     authorize_with :active
309     get :index, {
310       where: { any: ['contains', 'pshmckw'] }
311     }
312     assert_response :success
313     found = assigns(:objects).collect(&:uuid)
314     assert_equal 0, found.index('zzzzz-8i9sb-pshmckwoma9plh7')
315     assert_equal 1, found.count
316   end
317
318   test "search jobs by nonexistent column with < query" do
319     authorize_with :active
320     get :index, {
321       filters: [['is_borked', '<', 'fizzbuzz']]
322     }
323     assert_response 422
324   end
325
326   test "finish a job" do
327     authorize_with :active
328     put :update, {
329       id: jobs(:nearly_finished_job).uuid,
330       job: {
331         output: '551392cc37a317abf865b95f66f4ef94+101',
332         log: '9215de2a951a721f5f156bc08cf63ad7+93',
333         tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
334         success: true,
335         running: false,
336         finished_at: Time.now.to_s
337       }
338     }
339     assert_response :success
340   end
341
342   [:spectator, :admin].each_with_index do |which_token, i|
343     test "get job queue as #{which_token} user" do
344       authorize_with which_token
345       get :queue
346       assert_response :success
347       assert_equal i, assigns(:objects).count
348     end
349   end
350
351   test "get job queue as with a = filter" do
352     authorize_with :admin
353     get :queue, { filters: [['script','=','foo']] }
354     assert_response :success
355     assert_equal ['foo'], assigns(:objects).collect(&:script).uniq
356     assert_equal 0, assigns(:objects)[0].queue_position
357   end
358
359   test "get job queue as with a != filter" do
360     authorize_with :admin
361     get :queue, { filters: [['script','!=','foo']] }
362     assert_response :success
363     assert_equal 0, assigns(:objects).count
364   end
365
366   [:spectator, :admin].each do |which_token|
367     test "get queue_size as #{which_token} user" do
368       authorize_with which_token
369       get :queue_size
370       assert_response :success
371       assert_equal 1, JSON.parse(@response.body)["queue_size"]
372     end
373   end
374
375   test "job includes assigned nodes" do
376     authorize_with :active
377     get :show, {id: jobs(:nearly_finished_job).uuid}
378     assert_response :success
379     assert_equal([nodes(:busy).uuid], json_response["node_uuids"])
380   end
381
382   test "job lock success" do
383     authorize_with :active
384     post :lock, {id: jobs(:queued).uuid}
385     assert_response :success
386     job = Job.where(uuid: jobs(:queued).uuid).first
387     assert_equal "Running", job.state
388   end
389
390   test "job lock conflict" do
391     authorize_with :active
392     post :lock, {id: jobs(:running).uuid}
393     assert_response 422 # invalid state transition
394   end
395
396   test 'reject invalid commit in remote repository' do
397     authorize_with :active
398     url = "http://localhost:1/fake/fake.git"
399     fetch_remote_from_local_repo url, :foo
400     post :create, job: {
401       script: "hash",
402       script_version: "abc123",
403       repository: url,
404       script_parameters: {}
405     }
406     assert_response 422
407   end
408
409   test 'tag remote commit in internal repository' do
410     authorize_with :active
411     url = "http://localhost:1/fake/fake.git"
412     fetch_remote_from_local_repo url, :foo
413     post :create, job: {
414       script: "hash",
415       script_version: "master",
416       repository: url,
417       script_parameters: {}
418     }
419     assert_response :success
420     assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
421                  internal_tag(json_response['uuid']))
422   end
423
424   test 'tag local commit in internal repository' do
425     authorize_with :active
426     post :create, job: {
427       script: "hash",
428       script_version: "master",
429       repository: "active/foo",
430       script_parameters: {}
431     }
432     assert_response :success
433     assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
434                  internal_tag(json_response['uuid']))
435   end
436
437   test 'get job with components' do
438     authorize_with :active
439     get :show, {id: jobs(:running_job_with_components).uuid}
440     assert_response :success
441     assert_not_nil json_response["components"]
442     assert_equal ["component1", "component2"], json_response["components"].keys
443   end
444
445   [
446     [:active, :success],
447     [:system_user, :success],
448     [:admin, 403],
449   ].each do |user, expected|
450     test "add components to job locked by active user as #{user} user and expect #{expected}" do
451       authorize_with user
452       put :update, {
453         id: jobs(:running).uuid,
454         job: {
455           components: {"component1" => "value1", "component2" => "value2"}
456         }
457       }
458       assert_response expected
459       if expected == :success
460         assert_not_nil json_response["components"]
461         keys = json_response["components"].keys
462         assert_equal ["component1", "component2"], keys
463         assert_equal "value1", json_response["components"][keys[0]]
464       end
465     end
466   end
467
468   test 'get_delete components_get again for job with components' do
469     authorize_with :active
470     get :show, {id: jobs(:running_job_with_components).uuid}
471     assert_response :success
472     assert_not_nil json_response["components"]
473     assert_equal ["component1", "component2"], json_response["components"].keys
474
475     # delete second component
476     @test_counter = 0  # Reset executed action counter
477     @controller = Arvados::V1::JobsController.new
478     put :update, {
479       id: jobs(:running_job_with_components).uuid,
480       job: {
481         components: {"component1" => "zzzzz-8i9sb-jobuuid00000001"}
482       }
483     }
484     assert_response :success
485
486     @test_counter = 0  # Reset executed action counter
487     @controller = Arvados::V1::JobsController.new
488     get :show, {id: jobs(:running_job_with_components).uuid}
489     assert_response :success
490     assert_not_nil json_response["components"]
491     assert_equal ["component1"], json_response["components"].keys
492
493     # delete all components
494     @test_counter = 0  # Reset executed action counter
495     @controller = Arvados::V1::JobsController.new
496     put :update, {
497       id: jobs(:running_job_with_components).uuid,
498       job: {
499         components: {}
500       }
501     }
502     assert_response :success
503
504     @test_counter = 0  # Reset executed action counter
505     @controller = Arvados::V1::JobsController.new
506     get :show, {id: jobs(:running_job_with_components).uuid}
507     assert_response :success
508     assert_not_nil json_response["components"]
509     assert_equal [], json_response["components"].keys
510   end
511
512   test 'jobs.create disabled in config' do
513     Rails.configuration.disable_api_methods = ["jobs.create",
514                                                "pipeline_instances.create"]
515     authorize_with :active
516     post :create, job: {
517       script: "hash",
518       script_version: "master",
519       repository: "active/foo",
520       script_parameters: {}
521     }
522     assert_response 404
523   end
524 end