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({priority:1}))
106 c_high_priority, _ = minimal_new(common_attrs.merge({priority:2}))
107 assert_equal Container::Queued, c_low_priority.state
108 assert_equal Container::Queued, c_high_priority.state
109 reused = Container.find_reusable(common_attrs)
110 assert_not_nil reused
111 assert_equal reused.uuid, c_high_priority.uuid
114 test "find_reusable method should select latest completed container" do
115 set_user_from_auth :active
116 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "complete"}})
118 state: Container::Complete,
120 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
121 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
124 c_older, _ = minimal_new(common_attrs)
125 c_recent, _ = minimal_new(common_attrs)
127 set_user_from_auth :dispatch1
128 c_older.update_attributes!({state: Container::Locked})
129 c_older.update_attributes!({state: Container::Running})
130 c_older.update_attributes!(completed_attrs)
132 c_recent.update_attributes!({state: Container::Locked})
133 c_recent.update_attributes!({state: Container::Running})
134 c_recent.update_attributes!(completed_attrs)
136 reused = Container.find_reusable(common_attrs)
137 assert_not_nil reused
138 assert_equal reused.uuid, c_older.uuid
141 test "find_reusable method should not select completed container when inconsistent outputs exist" do
142 set_user_from_auth :active
143 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "complete"}, priority: 1})
145 state: Container::Complete,
147 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
150 set_user_from_auth :dispatch1
152 c_output1 = Container.create common_attrs
153 c_output2 = Container.create common_attrs
155 cr = ContainerRequest.new common_attrs
156 cr.state = ContainerRequest::Committed
157 cr.container_uuid = c_output1.uuid
160 cr = ContainerRequest.new common_attrs
161 cr.state = ContainerRequest::Committed
162 cr.container_uuid = c_output2.uuid
165 c_output1.update_attributes!({state: Container::Locked})
166 c_output1.update_attributes!({state: Container::Running})
167 c_output1.update_attributes!(completed_attrs.merge({output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'}))
169 c_output2.update_attributes!({state: Container::Locked})
170 c_output2.update_attributes!({state: Container::Running})
171 c_output2.update_attributes!(completed_attrs.merge({output: 'fa7aeb5140e2848d39b416daeef4ffc5+45'}))
173 reused = Container.find_reusable(common_attrs)
177 test "find_reusable method should select running container by start date" do
178 set_user_from_auth :active
179 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running"}})
180 c_slower, _ = minimal_new(common_attrs)
181 c_faster_started_first, _ = minimal_new(common_attrs)
182 c_faster_started_second, _ = minimal_new(common_attrs)
183 set_user_from_auth :dispatch1
184 c_slower.update_attributes!({state: Container::Locked})
185 c_slower.update_attributes!({state: Container::Running,
187 c_faster_started_first.update_attributes!({state: Container::Locked})
188 c_faster_started_first.update_attributes!({state: Container::Running,
190 c_faster_started_second.update_attributes!({state: Container::Locked})
191 c_faster_started_second.update_attributes!({state: Container::Running,
193 reused = Container.find_reusable(common_attrs)
194 assert_not_nil reused
195 # Selected container is the one that started first
196 assert_equal reused.uuid, c_faster_started_first.uuid
199 test "find_reusable method should select running container by progress" do
200 set_user_from_auth :active
201 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running2"}})
202 c_slower, _ = minimal_new(common_attrs)
203 c_faster_started_first, _ = minimal_new(common_attrs)
204 c_faster_started_second, _ = minimal_new(common_attrs)
205 set_user_from_auth :dispatch1
206 c_slower.update_attributes!({state: Container::Locked})
207 c_slower.update_attributes!({state: Container::Running,
209 c_faster_started_first.update_attributes!({state: Container::Locked})
210 c_faster_started_first.update_attributes!({state: Container::Running,
212 c_faster_started_second.update_attributes!({state: Container::Locked})
213 c_faster_started_second.update_attributes!({state: Container::Running,
215 reused = Container.find_reusable(common_attrs)
216 assert_not_nil reused
217 # Selected container is the one with most progress done
218 assert_equal reused.uuid, c_faster_started_second.uuid
221 test "find_reusable method should select locked container most likely to start sooner" do
222 set_user_from_auth :active
223 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "locked"}})
224 c_low_priority, _ = minimal_new(common_attrs)
225 c_high_priority_older, _ = minimal_new(common_attrs)
226 c_high_priority_newer, _ = minimal_new(common_attrs)
227 set_user_from_auth :dispatch1
228 c_low_priority.update_attributes!({state: Container::Locked,
230 c_high_priority_older.update_attributes!({state: Container::Locked,
232 c_high_priority_newer.update_attributes!({state: Container::Locked,
234 reused = Container.find_reusable(common_attrs)
235 assert_not_nil reused
236 assert_equal reused.uuid, c_high_priority_older.uuid
239 test "find_reusable method should select running over failed container" do
240 set_user_from_auth :active
241 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "failed_vs_running"}})
242 c_failed, _ = minimal_new(common_attrs)
243 c_running, _ = minimal_new(common_attrs)
244 set_user_from_auth :dispatch1
245 c_failed.update_attributes!({state: Container::Locked})
246 c_failed.update_attributes!({state: Container::Running})
247 c_failed.update_attributes!({state: Container::Complete,
249 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
250 output: 'ea10d51bcf88862dbcc36eb292017dfd+45'})
251 c_running.update_attributes!({state: Container::Locked})
252 c_running.update_attributes!({state: Container::Running,
254 reused = Container.find_reusable(common_attrs)
255 assert_not_nil reused
256 assert_equal reused.uuid, c_running.uuid
259 test "find_reusable method should select complete over running container" do
260 set_user_from_auth :active
261 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "completed_vs_running"}})
262 c_completed, _ = minimal_new(common_attrs)
263 c_running, _ = minimal_new(common_attrs)
264 set_user_from_auth :dispatch1
265 c_completed.update_attributes!({state: Container::Locked})
266 c_completed.update_attributes!({state: Container::Running})
267 c_completed.update_attributes!({state: Container::Complete,
269 log: 'ea10d51bcf88862dbcc36eb292017dfd+45',
270 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'})
271 c_running.update_attributes!({state: Container::Locked})
272 c_running.update_attributes!({state: Container::Running,
274 reused = Container.find_reusable(common_attrs)
275 assert_not_nil reused
276 assert_equal c_completed.uuid, reused.uuid
279 test "find_reusable method should select running over locked container" do
280 set_user_from_auth :active
281 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running_vs_locked"}})
282 c_locked, _ = minimal_new(common_attrs)
283 c_running, _ = minimal_new(common_attrs)
284 set_user_from_auth :dispatch1
285 c_locked.update_attributes!({state: Container::Locked})
286 c_running.update_attributes!({state: Container::Locked})
287 c_running.update_attributes!({state: Container::Running,
289 reused = Container.find_reusable(common_attrs)
290 assert_not_nil reused
291 assert_equal reused.uuid, c_running.uuid
294 test "find_reusable method should select locked over queued container" do
295 set_user_from_auth :active
296 common_attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "running_vs_locked"}})
297 c_locked, _ = minimal_new(common_attrs)
298 c_queued, _ = minimal_new(common_attrs)
299 set_user_from_auth :dispatch1
300 c_locked.update_attributes!({state: Container::Locked})
301 reused = Container.find_reusable(common_attrs)
302 assert_not_nil reused
303 assert_equal reused.uuid, c_locked.uuid
306 test "find_reusable method should not select failed container" do
307 set_user_from_auth :active
308 attrs = REUSABLE_COMMON_ATTRS.merge({environment: {"var" => "failed"}})
309 c, _ = minimal_new(attrs)
310 set_user_from_auth :dispatch1
311 c.update_attributes!({state: Container::Locked})
312 c.update_attributes!({state: Container::Running})
313 c.update_attributes!({state: Container::Complete,
315 reused = Container.find_reusable(attrs)
319 test "Container running" do
320 c, _ = minimal_new priority: 1
322 set_user_from_auth :dispatch1
323 check_illegal_updates c, [{state: Container::Running},
324 {state: Container::Complete}]
327 c.update_attributes! state: Container::Running
329 check_illegal_modify c
332 check_illegal_updates c, [{state: Container::Queued}]
335 c.update_attributes! priority: 3
338 test "Lock and unlock" do
339 c, cr = minimal_new priority: 0
341 set_user_from_auth :dispatch1
342 assert_equal Container::Queued, c.state
344 assert_raise(ActiveRecord::RecordInvalid) {c.lock} # "no priority"
346 assert cr.update_attributes priority: 1
348 refute c.update_attributes(state: Container::Running), "not locked"
350 refute c.update_attributes(state: Container::Complete), "not locked"
353 assert c.lock, show_errors(c)
354 assert c.locked_by_uuid
357 assert_raise(ArvadosModel::AlreadyLockedError) {c.lock}
360 assert c.unlock, show_errors(c)
361 refute c.locked_by_uuid
364 refute c.update_attributes(state: Container::Running), "not locked"
366 refute c.locked_by_uuid
369 assert c.lock, show_errors(c)
370 assert c.update_attributes(state: Container::Running), show_errors(c)
371 assert c.locked_by_uuid
374 auth_uuid_was = c.auth_uuid
376 assert_raise(ActiveRecord::RecordInvalid) {c.lock} # Running to Locked is not allowed
378 assert_raise(ActiveRecord::RecordInvalid) {c.unlock} # Running to Queued is not allowed
381 assert c.update_attributes(state: Container::Complete), show_errors(c)
382 refute c.locked_by_uuid
385 auth_exp = ApiClientAuthorization.find_by_uuid(auth_uuid_was).expires_at
386 assert_operator auth_exp, :<, db_current_time
389 test "Container queued cancel" do
391 set_user_from_auth :dispatch1
392 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
393 check_no_change_from_cancelled c
396 test "Container locked cancel" do
398 set_user_from_auth :dispatch1
399 assert c.lock, show_errors(c)
400 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
401 check_no_change_from_cancelled c
404 test "Container running cancel" do
406 set_user_from_auth :dispatch1
408 c.update_attributes! state: Container::Running
409 c.update_attributes! state: Container::Cancelled
410 check_no_change_from_cancelled c
413 test "Container create forbidden for non-admin" do
414 set_user_from_auth :active_trustedclient
415 c = Container.new DEFAULT_ATTRS
417 c.mounts = {"BAR" => "FOO"}
418 c.output_path = "/tmp"
420 c.runtime_constraints = {}
421 assert_raises(ArvadosModel::PermissionDeniedError) do
426 test "Container only set exit code on complete" do
428 set_user_from_auth :dispatch1
430 c.update_attributes! state: Container::Running
432 check_illegal_updates c, [{exit_code: 1},
433 {exit_code: 1, state: Container::Cancelled}]
435 assert c.update_attributes(exit_code: 1, state: Container::Complete)
438 test "locked_by_uuid can set output on running container" do
440 set_user_from_auth :dispatch1
442 c.update_attributes! state: Container::Running
444 assert_equal c.locked_by_uuid, Thread.current[:api_client_authorization].uuid
446 assert c.update_attributes output: collections(:collection_owned_by_active).portable_data_hash
447 assert c.update_attributes! state: Container::Complete
450 test "auth_uuid can set output on running container, but not change container state" do
452 set_user_from_auth :dispatch1
454 c.update_attributes! state: Container::Running
456 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
457 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
458 assert c.update_attributes output: collections(:collection_owned_by_active).portable_data_hash
460 assert_raises ArvadosModel::PermissionDeniedError do
461 # auth_uuid cannot set container state
462 c.update_attributes state: Container::Complete
466 test "not allowed to set output that is not readable by current user" do
468 set_user_from_auth :dispatch1
470 c.update_attributes! state: Container::Running
472 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(c.auth_uuid)
473 Thread.current[:user] = User.find_by_id(Thread.current[:api_client_authorization].user_id)
475 assert_raises ActiveRecord::RecordInvalid do
476 c.update_attributes! output: collections(:collection_not_readable_by_active).portable_data_hash
480 test "other token cannot set output on running container" do
482 set_user_from_auth :dispatch1
484 c.update_attributes! state: Container::Running
486 set_user_from_auth :not_running_container_auth
487 assert_raises ArvadosModel::PermissionDeniedError do
488 c.update_attributes! output: collections(:foo_file).portable_data_hash