Merge branch '3882-cancel-already-cancelled-job-TC' into 3882-cancel-already-cancelle...
[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    [:put, :update, {job:{cancelled_at: Time.now}}, :success],
106    [:put, :update, {job:{state: 'Cancelled'}}, :success],
107    [:put, :update, {job:{state: 'Running'}}, :unprocessable_entity],
108    [:put, :update, {job:{state: 'Failed'}}, :unprocessable_entity],
109    [:put, :update, {job:{state: 'Complete'}}, :unprocessable_entity],
110    [:post, :cancel, {}, :success],
111   ].each do |http_method, action, params, expected_response|
112     test "cancelled job stays cancelled after #{[http_method, action, params].inspect}" do
113       # We need to verify that "cancel" creates a trigger file, so first
114       # let's make sure there is no stale trigger file.
115       begin
116         File.unlink(Rails.configuration.crunch_refresh_trigger)
117       rescue Errno::ENOENT
118       end
119
120       authorize_with :active
121       self.send http_method, action, { id: jobs(:cancelled).uuid }.merge(params)
122       assert_response expected_response
123       if expected_response == :success
124         job = json_response
125         assert_not_nil job['cancelled_at'], 'job cancelled again using #{attribute}=#{value} did not have cancelled_at value'
126         assert_equal job['state'], 'Cancelled', 'cancelled again job state changed when updated using using #{attribute}=#{value}'
127       end
128       # Verify database record still says Cancelled
129       assert_equal 'Cancelled', Job.find(jobs(:cancelled).id).state, 'job was un-cancelled'
130     end
131   end
132
133   test "cancelled job updated to any other state change results in error" do
134     # We need to verify that "cancel" creates a trigger file, so first
135     # let's make sure there is no stale trigger file.
136     begin
137       File.unlink(Rails.configuration.crunch_refresh_trigger)
138     rescue Errno::ENOENT
139     end
140
141     authorize_with :active
142     put :update, {
143       id: jobs(:running_cancelled).uuid,
144       job: {
145         cancelled_at: nil
146       }
147     }
148     assert_response 422
149   end
150
151   ['abc.py', 'hash.py'].each do |script|
152     test "update job script attribute to #{script} without failing script_version check" do
153       authorize_with :admin
154       put :update, {
155         id: jobs(:uses_nonexistent_script_version).uuid,
156         job: {
157           script: script
158         }
159       }
160       assert_response :success
161       resp = assigns(:object)
162       assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
163     end
164   end
165
166   test "search jobs by uuid with >= query" do
167     authorize_with :active
168     get :index, {
169       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
170     }
171     assert_response :success
172     found = assigns(:objects).collect(&:uuid)
173     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
174     assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
175   end
176
177   test "search jobs by uuid with <= query" do
178     authorize_with :active
179     get :index, {
180       filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
181     }
182     assert_response :success
183     found = assigns(:objects).collect(&:uuid)
184     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
185     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
186   end
187
188   test "search jobs by uuid with >= and <= query" do
189     authorize_with :active
190     get :index, {
191       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
192               ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
193     }
194     assert_response :success
195     found = assigns(:objects).collect(&:uuid)
196     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
197   end
198
199   test "search jobs by uuid with < query" do
200     authorize_with :active
201     get :index, {
202       filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
203     }
204     assert_response :success
205     found = assigns(:objects).collect(&:uuid)
206     assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
207     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
208   end
209
210   test "search jobs by uuid with like query" do
211     authorize_with :active
212     get :index, {
213       filters: [['uuid', 'like', '%hmckwoma9pl%']]
214     }
215     assert_response :success
216     found = assigns(:objects).collect(&:uuid)
217     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
218   end
219
220   test "search jobs by uuid with 'in' query" do
221     authorize_with :active
222     get :index, {
223       filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
224                                 'zzzzz-8i9sb-pshmckwoma9plh7']]]
225     }
226     assert_response :success
227     found = assigns(:objects).collect(&:uuid)
228     assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
229                               'zzzzz-8i9sb-pshmckwoma9plh7']
230   end
231
232   test "search jobs by uuid with 'not in' query" do
233     exclude_uuids = [jobs(:running).uuid,
234                      jobs(:running_cancelled).uuid]
235     authorize_with :active
236     get :index, {
237       filters: [['uuid', 'not in', exclude_uuids]]
238     }
239     assert_response :success
240     found = assigns(:objects).collect(&:uuid)
241     assert_not_empty found, "'not in' query returned nothing"
242     assert_empty(found & exclude_uuids,
243                  "'not in' query returned uuids I asked not to get")
244   end
245
246   ['=', '!='].each do |operator|
247     [['uuid', 'zzzzz-8i9sb-pshmckwoma9plh7'],
248      ['output', nil]].each do |attr, operand|
249       test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
250         authorize_with :active
251         get :index, {
252           filters: [[attr, operator, operand]]
253         }
254         assert_response :success
255         values = assigns(:objects).collect { |x| x.send(attr) }
256         assert_not_empty values, "query should return non-empty result"
257         if operator == '='
258           assert_empty values - [operand], "query results do not satisfy query"
259         else
260           assert_empty values & [operand], "query results do not satisfy query"
261         end
262       end
263     end
264   end
265
266   test "search jobs by started_at with < query" do
267     authorize_with :active
268     get :index, {
269       filters: [['started_at', '<', Time.now.to_s]]
270     }
271     assert_response :success
272     found = assigns(:objects).collect(&:uuid)
273     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
274   end
275
276   test "search jobs by started_at with > query" do
277     authorize_with :active
278     get :index, {
279       filters: [['started_at', '>', Time.now.to_s]]
280     }
281     assert_response :success
282     assert_equal 0, assigns(:objects).count
283   end
284
285   test "search jobs by started_at with >= query on metric date" do
286     authorize_with :active
287     get :index, {
288       filters: [['started_at', '>=', '2014-01-01']]
289     }
290     assert_response :success
291     found = assigns(:objects).collect(&:uuid)
292     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
293   end
294
295   test "search jobs by started_at with >= query on metric date and time" do
296     authorize_with :active
297     get :index, {
298       filters: [['started_at', '>=', '2014-01-01 01:23:45']]
299     }
300     assert_response :success
301     found = assigns(:objects).collect(&:uuid)
302     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
303   end
304
305   test "search jobs with 'any' operator" do
306     authorize_with :active
307     get :index, {
308       where: { any: ['contains', 'pshmckw'] }
309     }
310     assert_response :success
311     found = assigns(:objects).collect(&:uuid)
312     assert_equal 0, found.index('zzzzz-8i9sb-pshmckwoma9plh7')
313     assert_equal 1, found.count
314   end
315
316   test "search jobs by nonexistent column with < query" do
317     authorize_with :active
318     get :index, {
319       filters: [['is_borked', '<', 'fizzbuzz']]
320     }
321     assert_response 422
322   end
323
324   test "finish a job" do
325     authorize_with :active
326     put :update, {
327       id: jobs(:nearly_finished_job).uuid,
328       job: {
329         output: '551392cc37a317abf865b95f66f4ef94+101',
330         log: '9215de2a951a721f5f156bc08cf63ad7+93',
331         tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
332         success: true,
333         running: false,
334         finished_at: Time.now.to_s
335       }
336     }
337     assert_response :success
338   end
339
340   [:spectator, :admin].each_with_index do |which_token, i|
341     test "get job queue as #{which_token} user" do
342       authorize_with which_token
343       get :queue
344       assert_response :success
345       assert_equal i, assigns(:objects).count
346     end
347   end
348
349   test "get job queue as with a = filter" do
350     authorize_with :admin
351     get :queue, { filters: [['script','=','foo']] }
352     assert_response :success
353     assert_equal ['foo'], assigns(:objects).collect(&:script).uniq
354     assert_equal 0, assigns(:objects)[0].queue_position
355   end
356
357   test "get job queue as with a != filter" do
358     authorize_with :admin
359     get :queue, { filters: [['script','!=','foo']] }
360     assert_response :success
361     assert_equal 0, assigns(:objects).count
362   end
363
364   [:spectator, :admin].each do |which_token|
365     test "get queue_size as #{which_token} user" do
366       authorize_with which_token
367       get :queue_size
368       assert_response :success
369       assert_equal 1, JSON.parse(@response.body)["queue_size"]
370     end
371   end
372
373   test "job includes assigned nodes" do
374     authorize_with :active
375     get :show, {id: jobs(:nearly_finished_job).uuid}
376     assert_response :success
377     assert_equal([nodes(:busy).uuid], json_response["node_uuids"])
378   end
379
380   test "job lock success" do
381     authorize_with :active
382     post :lock, {id: jobs(:queued).uuid}
383     assert_response :success
384     job = Job.where(uuid: jobs(:queued).uuid).first
385     assert_equal "Running", job.state
386   end
387
388   test "job lock conflict" do
389     authorize_with :active
390     post :lock, {id: jobs(:running).uuid}
391     assert_response 403 # forbidden
392   end
393 end