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 "find_reusable with logging disabled" do
344 set_user_from_auth :active
345 Rails.logger.expects(:info).never
346 Container.find_reusable(REUSABLE_COMMON_ATTRS)
349 test "find_reusable with logging enabled" do
350 set_user_from_auth :active
351 Rails.configuration.log_reuse_decisions = true
352 Rails.logger.expects(:info).at_least(3)
353 Container.find_reusable(REUSABLE_COMMON_ATTRS)
356 test "Container running" do
357 c, _ = minimal_new priority: 1
359 set_user_from_auth :dispatch1
360 check_illegal_updates c, [{state: Container::Running},
361 {state: Container::Complete}]
364 c.update_attributes! state: Container::Running
366 check_illegal_modify c
369 check_illegal_updates c, [{state: Container::Queued}]
372 c.update_attributes! priority: 3
375 test "Lock and unlock" do
376 c, cr = minimal_new priority: 0
378 set_user_from_auth :dispatch1
379 assert_equal Container::Queued, c.state
381 assert_raise(ArvadosModel::LockFailedError) do
386 assert cr.update_attributes priority: 1
388 refute c.update_attributes(state: Container::Running), "not locked"
390 refute c.update_attributes(state: Container::Complete), "not locked"
393 assert c.lock, show_errors(c)
394 assert c.locked_by_uuid
397 assert_raise(ArvadosModel::LockFailedError) {c.lock}
400 assert c.unlock, show_errors(c)
401 refute c.locked_by_uuid
404 refute c.update_attributes(state: Container::Running), "not locked"
406 refute c.locked_by_uuid
409 assert c.lock, show_errors(c)
410 assert c.update_attributes(state: Container::Running), show_errors(c)
411 assert c.locked_by_uuid
414 auth_uuid_was = c.auth_uuid
416 assert_raise(ArvadosModel::LockFailedError) do
417 # Running to Locked is not allowed
421 assert_raise(ArvadosModel::InvalidStateTransitionError) do
422 # Running to Queued is not allowed
427 assert c.update_attributes(state: Container::Complete), show_errors(c)
428 refute c.locked_by_uuid
431 auth_exp = ApiClientAuthorization.find_by_uuid(auth_uuid_was).expires_at
432 assert_operator auth_exp, :<, db_current_time
435 test "Container queued cancel" do
437 set_user_from_auth :dispatch1
438 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
439 check_no_change_from_cancelled c
442 test "Container locked cancel" do
444 set_user_from_auth :dispatch1
445 assert c.lock, show_errors(c)
446 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
447 check_no_change_from_cancelled c
450 test "Container running cancel" do
452 set_user_from_auth :dispatch1
454 c.update_attributes! state: Container::Running
455 c.update_attributes! state: Container::Cancelled
456 check_no_change_from_cancelled c
459 test "Container create forbidden for non-admin" do
460 set_user_from_auth :active_trustedclient
461 c = Container.new DEFAULT_ATTRS
463 c.mounts = {"BAR" => "FOO"}
464 c.output_path = "/tmp"
466 c.runtime_constraints = {}
467 assert_raises(ArvadosModel::PermissionDeniedError) do
472 test "Container only set exit code on complete" do
474 set_user_from_auth :dispatch1
476 c.update_attributes! state: Container::Running
478 check_illegal_updates c, [{exit_code: 1},
479 {exit_code: 1, state: Container::Cancelled}]
481 assert c.update_attributes(exit_code: 1, state: Container::Complete)
484 test "locked_by_uuid can set output on running container" do
486 set_user_from_auth :dispatch1
488 c.update_attributes! state: Container::Running
490 assert_equal c.locked_by_uuid, Thread.current[:api_client_authorization].uuid
492 assert c.update_attributes output: collections(:collection_owned_by_active).portable_data_hash
493 assert c.update_attributes! state: Container::Complete
496 test "auth_uuid can set output on running container, but not change container state" do
498 set_user_from_auth :dispatch1
500 c.update_attributes! state: Container::Running
502 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
503 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
504 assert c.update_attributes output: collections(:collection_owned_by_active).portable_data_hash
506 assert_raises ArvadosModel::PermissionDeniedError do
507 # auth_uuid cannot set container state
508 c.update_attributes state: Container::Complete
512 test "not allowed to set output that is not readable by current user" do
514 set_user_from_auth :dispatch1
516 c.update_attributes! state: Container::Running
518 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
519 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
521 assert_raises ActiveRecord::RecordInvalid do
522 c.update_attributes! output: collections(:collection_not_readable_by_active).portable_data_hash
526 test "other token cannot set output on running container" do
528 set_user_from_auth :dispatch1
530 c.update_attributes! state: Container::Running
532 set_user_from_auth :not_running_container_auth
533 assert_raises ArvadosModel::PermissionDeniedError do
534 c.update_attributes! output: collections(:foo_file).portable_data_hash
538 test "can set trashed output on running container" do
540 set_user_from_auth :dispatch1
542 c.update_attributes! state: Container::Running
544 output = Collection.unscoped.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3jk')
546 assert output.is_trashed
547 assert c.update_attributes output: output.portable_data_hash
548 assert c.update_attributes! state: Container::Complete
551 test "not allowed to set trashed output that is not readable by current user" do
553 set_user_from_auth :dispatch1
555 c.update_attributes! state: Container::Running
557 output = Collection.unscoped.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3jr')
559 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
560 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
562 assert_raises ActiveRecord::RecordInvalid do
563 c.update_attributes! output: output.portable_data_hash