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 = {container_image: "9ae44d5792468c58bcf85ce7353c7027+124",
16 command: ["echo", "hello"],
18 runtime_constraints: {"vcpus" => 4,
19 "ram" => 12000000000},
20 mounts: {"test" => {"kind" => "json"}},
21 environment: {"var" => 'val'}}
23 def minimal_new attrs={}
24 cr = ContainerRequest.new DEFAULT_ATTRS.merge(attrs)
25 cr.state = ContainerRequest::Committed
26 act_as_user users(:active) do
29 c = Container.find_by_uuid cr.container_uuid
34 def check_illegal_updates c, bad_updates
35 bad_updates.each do |u|
36 refute c.update_attributes(u), u.inspect
37 refute c.valid?, u.inspect
42 def check_illegal_modify c
43 check_illegal_updates c, [{command: ["echo", "bar"]},
44 {container_image: "arvados/apitestfixture:june10"},
46 {environment: {"FOO" => "BAR"}},
47 {mounts: {"FOO" => "BAR"}},
48 {output_path: "/tmp3"},
49 {locked_by_uuid: "zzzzz-gj3su-027z32aux8dg2s1"},
50 {auth_uuid: "zzzzz-gj3su-017z32aux8dg2s1"},
51 {runtime_constraints: {"FOO" => "BAR"}}]
54 def check_bogus_states c
55 check_illegal_updates c, [{state: nil},
59 def check_no_change_from_cancelled c
60 check_illegal_modify c
62 check_illegal_updates c, [{ priority: 3 },
63 { state: Container::Queued },
64 { state: Container::Locked },
65 { state: Container::Running },
66 { state: Container::Complete }]
69 test "Container create" do
71 c, _ = minimal_new(environment: {},
72 mounts: {"BAR" => "FOO"},
75 runtime_constraints: {"vcpus" => 1, "ram" => 1})
77 check_illegal_modify c
86 test "Container serialized hash attributes sorted before save" do
87 env = {"C" => 3, "B" => 2, "A" => 1}
88 m = {"F" => {"kind" => 3}, "E" => {"kind" => 2}, "D" => {"kind" => 1}}
89 rc = {"vcpus" => 1, "ram" => 1}
90 c, _ = minimal_new(environment: env, mounts: m, runtime_constraints: rc)
91 assert_equal c.environment.to_json, Container.deep_sort_hash(env).to_json
92 assert_equal c.mounts.to_json, Container.deep_sort_hash(m).to_json
93 assert_equal c.runtime_constraints.to_json, Container.deep_sort_hash(rc).to_json
96 test 'deep_sort_hash on array of hashes' do
97 a = {'z' => [[{'a' => 'a', 'b' => 'b'}]]}
98 b = {'z' => [[{'b' => 'b', 'a' => 'a'}]]}
99 assert_equal Container.deep_sort_hash(a).to_json, Container.deep_sort_hash(b).to_json
102 test "find_reusable method should select higher priority queued container" do
103 set_user_from_auth :active
104 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment:{"var" => "queued"}})
105 c_low_priority, _ = minimal_new(common_attrs.merge({use_existing:false, priority:1}))
106 c_high_priority, _ = minimal_new(common_attrs.merge({use_existing:false, priority:2}))
107 assert_not_equal c_low_priority.uuid, c_high_priority.uuid
108 assert_equal Container::Queued, c_low_priority.state
109 assert_equal Container::Queued, c_high_priority.state
110 reused = Container.find_reusable(common_attrs)
111 assert_not_nil reused
112 assert_equal reused.uuid, c_high_priority.uuid
115 test "find_reusable method should select latest completed container" do
116 set_user_from_auth :active
117 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "complete"}})
119 state: Container::Complete,
121 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
122 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
125 c_older, _ = minimal_new(common_attrs.merge({use_existing: false}))
126 c_recent, _ = minimal_new(common_attrs.merge({use_existing: false}))
127 assert_not_equal c_older.uuid, c_recent.uuid
129 set_user_from_auth :dispatch1
130 c_older.update_attributes!({state: Container::Locked})
131 c_older.update_attributes!({state: Container::Running})
132 c_older.update_attributes!(completed_attrs)
134 c_recent.update_attributes!({state: Container::Locked})
135 c_recent.update_attributes!({state: Container::Running})
136 c_recent.update_attributes!(completed_attrs)
138 reused = Container.find_reusable(common_attrs)
139 assert_not_nil reused
140 assert_equal reused.uuid, c_older.uuid
143 test "find_reusable method should not select completed container when inconsistent outputs exist" do
144 set_user_from_auth :active
145 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "complete"}, priority: 1})
147 state: Container::Complete,
149 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
152 set_user_from_auth :dispatch1
154 c_output1 = Container.create common_attrs
155 c_output2 = Container.create common_attrs
156 assert_not_equal c_output1.uuid, c_output2.uuid
158 cr = ContainerRequest.new common_attrs
159 cr.state = ContainerRequest::Committed
160 cr.container_uuid = c_output1.uuid
163 cr = ContainerRequest.new common_attrs
164 cr.state = ContainerRequest::Committed
165 cr.container_uuid = c_output2.uuid
168 c_output1.update_attributes!({state: Container::Locked})
169 c_output1.update_attributes!({state: Container::Running})
170 c_output1.update_attributes!(completed_attrs.merge({output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'}))
172 c_output2.update_attributes!({state: Container::Locked})
173 c_output2.update_attributes!({state: Container::Running})
174 c_output2.update_attributes!(completed_attrs.merge({output: 'fa7aeb5140e2848d39b416daeef4ffc5+45'}))
176 reused = Container.find_reusable(common_attrs)
180 test "find_reusable method should select running container by start date" do
181 set_user_from_auth :active
182 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running"}})
183 c_slower, _ = minimal_new(common_attrs.merge({use_existing: false}))
184 c_faster_started_first, _ = minimal_new(common_attrs.merge({use_existing: false}))
185 c_faster_started_second, _ = minimal_new(common_attrs.merge({use_existing: false}))
186 # Confirm the 3 container UUIDs are different.
187 assert_equal 3, [c_slower.uuid, c_faster_started_first.uuid, c_faster_started_second.uuid].uniq.length
188 set_user_from_auth :dispatch1
189 c_slower.update_attributes!({state: Container::Locked})
190 c_slower.update_attributes!({state: Container::Running,
192 c_faster_started_first.update_attributes!({state: Container::Locked})
193 c_faster_started_first.update_attributes!({state: Container::Running,
195 c_faster_started_second.update_attributes!({state: Container::Locked})
196 c_faster_started_second.update_attributes!({state: Container::Running,
198 reused = Container.find_reusable(common_attrs)
199 assert_not_nil reused
200 # Selected container is the one that started first
201 assert_equal reused.uuid, c_faster_started_first.uuid
204 test "find_reusable method should select running container by progress" do
205 set_user_from_auth :active
206 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running2"}})
207 c_slower, _ = minimal_new(common_attrs.merge({use_existing: false}))
208 c_faster_started_first, _ = minimal_new(common_attrs.merge({use_existing: false}))
209 c_faster_started_second, _ = minimal_new(common_attrs.merge({use_existing: false}))
210 # Confirm the 3 container UUIDs are different.
211 assert_equal 3, [c_slower.uuid, c_faster_started_first.uuid, c_faster_started_second.uuid].uniq.length
212 set_user_from_auth :dispatch1
213 c_slower.update_attributes!({state: Container::Locked})
214 c_slower.update_attributes!({state: Container::Running,
216 c_faster_started_first.update_attributes!({state: Container::Locked})
217 c_faster_started_first.update_attributes!({state: Container::Running,
219 c_faster_started_second.update_attributes!({state: Container::Locked})
220 c_faster_started_second.update_attributes!({state: Container::Running,
222 reused = Container.find_reusable(common_attrs)
223 assert_not_nil reused
224 # Selected container is the one with most progress done
225 assert_equal reused.uuid, c_faster_started_second.uuid
228 test "find_reusable method should select locked container most likely to start sooner" do
229 set_user_from_auth :active
230 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "locked"}})
231 c_low_priority, _ = minimal_new(common_attrs.merge({use_existing: false}))
232 c_high_priority_older, _ = minimal_new(common_attrs.merge({use_existing: false}))
233 c_high_priority_newer, _ = minimal_new(common_attrs.merge({use_existing: false}))
234 # Confirm the 3 container UUIDs are different.
235 assert_equal 3, [c_low_priority.uuid, c_high_priority_older.uuid, c_high_priority_newer.uuid].uniq.length
236 set_user_from_auth :dispatch1
237 c_low_priority.update_attributes!({state: Container::Locked,
239 c_high_priority_older.update_attributes!({state: Container::Locked,
241 c_high_priority_newer.update_attributes!({state: Container::Locked,
243 reused = Container.find_reusable(common_attrs)
244 assert_not_nil reused
245 assert_equal reused.uuid, c_high_priority_older.uuid
248 test "find_reusable method should select running over failed container" do
249 set_user_from_auth :active
250 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "failed_vs_running"}})
251 c_failed, _ = minimal_new(common_attrs.merge({use_existing: false}))
252 c_running, _ = minimal_new(common_attrs.merge({use_existing: false}))
253 assert_not_equal c_failed.uuid, c_running.uuid
254 set_user_from_auth :dispatch1
255 c_failed.update_attributes!({state: Container::Locked})
256 c_failed.update_attributes!({state: Container::Running})
257 c_failed.update_attributes!({state: Container::Complete,
259 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
260 output: 'ea10d51bcf88862dbcc36eb292017dfd+45'})
261 c_running.update_attributes!({state: Container::Locked})
262 c_running.update_attributes!({state: Container::Running,
264 reused = Container.find_reusable(common_attrs)
265 assert_not_nil reused
266 assert_equal reused.uuid, c_running.uuid
269 test "find_reusable method should select complete over running container" do
270 set_user_from_auth :active
271 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "completed_vs_running"}})
272 c_completed, _ = minimal_new(common_attrs.merge({use_existing: false}))
273 c_running, _ = minimal_new(common_attrs.merge({use_existing: false}))
274 assert_not_equal c_completed.uuid, c_running.uuid
275 set_user_from_auth :dispatch1
276 c_completed.update_attributes!({state: Container::Locked})
277 c_completed.update_attributes!({state: Container::Running})
278 c_completed.update_attributes!({state: Container::Complete,
280 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
281 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'})
282 c_running.update_attributes!({state: Container::Locked})
283 c_running.update_attributes!({state: Container::Running,
285 reused = Container.find_reusable(common_attrs)
286 assert_not_nil reused
287 assert_equal c_completed.uuid, reused.uuid
290 test "find_reusable method should select running over locked container" do
291 set_user_from_auth :active
292 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running_vs_locked"}})
293 c_locked, _ = minimal_new(common_attrs.merge({use_existing: false}))
294 c_running, _ = minimal_new(common_attrs.merge({use_existing: false}))
295 assert_not_equal c_running.uuid, c_locked.uuid
296 set_user_from_auth :dispatch1
297 c_locked.update_attributes!({state: Container::Locked})
298 c_running.update_attributes!({state: Container::Locked})
299 c_running.update_attributes!({state: Container::Running,
301 reused = Container.find_reusable(common_attrs)
302 assert_not_nil reused
303 assert_equal reused.uuid, c_running.uuid
306 test "find_reusable method should select locked over queued container" do
307 set_user_from_auth :active
308 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running_vs_locked"}})
309 c_locked, _ = minimal_new(common_attrs.merge({use_existing: false}))
310 c_queued, _ = minimal_new(common_attrs.merge({use_existing: false}))
311 assert_not_equal c_queued.uuid, c_locked.uuid
312 set_user_from_auth :dispatch1
313 c_locked.update_attributes!({state: Container::Locked})
314 reused = Container.find_reusable(common_attrs)
315 assert_not_nil reused
316 assert_equal reused.uuid, c_locked.uuid
319 test "find_reusable method should not select failed container" do
320 set_user_from_auth :active
321 attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "failed"}})
322 c, _ = minimal_new(attrs)
323 set_user_from_auth :dispatch1
324 c.update_attributes!({state: Container::Locked})
325 c.update_attributes!({state: Container::Running})
326 c.update_attributes!({state: Container::Complete,
328 reused = Container.find_reusable(attrs)
332 test "Container running" do
333 c, _ = minimal_new priority: 1
335 set_user_from_auth :dispatch1
336 check_illegal_updates c, [{state: Container::Running},
337 {state: Container::Complete}]
340 c.update_attributes! state: Container::Running
342 check_illegal_modify c
345 check_illegal_updates c, [{state: Container::Queued}]
348 c.update_attributes! priority: 3
351 test "Lock and unlock" do
352 c, cr = minimal_new priority: 0
354 set_user_from_auth :dispatch1
355 assert_equal Container::Queued, c.state
357 assert_raise(ActiveRecord::RecordInvalid) {c.lock} # "no priority"
359 assert cr.update_attributes priority: 1
361 refute c.update_attributes(state: Container::Running), "not locked"
363 refute c.update_attributes(state: Container::Complete), "not locked"
366 assert c.lock, show_errors(c)
367 assert c.locked_by_uuid
370 assert_raise(ArvadosModel::AlreadyLockedError) {c.lock}
373 assert c.unlock, show_errors(c)
374 refute c.locked_by_uuid
377 refute c.update_attributes(state: Container::Running), "not locked"
379 refute c.locked_by_uuid
382 assert c.lock, show_errors(c)
383 assert c.update_attributes(state: Container::Running), show_errors(c)
384 assert c.locked_by_uuid
387 auth_uuid_was = c.auth_uuid
389 assert_raise(ActiveRecord::RecordInvalid) {c.lock} # Running to Locked is not allowed
391 assert_raise(ActiveRecord::RecordInvalid) {c.unlock} # Running to Queued is not allowed
394 assert c.update_attributes(state: Container::Complete), show_errors(c)
395 refute c.locked_by_uuid
398 auth_exp = ApiClientAuthorization.find_by_uuid(auth_uuid_was).expires_at
399 assert_operator auth_exp, :<, db_current_time
402 test "Container queued cancel" do
404 set_user_from_auth :dispatch1
405 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
406 check_no_change_from_cancelled c
409 test "Container locked cancel" do
411 set_user_from_auth :dispatch1
412 assert c.lock, show_errors(c)
413 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
414 check_no_change_from_cancelled c
417 test "Container running cancel" do
419 set_user_from_auth :dispatch1
421 c.update_attributes! state: Container::Running
422 c.update_attributes! state: Container::Cancelled
423 check_no_change_from_cancelled c
426 test "Container create forbidden for non-admin" do
427 set_user_from_auth :active_trustedclient
428 c = Container.new DEFAULT_ATTRS
430 c.mounts = {"BAR" => "FOO"}
431 c.output_path = "/tmp"
433 c.runtime_constraints = {}
434 assert_raises(ArvadosModel::PermissionDeniedError) do
439 test "Container only set exit code on complete" do
441 set_user_from_auth :dispatch1
443 c.update_attributes! state: Container::Running
445 check_illegal_updates c, [{exit_code: 1},
446 {exit_code: 1, state: Container::Cancelled}]
448 assert c.update_attributes(exit_code: 1, state: Container::Complete)
451 test "locked_by_uuid can set output on running container" do
453 set_user_from_auth :dispatch1
455 c.update_attributes! state: Container::Running
457 assert_equal c.locked_by_uuid, Thread.current[:api_client_authorization].uuid
459 assert c.update_attributes output: collections(:collection_owned_by_active).portable_data_hash
460 assert c.update_attributes! state: Container::Complete
463 test "auth_uuid can set output on running container, but not change container state" do
465 set_user_from_auth :dispatch1
467 c.update_attributes! state: Container::Running
469 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
470 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
471 assert c.update_attributes output: collections(:collection_owned_by_active).portable_data_hash
473 assert_raises ArvadosModel::PermissionDeniedError do
474 # auth_uuid cannot set container state
475 c.update_attributes state: Container::Complete
479 test "not allowed to set output that is not readable by current user" do
481 set_user_from_auth :dispatch1
483 c.update_attributes! state: Container::Running
485 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
486 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
488 assert_raises ActiveRecord::RecordInvalid do
489 c.update_attributes! output: collections(:collection_not_readable_by_active).portable_data_hash
493 test "other token cannot set output on running container" do
495 set_user_from_auth :dispatch1
497 c.update_attributes! state: Container::Running
499 set_user_from_auth :not_running_container_auth
500 assert_raises ArvadosModel::PermissionDeniedError do
501 c.update_attributes! output: collections(:foo_file).portable_data_hash