X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/4fd40fe19bb3837efe50971b5d9359b0c1c87c93..HEAD:/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 98136aa53b..4ae914f4ad 100644 --- a/services/api/test/unit/container_request_test.rb +++ b/services/api/test/unit/container_request_test.rb @@ -14,19 +14,16 @@ class ContainerRequestTest < ActiveSupport::TestCase def with_container_auth(ctr) auth_was = Thread.current[:api_client_authorization] - client_was = Thread.current[:api_client] token_was = Thread.current[:token] user_was = Thread.current[:user] auth = ApiClientAuthorization.find_by_uuid(ctr.auth_uuid) Thread.current[:api_client_authorization] = auth - Thread.current[:api_client] = auth.api_client Thread.current[:token] = auth.token Thread.current[:user] = auth.user begin yield ensure Thread.current[:api_client_authorization] = auth_was - Thread.current[:api_client] = client_was Thread.current[:token] = token_was Thread.current[:user] = user_was end @@ -112,11 +109,15 @@ class ContainerRequestTest < ActiveSupport::TestCase {"mounts" => {"FOO" => {}}}, {"mounts" => {"FOO" => {"kind" => "tmp", "capacity" => 42.222}}}, {"command" => ["echo", 55]}, - {"environment" => {"FOO" => 55}} + {"environment" => {"FOO" => 55}}, + {"output_glob" => [false]}, + {"output_glob" => [["bad"]]}, + {"output_glob" => "bad"}, + {"output_glob" => ["nope", -1]}, ].each do |value| test "Create with invalid #{value}" do set_user_from_auth :active - assert_raises(ActiveRecord::RecordInvalid) do + assert_raises(ActiveRecord::RecordInvalid, Serializer::TypeMismatch) do cr = create_minimal_req!({state: "Committed", priority: 1}.merge(value)) cr.save! @@ -127,7 +128,7 @@ class ContainerRequestTest < ActiveSupport::TestCase set_user_from_auth :active cr = create_minimal_req!(state: "Uncommitted", priority: 1) cr.save! - assert_raises(ActiveRecord::RecordInvalid) do + assert_raises(ActiveRecord::RecordInvalid, Serializer::TypeMismatch) do cr = ContainerRequest.find_by_uuid cr.uuid cr.update!({state: "Committed", priority: 1}.merge(value)) @@ -1131,13 +1132,13 @@ class ContainerRequestTest < ActiveSupport::TestCase assert_equal ContainerRequest::Final, cr.state output_coll = Collection.find_by_uuid(cr.output_uuid) # Make sure the resulting output collection name include the original name - # plus the date + # plus the last 15 characters of uuid assert_not_equal output_name, output_coll.name, "more than one collection with the same owner and name" assert output_coll.name.include?(output_name), "New name should include original name" - assert_match /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z/, output_coll.name, - "New name should include ISO8601 date" + assert_match /#{output_coll.uuid[-15..-1]}/, output_coll.name, + "New name should include last 15 characters of uuid" end [[0, :check_output_ttl_0], @@ -1827,4 +1828,158 @@ class ContainerRequestTest < ActiveSupport::TestCase assert_equal 3+7+9, cr.cumulative_cost end + test "Service cannot use existing container" do + set_user_from_auth :active + cr = create_minimal_req! + cr.service = true + cr.use_existing = true + cr.state = "Committed" + assert_raises(ActiveRecord::RecordInvalid) do + cr.save! + end + end + + test "published_ports validation" do + set_user_from_auth :active + cr = create_minimal_req! + cr.use_existing = false + + # Bad port number + cr.service = true + cr.published_ports = { + "9000000" => { + "access" => "public", + "label" => "stuff", + "initial_path" => "", + } + } + assert_raises(ActiveRecord::RecordInvalid) do + cr.save! + end + + # Not a hash + cr.published_ports = { + "9000" => "" + } + assert_raises(ActiveRecord::RecordInvalid) do + cr.save! + end + + # empty hash + cr.published_ports = { + "9000" => { + } + } + assert_raises(ActiveRecord::RecordInvalid) do + cr.save! + end + + # missing access + cr.published_ports = { + "9000" => { + "label" => "stuff", + "initial_path" => "", + } + } + assert_raises(ActiveRecord::RecordInvalid) do + cr.save! + end + + # invalid access + cr.published_ports = { + "9000" => { + "access" => "peanuts", + "label" => "stuff", + "initial_path" => "", + } + } + assert_raises(ActiveRecord::RecordInvalid) do + cr.save! + end + + # missing label + cr.published_ports = { + "9000" => { + "access" => "public", + "initial_path" => "", + } + } + assert_raises(ActiveRecord::RecordInvalid) do + cr.save! + end + + # empty label + cr.published_ports = { + "9000" => { + "access" => "public", + "label" => "", + "initial_path" => "", + } + } + assert_raises(ActiveRecord::RecordInvalid) do + cr.save! + end + + # Missing initial_path + cr.published_ports = { + "9000" => { + "access" => "public", + "label" => "stuff", + } + } + assert_raises(ActiveRecord::RecordInvalid) do + cr.save! + end + + # All good! + cr.published_ports = { + "9000" => { + "access" => "public", + "label" => "stuff", + "initial_path" => "", + } + } + cr.save! + end + + test "container request in a project with trash_at in the future" do + # Tests edge case where a container request is created in a + # project which has trash_at set in the future. + # + # A user actually encountered this in the wild, they created a + # temporary project to run some tests and set it expire + # automatically as a cleanup operation. However, because of bug + # #22768, the containers were assigned priority 0. + # + # This tests that the behavior now works as intended, which is the + # container has nonzero priority while the project remains live, + # and then goes to zero once trash_at has passed. + + set_user_from_auth :active + + project = Group.create!(group_class: "project", name: "trashed_project", trash_at: Time.now+5.minutes) + + cr = create_minimal_req!({state: "Committed", priority: 500, owner_uuid: project.uuid}) + + assert_equal 500, cr.priority + + c = Container.find_by_uuid cr.container_uuid + + # Nonzero priority, which means runnable, because the project + # isn't trashed yet + assert_operator c.priority, :>, 0 + + project.trash_at = Time.now + project.save! + + c.reload + + # Project is now trashed, so the container has zero priority, + # which means it won't run and will be cancelled if it was already + # running. + assert_equal 0, c.priority + end + + + end