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
14 post :create, params: {
17 script_version: "master",
18 repository: "active/foo",
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']
30 test "normalize output and log uuids when creating job" do
31 authorize_with :active
32 post :create, params: {
35 script_version: "master",
36 script_parameters: {},
37 repository: "active/foo",
39 finished_at: Time.now,
42 output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
43 log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
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']
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']
58 test "normalize output and log uuids when updating job" do
59 authorize_with :active
61 foobar_job = jobs(:foobar)
63 new_output = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
64 new_log = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
65 put :update, params: {
66 id: foobar_job['uuid'],
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']
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.
86 File.unlink(Rails.configuration.Containers.JobsAPI.CrunchRefreshTrigger)
90 authorize_with :active
91 put :update, params: {
92 id: jobs(:running).uuid,
94 cancelled_at: 4.day.ago
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 ' +
108 File.exist?(Rails.configuration.Containers.JobsAPI.CrunchRefreshTrigger),
109 'trigger file should be created when job is cancelled')
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.
126 File.unlink(Rails.configuration.Containers.JobsAPI.CrunchRefreshTrigger)
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
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}'
138 # Verify database record still says Cancelled
139 assert_equal 'Cancelled', Job.find(jobs(:cancelled).id).state, 'job was un-cancelled'
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.
147 File.unlink(Rails.configuration.Containers.JobsAPI.CrunchRefreshTrigger)
151 authorize_with :active
152 put :update, params: {
153 id: jobs(:running_cancelled).uuid,
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,
170 assert_response :success
171 resp = assigns(:object)
172 assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
176 test "search jobs by uuid with >= query" do
177 authorize_with :active
178 get :index, params: {
179 filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
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')
187 test "search jobs by uuid with <= query" do
188 authorize_with :active
189 get :index, params: {
190 filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
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')
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']]
204 assert_response :success
205 found = assigns(:objects).collect(&:uuid)
206 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
209 test "search jobs by uuid with < query" do
210 authorize_with :active
211 get :index, params: {
212 filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
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')
220 test "search jobs by uuid with like query" do
221 authorize_with :active
222 get :index, params: {
223 filters: [['uuid', 'like', '%hmckwoma9pl%']]
225 assert_response :success
226 found = assigns(:objects).collect(&:uuid)
227 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
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']]]
236 assert_response :success
237 found = assigns(:objects).collect(&:uuid)
238 assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
239 'zzzzz-8i9sb-pshmckwoma9plh7']
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]]
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")
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]]
264 assert_response :success
265 values = assigns(:objects).collect { |x| x.send(attr) }
266 assert_not_empty values, "query should return non-empty result"
268 assert_empty values - [operand], "query results do not satisfy query"
270 assert_empty values & [operand], "query results do not satisfy query"
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]]
281 assert_response :success
282 found = assigns(:objects).collect(&:uuid)
283 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
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]]
291 assert_response :success
292 assert_equal 0, assigns(:objects).count
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']]
300 assert_response :success
301 found = assigns(:objects).collect(&:uuid)
302 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
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']]
310 assert_response :success
311 found = assigns(:objects).collect(&:uuid)
312 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
315 test "search jobs with 'any' operator" do
316 authorize_with :active
317 get :index, params: {
318 where: { any: ['contains', 'pshmckw'] }
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
326 test "search jobs by nonexistent column with < query" do
327 authorize_with :active
328 get :index, params: {
329 filters: [['is_borked', '<', 'fizzbuzz']]
334 test "finish a job" do
335 authorize_with :active
336 put :update, params: {
337 id: jobs(:nearly_finished_job).uuid,
339 output: '551392cc37a317abf865b95f66f4ef94+101',
340 log: '9215de2a951a721f5f156bc08cf63ad7+93',
341 tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
344 finished_at: Time.now.to_s
347 assert_response :success
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
354 assert_response :success
355 assert_equal i, assigns(:objects).count
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
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
374 [:spectator, :admin].each do |which_token|
375 test "get queue_size as #{which_token} user" do
376 authorize_with which_token
378 assert_response :success
379 assert_equal 1, JSON.parse(@response.body)["queue_size"]
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"])
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
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
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: {
411 script_version: "abc123",
413 script_parameters: {}
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: {
426 script_version: "master",
428 script_parameters: {}
431 assert_response :success
432 assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
433 internal_tag(json_response['uuid']))
436 test 'tag local commit in internal repository' do
437 authorize_with :active
438 post :create, params: {
441 script_version: "master",
442 repository: "active/foo",
443 script_parameters: {}
446 assert_response :success
447 assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
448 internal_tag(json_response['uuid']))
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
461 [:system_user, :success],
463 ].each do |user, expected|
464 test "add components to job locked by active user as #{user} user and expect #{expected}" do
466 put :update, params: {
467 id: jobs(:running).uuid,
469 components: {"component1" => "value1", "component2" => "value2"}
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]]
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: {
489 script_version: "master",
490 repository: "active/foo",
491 script_parameters: {}