Merge branch 'master' into 3889-functional-testing
[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   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.
107     begin
108       File.unlink(Rails.configuration.crunch_refresh_trigger)
109     rescue Errno::ENOENT
110     end
111
112     authorize_with :active
113     put :update, {
114       id: jobs(:running_cancelled).uuid,
115       job: {
116         cancelled_at: nil
117       }
118     }
119     job = JSON.parse(@response.body)
120     assert_not_nil job['cancelled_at'], 'un-cancelled job stays cancelled'
121   end
122
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
126       put :update, {
127         id: jobs(:uses_nonexistent_script_version).uuid,
128         job: {
129           script: script
130         }
131       }
132       assert_response :success
133       resp = assigns(:object)
134       assert_equal jobs(:uses_nonexistent_script_version).script_version, resp['script_version']
135     end
136   end
137
138   test "search jobs by uuid with >= query" do
139     authorize_with :active
140     get :index, {
141       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
142     }
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')
147   end
148
149   test "search jobs by uuid with <= query" do
150     authorize_with :active
151     get :index, {
152       filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
153     }
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')
158   end
159
160   test "search jobs by uuid with >= and <= query" do
161     authorize_with :active
162     get :index, {
163       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
164               ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
165     }
166     assert_response :success
167     found = assigns(:objects).collect(&:uuid)
168     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
169   end
170
171   test "search jobs by uuid with < query" do
172     authorize_with :active
173     get :index, {
174       filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
175     }
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')
180   end
181
182   test "search jobs by uuid with like query" do
183     authorize_with :active
184     get :index, {
185       filters: [['uuid', 'like', '%hmckwoma9pl%']]
186     }
187     assert_response :success
188     found = assigns(:objects).collect(&:uuid)
189     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
190   end
191
192   test "search jobs by uuid with 'in' query" do
193     authorize_with :active
194     get :index, {
195       filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
196                                 'zzzzz-8i9sb-pshmckwoma9plh7']]]
197     }
198     assert_response :success
199     found = assigns(:objects).collect(&:uuid)
200     assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
201                               'zzzzz-8i9sb-pshmckwoma9plh7']
202   end
203
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
208     get :index, {
209       filters: [['uuid', 'not in', exclude_uuids]]
210     }
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")
216   end
217
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
223         get :index, {
224           filters: [[attr, operator, operand]]
225         }
226         assert_response :success
227         values = assigns(:objects).collect { |x| x.send(attr) }
228         assert_not_empty values, "query should return non-empty result"
229         if operator == '='
230           assert_empty values - [operand], "query results do not satisfy query"
231         else
232           assert_empty values & [operand], "query results do not satisfy query"
233         end
234       end
235     end
236   end
237
238   test "search jobs by started_at with < query" do
239     authorize_with :active
240     get :index, {
241       filters: [['started_at', '<', Time.now.to_s]]
242     }
243     assert_response :success
244     found = assigns(:objects).collect(&:uuid)
245     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
246   end
247
248   test "search jobs by started_at with > query" do
249     authorize_with :active
250     get :index, {
251       filters: [['started_at', '>', Time.now.to_s]]
252     }
253     assert_response :success
254     assert_equal 0, assigns(:objects).count
255   end
256
257   test "search jobs by started_at with >= query on metric date" do
258     authorize_with :active
259     get :index, {
260       filters: [['started_at', '>=', '2014-01-01']]
261     }
262     assert_response :success
263     found = assigns(:objects).collect(&:uuid)
264     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
265   end
266
267   test "search jobs by started_at with >= query on metric date and time" do
268     authorize_with :active
269     get :index, {
270       filters: [['started_at', '>=', '2014-01-01 01:23:45']]
271     }
272     assert_response :success
273     found = assigns(:objects).collect(&:uuid)
274     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
275   end
276
277   test "search jobs with 'any' operator" do
278     authorize_with :active
279     get :index, {
280       where: { any: ['contains', 'pshmckw'] }
281     }
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
286   end
287
288   test "search jobs by nonexistent column with < query" do
289     authorize_with :active
290     get :index, {
291       filters: [['is_borked', '<', 'fizzbuzz']]
292     }
293     assert_response 422
294   end
295
296   test "finish a job" do
297     authorize_with :active
298     put :update, {
299       id: jobs(:nearly_finished_job).uuid,
300       job: {
301         output: '551392cc37a317abf865b95f66f4ef94+101',
302         log: '9215de2a951a721f5f156bc08cf63ad7+93',
303         tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
304         success: true,
305         running: false,
306         finished_at: Time.now.to_s
307       }
308     }
309     assert_response :success
310   end
311
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
315       get :queue
316       assert_response :success
317       assert_equal i, assigns(:objects).count
318     end
319   end
320
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
327   end
328
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
334   end
335
336   [:spectator, :admin].each do |which_token|
337     test "get queue_size as #{which_token} user" do
338       authorize_with which_token
339       get :queue_size
340       assert_response :success
341       assert_equal 1, JSON.parse(@response.body)["queue_size"]
342     end
343   end
344
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"])
350   end
351 end