X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/96cc8940e7926453f4728c5aec1374e7b99db201..63d0f330ab35e59dd871508e936ff74c249b94a4:/services/api/test/unit/container_request_test.rb diff --git a/services/api/test/unit/container_request_test.rb b/services/api/test/unit/container_request_test.rb index ebd6a5a19a..34aa442c09 100644 --- a/services/api/test/unit/container_request_test.rb +++ b/services/api/test/unit/container_request_test.rb @@ -129,7 +129,7 @@ class ContainerRequestTest < ActiveSupport::TestCase assert_equal({}, c.environment) assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts) assert_equal "/out", c.output_path - assert_equal({"vcpus" => 2, "ram" => 30}, c.runtime_constraints) + assert_equal({"keep_cache_ram"=>268435456, "vcpus" => 2, "ram" => 30}, c.runtime_constraints) assert_equal 1, c.priority assert_raises(ActiveRecord::RecordInvalid) do @@ -202,7 +202,7 @@ class ContainerRequestTest < ActiveSupport::TestCase test "Request is finalized when its container is cancelled" do set_user_from_auth :active - cr = create_minimal_req!(priority: 1, state: "Committed") + cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 1) act_as_system_user do Container.find_by_uuid(cr.container_uuid). @@ -215,7 +215,10 @@ class ContainerRequestTest < ActiveSupport::TestCase test "Request is finalized when its container is completed" do set_user_from_auth :active - cr = create_minimal_req!(priority: 1, state: "Committed") + project = groups(:private) + cr = create_minimal_req!(owner_uuid: project.uuid, + priority: 1, + state: "Committed") c = act_as_system_user do c = Container.find_by_uuid(cr.container_uuid) @@ -228,21 +231,31 @@ class ContainerRequestTest < ActiveSupport::TestCase assert_equal "Committed", cr.state act_as_system_user do - c.update_attributes!(state: Container::Complete) + c.update_attributes!(state: Container::Complete, + output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45', + log: 'fa7aeb5140e2848d39b416daeef4ffc5+45') end cr.reload assert_equal "Final", cr.state + ['output', 'log'].each do |out_type| + pdh = Container.find_by_uuid(cr.container_uuid).send(out_type) + assert_equal(1, Collection.where(portable_data_hash: pdh, + owner_uuid: project.uuid).count, + "Container #{out_type} should be copied to #{project.uuid}") + end end test "Container makes container request, then is cancelled" do set_user_from_auth :active - cr = create_minimal_req!(priority: 5, state: "Committed") + cr = create_minimal_req!(priority: 5, state: "Committed", container_count_max: 1) c = Container.find_by_uuid cr.container_uuid assert_equal 5, c.priority - cr2 = create_minimal_req!(priority: 10, state: "Committed", requesting_container_uuid: c.uuid) + cr2 = create_minimal_req! + cr2.update_attributes!(priority: 10, state: "Committed", requesting_container_uuid: c.uuid, command: ["echo", "foo2"], container_count_max: 1) + cr2.reload c2 = Container.find_by_uuid cr2.container_uuid assert_equal 10, c2.priority @@ -263,7 +276,7 @@ class ContainerRequestTest < ActiveSupport::TestCase end [ - ['active', 'zzzzz-dz642-runningcontainr'], + ['running_container_auth', 'zzzzz-dz642-runningcontainr'], ['active_no_prefs', nil], ].each do |token, expected| test "create as #{token} and expect requesting_container_uuid to be #{expected}" do @@ -371,10 +384,12 @@ class ContainerRequestTest < ActiveSupport::TestCase test "container_image_for_container(pdh)" do set_user_from_auth :active - pdh = collections(:docker_image).portable_data_hash - cr = ContainerRequest.new(container_image: pdh) - resolved = cr.send :container_image_for_container - assert_equal resolved, pdh + [:docker_image, :docker_image_1_12].each do |coll| + pdh = collections(coll).portable_data_hash + cr = ContainerRequest.new(container_image: pdh) + resolved = cr.send :container_image_for_container + assert_equal resolved, pdh + end end ['acbd18db4cc2f85cedef654fccc4a4d8+3', @@ -395,4 +410,146 @@ class ContainerRequestTest < ActiveSupport::TestCase assert_not_empty Container.readable_by(users(:active)).where(uuid: containers(:running).uuid) assert_empty Container.readable_by(users(:spectator)).where(uuid: containers(:running).uuid) end + + [ + [{"var" => "value1"}, {"var" => "value1"}, nil], + [{"var" => "value1"}, {"var" => "value1"}, true], + [{"var" => "value1"}, {"var" => "value1"}, false], + [{"var" => "value1"}, {"var" => "value2"}, nil], + ].each do |env1, env2, use_existing| + 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 + common_attrs = {cwd: "test", + priority: 1, + command: ["echo", "hello"], + output_path: "test", + runtime_constraints: {"vcpus" => 4, + "ram" => 12000000000, + "keep_cache_ram" => 268435456}, + mounts: {"test" => {"kind" => "json"}}} + set_user_from_auth :active + cr1 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Committed, + environment: env1})) + if use_existing.nil? + # Testing with use_existing default value + cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted, + environment: env2})) + else + + cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted, + environment: env2, + use_existing: use_existing})) + end + assert_not_nil cr1.container_uuid + assert_nil cr2.container_uuid + + # Update cr2 to commited state and check for container equality on different cases: + # * When env1 and env2 are equal and use_existing is true, the same container + # should be assigned. + # * When use_existing is false, a different container should be assigned. + # * When env1 and env2 are different, a different container should be assigned. + cr2.update_attributes!({state: ContainerRequest::Committed}) + assert_equal (cr2.use_existing == true and (env1 == env2)), + (cr1.container_uuid == cr2.container_uuid) + end + end + + test "requesting_container_uuid at create is not allowed" do + set_user_from_auth :active + assert_raises(ActiveRecord::RecordNotSaved) do + create_minimal_req!(state: "Uncommitted", priority: 1, requesting_container_uuid: 'youcantdothat') + end + end + + test "Retry on container cancelled" do + set_user_from_auth :active + cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2) + prev_container_uuid = cr.container_uuid + + c = act_as_system_user do + c = Container.find_by_uuid(cr.container_uuid) + c.update_attributes!(state: Container::Locked) + c.update_attributes!(state: Container::Running) + c + end + + cr.reload + assert_equal "Committed", cr.state + assert_equal prev_container_uuid, cr.container_uuid + prev_container_uuid = cr.container_uuid + + act_as_system_user do + c.update_attributes!(state: Container::Cancelled) + end + + cr.reload + assert_equal "Committed", cr.state + assert_not_equal prev_container_uuid, cr.container_uuid + prev_container_uuid = cr.container_uuid + + c = act_as_system_user do + c = Container.find_by_uuid(cr.container_uuid) + c.update_attributes!(state: Container::Cancelled) + c + end + + cr.reload + assert_equal "Final", cr.state + assert_equal prev_container_uuid, cr.container_uuid + end + + test "Finalize committed request when reusing a finished container" do + set_user_from_auth :active + cr = create_minimal_req!(priority: 1, state: ContainerRequest::Committed) + cr.reload + assert_equal ContainerRequest::Committed, cr.state + act_as_system_user do + c = Container.find_by_uuid(cr.container_uuid) + c.update_attributes!(state: Container::Locked) + c.update_attributes!(state: Container::Running) + c.update_attributes!(state: Container::Complete, + exit_code: 0, + output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45', + log: 'fa7aeb5140e2848d39b416daeef4ffc5+45') + end + cr.reload + assert_equal ContainerRequest::Final, cr.state + + cr2 = create_minimal_req!(priority: 1, state: ContainerRequest::Committed) + assert_equal cr.container_uuid, cr2.container_uuid + assert_equal ContainerRequest::Final, cr2.state + + cr3 = create_minimal_req!(priority: 1, state: ContainerRequest::Uncommitted) + assert_equal ContainerRequest::Uncommitted, cr3.state + cr3.update_attributes!(state: ContainerRequest::Committed) + assert_equal cr.container_uuid, cr3.container_uuid + assert_equal ContainerRequest::Final, cr3.state + end + + [ + [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => 100}, ContainerRequest::Committed, 100], + [{"vcpus" => 1, "ram" => 123}, ContainerRequest::Uncommitted], + [{"vcpus" => 1, "ram" => 123}, ContainerRequest::Committed], + [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => -1}, ContainerRequest::Committed, ActiveRecord::RecordInvalid], + [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => '123'}, ContainerRequest::Committed, ActiveRecord::RecordInvalid], + ].each do |rc, state, expected| + test "create container request with #{rc} in state #{state} and verify keep_cache_ram #{expected}" do + common_attrs = {cwd: "test", + priority: 1, + command: ["echo", "hello"], + output_path: "test", + runtime_constraints: rc, + mounts: {"test" => {"kind" => "json"}}} + set_user_from_auth :active + + if expected == ActiveRecord::RecordInvalid + assert_raises(ActiveRecord::RecordInvalid) do + create_minimal_req!(common_attrs.merge({state: state})) + end + else + cr = create_minimal_req!(common_attrs.merge({state: state})) + expected = Rails.configuration.container_default_keep_cache_ram if state == ContainerRequest::Committed and expected.nil? + assert_equal expected, cr.runtime_constraints['keep_cache_ram'] + end + end + end end