3 class ContainerRequestTest < ActiveSupport::TestCase
4 def create_minimal_req! attrs={}
6 command: ["echo", "foo"],
7 container_image: links(:docker_image_collection_tag).name,
10 mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}},
12 runtime_constraints: {"vcpus" => 1, "ram" => 2},
16 cr = ContainerRequest.create!(defaults.merge(attrs))
21 def check_bogus_states cr
22 [nil, "Flubber"].each do |state|
23 assert_raises(ActiveRecord::RecordInvalid) do
31 test "Container request create" do
32 set_user_from_auth :active
33 cr = create_minimal_req!
35 assert_nil cr.container_uuid
36 assert_nil cr.priority
40 # Ensure we can modify all attributes
41 cr.command = ["echo", "foo3"]
42 cr.container_image = "img3"
44 cr.environment = {"BUP" => "BOP"}
45 cr.mounts = {"BAR" => "BAZ"}
46 cr.output_path = "/tmp4"
48 cr.runtime_constraints = {"vcpus" => 4}
50 cr.description = "bar3"
53 assert_nil cr.container_uuid
58 {"vcpus" => 1, "ram" => nil},
59 {"vcpus" => 0, "ram" => 123},
60 {"vcpus" => "1", "ram" => "123"}
61 ].each do |invalid_constraints|
62 test "Create with #{invalid_constraints}" do
63 set_user_from_auth :active
64 assert_raises(ActiveRecord::RecordInvalid) do
65 cr = create_minimal_req!(state: "Committed",
67 runtime_constraints: invalid_constraints)
72 test "Update with #{invalid_constraints}" do
73 set_user_from_auth :active
74 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
76 assert_raises(ActiveRecord::RecordInvalid) do
77 cr = ContainerRequest.find_by_uuid cr.uuid
78 cr.update_attributes!(state: "Committed",
79 runtime_constraints: invalid_constraints)
84 test "Update from fixture" do
85 set_user_from_auth :active
86 cr = ContainerRequest.find_by_uuid(container_requests(:running).uuid)
87 cr.update_attributes!(description: "New description")
88 assert_equal "New description", cr.description
91 test "Update with valid runtime constraints" do
92 set_user_from_auth :active
93 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
95 cr = ContainerRequest.find_by_uuid cr.uuid
96 cr.update_attributes!(state: "Committed",
97 runtime_constraints: {"vcpus" => 1, "ram" => 23})
98 assert_not_nil cr.container_uuid
101 test "Container request priority must be non-nil" do
102 set_user_from_auth :active
103 cr = create_minimal_req!(priority: nil)
104 cr.state = "Committed"
105 assert_raises(ActiveRecord::RecordInvalid) do
110 test "Container request commit" do
111 set_user_from_auth :active
112 cr = create_minimal_req!(runtime_constraints: {"vcpus" => 2, "ram" => 30})
114 assert_nil cr.container_uuid
117 cr.state = "Committed"
123 assert_not_nil cr.container_uuid
124 c = Container.find_by_uuid cr.container_uuid
126 assert_equal ["echo", "foo"], c.command
127 assert_equal collections(:docker_image).portable_data_hash, c.container_image
128 assert_equal "/tmp", c.cwd
129 assert_equal({}, c.environment)
130 assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts)
131 assert_equal "/out", c.output_path
132 assert_equal({"vcpus" => 2, "ram" => 30}, c.runtime_constraints)
133 assert_equal 1, c.priority
135 assert_raises(ActiveRecord::RecordInvalid) do
145 assert_equal 0, cr.priority
146 assert_equal 0, c.priority
150 test "Container request max priority" do
151 set_user_from_auth :active
152 cr = create_minimal_req!(priority: 5, state: "Committed")
154 c = Container.find_by_uuid cr.container_uuid
155 assert_equal 5, c.priority
157 cr2 = create_minimal_req!
159 cr2.state = "Committed"
160 cr2.container_uuid = cr.container_uuid
161 act_as_system_user do
165 # cr and cr2 have priority 5 and 10, and are being satisfied by
166 # the same container c, so c's priority should be
169 assert_equal 10, c.priority
171 cr2.update_attributes!(priority: 0)
174 assert_equal 5, c.priority
176 cr.update_attributes!(priority: 0)
179 assert_equal 0, c.priority
183 test "Independent container requests" do
184 set_user_from_auth :active
185 cr1 = create_minimal_req!(command: ["foo", "1"], priority: 5, state: "Committed")
186 cr2 = create_minimal_req!(command: ["foo", "2"], priority: 10, state: "Committed")
188 c1 = Container.find_by_uuid cr1.container_uuid
189 assert_equal 5, c1.priority
191 c2 = Container.find_by_uuid cr2.container_uuid
192 assert_equal 10, c2.priority
194 cr1.update_attributes!(priority: 0)
197 assert_equal 0, c1.priority
200 assert_equal 10, c2.priority
203 test "Request is finalized when its container is cancelled" do
204 set_user_from_auth :active
205 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 1)
207 act_as_system_user do
208 Container.find_by_uuid(cr.container_uuid).
209 update_attributes!(state: Container::Cancelled)
213 assert_equal "Final", cr.state
216 test "Request is finalized when its container is completed" do
217 set_user_from_auth :active
218 project = groups(:private)
219 cr = create_minimal_req!(owner_uuid: project.uuid,
223 c = act_as_system_user do
224 c = Container.find_by_uuid(cr.container_uuid)
225 c.update_attributes!(state: Container::Locked)
226 c.update_attributes!(state: Container::Running)
231 assert_equal "Committed", cr.state
233 act_as_system_user do
234 c.update_attributes!(state: Container::Complete,
235 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
236 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
240 assert_equal "Final", cr.state
241 ['output', 'log'].each do |out_type|
242 pdh = Container.find_by_uuid(cr.container_uuid).send(out_type)
243 assert_equal(1, Collection.where(portable_data_hash: pdh,
244 owner_uuid: project.uuid).count,
245 "Container #{out_type} should be copied to #{project.uuid}")
249 test "Container makes container request, then is cancelled" do
250 set_user_from_auth :active
251 cr = create_minimal_req!(priority: 5, state: "Committed", container_count_max: 1)
253 c = Container.find_by_uuid cr.container_uuid
254 assert_equal 5, c.priority
256 cr2 = create_minimal_req!
257 cr2.update_attributes!(priority: 10, state: "Committed", requesting_container_uuid: c.uuid, command: ["echo", "foo2"], container_count_max: 1)
260 c2 = Container.find_by_uuid cr2.container_uuid
261 assert_equal 10, c2.priority
263 act_as_system_user do
264 c.state = "Cancelled"
269 assert_equal "Final", cr.state
272 assert_equal 0, cr2.priority
275 assert_equal 0, c2.priority
279 ['active', 'zzzzz-dz642-runningcontainr'],
280 ['active_no_prefs', nil],
281 ].each do |token, expected|
282 test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
283 set_user_from_auth token
284 cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
285 assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
286 assert_equal expected, cr.requesting_container_uuid
290 [[{"vcpus" => [2, nil]},
291 lambda { |resolved| resolved["vcpus"] == 2 }],
292 [{"vcpus" => [3, 7]},
293 lambda { |resolved| resolved["vcpus"] == 3 }],
295 lambda { |resolved| resolved["vcpus"] == 4 }],
296 [{"ram" => [1000000000, 2000000000]},
297 lambda { |resolved| resolved["ram"] == 1000000000 }],
298 [{"ram" => [1234234234]},
299 lambda { |resolved| resolved["ram"] == 1234234234 }],
300 ].each do |rc, okfunc|
301 test "resolve runtime constraint range #{rc} to values" do
302 cr = ContainerRequest.new(runtime_constraints: rc)
303 resolved = cr.send :runtime_constraints_for_container
304 assert(okfunc.call(resolved),
305 "container runtime_constraints was #{resolved.inspect}")
310 "kind" => "collection",
311 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
314 resolved["/out"] == {
315 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
316 "kind" => "collection",
321 "kind" => "collection",
322 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
323 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
326 resolved["/out"] == {
327 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
328 "kind" => "collection",
332 ].each do |mounts, okfunc|
333 test "resolve mounts #{mounts.inspect} to values" do
334 set_user_from_auth :active
335 cr = ContainerRequest.new(mounts: mounts)
336 resolved = cr.send :mounts_for_container
337 assert(okfunc.call(resolved),
338 "mounts_for_container returned #{resolved.inspect}")
342 test 'mount unreadable collection' do
343 set_user_from_auth :spectator
346 "kind" => "collection",
347 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
351 cr = ContainerRequest.new(mounts: m)
352 assert_raises(ArvadosModel::UnresolvableContainerError) do
353 cr.send :mounts_for_container
357 test 'mount collection with mismatched UUID and PDH' do
358 set_user_from_auth :active
361 "kind" => "collection",
362 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
363 "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
367 cr = ContainerRequest.new(mounts: m)
368 assert_raises(ArgumentError) do
369 cr.send :mounts_for_container
373 ['arvados/apitestfixture:latest',
374 'arvados/apitestfixture',
375 'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
377 test "container_image_for_container(#{tag.inspect})" do
378 set_user_from_auth :active
379 cr = ContainerRequest.new(container_image: tag)
380 resolved = cr.send :container_image_for_container
381 assert_equal resolved, collections(:docker_image).portable_data_hash
385 test "container_image_for_container(pdh)" do
386 set_user_from_auth :active
387 pdh = collections(:docker_image).portable_data_hash
388 cr = ContainerRequest.new(container_image: pdh)
389 resolved = cr.send :container_image_for_container
390 assert_equal resolved, pdh
393 ['acbd18db4cc2f85cedef654fccc4a4d8+3',
395 'arvados/apitestfixture:ENOEXIST',
397 test "container_image_for_container(#{img.inspect}) => 422" do
398 set_user_from_auth :active
399 cr = ContainerRequest.new(container_image: img)
400 assert_raises(ArvadosModel::UnresolvableContainerError) do
401 cr.send :container_image_for_container
406 test "requestor can retrieve container owned by dispatch" do
407 assert_not_empty Container.readable_by(users(:admin)).where(uuid: containers(:running).uuid)
408 assert_not_empty Container.readable_by(users(:active)).where(uuid: containers(:running).uuid)
409 assert_empty Container.readable_by(users(:spectator)).where(uuid: containers(:running).uuid)
413 [{"var" => "value1"}, {"var" => "value1"}],
414 [{"var" => "value1"}, {"var" => "value2"}]
415 ].each do |env1, env2|
416 test "Container request #{(env1 == env2) ? 'does' : 'does not'} reuse container when committed" do
417 common_attrs = {cwd: "test",
419 command: ["echo", "hello"],
421 runtime_constraints: {"vcpus" => 4,
422 "ram" => 12000000000},
423 mounts: {"test" => {"kind" => "json"}}}
424 set_user_from_auth :active
425 cr1 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Committed,
427 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
429 assert_not_nil cr1.container_uuid
430 assert_nil cr2.container_uuid
432 # Update cr2 to commited state and check for container equality on both cases,
433 # when env1 and env2 are equal the same container should be assigned, and
434 # when env1 and env2 are different, cr2 container should be different.
435 cr2.update_attributes!({state: ContainerRequest::Committed})
436 assert_equal (env1 == env2), (cr1.container_uuid == cr2.container_uuid)
440 test "requesting_container_uuid at create is not allowed" do
441 set_user_from_auth :active
442 assert_raises(ActiveRecord::RecordNotSaved) do
443 create_minimal_req!(state: "Uncommitted", priority: 1, requesting_container_uuid: 'youcantdothat')
447 test "Retry on container cancelled" do
448 set_user_from_auth :active
449 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2)
450 prev_container_uuid = cr.container_uuid
452 c = act_as_system_user do
453 c = Container.find_by_uuid(cr.container_uuid)
454 c.update_attributes!(state: Container::Locked)
455 c.update_attributes!(state: Container::Running)
460 assert_equal "Committed", cr.state
461 assert_equal prev_container_uuid, cr.container_uuid
462 prev_container_uuid = cr.container_uuid
464 act_as_system_user do
465 c.update_attributes!(state: Container::Cancelled)
469 assert_equal "Committed", cr.state
470 assert_not_equal prev_container_uuid, cr.container_uuid
471 prev_container_uuid = cr.container_uuid
473 c = act_as_system_user do
474 c = Container.find_by_uuid(cr.container_uuid)
475 c.update_attributes!(state: Container::Cancelled)
480 assert_equal "Final", cr.state
481 assert_equal prev_container_uuid, cr.container_uuid
484 test "Finalize committed request when reusing a finished container" do
485 set_user_from_auth :active
486 cr = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
488 assert_equal ContainerRequest::Committed, cr.state
489 act_as_system_user do
490 c = Container.find_by_uuid(cr.container_uuid)
491 c.update_attributes!(state: Container::Locked)
492 c.update_attributes!(state: Container::Running)
493 c.update_attributes!(state: Container::Complete,
495 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
496 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
499 assert_equal ContainerRequest::Final, cr.state
501 cr2 = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
502 assert_equal cr.container_uuid, cr2.container_uuid
503 assert_equal ContainerRequest::Final, cr2.state
505 cr3 = create_minimal_req!(priority: 1, state: ContainerRequest::Uncommitted)
506 assert_equal ContainerRequest::Uncommitted, cr3.state
507 cr3.update_attributes!(state: ContainerRequest::Committed)
508 assert_equal cr.container_uuid, cr3.container_uuid
509 assert_equal ContainerRequest::Final, cr3.state