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')
104 test "cancelling a cancelled jobs stays cancelled" do
105 # We need to verify that "cancel" creates a trigger file, so first
106 # let's make sure there is no stale trigger file.
108 File.unlink(Rails.configuration.crunch_refresh_trigger)
112 authorize_with :active
114 id: jobs(:running_cancelled).uuid,
119 job = JSON.parse(@response.body)
120 assert_not_nil job['cancelled_at'], 'un-cancelled job stays cancelled'
123 ['abc.py', 'hash.py'].each do |script|
124 test "update job script attribute to #{script} without failing script_version check" do
125 authorize_with :admin
127 id: jobs(:uses_nonexistent_script_version).uuid,
132 assert_response :success
133 resp = assigns(:object)
134 assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
138 test "search jobs by uuid with >= query" do
139 authorize_with :active
141 filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
143 assert_response :success
144 found = assigns(:objects).collect(&:uuid)
145 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
146 assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
149 test "search jobs by uuid with <= query" do
150 authorize_with :active
152 filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
154 assert_response :success
155 found = assigns(:objects).collect(&:uuid)
156 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
157 assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
160 test "search jobs by uuid with >= and <= query" do
161 authorize_with :active
163 filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
164 ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
166 assert_response :success
167 found = assigns(:objects).collect(&:uuid)
168 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
171 test "search jobs by uuid with < query" do
172 authorize_with :active
174 filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
176 assert_response :success
177 found = assigns(:objects).collect(&:uuid)
178 assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
179 assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
182 test "search jobs by uuid with like query" do
183 authorize_with :active
185 filters: [['uuid', 'like', '%hmckwoma9pl%']]
187 assert_response :success
188 found = assigns(:objects).collect(&:uuid)
189 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
192 test "search jobs by uuid with 'in' query" do
193 authorize_with :active
195 filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
196 'zzzzz-8i9sb-pshmckwoma9plh7']]]
198 assert_response :success
199 found = assigns(:objects).collect(&:uuid)
200 assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
201 'zzzzz-8i9sb-pshmckwoma9plh7']
204 test "search jobs by uuid with 'not in' query" do
205 exclude_uuids = [jobs(:running).uuid,
206 jobs(:running_cancelled).uuid]
207 authorize_with :active
209 filters: [['uuid', 'not in', exclude_uuids]]
211 assert_response :success
212 found = assigns(:objects).collect(&:uuid)
213 assert_not_empty found, "'not in' query returned nothing"
214 assert_empty(found & exclude_uuids,
215 "'not in' query returned uuids I asked not to get")
218 ['=', '!='].each do |operator|
219 [['uuid', 'zzzzz-8i9sb-pshmckwoma9plh7'],
220 ['output', nil]].each do |attr, operand|
221 test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
222 authorize_with :active
224 filters: [[attr, operator, operand]]
226 assert_response :success
227 values = assigns(:objects).collect { |x| x.send(attr) }
228 assert_not_empty values, "query should return non-empty result"
230 assert_empty values - [operand], "query results do not satisfy query"
232 assert_empty values & [operand], "query results do not satisfy query"
238 test "search jobs by started_at with < query" do
239 authorize_with :active
241 filters: [['started_at', '<', Time.now.to_s]]
243 assert_response :success
244 found = assigns(:objects).collect(&:uuid)
245 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
248 test "search jobs by started_at with > query" do
249 authorize_with :active
251 filters: [['started_at', '>', Time.now.to_s]]
253 assert_response :success
254 assert_equal 0, assigns(:objects).count
257 test "search jobs by started_at with >= query on metric date" do
258 authorize_with :active
260 filters: [['started_at', '>=', '2014-01-01']]
262 assert_response :success
263 found = assigns(:objects).collect(&:uuid)
264 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
267 test "search jobs by started_at with >= query on metric date and time" do
268 authorize_with :active
270 filters: [['started_at', '>=', '2014-01-01 01:23:45']]
272 assert_response :success
273 found = assigns(:objects).collect(&:uuid)
274 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
277 test "search jobs with 'any' operator" do
278 authorize_with :active
280 where: { any: ['contains', 'pshmckw'] }
282 assert_response :success
283 found = assigns(:objects).collect(&:uuid)
284 assert_equal 0, found.index('zzzzz-8i9sb-pshmckwoma9plh7')
285 assert_equal 1, found.count
288 test "search jobs by nonexistent column with < query" do
289 authorize_with :active
291 filters: [['is_borked', '<', 'fizzbuzz']]
296 test "finish a job" do
297 authorize_with :active
299 id: jobs(:nearly_finished_job).uuid,
301 output: '551392cc37a317abf865b95f66f4ef94+101',
302 log: '9215de2a951a721f5f156bc08cf63ad7+93',
303 tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
306 finished_at: Time.now.to_s
309 assert_response :success
312 [:spectator, :admin].each_with_index do |which_token, i|
313 test "get job queue as #{which_token} user" do
314 authorize_with which_token
316 assert_response :success
317 assert_equal i, assigns(:objects).count
321 test "get job queue as with a = filter" do
322 authorize_with :admin
323 get :queue, { filters: [['script','=','foo']] }
324 assert_response :success
325 assert_equal ['foo'], assigns(:objects).collect(&:script).uniq
326 assert_equal 0, assigns(:objects)[0].queue_position
329 test "get job queue as with a != filter" do
330 authorize_with :admin
331 get :queue, { filters: [['script','!=','foo']] }
332 assert_response :success
333 assert_equal 0, assigns(:objects).count
336 [:spectator, :admin].each do |which_token|
337 test "get queue_size as #{which_token} user" do
338 authorize_with which_token
340 assert_response :success
341 assert_equal 1, JSON.parse(@response.body)["queue_size"]
345 test "job includes assigned nodes" do
346 authorize_with :active
347 get :show, {id: jobs(:nearly_finished_job).uuid}
348 assert_response :success
349 assert_equal([nodes(:busy).uuid], json_response["node_uuids"])