3882: refactor tests
[arvados.git] / services / api / test / functional / arvados / v1 / jobs_controller_test.rb
1 require 'test_helper'
2 require 'helpers/git_test_helper'
3
4 class Arvados::V1::JobsControllerTest < ActionController::TestCase
5
6   include GitTestHelper
7
8   test "submit a job" do
9     authorize_with :active
10     post :create, job: {
11       script: "hash",
12       script_version: "master",
13       repository: "foo",
14       script_parameters: {}
15     }
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']
22   end
23
24   test "normalize output and log uuids when creating job" do
25     authorize_with :active
26     post :create, job: {
27       script: "hash",
28       script_version: "master",
29       script_parameters: {},
30       repository: "foo",
31       started_at: Time.now,
32       finished_at: Time.now,
33       running: false,
34       success: true,
35       output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
36       log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
37     }
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']
44
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']
48   end
49
50   test "normalize output and log uuids when updating job" do
51     authorize_with :active
52
53     foobar_job = jobs(:foobar)
54
55     new_output = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
56     new_log = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
57     put :update, {
58       id: foobar_job['uuid'],
59       job: {
60         output: new_output,
61         log: new_log
62       }
63     }
64
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']
72   end
73
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.
77     begin
78       File.unlink(Rails.configuration.crunch_refresh_trigger)
79     rescue Errno::ENOENT
80     end
81
82     authorize_with :active
83     put :update, {
84       id: jobs(:running).uuid,
85       job: {
86         cancelled_at: 4.day.ago
87       }
88     }
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 ' +
98                  job['cancelled_at'])
99     assert_equal(true,
100                  File.exists?(Rails.configuration.crunch_refresh_trigger),
101                  'trigger file should be created when job is cancelled')
102   end
103
104   [
105     ['cancelled_at', Time.now],
106     ['state', 'Cancelled'],
107     ['state', 'Running'],
108     ['state', 'Failed'],
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.
114       begin
115         File.unlink(Rails.configuration.crunch_refresh_trigger)
116       rescue Errno::ENOENT
117       end
118
119       authorize_with :active
120       put :update, {
121         id: jobs(:cancelled).uuid,
122         job: {
123           attribute => value
124         }
125       }
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}'
129     end
130   end
131
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.
135     begin
136       File.unlink(Rails.configuration.crunch_refresh_trigger)
137     rescue Errno::ENOENT
138     end
139
140     authorize_with :active
141     put :update, {
142       id: jobs(:running_cancelled).uuid,
143       job: {
144         cancelled_at: nil
145       }
146     }
147     assert_response 422
148   end
149
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
153       put :update, {
154         id: jobs(:uses_nonexistent_script_version).uuid,
155         job: {
156           script: script
157         }
158       }
159       assert_response :success
160       resp = assigns(:object)
161       assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
162     end
163   end
164
165   test "search jobs by uuid with >= query" do
166     authorize_with :active
167     get :index, {
168       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
169     }
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')
174   end
175
176   test "search jobs by uuid with <= query" do
177     authorize_with :active
178     get :index, {
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 true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
185   end
186
187   test "search jobs by uuid with >= and <= query" do
188     authorize_with :active
189     get :index, {
190       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
191               ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
192     }
193     assert_response :success
194     found = assigns(:objects).collect(&:uuid)
195     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
196   end
197
198   test "search jobs by uuid with < query" do
199     authorize_with :active
200     get :index, {
201       filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
202     }
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')
207   end
208
209   test "search jobs by uuid with like query" do
210     authorize_with :active
211     get :index, {
212       filters: [['uuid', 'like', '%hmckwoma9pl%']]
213     }
214     assert_response :success
215     found = assigns(:objects).collect(&:uuid)
216     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
217   end
218
219   test "search jobs by uuid with 'in' query" do
220     authorize_with :active
221     get :index, {
222       filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
223                                 'zzzzz-8i9sb-pshmckwoma9plh7']]]
224     }
225     assert_response :success
226     found = assigns(:objects).collect(&:uuid)
227     assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
228                               'zzzzz-8i9sb-pshmckwoma9plh7']
229   end
230
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
235     get :index, {
236       filters: [['uuid', 'not in', exclude_uuids]]
237     }
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")
243   end
244
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
250         get :index, {
251           filters: [[attr, operator, operand]]
252         }
253         assert_response :success
254         values = assigns(:objects).collect { |x| x.send(attr) }
255         assert_not_empty values, "query should return non-empty result"
256         if operator == '='
257           assert_empty values - [operand], "query results do not satisfy query"
258         else
259           assert_empty values & [operand], "query results do not satisfy query"
260         end
261       end
262     end
263   end
264
265   test "search jobs by started_at with < query" do
266     authorize_with :active
267     get :index, {
268       filters: [['started_at', '<', Time.now.to_s]]
269     }
270     assert_response :success
271     found = assigns(:objects).collect(&:uuid)
272     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
273   end
274
275   test "search jobs by started_at with > query" do
276     authorize_with :active
277     get :index, {
278       filters: [['started_at', '>', Time.now.to_s]]
279     }
280     assert_response :success
281     assert_equal 0, assigns(:objects).count
282   end
283
284   test "search jobs by started_at with >= query on metric date" do
285     authorize_with :active
286     get :index, {
287       filters: [['started_at', '>=', '2014-01-01']]
288     }
289     assert_response :success
290     found = assigns(:objects).collect(&:uuid)
291     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
292   end
293
294   test "search jobs by started_at with >= query on metric date and time" do
295     authorize_with :active
296     get :index, {
297       filters: [['started_at', '>=', '2014-01-01 01:23:45']]
298     }
299     assert_response :success
300     found = assigns(:objects).collect(&:uuid)
301     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
302   end
303
304   test "search jobs with 'any' operator" do
305     authorize_with :active
306     get :index, {
307       where: { any: ['contains', 'pshmckw'] }
308     }
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
313   end
314
315   test "search jobs by nonexistent column with < query" do
316     authorize_with :active
317     get :index, {
318       filters: [['is_borked', '<', 'fizzbuzz']]
319     }
320     assert_response 422
321   end
322
323   test "finish a job" do
324     authorize_with :active
325     put :update, {
326       id: jobs(:nearly_finished_job).uuid,
327       job: {
328         output: '551392cc37a317abf865b95f66f4ef94+101',
329         log: '9215de2a951a721f5f156bc08cf63ad7+93',
330         tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
331         success: true,
332         running: false,
333         finished_at: Time.now.to_s
334       }
335     }
336     assert_response :success
337   end
338
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
342       get :queue
343       assert_response :success
344       assert_equal i, assigns(:objects).count
345     end
346   end
347
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
354   end
355
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
361   end
362
363   [:spectator, :admin].each do |which_token|
364     test "get queue_size as #{which_token} user" do
365       authorize_with which_token
366       get :queue_size
367       assert_response :success
368       assert_equal 1, JSON.parse(@response.body)["queue_size"]
369     end
370   end
371
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"])
377   end
378
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
385   end
386
387   test "job lock conflict" do
388     authorize_with :active
389     post :lock, {id: jobs(:running).uuid}
390     assert_response 403 # forbidden
391   end
392 end