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({"keep_cache_ram"=>268435456, "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 output_pdh = '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
234 log_pdh = 'fa7aeb5140e2848d39b416daeef4ffc5+45'
235 act_as_system_user do
236 c.update_attributes!(state: Container::Complete,
242 assert_equal "Final", cr.state
243 ['output', 'log'].each do |out_type|
244 pdh = Container.find_by_uuid(cr.container_uuid).send(out_type)
245 assert_equal(1, Collection.where(portable_data_hash: pdh,
246 owner_uuid: project.uuid).count,
247 "Container #{out_type} should be copied to #{project.uuid}")
249 assert_not_nil cr.output_uuid
250 assert_not_nil cr.log_uuid
251 output = Collection.find_by_uuid cr.output_uuid
252 assert_equal output_pdh, output.portable_data_hash
253 log = Collection.find_by_uuid cr.log_uuid
254 assert_equal log_pdh, log.portable_data_hash
257 test "Container makes container request, then is cancelled" do
258 set_user_from_auth :active
259 cr = create_minimal_req!(priority: 5, state: "Committed", container_count_max: 1)
261 c = Container.find_by_uuid cr.container_uuid
262 assert_equal 5, c.priority
264 cr2 = create_minimal_req!
265 cr2.update_attributes!(priority: 10, state: "Committed", requesting_container_uuid: c.uuid, command: ["echo", "foo2"], container_count_max: 1)
268 c2 = Container.find_by_uuid cr2.container_uuid
269 assert_equal 10, c2.priority
271 act_as_system_user do
272 c.state = "Cancelled"
277 assert_equal "Final", cr.state
280 assert_equal 0, cr2.priority
283 assert_equal 0, c2.priority
287 ['running_container_auth', 'zzzzz-dz642-runningcontainr'],
288 ['active_no_prefs', nil],
289 ].each do |token, expected|
290 test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
291 set_user_from_auth token
292 cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
293 assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
294 assert_equal expected, cr.requesting_container_uuid
298 [[{"vcpus" => [2, nil]},
299 lambda { |resolved| resolved["vcpus"] == 2 }],
300 [{"vcpus" => [3, 7]},
301 lambda { |resolved| resolved["vcpus"] == 3 }],
303 lambda { |resolved| resolved["vcpus"] == 4 }],
304 [{"ram" => [1000000000, 2000000000]},
305 lambda { |resolved| resolved["ram"] == 1000000000 }],
306 [{"ram" => [1234234234]},
307 lambda { |resolved| resolved["ram"] == 1234234234 }],
308 ].each do |rc, okfunc|
309 test "resolve runtime constraint range #{rc} to values" do
310 cr = ContainerRequest.new(runtime_constraints: rc)
311 resolved = cr.send :runtime_constraints_for_container
312 assert(okfunc.call(resolved),
313 "container runtime_constraints was #{resolved.inspect}")
318 "kind" => "collection",
319 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
322 resolved["/out"] == {
323 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
324 "kind" => "collection",
329 "kind" => "collection",
330 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
331 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
334 resolved["/out"] == {
335 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
336 "kind" => "collection",
340 ].each do |mounts, okfunc|
341 test "resolve mounts #{mounts.inspect} to values" do
342 set_user_from_auth :active
343 cr = ContainerRequest.new(mounts: mounts)
344 resolved = cr.send :mounts_for_container
345 assert(okfunc.call(resolved),
346 "mounts_for_container returned #{resolved.inspect}")
350 test 'mount unreadable collection' do
351 set_user_from_auth :spectator
354 "kind" => "collection",
355 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
359 cr = ContainerRequest.new(mounts: m)
360 assert_raises(ArvadosModel::UnresolvableContainerError) do
361 cr.send :mounts_for_container
365 test 'mount collection with mismatched UUID and PDH' do
366 set_user_from_auth :active
369 "kind" => "collection",
370 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
371 "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
375 cr = ContainerRequest.new(mounts: m)
376 assert_raises(ArgumentError) do
377 cr.send :mounts_for_container
381 ['arvados/apitestfixture:latest',
382 'arvados/apitestfixture',
383 'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
385 test "container_image_for_container(#{tag.inspect})" do
386 set_user_from_auth :active
387 cr = ContainerRequest.new(container_image: tag)
388 resolved = cr.send :container_image_for_container
389 assert_equal resolved, collections(:docker_image).portable_data_hash
393 test "container_image_for_container(pdh)" do
394 set_user_from_auth :active
395 [:docker_image, :docker_image_1_12].each do |coll|
396 pdh = collections(coll).portable_data_hash
397 cr = ContainerRequest.new(container_image: pdh)
398 resolved = cr.send :container_image_for_container
399 assert_equal resolved, pdh
403 ['acbd18db4cc2f85cedef654fccc4a4d8+3',
405 'arvados/apitestfixture:ENOEXIST',
407 test "container_image_for_container(#{img.inspect}) => 422" do
408 set_user_from_auth :active
409 cr = ContainerRequest.new(container_image: img)
410 assert_raises(ArvadosModel::UnresolvableContainerError) do
411 cr.send :container_image_for_container
416 test "requestor can retrieve container owned by dispatch" do
417 assert_not_empty Container.readable_by(users(:admin)).where(uuid: containers(:running).uuid)
418 assert_not_empty Container.readable_by(users(:active)).where(uuid: containers(:running).uuid)
419 assert_empty Container.readable_by(users(:spectator)).where(uuid: containers(:running).uuid)
423 [{"var" => "value1"}, {"var" => "value1"}, nil],
424 [{"var" => "value1"}, {"var" => "value1"}, true],
425 [{"var" => "value1"}, {"var" => "value1"}, false],
426 [{"var" => "value1"}, {"var" => "value2"}, nil],
427 ].each do |env1, env2, use_existing|
428 test "Container request #{((env1 == env2) and (use_existing.nil? or use_existing == true)) ? 'does' : 'does not'} reuse container when committed#{use_existing.nil? ? '' : use_existing ? ' and use_existing == true' : ' and use_existing == false'}" do
429 common_attrs = {cwd: "test",
431 command: ["echo", "hello"],
433 runtime_constraints: {"vcpus" => 4,
434 "ram" => 12000000000,
435 "keep_cache_ram" => 268435456},
436 mounts: {"test" => {"kind" => "json"}}}
437 set_user_from_auth :active
438 cr1 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Committed,
441 # Testing with use_existing default value
442 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
446 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
448 use_existing: use_existing}))
450 assert_not_nil cr1.container_uuid
451 assert_nil cr2.container_uuid
453 # Update cr2 to commited state and check for container equality on different cases:
454 # * When env1 and env2 are equal and use_existing is true, the same container
455 # should be assigned.
456 # * When use_existing is false, a different container should be assigned.
457 # * When env1 and env2 are different, a different container should be assigned.
458 cr2.update_attributes!({state: ContainerRequest::Committed})
459 assert_equal (cr2.use_existing == true and (env1 == env2)),
460 (cr1.container_uuid == cr2.container_uuid)
464 test "requesting_container_uuid at create is not allowed" do
465 set_user_from_auth :active
466 assert_raises(ActiveRecord::RecordNotSaved) do
467 create_minimal_req!(state: "Uncommitted", priority: 1, requesting_container_uuid: 'youcantdothat')
471 test "Retry on container cancelled" do
472 set_user_from_auth :active
473 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2)
474 cr2 = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2, command: ["echo", "baz"])
475 prev_container_uuid = cr.container_uuid
477 c = act_as_system_user do
478 c = Container.find_by_uuid(cr.container_uuid)
479 c.update_attributes!(state: Container::Locked)
480 c.update_attributes!(state: Container::Running)
486 assert_equal "Committed", cr.state
487 assert_equal prev_container_uuid, cr.container_uuid
488 assert_not_equal cr2.container_uuid, cr.container_uuid
489 prev_container_uuid = cr.container_uuid
491 act_as_system_user do
492 c.update_attributes!(state: Container::Cancelled)
497 assert_equal "Committed", cr.state
498 assert_not_equal prev_container_uuid, cr.container_uuid
499 assert_not_equal cr2.container_uuid, cr.container_uuid
500 prev_container_uuid = cr.container_uuid
502 c = act_as_system_user do
503 c = Container.find_by_uuid(cr.container_uuid)
504 c.update_attributes!(state: Container::Cancelled)
510 assert_equal "Final", cr.state
511 assert_equal prev_container_uuid, cr.container_uuid
512 assert_not_equal cr2.container_uuid, cr.container_uuid
515 test "Output collection name setting using output_name with name collision resolution" do
516 set_user_from_auth :active
517 output_name = collections(:foo_file).name
519 cr = create_minimal_req!(priority: 1,
520 state: ContainerRequest::Committed,
521 output_name: output_name)
522 act_as_system_user do
523 c = Container.find_by_uuid(cr.container_uuid)
524 c.update_attributes!(state: Container::Locked)
525 c.update_attributes!(state: Container::Running)
526 c.update_attributes!(state: Container::Complete,
528 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
529 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
532 assert_equal ContainerRequest::Final, cr.state
533 output_coll = Collection.find_by_uuid(cr.output_uuid)
534 # Make sure the resulting output collection name include the original name
536 assert_not_equal output_name, output_coll.name,
537 "It shouldn't exist more than one collection with the same owner and name '${output_name}'"
538 assert output_coll.name.include?(output_name),
539 "New name should include original name"
540 assert_match /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/, output_coll.name,
541 "New name should include ISO8601 date"
544 test "Finalize committed request when reusing a finished container" do
545 set_user_from_auth :active
546 cr = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
548 assert_equal ContainerRequest::Committed, cr.state
549 act_as_system_user do
550 c = Container.find_by_uuid(cr.container_uuid)
551 c.update_attributes!(state: Container::Locked)
552 c.update_attributes!(state: Container::Running)
553 c.update_attributes!(state: Container::Complete,
555 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
556 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
559 assert_equal ContainerRequest::Final, cr.state
561 cr2 = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
562 assert_equal cr.container_uuid, cr2.container_uuid
563 assert_equal ContainerRequest::Final, cr2.state
565 cr3 = create_minimal_req!(priority: 1, state: ContainerRequest::Uncommitted)
566 assert_equal ContainerRequest::Uncommitted, cr3.state
567 cr3.update_attributes!(state: ContainerRequest::Committed)
568 assert_equal cr.container_uuid, cr3.container_uuid
569 assert_equal ContainerRequest::Final, cr3.state
573 [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => 100}, ContainerRequest::Committed, 100],
574 [{"vcpus" => 1, "ram" => 123}, ContainerRequest::Uncommitted],
575 [{"vcpus" => 1, "ram" => 123}, ContainerRequest::Committed],
576 [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => -1}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
577 [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => '123'}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
578 ].each do |rc, state, expected|
579 test "create container request with #{rc} in state #{state} and verify keep_cache_ram #{expected}" do
580 common_attrs = {cwd: "test",
582 command: ["echo", "hello"],
584 runtime_constraints: rc,
585 mounts: {"test" => {"kind" => "json"}}}
586 set_user_from_auth :active
588 if expected == ActiveRecord::RecordInvalid
589 assert_raises(ActiveRecord::RecordInvalid) do
590 create_minimal_req!(common_attrs.merge({state: state}))
593 cr = create_minimal_req!(common_attrs.merge({state: state}))
594 expected = Rails.configuration.container_default_keep_cache_ram if state == ContainerRequest::Committed and expected.nil?
595 assert_equal expected, cr.runtime_constraints['keep_cache_ram']
601 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
602 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Uncommitted],
603 [{"partitions" => "fastcpu"}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
604 [{"partitions" => "fastcpu"}, ContainerRequest::Uncommitted],
605 [{"partitions" => ["fastcpu","vfastcpu"]}, ContainerRequest::Committed],
606 ].each do |sp, state, expected|
607 test "create container request with scheduling_parameters #{sp} in state #{state} and verify #{expected}" do
608 common_attrs = {cwd: "test",
610 command: ["echo", "hello"],
612 scheduling_parameters: sp,
613 mounts: {"test" => {"kind" => "json"}}}
614 set_user_from_auth :active
616 if expected == ActiveRecord::RecordInvalid
617 assert_raises(ActiveRecord::RecordInvalid) do
618 create_minimal_req!(common_attrs.merge({state: state}))
621 cr = create_minimal_req!(common_attrs.merge({state: state}))
622 assert_equal sp, cr.scheduling_parameters
624 if state == ContainerRequest::Committed
625 c = Container.find_by_uuid(cr.container_uuid)
626 assert_equal sp, c.scheduling_parameters