2 require 'helpers/docker_migration_helper'
4 class ContainerRequestTest < ActiveSupport::TestCase
5 include DockerMigrationHelper
7 def create_minimal_req! attrs={}
9 command: ["echo", "foo"],
10 container_image: links(:docker_image_collection_tag).name,
13 mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}},
15 runtime_constraints: {"vcpus" => 1, "ram" => 2},
19 cr = ContainerRequest.create!(defaults.merge(attrs))
24 def check_bogus_states cr
25 [nil, "Flubber"].each do |state|
26 assert_raises(ActiveRecord::RecordInvalid) do
34 test "Container request create" do
35 set_user_from_auth :active
36 cr = create_minimal_req!
38 assert_nil cr.container_uuid
39 assert_nil cr.priority
43 # Ensure we can modify all attributes
44 cr.command = ["echo", "foo3"]
45 cr.container_image = "img3"
47 cr.environment = {"BUP" => "BOP"}
48 cr.mounts = {"BAR" => "BAZ"}
49 cr.output_path = "/tmp4"
51 cr.runtime_constraints = {"vcpus" => 4}
53 cr.description = "bar3"
56 assert_nil cr.container_uuid
61 {"vcpus" => 1, "ram" => nil},
62 {"vcpus" => 0, "ram" => 123},
63 {"vcpus" => "1", "ram" => "123"}
64 ].each do |invalid_constraints|
65 test "Create with #{invalid_constraints}" do
66 set_user_from_auth :active
67 assert_raises(ActiveRecord::RecordInvalid) do
68 cr = create_minimal_req!(state: "Committed",
70 runtime_constraints: invalid_constraints)
75 test "Update with #{invalid_constraints}" do
76 set_user_from_auth :active
77 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
79 assert_raises(ActiveRecord::RecordInvalid) do
80 cr = ContainerRequest.find_by_uuid cr.uuid
81 cr.update_attributes!(state: "Committed",
82 runtime_constraints: invalid_constraints)
87 test "Update from fixture" do
88 set_user_from_auth :active
89 cr = ContainerRequest.find_by_uuid(container_requests(:running).uuid)
90 cr.update_attributes!(description: "New description")
91 assert_equal "New description", cr.description
94 test "Update with valid runtime constraints" do
95 set_user_from_auth :active
96 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
98 cr = ContainerRequest.find_by_uuid cr.uuid
99 cr.update_attributes!(state: "Committed",
100 runtime_constraints: {"vcpus" => 1, "ram" => 23})
101 assert_not_nil cr.container_uuid
104 test "Container request priority must be non-nil" do
105 set_user_from_auth :active
106 cr = create_minimal_req!(priority: nil)
107 cr.state = "Committed"
108 assert_raises(ActiveRecord::RecordInvalid) do
113 test "Container request commit" do
114 set_user_from_auth :active
115 cr = create_minimal_req!(runtime_constraints: {"vcpus" => 2, "ram" => 30})
117 assert_nil cr.container_uuid
120 cr.state = "Committed"
126 assert_not_nil cr.container_uuid
127 c = Container.find_by_uuid cr.container_uuid
129 assert_equal ["echo", "foo"], c.command
130 assert_equal collections(:docker_image).portable_data_hash, c.container_image
131 assert_equal "/tmp", c.cwd
132 assert_equal({}, c.environment)
133 assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts)
134 assert_equal "/out", c.output_path
135 assert_equal({"keep_cache_ram"=>268435456, "vcpus" => 2, "ram" => 30}, c.runtime_constraints)
136 assert_equal 1, c.priority
138 assert_raises(ActiveRecord::RecordInvalid) do
148 assert_equal 0, cr.priority
149 assert_equal 0, c.priority
153 test "Container request max priority" do
154 set_user_from_auth :active
155 cr = create_minimal_req!(priority: 5, state: "Committed")
157 c = Container.find_by_uuid cr.container_uuid
158 assert_equal 5, c.priority
160 cr2 = create_minimal_req!
162 cr2.state = "Committed"
163 cr2.container_uuid = cr.container_uuid
164 act_as_system_user do
168 # cr and cr2 have priority 5 and 10, and are being satisfied by
169 # the same container c, so c's priority should be
172 assert_equal 10, c.priority
174 cr2.update_attributes!(priority: 0)
177 assert_equal 5, c.priority
179 cr.update_attributes!(priority: 0)
182 assert_equal 0, c.priority
186 test "Independent container requests" do
187 set_user_from_auth :active
188 cr1 = create_minimal_req!(command: ["foo", "1"], priority: 5, state: "Committed")
189 cr2 = create_minimal_req!(command: ["foo", "2"], priority: 10, state: "Committed")
191 c1 = Container.find_by_uuid cr1.container_uuid
192 assert_equal 5, c1.priority
194 c2 = Container.find_by_uuid cr2.container_uuid
195 assert_equal 10, c2.priority
197 cr1.update_attributes!(priority: 0)
200 assert_equal 0, c1.priority
203 assert_equal 10, c2.priority
206 test "Request is finalized when its container is cancelled" do
207 set_user_from_auth :active
208 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 1)
210 act_as_system_user do
211 Container.find_by_uuid(cr.container_uuid).
212 update_attributes!(state: Container::Cancelled)
216 assert_equal "Final", cr.state
219 test "Request is finalized when its container is completed" do
220 set_user_from_auth :active
221 project = groups(:private)
222 cr = create_minimal_req!(owner_uuid: project.uuid,
226 c = act_as_system_user do
227 c = Container.find_by_uuid(cr.container_uuid)
228 c.update_attributes!(state: Container::Locked)
229 c.update_attributes!(state: Container::Running)
234 assert_equal "Committed", cr.state
236 output_pdh = '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
237 log_pdh = 'fa7aeb5140e2848d39b416daeef4ffc5+45'
238 act_as_system_user do
239 c.update_attributes!(state: Container::Complete,
245 assert_equal "Final", cr.state
246 ['output', 'log'].each do |out_type|
247 pdh = Container.find_by_uuid(cr.container_uuid).send(out_type)
248 assert_equal(1, Collection.where(portable_data_hash: pdh,
249 owner_uuid: project.uuid).count,
250 "Container #{out_type} should be copied to #{project.uuid}")
252 assert_not_nil cr.output_uuid
253 assert_not_nil cr.log_uuid
254 output = Collection.find_by_uuid cr.output_uuid
255 assert_equal output_pdh, output.portable_data_hash
256 log = Collection.find_by_uuid cr.log_uuid
257 assert_equal log_pdh, log.portable_data_hash
260 test "Container makes container request, then is cancelled" do
261 set_user_from_auth :active
262 cr = create_minimal_req!(priority: 5, state: "Committed", container_count_max: 1)
264 c = Container.find_by_uuid cr.container_uuid
265 assert_equal 5, c.priority
267 cr2 = create_minimal_req!
268 cr2.update_attributes!(priority: 10, state: "Committed", requesting_container_uuid: c.uuid, command: ["echo", "foo2"], container_count_max: 1)
271 c2 = Container.find_by_uuid cr2.container_uuid
272 assert_equal 10, c2.priority
274 act_as_system_user do
275 c.state = "Cancelled"
280 assert_equal "Final", cr.state
283 assert_equal 0, cr2.priority
286 assert_equal 0, c2.priority
290 ['running_container_auth', 'zzzzz-dz642-runningcontainr'],
291 ['active_no_prefs', nil],
292 ].each do |token, expected|
293 test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
294 set_user_from_auth token
295 cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
296 assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
297 assert_equal expected, cr.requesting_container_uuid
301 [[{"vcpus" => [2, nil]},
302 lambda { |resolved| resolved["vcpus"] == 2 }],
303 [{"vcpus" => [3, 7]},
304 lambda { |resolved| resolved["vcpus"] == 3 }],
306 lambda { |resolved| resolved["vcpus"] == 4 }],
307 [{"ram" => [1000000000, 2000000000]},
308 lambda { |resolved| resolved["ram"] == 1000000000 }],
309 [{"ram" => [1234234234]},
310 lambda { |resolved| resolved["ram"] == 1234234234 }],
311 ].each do |rc, okfunc|
312 test "resolve runtime constraint range #{rc} to values" do
313 cr = ContainerRequest.new(runtime_constraints: rc)
314 resolved = cr.send :runtime_constraints_for_container
315 assert(okfunc.call(resolved),
316 "container runtime_constraints was #{resolved.inspect}")
321 "kind" => "collection",
322 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
325 resolved["/out"] == {
326 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
327 "kind" => "collection",
332 "kind" => "collection",
333 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
334 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
337 resolved["/out"] == {
338 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
339 "kind" => "collection",
343 ].each do |mounts, okfunc|
344 test "resolve mounts #{mounts.inspect} to values" do
345 set_user_from_auth :active
346 cr = ContainerRequest.new(mounts: mounts)
347 resolved = cr.send :mounts_for_container
348 assert(okfunc.call(resolved),
349 "mounts_for_container returned #{resolved.inspect}")
353 test 'mount unreadable collection' do
354 set_user_from_auth :spectator
357 "kind" => "collection",
358 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
362 cr = ContainerRequest.new(mounts: m)
363 assert_raises(ArvadosModel::UnresolvableContainerError) do
364 cr.send :mounts_for_container
368 test 'mount collection with mismatched UUID and PDH' do
369 set_user_from_auth :active
372 "kind" => "collection",
373 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
374 "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
378 cr = ContainerRequest.new(mounts: m)
379 assert_raises(ArgumentError) do
380 cr.send :mounts_for_container
384 ['arvados/apitestfixture:latest',
385 'arvados/apitestfixture',
386 'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
388 test "container_image_for_container(#{tag.inspect})" do
389 set_user_from_auth :active
390 cr = ContainerRequest.new(container_image: tag)
391 resolved = cr.send :container_image_for_container
392 assert_equal resolved, collections(:docker_image).portable_data_hash
396 test "container_image_for_container(pdh)" do
397 set_user_from_auth :active
398 [:docker_image, :docker_image_1_12].each do |coll|
399 pdh = collections(coll).portable_data_hash
400 cr = ContainerRequest.new(container_image: pdh)
401 resolved = cr.send :container_image_for_container
402 assert_equal resolved, pdh
406 ['acbd18db4cc2f85cedef654fccc4a4d8+3',
408 'arvados/apitestfixture:ENOEXIST',
410 test "container_image_for_container(#{img.inspect}) => 422" do
411 set_user_from_auth :active
412 cr = ContainerRequest.new(container_image: img)
413 assert_raises(ArvadosModel::UnresolvableContainerError) do
414 cr.send :container_image_for_container
419 test "migrated docker image" do
420 Rails.configuration.docker_image_formats = ['v2']
421 add_docker19_migration_link
423 set_user_from_auth :active
424 cr = create_minimal_req!(command: ["true", "1"],
425 container_image: collections(:docker_image).portable_data_hash)
426 assert_equal(cr.send(:container_image_for_container),
427 collections(:docker_image_1_12).portable_data_hash)
429 cr = create_minimal_req!(command: ["true", "2"],
430 container_image: links(:docker_image_collection_tag).name)
431 assert_equal(cr.send(:container_image_for_container),
432 collections(:docker_image_1_12).portable_data_hash)
435 test "requestor can retrieve container owned by dispatch" do
436 assert_not_empty Container.readable_by(users(:admin)).where(uuid: containers(:running).uuid)
437 assert_not_empty Container.readable_by(users(:active)).where(uuid: containers(:running).uuid)
438 assert_empty Container.readable_by(users(:spectator)).where(uuid: containers(:running).uuid)
442 [{"var" => "value1"}, {"var" => "value1"}, nil],
443 [{"var" => "value1"}, {"var" => "value1"}, true],
444 [{"var" => "value1"}, {"var" => "value1"}, false],
445 [{"var" => "value1"}, {"var" => "value2"}, nil],
446 ].each do |env1, env2, use_existing|
447 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
448 common_attrs = {cwd: "test",
450 command: ["echo", "hello"],
452 runtime_constraints: {"vcpus" => 4,
453 "ram" => 12000000000,
454 "keep_cache_ram" => 268435456},
455 mounts: {"test" => {"kind" => "json"}}}
456 set_user_from_auth :active
457 cr1 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Committed,
460 # Testing with use_existing default value
461 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
465 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
467 use_existing: use_existing}))
469 assert_not_nil cr1.container_uuid
470 assert_nil cr2.container_uuid
472 # Update cr2 to commited state and check for container equality on different cases:
473 # * When env1 and env2 are equal and use_existing is true, the same container
474 # should be assigned.
475 # * When use_existing is false, a different container should be assigned.
476 # * When env1 and env2 are different, a different container should be assigned.
477 cr2.update_attributes!({state: ContainerRequest::Committed})
478 assert_equal (cr2.use_existing == true and (env1 == env2)),
479 (cr1.container_uuid == cr2.container_uuid)
483 test "requesting_container_uuid at create is not allowed" do
484 set_user_from_auth :active
485 assert_raises(ActiveRecord::RecordNotSaved) do
486 create_minimal_req!(state: "Uncommitted", priority: 1, requesting_container_uuid: 'youcantdothat')
490 test "Retry on container cancelled" do
491 set_user_from_auth :active
492 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2)
493 cr2 = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2, command: ["echo", "baz"])
494 prev_container_uuid = cr.container_uuid
496 c = act_as_system_user do
497 c = Container.find_by_uuid(cr.container_uuid)
498 c.update_attributes!(state: Container::Locked)
499 c.update_attributes!(state: Container::Running)
505 assert_equal "Committed", cr.state
506 assert_equal prev_container_uuid, cr.container_uuid
507 assert_not_equal cr2.container_uuid, cr.container_uuid
508 prev_container_uuid = cr.container_uuid
510 act_as_system_user do
511 c.update_attributes!(state: Container::Cancelled)
516 assert_equal "Committed", cr.state
517 assert_not_equal prev_container_uuid, cr.container_uuid
518 assert_not_equal cr2.container_uuid, cr.container_uuid
519 prev_container_uuid = cr.container_uuid
521 c = act_as_system_user do
522 c = Container.find_by_uuid(cr.container_uuid)
523 c.update_attributes!(state: Container::Cancelled)
529 assert_equal "Final", cr.state
530 assert_equal prev_container_uuid, cr.container_uuid
531 assert_not_equal cr2.container_uuid, cr.container_uuid
534 test "Output collection name setting using output_name with name collision resolution" do
535 set_user_from_auth :active
536 output_name = collections(:foo_file).name
538 cr = create_minimal_req!(priority: 1,
539 state: ContainerRequest::Committed,
540 output_name: output_name)
541 act_as_system_user do
542 c = Container.find_by_uuid(cr.container_uuid)
543 c.update_attributes!(state: Container::Locked)
544 c.update_attributes!(state: Container::Running)
545 c.update_attributes!(state: Container::Complete,
547 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
548 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
551 assert_equal ContainerRequest::Final, cr.state
552 output_coll = Collection.find_by_uuid(cr.output_uuid)
553 # Make sure the resulting output collection name include the original name
555 assert_not_equal output_name, output_coll.name,
556 "It shouldn't exist more than one collection with the same owner and name '${output_name}'"
557 assert output_coll.name.include?(output_name),
558 "New name should include original name"
559 assert_match /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/, output_coll.name,
560 "New name should include ISO8601 date"
563 test "Finalize committed request when reusing a finished container" do
564 set_user_from_auth :active
565 cr = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
567 assert_equal ContainerRequest::Committed, cr.state
568 act_as_system_user do
569 c = Container.find_by_uuid(cr.container_uuid)
570 c.update_attributes!(state: Container::Locked)
571 c.update_attributes!(state: Container::Running)
572 c.update_attributes!(state: Container::Complete,
574 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
575 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
578 assert_equal ContainerRequest::Final, cr.state
580 cr2 = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
581 assert_equal cr.container_uuid, cr2.container_uuid
582 assert_equal ContainerRequest::Final, cr2.state
584 cr3 = create_minimal_req!(priority: 1, state: ContainerRequest::Uncommitted)
585 assert_equal ContainerRequest::Uncommitted, cr3.state
586 cr3.update_attributes!(state: ContainerRequest::Committed)
587 assert_equal cr.container_uuid, cr3.container_uuid
588 assert_equal ContainerRequest::Final, cr3.state
592 [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => 100}, ContainerRequest::Committed, 100],
593 [{"vcpus" => 1, "ram" => 123}, ContainerRequest::Uncommitted],
594 [{"vcpus" => 1, "ram" => 123}, ContainerRequest::Committed],
595 [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => -1}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
596 [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => '123'}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
597 ].each do |rc, state, expected|
598 test "create container request with #{rc} in state #{state} and verify keep_cache_ram #{expected}" do
599 common_attrs = {cwd: "test",
601 command: ["echo", "hello"],
603 runtime_constraints: rc,
604 mounts: {"test" => {"kind" => "json"}}}
605 set_user_from_auth :active
607 if expected == ActiveRecord::RecordInvalid
608 assert_raises(ActiveRecord::RecordInvalid) do
609 create_minimal_req!(common_attrs.merge({state: state}))
612 cr = create_minimal_req!(common_attrs.merge({state: state}))
613 expected = Rails.configuration.container_default_keep_cache_ram if state == ContainerRequest::Committed and expected.nil?
614 assert_equal expected, cr.runtime_constraints['keep_cache_ram']
620 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
621 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Uncommitted],
622 [{"partitions" => "fastcpu"}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
623 [{"partitions" => "fastcpu"}, ContainerRequest::Uncommitted],
624 [{"partitions" => ["fastcpu","vfastcpu"]}, ContainerRequest::Committed],
625 ].each do |sp, state, expected|
626 test "create container request with scheduling_parameters #{sp} in state #{state} and verify #{expected}" do
627 common_attrs = {cwd: "test",
629 command: ["echo", "hello"],
631 scheduling_parameters: sp,
632 mounts: {"test" => {"kind" => "json"}}}
633 set_user_from_auth :active
635 if expected == ActiveRecord::RecordInvalid
636 assert_raises(ActiveRecord::RecordInvalid) do
637 create_minimal_req!(common_attrs.merge({state: state}))
640 cr = create_minimal_req!(common_attrs.merge({state: state}))
641 assert_equal sp, cr.scheduling_parameters
643 if state == ContainerRequest::Committed
644 c = Container.find_by_uuid(cr.container_uuid)
645 assert_equal sp, c.scheduling_parameters