13996: Update tests for cleaner config access
[arvados.git] / services / api / test / functional / arvados / v1 / jobs_controller_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6 require 'helpers/git_test_helper'
7
8 class Arvados::V1::JobsControllerTest < ActionController::TestCase
9
10   include GitTestHelper
11
12   test "submit a job" do
13     authorize_with :active
14     post :create, params: {
15       job: {
16         script: "hash",
17         script_version: "master",
18         repository: "active/foo",
19         script_parameters: {}
20       }
21     }
22     assert_response :success
23     assert_not_nil assigns(:object)
24     new_job = JSON.parse(@response.body)
25     assert_not_nil new_job['uuid']
26     assert_not_nil new_job['script_version'].match(/^[0-9a-f]{40}$/)
27     assert_equal 0, new_job['priority']
28   end
29
30   test "normalize output and log uuids when creating job" do
31     authorize_with :active
32     post :create, params: {
33       job: {
34         script: "hash",
35         script_version: "master",
36         script_parameters: {},
37         repository: "active/foo",
38         started_at: Time.now,
39         finished_at: Time.now,
40         running: false,
41         success: true,
42         output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
43         log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
44       }
45     }
46     assert_response :success
47     assert_not_nil assigns(:object)
48     new_job = assigns(:object)
49     assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['log']
50     assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['output']
51     version = new_job['script_version']
52
53     # Make sure version doesn't get mangled by normalize
54     assert_not_nil version.match(/^[0-9a-f]{40}$/)
55     assert_equal 'master', json_response['supplied_script_version']
56   end
57
58   test "normalize output and log uuids when updating job" do
59     authorize_with :active
60
61     foobar_job = jobs(:foobar)
62
63     new_output = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
64     new_log = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
65     put :update, params: {
66       id: foobar_job['uuid'],
67       job: {
68         output: new_output,
69         log: new_log
70       }
71     }
72
73     updated_job = json_response
74     assert_not_equal foobar_job['log'], updated_job['log']
75     assert_not_equal new_log, updated_job['log']  # normalized during update
76     assert_equal new_log[0,new_log.rindex('+')], updated_job['log']
77     assert_not_equal foobar_job['output'], updated_job['output']
78     assert_not_equal new_output, updated_job['output']  # normalized during update
79     assert_equal new_output[0,new_output.rindex('+')], updated_job['output']
80   end
81
82   test "cancel a running job" do
83     # We need to verify that "cancel" creates a trigger file, so first
84     # let's make sure there is no stale trigger file.
85     begin
86       File.unlink(Rails.configuration.Containers.JobsAPI.CrunchRefreshTrigger)
87     rescue Errno::ENOENT
88     end
89
90     authorize_with :active
91     put :update, params: {
92       id: jobs(:running).uuid,
93       job: {
94         cancelled_at: 4.day.ago
95       }
96     }
97     assert_response :success
98     assert_not_nil assigns(:object)
99     job = JSON.parse(@response.body)
100     assert_not_nil job['uuid']
101     assert_not_nil job['cancelled_at']
102     assert_not_nil job['cancelled_by_user_uuid']
103     assert_not_nil job['cancelled_by_client_uuid']
104     assert_equal(true, Time.parse(job['cancelled_at']) > 1.minute.ago,
105                  'server should correct bogus cancelled_at ' +
106                  job['cancelled_at'])
107     assert_equal(true,
108                  File.exist?(Rails.configuration.Containers.JobsAPI.CrunchRefreshTrigger),
109                  'trigger file should be created when job is cancelled')
110   end
111
112   [
113    [:put, :update, {job:{cancelled_at: Time.now}}, :success],
114    [:put, :update, {job:{cancelled_at: nil}}, :unprocessable_entity],
115    [:put, :update, {job:{state: 'Cancelled'}}, :success],
116    [:put, :update, {job:{state: 'Queued'}}, :unprocessable_entity],
117    [:put, :update, {job:{state: 'Running'}}, :unprocessable_entity],
118    [:put, :update, {job:{state: 'Failed'}}, :unprocessable_entity],
119    [:put, :update, {job:{state: 'Complete'}}, :unprocessable_entity],
120    [:post, :cancel, {}, :success],
121   ].each do |http_method, action, params, expected_response|
122     test "cancelled job stays cancelled after #{[http_method, action, params].inspect}" do
123       # We need to verify that "cancel" creates a trigger file, so first
124       # let's make sure there is no stale trigger file.
125       begin
126         File.unlink(Rails.configuration.Containers.JobsAPI.CrunchRefreshTrigger)
127       rescue Errno::ENOENT
128       end
129
130       authorize_with :active
131       self.send http_method, action, params: { id: jobs(:cancelled).uuid }.merge(params)
132       assert_response expected_response
133       if expected_response == :success
134         job = json_response
135         assert_not_nil job['cancelled_at'], 'job cancelled again using #{attribute}=#{value} did not have cancelled_at value'
136         assert_equal job['state'], 'Cancelled', 'cancelled again job state changed when updated using using #{attribute}=#{value}'
137       end
138       # Verify database record still says Cancelled
139       assert_equal 'Cancelled', Job.find(jobs(:cancelled).id).state, 'job was un-cancelled'
140     end
141   end
142
143   test "cancelled job updated to any other state change results in error" do
144     # We need to verify that "cancel" creates a trigger file, so first
145     # let's make sure there is no stale trigger file.
146     begin
147       File.unlink(Rails.configuration.Containers.JobsAPI.CrunchRefreshTrigger)
148     rescue Errno::ENOENT
149     end
150
151     authorize_with :active
152     put :update, params: {
153       id: jobs(:running_cancelled).uuid,
154       job: {
155         cancelled_at: nil
156       }
157     }
158     assert_response 422
159   end
160
161   ['abc.py', 'hash.py'].each do |script|
162     test "update job script attribute to #{script} without failing script_version check" do
163       authorize_with :admin
164       put :update, params: {
165         id: jobs(:uses_nonexistent_script_version).uuid,
166         job: {
167           script: script
168         }
169       }
170       assert_response :success
171       resp = assigns(:object)
172       assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
173     end
174   end
175
176   test "search jobs by uuid with >= query" do
177     authorize_with :active
178     get :index, params: {
179       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
180     }
181     assert_response :success
182     found = assigns(:objects).collect(&:uuid)
183     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
184     assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
185   end
186
187   test "search jobs by uuid with <= query" do
188     authorize_with :active
189     get :index, params: {
190       filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
191     }
192     assert_response :success
193     found = assigns(:objects).collect(&:uuid)
194     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
195     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
196   end
197
198   test "search jobs by uuid with >= and <= query" do
199     authorize_with :active
200     get :index, params: {
201       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
202               ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
203     }
204     assert_response :success
205     found = assigns(:objects).collect(&:uuid)
206     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
207   end
208
209   test "search jobs by uuid with < query" do
210     authorize_with :active
211     get :index, params: {
212       filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
213     }
214     assert_response :success
215     found = assigns(:objects).collect(&:uuid)
216     assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
217     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
218   end
219
220   test "search jobs by uuid with like query" do
221     authorize_with :active
222     get :index, params: {
223       filters: [['uuid', 'like', '%hmckwoma9pl%']]
224     }
225     assert_response :success
226     found = assigns(:objects).collect(&:uuid)
227     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
228   end
229
230   test "search jobs by uuid with 'in' query" do
231     authorize_with :active
232     get :index, params: {
233       filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
234                                 'zzzzz-8i9sb-pshmckwoma9plh7']]]
235     }
236     assert_response :success
237     found = assigns(:objects).collect(&:uuid)
238     assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
239                               'zzzzz-8i9sb-pshmckwoma9plh7']
240   end
241
242   test "search jobs by uuid with 'not in' query" do
243     exclude_uuids = [jobs(:running).uuid,
244                      jobs(:running_cancelled).uuid]
245     authorize_with :active
246     get :index, params: {
247       filters: [['uuid', 'not in', exclude_uuids]]
248     }
249     assert_response :success
250     found = assigns(:objects).collect(&:uuid)
251     assert_not_empty found, "'not in' query returned nothing"
252     assert_empty(found & exclude_uuids,
253                  "'not in' query returned uuids I asked not to get")
254   end
255
256   ['=', '!='].each do |operator|
257     [['uuid', 'zzzzz-8i9sb-pshmckwoma9plh7'],
258      ['output', nil]].each do |attr, operand|
259       test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
260         authorize_with :active
261         get :index, params: {
262           filters: [[attr, operator, operand]]
263         }
264         assert_response :success
265         values = assigns(:objects).collect { |x| x.send(attr) }
266         assert_not_empty values, "query should return non-empty result"
267         if operator == '='
268           assert_empty values - [operand], "query results do not satisfy query"
269         else
270           assert_empty values & [operand], "query results do not satisfy query"
271         end
272       end
273     end
274   end
275
276   test "search jobs by started_at with < query" do
277     authorize_with :active
278     get :index, params: {
279       filters: [['started_at', '<', Time.now.to_s]]
280     }
281     assert_response :success
282     found = assigns(:objects).collect(&:uuid)
283     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
284   end
285
286   test "search jobs by started_at with > query" do
287     authorize_with :active
288     get :index, params: {
289       filters: [['started_at', '>', Time.now.to_s]]
290     }
291     assert_response :success
292     assert_equal 0, assigns(:objects).count
293   end
294
295   test "search jobs by started_at with >= query on metric date" do
296     authorize_with :active
297     get :index, params: {
298       filters: [['started_at', '>=', '2014-01-01']]
299     }
300     assert_response :success
301     found = assigns(:objects).collect(&:uuid)
302     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
303   end
304
305   test "search jobs by started_at with >= query on metric date and time" do
306     authorize_with :active
307     get :index, params: {
308       filters: [['started_at', '>=', '2014-01-01 01:23:45']]
309     }
310     assert_response :success
311     found = assigns(:objects).collect(&:uuid)
312     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
313   end
314
315   test "search jobs with 'any' operator" do
316     authorize_with :active
317     get :index, params: {
318       where: { any: ['contains', 'pshmckw'] }
319     }
320     assert_response :success
321     found = assigns(:objects).collect(&:uuid)
322     assert_equal 0, found.index('zzzzz-8i9sb-pshmckwoma9plh7')
323     assert_equal 1, found.count
324   end
325
326   test "search jobs by nonexistent column with < query" do
327     authorize_with :active
328     get :index, params: {
329       filters: [['is_borked', '<', 'fizzbuzz']]
330     }
331     assert_response 422
332   end
333
334   test "finish a job" do
335     authorize_with :active
336     put :update, params: {
337       id: jobs(:nearly_finished_job).uuid,
338       job: {
339         output: '551392cc37a317abf865b95f66f4ef94+101',
340         log: '9215de2a951a721f5f156bc08cf63ad7+93',
341         tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
342         success: true,
343         running: false,
344         finished_at: Time.now.to_s
345       }
346     }
347     assert_response :success
348   end
349
350   [:spectator, :admin].each_with_index do |which_token, i|
351     test "get job queue as #{which_token} user" do
352       authorize_with which_token
353       get :queue
354       assert_response :success
355       assert_equal i, assigns(:objects).count
356     end
357   end
358
359   test "get job queue as with a = filter" do
360     authorize_with :admin
361     get :queue, params: { filters: [['script','=','foo']] }
362     assert_response :success
363     assert_equal ['foo'], assigns(:objects).collect(&:script).uniq
364     assert_equal 0, assigns(:objects)[0].queue_position
365   end
366
367   test "get job queue as with a != filter" do
368     authorize_with :admin
369     get :queue, params: { filters: [['script','!=','foo']] }
370     assert_response :success
371     assert_equal 0, assigns(:objects).count
372   end
373
374   [:spectator, :admin].each do |which_token|
375     test "get queue_size as #{which_token} user" do
376       authorize_with which_token
377       get :queue_size
378       assert_response :success
379       assert_equal 1, JSON.parse(@response.body)["queue_size"]
380     end
381   end
382
383   test "job includes assigned nodes" do
384     authorize_with :active
385     get :show, params: {id: jobs(:nearly_finished_job).uuid}
386     assert_response :success
387     assert_equal([nodes(:busy).uuid], json_response["node_uuids"])
388   end
389
390   test "job lock success" do
391     authorize_with :active
392     post :lock, params: {id: jobs(:queued).uuid}
393     assert_response :success
394     job = Job.where(uuid: jobs(:queued).uuid).first
395     assert_equal "Running", job.state
396   end
397
398   test "job lock conflict" do
399     authorize_with :active
400     post :lock, params: {id: jobs(:running).uuid}
401     assert_response 422 # invalid state transition
402   end
403
404   test 'reject invalid commit in remote repository' do
405     authorize_with :active
406     url = "http://localhost:1/fake/fake.git"
407     fetch_remote_from_local_repo url, :foo
408     post :create, params: {
409       job: {
410         script: "hash",
411         script_version: "abc123",
412         repository: url,
413         script_parameters: {}
414       }
415     }
416     assert_response 422
417   end
418
419   test 'tag remote commit in internal repository' do
420     authorize_with :active
421     url = "http://localhost:1/fake/fake.git"
422     fetch_remote_from_local_repo url, :foo
423     post :create, params: {
424       job: {
425         script: "hash",
426         script_version: "master",
427         repository: url,
428         script_parameters: {}
429       }
430     }
431     assert_response :success
432     assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
433                  internal_tag(json_response['uuid']))
434   end
435
436   test 'tag local commit in internal repository' do
437     authorize_with :active
438     post :create, params: {
439       job: {
440         script: "hash",
441         script_version: "master",
442         repository: "active/foo",
443         script_parameters: {}
444       }
445     }
446     assert_response :success
447     assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
448                  internal_tag(json_response['uuid']))
449   end
450
451   test 'get job with components' do
452     authorize_with :active
453     get :show, params: {id: jobs(:running_job_with_components).uuid}
454     assert_response :success
455     assert_not_nil json_response["components"]
456     assert_equal ["component1", "component2"], json_response["components"].keys
457   end
458
459   [
460     [:active, :success],
461     [:system_user, :success],
462     [:admin, 403],
463   ].each do |user, expected|
464     test "add components to job locked by active user as #{user} user and expect #{expected}" do
465       authorize_with user
466       put :update, params: {
467         id: jobs(:running).uuid,
468         job: {
469           components: {"component1" => "value1", "component2" => "value2"}
470         }
471       }
472       assert_response expected
473       if expected == :success
474         assert_not_nil json_response["components"]
475         keys = json_response["components"].keys
476         assert_equal ["component1", "component2"], keys
477         assert_equal "value1", json_response["components"][keys[0]]
478       end
479     end
480   end
481
482   test 'jobs.create disabled in config' do
483     Rails.configuration.API.DisabledAPIs = ["jobs.create",
484                                                "pipeline_instances.create"]
485     authorize_with :active
486     post :create, params: {
487       job: {
488         script: "hash",
489         script_version: "master",
490         repository: "active/foo",
491         script_parameters: {}
492       }
493     }
494     assert_response 404
495   end
496 end