+
+ cr2.reload
+ assert_equal 10, cr2.priority
+
+ c2.reload
+ assert_equal 10, c2.priority
+ end
+
+ [
+ ['running_container_auth', 'zzzzz-dz642-runningcontainr', 12],
+ ['active_no_prefs', nil, 0],
+ ].each do |token, expected, expected_priority|
+ test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
+ set_user_from_auth token
+ cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
+ assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
+ assert_equal expected, cr.requesting_container_uuid
+ assert_equal expected_priority, cr.priority
+ end
+ end
+
+ [[{"vcpus" => [2, nil]},
+ lambda { |resolved| resolved["vcpus"] == 2 }],
+ [{"vcpus" => [3, 7]},
+ lambda { |resolved| resolved["vcpus"] == 3 }],
+ [{"vcpus" => 4},
+ lambda { |resolved| resolved["vcpus"] == 4 }],
+ [{"ram" => [1000000000, 2000000000]},
+ lambda { |resolved| resolved["ram"] == 1000000000 }],
+ [{"ram" => [1234234234]},
+ lambda { |resolved| resolved["ram"] == 1234234234 }],
+ ].each do |rc, okfunc|
+ test "resolve runtime constraint range #{rc} to values" do
+ resolved = Container.resolve_runtime_constraints(rc)
+ assert(okfunc.call(resolved),
+ "container runtime_constraints was #{resolved.inspect}")
+ end
+ end
+
+ [[{"/out" => {
+ "kind" => "collection",
+ "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
+ "path" => "/foo"}},
+ lambda do |resolved|
+ resolved["/out"] == {
+ "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
+ "kind" => "collection",
+ "path" => "/foo",
+ }
+ end],
+ [{"/out" => {
+ "kind" => "collection",
+ "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
+ "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
+ "path" => "/foo"}},
+ lambda do |resolved|
+ resolved["/out"] == {
+ "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
+ "kind" => "collection",
+ "path" => "/foo",
+ }
+ end],
+ ].each do |mounts, okfunc|
+ test "resolve mounts #{mounts.inspect} to values" do
+ set_user_from_auth :active
+ resolved = Container.resolve_mounts(mounts)
+ assert(okfunc.call(resolved),
+ "Container.resolve_mounts returned #{resolved.inspect}")
+ end
+ end
+
+ test 'mount unreadable collection' do
+ set_user_from_auth :spectator
+ m = {
+ "/foo" => {
+ "kind" => "collection",
+ "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
+ "path" => "/foo",
+ },
+ }
+ assert_raises(ArvadosModel::UnresolvableContainerError) do
+ Container.resolve_mounts(m)
+ end
+ end
+
+ test 'mount collection with mismatched UUID and PDH' do
+ set_user_from_auth :active
+ m = {
+ "/foo" => {
+ "kind" => "collection",
+ "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
+ "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
+ "path" => "/foo",
+ },
+ }
+ assert_raises(ArgumentError) do
+ Container.resolve_mounts(m)
+ end
+ end
+
+ ['arvados/apitestfixture:latest',
+ 'arvados/apitestfixture',
+ 'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
+ ].each do |tag|
+ test "Container.resolve_container_image(#{tag.inspect})" do
+ set_user_from_auth :active
+ resolved = Container.resolve_container_image(tag)
+ assert_equal resolved, collections(:docker_image).portable_data_hash
+ end
+ end
+
+ test "Container.resolve_container_image(pdh)" do
+ set_user_from_auth :active
+ [[:docker_image, 'v1'], [:docker_image_1_12, 'v2']].each do |coll, ver|
+ Rails.configuration.docker_image_formats = [ver]
+ pdh = collections(coll).portable_data_hash
+ resolved = Container.resolve_container_image(pdh)
+ assert_equal resolved, pdh
+ end
+ end
+
+ ['acbd18db4cc2f85cedef654fccc4a4d8+3',
+ 'ENOEXIST',
+ 'arvados/apitestfixture:ENOEXIST',
+ ].each do |img|
+ test "container_image_for_container(#{img.inspect}) => 422" do
+ set_user_from_auth :active
+ assert_raises(ArvadosModel::UnresolvableContainerError) do
+ Container.resolve_container_image(img)
+ end
+ end
+ end
+
+ test "migrated docker image" do
+ Rails.configuration.docker_image_formats = ['v2']
+ add_docker19_migration_link
+
+ # Test that it returns only v2 images even though request is for v1 image.
+
+ set_user_from_auth :active
+ cr = create_minimal_req!(command: ["true", "1"],
+ container_image: collections(:docker_image).portable_data_hash)
+ assert_equal(Container.resolve_container_image(cr.container_image),
+ collections(:docker_image_1_12).portable_data_hash)
+
+ cr = create_minimal_req!(command: ["true", "2"],
+ container_image: links(:docker_image_collection_tag).name)
+ assert_equal(Container.resolve_container_image(cr.container_image),
+ collections(:docker_image_1_12).portable_data_hash)
+ end
+
+ test "use unmigrated docker image" do
+ Rails.configuration.docker_image_formats = ['v1']
+ add_docker19_migration_link
+
+ # Test that it returns only supported v1 images even though there is a
+ # migration link.
+
+ set_user_from_auth :active
+ cr = create_minimal_req!(command: ["true", "1"],
+ container_image: collections(:docker_image).portable_data_hash)
+ assert_equal(Container.resolve_container_image(cr.container_image),
+ collections(:docker_image).portable_data_hash)
+
+ cr = create_minimal_req!(command: ["true", "2"],
+ container_image: links(:docker_image_collection_tag).name)
+ assert_equal(Container.resolve_container_image(cr.container_image),
+ collections(:docker_image).portable_data_hash)
+ end
+
+ test "incompatible docker image v1" do
+ Rails.configuration.docker_image_formats = ['v1']
+ add_docker19_migration_link
+
+ # Don't return unsupported v2 image even if we ask for it directly.
+ set_user_from_auth :active
+ cr = create_minimal_req!(command: ["true", "1"],
+ container_image: collections(:docker_image_1_12).portable_data_hash)
+ assert_raises(ArvadosModel::UnresolvableContainerError) do
+ Container.resolve_container_image(cr.container_image)
+ end
+ end
+
+ test "incompatible docker image v2" do
+ Rails.configuration.docker_image_formats = ['v2']
+ # No migration link, don't return unsupported v1 image,
+
+ set_user_from_auth :active
+ cr = create_minimal_req!(command: ["true", "1"],
+ container_image: collections(:docker_image).portable_data_hash)
+ assert_raises(ArvadosModel::UnresolvableContainerError) do
+ Container.resolve_container_image(cr.container_image)
+ end
+ cr = create_minimal_req!(command: ["true", "2"],
+ container_image: links(:docker_image_collection_tag).name)
+ assert_raises(ArvadosModel::UnresolvableContainerError) do
+ Container.resolve_container_image(cr.container_image)
+ end
+ end
+
+ test "requestor can retrieve container owned by dispatch" do
+ assert_not_empty Container.readable_by(users(:admin)).where(uuid: containers(:running).uuid)
+ 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},
+ 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)
+ cr2 = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2, command: ["echo", "baz"])
+ 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
+ cr2.reload