X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/b86728d23fa94fa80460be443e3e963376bc47d9..9ec15072e6631578584bd9c08d26025e807a8d48:/services/api/test/unit/container_test.rb diff --git a/services/api/test/unit/container_test.rb b/services/api/test/unit/container_test.rb index 5a19f05ee4..eb4f35fea3 100644 --- a/services/api/test/unit/container_test.rb +++ b/services/api/test/unit/container_test.rb @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + require 'test_helper' class ContainerTest < ActiveSupport::TestCase @@ -11,14 +15,22 @@ class ContainerTest < ActiveSupport::TestCase runtime_constraints: {"vcpus" => 1, "ram" => 1}, } - REUSABLE_COMMON_ATTRS = {container_image: "9ae44d5792468c58bcf85ce7353c7027+124", - cwd: "test", - command: ["echo", "hello"], - output_path: "test", - runtime_constraints: {"vcpus" => 4, - "ram" => 12000000000}, - mounts: {"test" => {"kind" => "json"}}, - environment: {"var" => 'val'}} + REUSABLE_COMMON_ATTRS = { + container_image: "9ae44d5792468c58bcf85ce7353c7027+124", + cwd: "test", + command: ["echo", "hello"], + output_path: "test", + runtime_constraints: { + "ram" => 12000000000, + "vcpus" => 4, + }, + mounts: { + "test" => {"kind" => "json"}, + }, + environment: { + "var" => "val", + }, + } def minimal_new attrs={} cr = ContainerRequest.new DEFAULT_ATTRS.merge(attrs) @@ -83,10 +95,46 @@ class ContainerTest < ActiveSupport::TestCase end end + test "Container valid priority" do + act_as_system_user do + c, _ = minimal_new(environment: {}, + mounts: {"BAR" => "FOO"}, + output_path: "/tmp", + priority: 1, + runtime_constraints: {"vcpus" => 1, "ram" => 1}) + + assert_raises(ActiveRecord::RecordInvalid) do + c.priority = -1 + c.save! + end + + c.priority = 0 + c.save! + + c.priority = 1 + c.save! + + c.priority = 500 + c.save! + + c.priority = 999 + c.save! + + c.priority = 1000 + c.save! + + assert_raises(ActiveRecord::RecordInvalid) do + c.priority = 1001 + c.save! + end + end + end + + test "Container serialized hash attributes sorted before save" do env = {"C" => 3, "B" => 2, "A" => 1} m = {"F" => {"kind" => 3}, "E" => {"kind" => 2}, "D" => {"kind" => 1}} - rc = {"vcpus" => 1, "ram" => 1} + rc = {"vcpus" => 1, "ram" => 1, "keep_cache_ram" => 1} c, _ = minimal_new(environment: env, mounts: m, runtime_constraints: rc) assert_equal c.environment.to_json, Container.deep_sort_hash(env).to_json assert_equal c.mounts.to_json, Container.deep_sort_hash(m).to_json @@ -149,21 +197,21 @@ class ContainerTest < ActiveSupport::TestCase log: 'ea10d51bcf88862dbcc36eb292017dfd+45', } - set_user_from_auth :dispatch1 - - c_output1 = Container.create common_attrs - c_output2 = Container.create common_attrs - assert_not_equal c_output1.uuid, c_output2.uuid - cr = ContainerRequest.new common_attrs + cr.use_existing = false cr.state = ContainerRequest::Committed - cr.container_uuid = c_output1.uuid cr.save! + c_output1 = Container.where(uuid: cr.container_uuid).first cr = ContainerRequest.new common_attrs + cr.use_existing = false cr.state = ContainerRequest::Committed - cr.container_uuid = c_output2.uuid cr.save! + c_output2 = Container.where(uuid: cr.container_uuid).first + + assert_not_equal c_output1.uuid, c_output2.uuid + + set_user_from_auth :dispatch1 out1 = '1f4b0bc7583c2a7f9102c395f4ffc5e3+45' log1 = collections(:real_log_collection).portable_data_hash @@ -176,9 +224,8 @@ class ContainerTest < ActiveSupport::TestCase c_output2.update_attributes!({state: Container::Running}) c_output2.update_attributes!(completed_attrs.merge({log: log1, output: out2})) - reused = Container.find_reusable(common_attrs) - assert_not_nil reused - assert_equal reused.uuid, c_output1.uuid + reused = Container.resolve(ContainerRequest.new(common_attrs)) + assert_equal c_output1.uuid, reused.uuid end test "find_reusable method should select running container by start date" do @@ -333,6 +380,19 @@ class ContainerTest < ActiveSupport::TestCase assert_nil reused end + test "find_reusable with logging disabled" do + set_user_from_auth :active + Rails.logger.expects(:info).never + Container.find_reusable(REUSABLE_COMMON_ATTRS) + end + + test "find_reusable with logging enabled" do + set_user_from_auth :active + Rails.configuration.log_reuse_decisions = true + Rails.logger.expects(:info).at_least(3) + Container.find_reusable(REUSABLE_COMMON_ATTRS) + end + test "Container running" do c, _ = minimal_new priority: 1 @@ -358,7 +418,10 @@ class ContainerTest < ActiveSupport::TestCase set_user_from_auth :dispatch1 assert_equal Container::Queued, c.state - assert_raise(ActiveRecord::RecordInvalid) {c.lock} # "no priority" + assert_raise(ArvadosModel::LockFailedError) do + # "no priority" + c.lock + end c.reload assert cr.update_attributes priority: 1 @@ -371,7 +434,7 @@ class ContainerTest < ActiveSupport::TestCase assert c.locked_by_uuid assert c.auth_uuid - assert_raise(ArvadosModel::AlreadyLockedError) {c.lock} + assert_raise(ArvadosModel::LockFailedError) {c.lock} c.reload assert c.unlock, show_errors(c) @@ -390,9 +453,15 @@ class ContainerTest < ActiveSupport::TestCase auth_uuid_was = c.auth_uuid - assert_raise(ActiveRecord::RecordInvalid) {c.lock} # Running to Locked is not allowed + assert_raise(ArvadosModel::LockFailedError) do + # Running to Locked is not allowed + c.lock + end c.reload - assert_raise(ActiveRecord::RecordInvalid) {c.unlock} # Running to Queued is not allowed + assert_raise(ArvadosModel::InvalidStateTransitionError) do + # Running to Queued is not allowed + c.unlock + end c.reload assert c.update_attributes(state: Container::Complete), show_errors(c) @@ -410,6 +479,10 @@ class ContainerTest < ActiveSupport::TestCase check_no_change_from_cancelled c end + test "Container queued count" do + assert_equal 1, Container.readable_by(users(:active)).where(state: "Queued").count + end + test "Container locked cancel" do c, _ = minimal_new set_user_from_auth :dispatch1 @@ -418,6 +491,17 @@ class ContainerTest < ActiveSupport::TestCase check_no_change_from_cancelled c end + test "Container locked cancel with log" do + c, _ = minimal_new + set_user_from_auth :dispatch1 + assert c.lock, show_errors(c) + assert c.update_attributes( + state: Container::Cancelled, + log: collections(:real_log_collection).portable_data_hash, + ), show_errors(c) + check_no_change_from_cancelled c + end + test "Container running cancel" do c, _ = minimal_new set_user_from_auth :dispatch1