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 = JSON.parse(@response.body)
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}$/)
53 assert_equal version, JSON.parse(@response.body)['script_version']
56 test "cancel a running job" do
57 # We need to verify that "cancel" creates a trigger file, so first
58 # let's make sure there is no stale trigger file.
60 File.unlink(Rails.configuration.crunch_refresh_trigger)
64 authorize_with :active
66 id: jobs(:running).uuid,
68 cancelled_at: 4.day.ago
71 assert_response :success
72 assert_not_nil assigns(:object)
73 job = JSON.parse(@response.body)
74 assert_not_nil job['uuid']
75 assert_not_nil job['cancelled_at']
76 assert_not_nil job['cancelled_by_user_uuid']
77 assert_not_nil job['cancelled_by_client_uuid']
78 assert_equal(true, Time.parse(job['cancelled_at']) > 1.minute.ago,
79 'server should correct bogus cancelled_at ' +
82 File.exists?(Rails.configuration.crunch_refresh_trigger),
83 'trigger file should be created when job is cancelled')
86 id: jobs(:running).uuid,
91 job = JSON.parse(@response.body)
92 assert_not_nil job['cancelled_at'], 'un-cancelled job stays cancelled'
95 test "update a job without failing script_version check" do
98 id: jobs(:uses_nonexistent_script_version).uuid,
100 owner_uuid: users(:admin).uuid
103 assert_response :success
105 id: jobs(:uses_nonexistent_script_version).uuid,
107 owner_uuid: users(:active).uuid
110 assert_response :success
113 test "search jobs by uuid with >= query" do
114 authorize_with :active
116 filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
118 assert_response :success
119 found = assigns(:objects).collect(&:uuid)
120 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
121 assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
124 test "search jobs by uuid with <= query" do
125 authorize_with :active
127 filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
129 assert_response :success
130 found = assigns(:objects).collect(&:uuid)
131 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
132 assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
135 test "search jobs by uuid with >= and <= query" do
136 authorize_with :active
138 filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
139 ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
141 assert_response :success
142 found = assigns(:objects).collect(&:uuid)
143 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
146 test "search jobs by uuid with < query" do
147 authorize_with :active
149 filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
151 assert_response :success
152 found = assigns(:objects).collect(&:uuid)
153 assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
154 assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
157 test "search jobs by uuid with like query" do
158 authorize_with :active
160 filters: [['uuid', 'like', '%hmckwoma9pl%']]
162 assert_response :success
163 found = assigns(:objects).collect(&:uuid)
164 assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
167 test "search jobs by uuid with 'in' query" do
168 authorize_with :active
170 filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
171 'zzzzz-8i9sb-pshmckwoma9plh7']]]
173 assert_response :success
174 found = assigns(:objects).collect(&:uuid)
175 assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
176 'zzzzz-8i9sb-pshmckwoma9plh7']
179 test "search jobs by uuid with 'not in' query" do
180 exclude_uuids = [jobs(:running).uuid,
181 jobs(:running_cancelled).uuid]
182 authorize_with :active
184 filters: [['uuid', 'not in', exclude_uuids]]
186 assert_response :success
187 found = assigns(:objects).collect(&:uuid)
188 assert_not_empty found, "'not in' query returned nothing"
189 assert_empty(found & exclude_uuids,
190 "'not in' query returned uuids I asked not to get")
193 ['=', '!='].each do |operator|
194 [['uuid', 'zzzzz-8i9sb-pshmckwoma9plh7'],
195 ['output', nil]].each do |attr, operand|
196 test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
197 authorize_with :active
199 filters: [[attr, operator, operand]]
201 assert_response :success
202 values = assigns(:objects).collect { |x| x.send(attr) }
203 assert_not_empty values, "query should return non-empty result"
205 assert_empty values - [operand], "query results do not satisfy query"
207 assert_empty values & [operand], "query results do not satisfy query"
213 test "search jobs by started_at with < query" do
214 authorize_with :active
216 filters: [['started_at', '<', Time.now.to_s]]
218 assert_response :success
219 found = assigns(:objects).collect(&:uuid)
220 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
223 test "search jobs by started_at with > query" do
224 authorize_with :active
226 filters: [['started_at', '>', Time.now.to_s]]
228 assert_response :success
229 assert_equal 0, assigns(:objects).count
232 test "search jobs by started_at with >= query on metric date" do
233 authorize_with :active
235 filters: [['started_at', '>=', '2014-01-01']]
237 assert_response :success
238 found = assigns(:objects).collect(&:uuid)
239 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
242 test "search jobs by started_at with >= query on metric date and time" do
243 authorize_with :active
245 filters: [['started_at', '>=', '2014-01-01 01:23:45']]
247 assert_response :success
248 found = assigns(:objects).collect(&:uuid)
249 assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
252 test "search jobs with 'any' operator" do
253 authorize_with :active
255 where: { any: ['contains', 'pshmckw'] }
257 assert_response :success
258 found = assigns(:objects).collect(&:uuid)
259 assert_equal 0, found.index('zzzzz-8i9sb-pshmckwoma9plh7')
260 assert_equal 1, found.count
263 test "search jobs by nonexistent column with < query" do
264 authorize_with :active
266 filters: [['is_borked', '<', 'fizzbuzz']]
271 test "finish a job" do
272 authorize_with :active
274 id: jobs(:nearly_finished_job).uuid,
276 output: '551392cc37a317abf865b95f66f4ef94+101',
277 log: '9215de2a951a721f5f156bc08cf63ad7+93',
278 tasks_summary: {done: 1, running: 0, todo: 0, failed: 0},
281 finished_at: Time.now.to_s
284 assert_response :success
287 [:active, :admin].each do |which_token|
288 test "get job queue as #{which_token} user" do
289 authorize_with which_token
291 assert_response :success
292 assert_operator 1, :<=, assigns(:objects).count
294 test "get job queue as #{which_token} user, with a filter" do
295 authorize_with which_token
296 get :queue, { filters: [['script','=','foo']] }
297 assert_response :success
298 assert_equal ['foo'], assigns(:objects).collect(&:script).uniq