1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 require 'helpers/git_test_helper'
8 class Arvados::V1::JobsControllerTest < ActionController::TestCase
12 test "submit a job" do
13 authorize_with :active
16 script_version: "master",
17 repository: "active/foo",
20 assert_response :success
21 assert_not_nil assigns(:object)
22 new_job = JSON.parse(@response.body)
23 assert_not_nil new_job['uuid']
24 assert_not_nil new_job['script_version'].match(/^[0-9a-f]{40}$/)
25 assert_equal 0, new_job['priority']
28 test "normalize output and log uuids when creating job" do
29 authorize_with :active
32 script_version: "master",
33 script_parameters: {},
34 repository: "active/foo",
36 finished_at: Time.now,
39 output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
40 log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
42 assert_response :success
43 assert_not_nil assigns(:object)
44 new_job = assigns(:object)
45 assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['log']
46 assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['output']
47 version = new_job['script_version']
49 # Make sure version doesn't get mangled by normalize
50 assert_not_nil version.match(/^[0-9a-f]{40}$/)
51 assert_equal 'master', json_response['supplied_script_version']
54 test "normalize output and log uuids when updating job" do
55 authorize_with :active
57 foobar_job = jobs(:foobar)
59 new_output = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
60 new_log = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
62 id: foobar_job['uuid'],
69 updated_job = json_response
70 assert_not_equal foobar_job['log'], updated_job['log']
71 assert_not_equal new_log, updated_job['log'] # normalized during update
72 assert_equal new_log[0,new_log.rindex('+')], updated_job['log']
73 assert_not_equal foobar_job['output'], updated_job['output']
74 assert_not_equal new_output, updated_job['output'] # normalized during update
75 assert_equal new_output[0,new_output.rindex('+')], updated_job['output']
78 test "cancel a running job" do
79 # We need to verify that "cancel" creates a trigger file, so first
80 # let's make sure there is no stale trigger file.
82 File.unlink(Rails.configuration.crunch_refresh_trigger)
86 authorize_with :active
88 id: jobs(:running).uuid,
90 cancelled_at: 4.day.ago
93 assert_response :success
94 assert_not_nil assigns(:object)
95 job = JSON.parse(@response.body)
96 assert_not_nil job['uuid']
97 assert_not_nil job['cancelled_at']
98 assert_not_nil job['cancelled_by_user_uuid']
99 assert_not_nil job['cancelled_by_client_uuid']
100 assert_equal(true, Time.parse(job['cancelled_at']) > 1.minute.ago,
101 'server should correct bogus cancelled_at ' +
104 File.exist?(Rails.configuration.crunch_refresh_trigger),
105 'trigger file should be created when job is cancelled')
109 [:put, :update, {job:{cancelled_at: Time.now}}, :success],
110 [:put, :update, {job:{cancelled_at: nil}}, :unprocessable_entity],
111 [:put, :update, {job:{state: 'Cancelled'}}, :success],
112 [:put, :update, {job:{state: 'Queued'}}, :unprocessable_entity],
113 [:put, :update, {job:{state: 'Running'}}, :unprocessable_entity],
114 [:put, :update, {job:{state: 'Failed'}}, :unprocessable_entity],
115 [:put, :update, {job:{state: 'Complete'}}, :unprocessable_entity],
116 [:post, :cancel, {}, :success],
117 ].each do |http_method, action, params, expected_response|
118 test "cancelled job stays cancelled after #{[http_method, action, params].inspect}" do
119 # We need to verify that "cancel" creates a trigger file, so first
120 # let's make sure there is no stale trigger file.
122 File.unlink(Rails.configuration.crunch_refresh_trigger)
126 authorize_with :active
127 self.send http_method, action, { id: jobs(:cancelled).uuid }.merge(params)
128 assert_response expected_response
129 if expected_response == :success
131 assert_not_nil job['cancelled_at'], 'job cancelled again using #{attribute}=#{value} did not have cancelled_at value'
132 assert_equal job['state'], 'Cancelled', 'cancelled again job state changed when updated using using #{attribute}=#{value}'
134 # Verify database record still says Cancelled
135 assert_equal 'Cancelled', Job.find(jobs(:cancelled).id).state, 'job was un-cancelled'
139 test "cancelled job updated to any other state change results in error" do
140 # We need to verify that "cancel" creates a trigger file, so first
141 # let's make sure there is no stale trigger file.
143 File.unlink(Rails.configuration.crunch_refresh_trigger)
147 authorize_with :active
149 id: jobs(:running_cancelled).uuid,
157 ['abc.py', 'hash.py'].each do |script|
158 test "update job script attribute to #{script} without failing script_version check" do
159 authorize_with :admin
161 id: jobs(:uses_nonexistent_script_version).uuid,
166 assert_response :success
167 resp = assigns(:object)
168 assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
172 test "search jobs by uuid with >= query" do
173 authorize_with :active
175 filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
177 assert_response :success
178 found = assigns(:objects).collect(&:uuid)
179 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
180 assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
183 test "search jobs by uuid with <= query" do
184 authorize_with :active
186 filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
188 assert_response :success
189 found = assigns(:objects).collect(&:uuid)
190 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
191 assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
194 test "search jobs by uuid with >= and <= query" do
195 authorize_with :active
197 filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
198 ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
200 assert_response :success
201 found = assigns(:objects).collect(&:uuid)
202 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
205 test "search jobs by uuid with < query" do
206 authorize_with :active
208 filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
210 assert_response :success
211 found = assigns(:objects).collect(&:uuid)
212 assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
213 assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
216 test "search jobs by uuid with like query" do
217 authorize_with :active
219 filters: [['uuid', 'like', '%hmckwoma9pl%']]
221 assert_response :success
222 found = assigns(:objects).collect(&:uuid)
223 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
226 test "search jobs by uuid with 'in' query" do
227 authorize_with :active
229 filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
230 'zzzzz-8i9sb-pshmckwoma9plh7']]]
232 assert_response :success
233 found = assigns(:objects).collect(&:uuid)
234 assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
235 'zzzzz-8i9sb-pshmckwoma9plh7']
238 test "search jobs by uuid with 'not in' query" do
239 exclude_uuids = [jobs(:running).uuid,
240 jobs(:running_cancelled).uuid]
241 authorize_with :active
243 filters: [['uuid', 'not in', exclude_uuids]]
245 assert_response :success
246 found = assigns(:objects).collect(&:uuid)
247 assert_not_empty found, "'not in' query returned nothing"
248 assert_empty(found & exclude_uuids,
249 "'not in' query returned uuids I asked not to get")
252 ['=', '!='].each do |operator|
253 [['uuid', 'zzzzz-8i9sb-pshmckwoma9plh7'],
254 ['output', nil]].each do |attr, operand|
255 test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
256 authorize_with :active
258 filters: [[attr, operator, operand]]
260 assert_response :success
261 values = assigns(:objects).collect { |x| x.send(attr) }
262 assert_not_empty values, "query should return non-empty result"
264 assert_empty values - [operand], "query results do not satisfy query"
266 assert_empty values & [operand], "query results do not satisfy query"
272 test "search jobs by started_at with < query" do
273 authorize_with :active
275 filters: [['started_at', '<', Time.now.to_s]]
277 assert_response :success
278 found = assigns(:objects).collect(&:uuid)
279 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
282 test "search jobs by started_at with > query" do
283 authorize_with :active
285 filters: [['started_at', '>', Time.now.to_s]]
287 assert_response :success
288 assert_equal 0, assigns(:objects).count
291 test "search jobs by started_at with >= query on metric date" do
292 authorize_with :active
294 filters: [['started_at', '>=', '2014-01-01']]
296 assert_response :success
297 found = assigns(:objects).collect(&:uuid)
298 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
301 test "search jobs by started_at with >= query on metric date and time" do
302 authorize_with :active
304 filters: [['started_at', '>=', '2014-01-01 01:23:45']]
306 assert_response :success
307 found = assigns(:objects).collect(&:uuid)
308 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
311 test "search jobs with 'any' operator" do
312 authorize_with :active
314 where: { any: ['contains', 'pshmckw'] }
316 assert_response :success
317 found = assigns(:objects).collect(&:uuid)
318 assert_equal 0, found.index('zzzzz-8i9sb-pshmckwoma9plh7')
319 assert_equal 1, found.count
322 test "search jobs by nonexistent column with < query" do
323 authorize_with :active
325 filters: [['is_borked', '<', 'fizzbuzz']]
330 test "finish a job" do
331 authorize_with :active
333 id: jobs(:nearly_finished_job).uuid,
335 output: '551392cc37a317abf865b95f66f4ef94+101',
336 log: '9215de2a951a721f5f156bc08cf63ad7+93',
337 tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
340 finished_at: Time.now.to_s
343 assert_response :success
346 [:spectator, :admin].each_with_index do |which_token, i|
347 test "get job queue as #{which_token} user" do
348 authorize_with which_token
350 assert_response :success
351 assert_equal i, assigns(:objects).count
355 test "get job queue as with a = filter" do
356 authorize_with :admin
357 get :queue, { filters: [['script','=','foo']] }
358 assert_response :success
359 assert_equal ['foo'], assigns(:objects).collect(&:script).uniq
360 assert_equal 0, assigns(:objects)[0].queue_position
363 test "get job queue as with a != filter" do
364 authorize_with :admin
365 get :queue, { filters: [['script','!=','foo']] }
366 assert_response :success
367 assert_equal 0, assigns(:objects).count
370 [:spectator, :admin].each do |which_token|
371 test "get queue_size as #{which_token} user" do
372 authorize_with which_token
374 assert_response :success
375 assert_equal 1, JSON.parse(@response.body)["queue_size"]
379 test "job includes assigned nodes" do
380 authorize_with :active
381 get :show, {id: jobs(:nearly_finished_job).uuid}
382 assert_response :success
383 assert_equal([nodes(:busy).uuid], json_response["node_uuids"])
386 test "job lock success" do
387 authorize_with :active
388 post :lock, {id: jobs(:queued).uuid}
389 assert_response :success
390 job = Job.where(uuid: jobs(:queued).uuid).first
391 assert_equal "Running", job.state
394 test "job lock conflict" do
395 authorize_with :active
396 post :lock, {id: jobs(:running).uuid}
397 assert_response 422 # invalid state transition
400 test 'reject invalid commit in remote repository' do
401 authorize_with :active
402 url = "http://localhost:1/fake/fake.git"
403 fetch_remote_from_local_repo url, :foo
406 script_version: "abc123",
408 script_parameters: {}
413 test 'tag remote commit in internal repository' do
414 authorize_with :active
415 url = "http://localhost:1/fake/fake.git"
416 fetch_remote_from_local_repo url, :foo
419 script_version: "master",
421 script_parameters: {}
423 assert_response :success
424 assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
425 internal_tag(json_response['uuid']))
428 test 'tag local commit in internal repository' do
429 authorize_with :active
432 script_version: "master",
433 repository: "active/foo",
434 script_parameters: {}
436 assert_response :success
437 assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
438 internal_tag(json_response['uuid']))
441 test 'get job with components' do
442 authorize_with :active
443 get :show, {id: jobs(:running_job_with_components).uuid}
444 assert_response :success
445 assert_not_nil json_response["components"]
446 assert_equal ["component1", "component2"], json_response["components"].keys
451 [:system_user, :success],
453 ].each do |user, expected|
454 test "add components to job locked by active user as #{user} user and expect #{expected}" do
457 id: jobs(:running).uuid,
459 components: {"component1" => "value1", "component2" => "value2"}
462 assert_response expected
463 if expected == :success
464 assert_not_nil json_response["components"]
465 keys = json_response["components"].keys
466 assert_equal ["component1", "component2"], keys
467 assert_equal "value1", json_response["components"][keys[0]]
472 test 'get_delete components_get again for job with components' do
473 authorize_with :active
474 get :show, {id: jobs(:running_job_with_components).uuid}
475 assert_response :success
476 assert_not_nil json_response["components"]
477 assert_equal ["component1", "component2"], json_response["components"].keys
479 # delete second component
480 @test_counter = 0 # Reset executed action counter
481 @controller = Arvados::V1::JobsController.new
483 id: jobs(:running_job_with_components).uuid,
485 components: {"component1" => "zzzzz-8i9sb-jobuuid00000001"}
488 assert_response :success
490 @test_counter = 0 # Reset executed action counter
491 @controller = Arvados::V1::JobsController.new
492 get :show, {id: jobs(:running_job_with_components).uuid}
493 assert_response :success
494 assert_not_nil json_response["components"]
495 assert_equal ["component1"], json_response["components"].keys
497 # delete all components
498 @test_counter = 0 # Reset executed action counter
499 @controller = Arvados::V1::JobsController.new
501 id: jobs(:running_job_with_components).uuid,
506 assert_response :success
508 @test_counter = 0 # Reset executed action counter
509 @controller = Arvados::V1::JobsController.new
510 get :show, {id: jobs(:running_job_with_components).uuid}
511 assert_response :success
512 assert_not_nil json_response["components"]
513 assert_equal [], json_response["components"].keys
516 test 'jobs.create disabled in config' do
517 Rails.configuration.disable_api_methods = ["jobs.create",
518 "pipeline_instances.create"]
519 authorize_with :active
522 script_version: "master",
523 repository: "active/foo",
524 script_parameters: {}