3 class ContainerTest < ActiveSupport::TestCase
7 command: ['echo', 'foo'],
8 container_image: 'fa3c1a9cb6783f85f2ecda037e07b8c3+167',
11 runtime_constraints: {"vcpus" => 1, "ram" => 1},
14 REUSABLE_COMMON_ATTRS = {
15 container_image: "9ae44d5792468c58bcf85ce7353c7027+124",
17 command: ["echo", "hello"],
19 runtime_constraints: {
24 "test" => {"kind" => "json"},
31 def minimal_new attrs={}
32 cr = ContainerRequest.new DEFAULT_ATTRS.merge(attrs)
33 cr.state = ContainerRequest::Committed
34 act_as_user users(:active) do
37 c = Container.find_by_uuid cr.container_uuid
42 def check_illegal_updates c, bad_updates
43 bad_updates.each do |u|
44 refute c.update_attributes(u), u.inspect
45 refute c.valid?, u.inspect
50 def check_illegal_modify c
51 check_illegal_updates c, [{command: ["echo", "bar"]},
52 {container_image: "arvados/apitestfixture:june10"},
54 {environment: {"FOO" => "BAR"}},
55 {mounts: {"FOO" => "BAR"}},
56 {output_path: "/tmp3"},
57 {locked_by_uuid: "zzzzz-gj3su-027z32aux8dg2s1"},
58 {auth_uuid: "zzzzz-gj3su-017z32aux8dg2s1"},
59 {runtime_constraints: {"FOO" => "BAR"}}]
62 def check_bogus_states c
63 check_illegal_updates c, [{state: nil},
67 def check_no_change_from_cancelled c
68 check_illegal_modify c
70 check_illegal_updates c, [{ priority: 3 },
71 { state: Container::Queued },
72 { state: Container::Locked },
73 { state: Container::Running },
74 { state: Container::Complete }]
77 test "Container create" do
79 c, _ = minimal_new(environment: {},
80 mounts: {"BAR" => "FOO"},
83 runtime_constraints: {"vcpus" => 1, "ram" => 1})
85 check_illegal_modify c
94 test "Container serialized hash attributes sorted before save" do
95 env = {"C" => 3, "B" => 2, "A" => 1}
96 m = {"F" => {"kind" => 3}, "E" => {"kind" => 2}, "D" => {"kind" => 1}}
97 rc = {"vcpus" => 1, "ram" => 1, "keep_cache_ram" => 1}
98 c, _ = minimal_new(environment: env, mounts: m, runtime_constraints: rc)
99 assert_equal c.environment.to_json, Container.deep_sort_hash(env).to_json
100 assert_equal c.mounts.to_json, Container.deep_sort_hash(m).to_json
101 assert_equal c.runtime_constraints.to_json, Container.deep_sort_hash(rc).to_json
104 test 'deep_sort_hash on array of hashes' do
105 a = {'z' => [[{'a' => 'a', 'b' => 'b'}]]}
106 b = {'z' => [[{'b' => 'b', 'a' => 'a'}]]}
107 assert_equal Container.deep_sort_hash(a).to_json, Container.deep_sort_hash(b).to_json
110 test "find_reusable method should select higher priority queued container" do
111 set_user_from_auth :active
112 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment:{"var" => "queued"}})
113 c_low_priority, _ = minimal_new(common_attrs.merge({use_existing:false, priority:1}))
114 c_high_priority, _ = minimal_new(common_attrs.merge({use_existing:false, priority:2}))
115 assert_not_equal c_low_priority.uuid, c_high_priority.uuid
116 assert_equal Container::Queued, c_low_priority.state
117 assert_equal Container::Queued, c_high_priority.state
118 reused = Container.find_reusable(common_attrs)
119 assert_not_nil reused
120 assert_equal reused.uuid, c_high_priority.uuid
123 test "find_reusable method should select latest completed container" do
124 set_user_from_auth :active
125 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "complete"}})
127 state: Container::Complete,
129 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
130 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
133 c_older, _ = minimal_new(common_attrs.merge({use_existing: false}))
134 c_recent, _ = minimal_new(common_attrs.merge({use_existing: false}))
135 assert_not_equal c_older.uuid, c_recent.uuid
137 set_user_from_auth :dispatch1
138 c_older.update_attributes!({state: Container::Locked})
139 c_older.update_attributes!({state: Container::Running})
140 c_older.update_attributes!(completed_attrs)
142 c_recent.update_attributes!({state: Container::Locked})
143 c_recent.update_attributes!({state: Container::Running})
144 c_recent.update_attributes!(completed_attrs)
146 reused = Container.find_reusable(common_attrs)
147 assert_not_nil reused
148 assert_equal reused.uuid, c_older.uuid
151 test "find_reusable method should select oldest completed container when inconsistent outputs exist" do
152 set_user_from_auth :active
153 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "complete"}, priority: 1})
155 state: Container::Complete,
157 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
160 cr = ContainerRequest.new common_attrs
161 cr.use_existing = false
162 cr.state = ContainerRequest::Committed
164 c_output1 = Container.where(uuid: cr.container_uuid).first
166 cr = ContainerRequest.new common_attrs
167 cr.use_existing = false
168 cr.state = ContainerRequest::Committed
170 c_output2 = Container.where(uuid: cr.container_uuid).first
172 assert_not_equal c_output1.uuid, c_output2.uuid
174 set_user_from_auth :dispatch1
176 out1 = '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
177 log1 = collections(:real_log_collection).portable_data_hash
178 c_output1.update_attributes!({state: Container::Locked})
179 c_output1.update_attributes!({state: Container::Running})
180 c_output1.update_attributes!(completed_attrs.merge({log: log1, output: out1}))
182 out2 = 'fa7aeb5140e2848d39b416daeef4ffc5+45'
183 c_output2.update_attributes!({state: Container::Locked})
184 c_output2.update_attributes!({state: Container::Running})
185 c_output2.update_attributes!(completed_attrs.merge({log: log1, output: out2}))
187 reused = Container.resolve(ContainerRequest.new(common_attrs))
188 assert_equal c_output1.uuid, reused.uuid
191 test "find_reusable method should select running container by start date" do
192 set_user_from_auth :active
193 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running"}})
194 c_slower, _ = minimal_new(common_attrs.merge({use_existing: false}))
195 c_faster_started_first, _ = minimal_new(common_attrs.merge({use_existing: false}))
196 c_faster_started_second, _ = minimal_new(common_attrs.merge({use_existing: false}))
197 # Confirm the 3 container UUIDs are different.
198 assert_equal 3, [c_slower.uuid, c_faster_started_first.uuid, c_faster_started_second.uuid].uniq.length
199 set_user_from_auth :dispatch1
200 c_slower.update_attributes!({state: Container::Locked})
201 c_slower.update_attributes!({state: Container::Running,
203 c_faster_started_first.update_attributes!({state: Container::Locked})
204 c_faster_started_first.update_attributes!({state: Container::Running,
206 c_faster_started_second.update_attributes!({state: Container::Locked})
207 c_faster_started_second.update_attributes!({state: Container::Running,
209 reused = Container.find_reusable(common_attrs)
210 assert_not_nil reused
211 # Selected container is the one that started first
212 assert_equal reused.uuid, c_faster_started_first.uuid
215 test "find_reusable method should select running container by progress" do
216 set_user_from_auth :active
217 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running2"}})
218 c_slower, _ = minimal_new(common_attrs.merge({use_existing: false}))
219 c_faster_started_first, _ = minimal_new(common_attrs.merge({use_existing: false}))
220 c_faster_started_second, _ = minimal_new(common_attrs.merge({use_existing: false}))
221 # Confirm the 3 container UUIDs are different.
222 assert_equal 3, [c_slower.uuid, c_faster_started_first.uuid, c_faster_started_second.uuid].uniq.length
223 set_user_from_auth :dispatch1
224 c_slower.update_attributes!({state: Container::Locked})
225 c_slower.update_attributes!({state: Container::Running,
227 c_faster_started_first.update_attributes!({state: Container::Locked})
228 c_faster_started_first.update_attributes!({state: Container::Running,
230 c_faster_started_second.update_attributes!({state: Container::Locked})
231 c_faster_started_second.update_attributes!({state: Container::Running,
233 reused = Container.find_reusable(common_attrs)
234 assert_not_nil reused
235 # Selected container is the one with most progress done
236 assert_equal reused.uuid, c_faster_started_second.uuid
239 test "find_reusable method should select locked container most likely to start sooner" do
240 set_user_from_auth :active
241 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "locked"}})
242 c_low_priority, _ = minimal_new(common_attrs.merge({use_existing: false}))
243 c_high_priority_older, _ = minimal_new(common_attrs.merge({use_existing: false}))
244 c_high_priority_newer, _ = minimal_new(common_attrs.merge({use_existing: false}))
245 # Confirm the 3 container UUIDs are different.
246 assert_equal 3, [c_low_priority.uuid, c_high_priority_older.uuid, c_high_priority_newer.uuid].uniq.length
247 set_user_from_auth :dispatch1
248 c_low_priority.update_attributes!({state: Container::Locked,
250 c_high_priority_older.update_attributes!({state: Container::Locked,
252 c_high_priority_newer.update_attributes!({state: Container::Locked,
254 reused = Container.find_reusable(common_attrs)
255 assert_not_nil reused
256 assert_equal reused.uuid, c_high_priority_older.uuid
259 test "find_reusable method should select running over failed container" do
260 set_user_from_auth :active
261 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "failed_vs_running"}})
262 c_failed, _ = minimal_new(common_attrs.merge({use_existing: false}))
263 c_running, _ = minimal_new(common_attrs.merge({use_existing: false}))
264 assert_not_equal c_failed.uuid, c_running.uuid
265 set_user_from_auth :dispatch1
266 c_failed.update_attributes!({state: Container::Locked})
267 c_failed.update_attributes!({state: Container::Running})
268 c_failed.update_attributes!({state: Container::Complete,
270 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
271 output: 'ea10d51bcf88862dbcc36eb292017dfd+45'})
272 c_running.update_attributes!({state: Container::Locked})
273 c_running.update_attributes!({state: Container::Running,
275 reused = Container.find_reusable(common_attrs)
276 assert_not_nil reused
277 assert_equal reused.uuid, c_running.uuid
280 test "find_reusable method should select complete over running container" do
281 set_user_from_auth :active
282 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "completed_vs_running"}})
283 c_completed, _ = minimal_new(common_attrs.merge({use_existing: false}))
284 c_running, _ = minimal_new(common_attrs.merge({use_existing: false}))
285 assert_not_equal c_completed.uuid, c_running.uuid
286 set_user_from_auth :dispatch1
287 c_completed.update_attributes!({state: Container::Locked})
288 c_completed.update_attributes!({state: Container::Running})
289 c_completed.update_attributes!({state: Container::Complete,
291 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
292 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'})
293 c_running.update_attributes!({state: Container::Locked})
294 c_running.update_attributes!({state: Container::Running,
296 reused = Container.find_reusable(common_attrs)
297 assert_not_nil reused
298 assert_equal c_completed.uuid, reused.uuid
301 test "find_reusable method should select running over locked container" do
302 set_user_from_auth :active
303 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running_vs_locked"}})
304 c_locked, _ = minimal_new(common_attrs.merge({use_existing: false}))
305 c_running, _ = minimal_new(common_attrs.merge({use_existing: false}))
306 assert_not_equal c_running.uuid, c_locked.uuid
307 set_user_from_auth :dispatch1
308 c_locked.update_attributes!({state: Container::Locked})
309 c_running.update_attributes!({state: Container::Locked})
310 c_running.update_attributes!({state: Container::Running,
312 reused = Container.find_reusable(common_attrs)
313 assert_not_nil reused
314 assert_equal reused.uuid, c_running.uuid
317 test "find_reusable method should select locked over queued container" do
318 set_user_from_auth :active
319 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running_vs_locked"}})
320 c_locked, _ = minimal_new(common_attrs.merge({use_existing: false}))
321 c_queued, _ = minimal_new(common_attrs.merge({use_existing: false}))
322 assert_not_equal c_queued.uuid, c_locked.uuid
323 set_user_from_auth :dispatch1
324 c_locked.update_attributes!({state: Container::Locked})
325 reused = Container.find_reusable(common_attrs)
326 assert_not_nil reused
327 assert_equal reused.uuid, c_locked.uuid
330 test "find_reusable method should not select failed container" do
331 set_user_from_auth :active
332 attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "failed"}})
333 c, _ = minimal_new(attrs)
334 set_user_from_auth :dispatch1
335 c.update_attributes!({state: Container::Locked})
336 c.update_attributes!({state: Container::Running})
337 c.update_attributes!({state: Container::Complete,
339 reused = Container.find_reusable(attrs)
343 test "Container running" do
344 c, _ = minimal_new priority: 1
346 set_user_from_auth :dispatch1
347 check_illegal_updates c, [{state: Container::Running},
348 {state: Container::Complete}]
351 c.update_attributes! state: Container::Running
353 check_illegal_modify c
356 check_illegal_updates c, [{state: Container::Queued}]
359 c.update_attributes! priority: 3
362 test "Lock and unlock" do
363 c, cr = minimal_new priority: 0
365 set_user_from_auth :dispatch1
366 assert_equal Container::Queued, c.state
368 assert_raise(ActiveRecord::RecordInvalid) {c.lock} # "no priority"
370 assert cr.update_attributes priority: 1
372 refute c.update_attributes(state: Container::Running), "not locked"
374 refute c.update_attributes(state: Container::Complete), "not locked"
377 assert c.lock, show_errors(c)
378 assert c.locked_by_uuid
381 assert_raise(ArvadosModel::AlreadyLockedError) {c.lock}
384 assert c.unlock, show_errors(c)
385 refute c.locked_by_uuid
388 refute c.update_attributes(state: Container::Running), "not locked"
390 refute c.locked_by_uuid
393 assert c.lock, show_errors(c)
394 assert c.update_attributes(state: Container::Running), show_errors(c)
395 assert c.locked_by_uuid
398 auth_uuid_was = c.auth_uuid
400 assert_raise(ActiveRecord::RecordInvalid) {c.lock} # Running to Locked is not allowed
402 assert_raise(ActiveRecord::RecordInvalid) {c.unlock} # Running to Queued is not allowed
405 assert c.update_attributes(state: Container::Complete), show_errors(c)
406 refute c.locked_by_uuid
409 auth_exp = ApiClientAuthorization.find_by_uuid(auth_uuid_was).expires_at
410 assert_operator auth_exp, :<, db_current_time
413 test "Container queued cancel" do
415 set_user_from_auth :dispatch1
416 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
417 check_no_change_from_cancelled c
420 test "Container locked cancel" do
422 set_user_from_auth :dispatch1
423 assert c.lock, show_errors(c)
424 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
425 check_no_change_from_cancelled c
428 test "Container running cancel" do
430 set_user_from_auth :dispatch1
432 c.update_attributes! state: Container::Running
433 c.update_attributes! state: Container::Cancelled
434 check_no_change_from_cancelled c
437 test "Container create forbidden for non-admin" do
438 set_user_from_auth :active_trustedclient
439 c = Container.new DEFAULT_ATTRS
441 c.mounts = {"BAR" => "FOO"}
442 c.output_path = "/tmp"
444 c.runtime_constraints = {}
445 assert_raises(ArvadosModel::PermissionDeniedError) do
450 test "Container only set exit code on complete" do
452 set_user_from_auth :dispatch1
454 c.update_attributes! state: Container::Running
456 check_illegal_updates c, [{exit_code: 1},
457 {exit_code: 1, state: Container::Cancelled}]
459 assert c.update_attributes(exit_code: 1, state: Container::Complete)
462 test "locked_by_uuid can set output on running container" do
464 set_user_from_auth :dispatch1
466 c.update_attributes! state: Container::Running
468 assert_equal c.locked_by_uuid, Thread.current[:api_client_authorization].uuid
470 assert c.update_attributes output: collections(:collection_owned_by_active).portable_data_hash
471 assert c.update_attributes! state: Container::Complete
474 test "auth_uuid can set output on running container, but not change container state" do
476 set_user_from_auth :dispatch1
478 c.update_attributes! state: Container::Running
480 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
481 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
482 assert c.update_attributes output: collections(:collection_owned_by_active).portable_data_hash
484 assert_raises ArvadosModel::PermissionDeniedError do
485 # auth_uuid cannot set container state
486 c.update_attributes state: Container::Complete
490 test "not allowed to set output that is not readable by current user" do
492 set_user_from_auth :dispatch1
494 c.update_attributes! state: Container::Running
496 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
497 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
499 assert_raises ActiveRecord::RecordInvalid do
500 c.update_attributes! output: collections(:collection_not_readable_by_active).portable_data_hash
504 test "other token cannot set output on running container" do
506 set_user_from_auth :dispatch1
508 c.update_attributes! state: Container::Running
510 set_user_from_auth :not_running_container_auth
511 assert_raises ArvadosModel::PermissionDeniedError do
512 c.update_attributes! output: collections(:foo_file).portable_data_hash
516 test "can set trashed output on running container" do
518 set_user_from_auth :dispatch1
520 c.update_attributes! state: Container::Running
522 output = Collection.unscoped.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3jk')
524 assert output.is_trashed
525 assert c.update_attributes output: output.portable_data_hash
526 assert c.update_attributes! state: Container::Complete
529 test "not allowed to set trashed output that is not readable by current user" do
531 set_user_from_auth :dispatch1
533 c.update_attributes! state: Container::Running
535 output = Collection.unscoped.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3jr')
537 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
538 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
540 assert_raises ActiveRecord::RecordInvalid do
541 c.update_attributes! output: output.portable_data_hash