2 require 'helpers/git_test_helper'
4 class Arvados::V1::JobsControllerTest < ActionController::TestCase
12 script_version: "master",
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']
24 test "normalize output and log uuids when creating job" do
25 authorize_with :active
28 script_version: "master",
29 script_parameters: {},
32 finished_at: Time.now,
35 output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
36 log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
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']
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']
50 test "normalize output and log uuids when updating job" do
51 authorize_with :active
53 foobar_job = jobs(:foobar)
55 new_output = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
56 new_log = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
58 id: foobar_job['uuid'],
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']
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.
78 File.unlink(Rails.configuration.crunch_refresh_trigger)
82 authorize_with :active
84 id: jobs(:running).uuid,
86 cancelled_at: 4.day.ago
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 ' +
100 File.exists?(Rails.configuration.crunch_refresh_trigger),
101 'trigger file should be created when job is cancelled')
105 ['cancelled_at', Time.now],
106 ['state', 'Cancelled'],
107 ['state', 'Running'],
109 ['state', 'Complete'],
110 ].each do |attribute, value|
111 test "cancelled job stays cancelled when updated using #{attribute} #{value}" do
112 # We need to verify that "cancel" creates a trigger file, so first
113 # let's make sure there is no stale trigger file.
115 File.unlink(Rails.configuration.crunch_refresh_trigger)
119 authorize_with :active
121 id: jobs(:cancelled).uuid,
126 job = JSON.parse(@response.body)
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}'
132 test "cancelled job updated to any other state change results in error" do
133 # We need to verify that "cancel" creates a trigger file, so first
134 # let's make sure there is no stale trigger file.
136 File.unlink(Rails.configuration.crunch_refresh_trigger)
140 authorize_with :active
142 id: jobs(:running_cancelled).uuid,
150 ['abc.py', 'hash.py'].each do |script|
151 test "update job script attribute to #{script} without failing script_version check" do
152 authorize_with :admin
154 id: jobs(:uses_nonexistent_script_version).uuid,
159 assert_response :success
160 resp = assigns(:object)
161 assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
165 test "search jobs by uuid with >= query" do
166 authorize_with :active
168 filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
170 assert_response :success
171 found = assigns(:objects).collect(&:uuid)
172 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
173 assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
176 test "search jobs by uuid with <= query" do
177 authorize_with :active
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 true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
187 test "search jobs by uuid with >= and <= query" do
188 authorize_with :active
190 filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
191 ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
193 assert_response :success
194 found = assigns(:objects).collect(&:uuid)
195 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
198 test "search jobs by uuid with < query" do
199 authorize_with :active
201 filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
203 assert_response :success
204 found = assigns(:objects).collect(&:uuid)
205 assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
206 assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
209 test "search jobs by uuid with like query" do
210 authorize_with :active
212 filters: [['uuid', 'like', '%hmckwoma9pl%']]
214 assert_response :success
215 found = assigns(:objects).collect(&:uuid)
216 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
219 test "search jobs by uuid with 'in' query" do
220 authorize_with :active
222 filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
223 'zzzzz-8i9sb-pshmckwoma9plh7']]]
225 assert_response :success
226 found = assigns(:objects).collect(&:uuid)
227 assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
228 'zzzzz-8i9sb-pshmckwoma9plh7']
231 test "search jobs by uuid with 'not in' query" do
232 exclude_uuids = [jobs(:running).uuid,
233 jobs(:running_cancelled).uuid]
234 authorize_with :active
236 filters: [['uuid', 'not in', exclude_uuids]]
238 assert_response :success
239 found = assigns(:objects).collect(&:uuid)
240 assert_not_empty found, "'not in' query returned nothing"
241 assert_empty(found & exclude_uuids,
242 "'not in' query returned uuids I asked not to get")
245 ['=', '!='].each do |operator|
246 [['uuid', 'zzzzz-8i9sb-pshmckwoma9plh7'],
247 ['output', nil]].each do |attr, operand|
248 test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
249 authorize_with :active
251 filters: [[attr, operator, operand]]
253 assert_response :success
254 values = assigns(:objects).collect { |x| x.send(attr) }
255 assert_not_empty values, "query should return non-empty result"
257 assert_empty values - [operand], "query results do not satisfy query"
259 assert_empty values & [operand], "query results do not satisfy query"
265 test "search jobs by started_at with < query" do
266 authorize_with :active
268 filters: [['started_at', '<', Time.now.to_s]]
270 assert_response :success
271 found = assigns(:objects).collect(&:uuid)
272 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
275 test "search jobs by started_at with > query" do
276 authorize_with :active
278 filters: [['started_at', '>', Time.now.to_s]]
280 assert_response :success
281 assert_equal 0, assigns(:objects).count
284 test "search jobs by started_at with >= query on metric date" do
285 authorize_with :active
287 filters: [['started_at', '>=', '2014-01-01']]
289 assert_response :success
290 found = assigns(:objects).collect(&:uuid)
291 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
294 test "search jobs by started_at with >= query on metric date and time" do
295 authorize_with :active
297 filters: [['started_at', '>=', '2014-01-01 01:23:45']]
299 assert_response :success
300 found = assigns(:objects).collect(&:uuid)
301 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
304 test "search jobs with 'any' operator" do
305 authorize_with :active
307 where: { any: ['contains', 'pshmckw'] }
309 assert_response :success
310 found = assigns(:objects).collect(&:uuid)
311 assert_equal 0, found.index('zzzzz-8i9sb-pshmckwoma9plh7')
312 assert_equal 1, found.count
315 test "search jobs by nonexistent column with < query" do
316 authorize_with :active
318 filters: [['is_borked', '<', 'fizzbuzz']]
323 test "finish a job" do
324 authorize_with :active
326 id: jobs(:nearly_finished_job).uuid,
328 output: '551392cc37a317abf865b95f66f4ef94+101',
329 log: '9215de2a951a721f5f156bc08cf63ad7+93',
330 tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
333 finished_at: Time.now.to_s
336 assert_response :success
339 [:spectator, :admin].each_with_index do |which_token, i|
340 test "get job queue as #{which_token} user" do
341 authorize_with which_token
343 assert_response :success
344 assert_equal i, assigns(:objects).count
348 test "get job queue as with a = filter" do
349 authorize_with :admin
350 get :queue, { filters: [['script','=','foo']] }
351 assert_response :success
352 assert_equal ['foo'], assigns(:objects).collect(&:script).uniq
353 assert_equal 0, assigns(:objects)[0].queue_position
356 test "get job queue as with a != filter" do
357 authorize_with :admin
358 get :queue, { filters: [['script','!=','foo']] }
359 assert_response :success
360 assert_equal 0, assigns(:objects).count
363 [:spectator, :admin].each do |which_token|
364 test "get queue_size as #{which_token} user" do
365 authorize_with which_token
367 assert_response :success
368 assert_equal 1, JSON.parse(@response.body)["queue_size"]
372 test "job includes assigned nodes" do
373 authorize_with :active
374 get :show, {id: jobs(:nearly_finished_job).uuid}
375 assert_response :success
376 assert_equal([nodes(:busy).uuid], json_response["node_uuids"])
379 test "job lock success" do
380 authorize_with :active
381 post :lock, {id: jobs(:queued).uuid}
382 assert_response :success
383 job = Job.where(uuid: jobs(:queued).uuid).first
384 assert_equal "Running", job.state
387 test "job lock conflict" do
388 authorize_with :active
389 post :lock, {id: jobs(:running).uuid}
390 assert_response 403 # forbidden