Merge branch 'master' into 9372-container-display
[arvados.git] / services / api / test / unit / container_request_test.rb
index 1248475ffa12418413d085d3071b7bf64d2f868a..df89b93bf4bba59b90bd11e0cff3afd41b921739 100644 (file)
@@ -74,68 +74,309 @@ class ContainerRequestTest < ActiveSupport::TestCase
 
   test "Container request create" do
     set_user_from_auth :active_trustedclient
-    c = ContainerRequest.new
-    c.command = ["echo", "foo"]
-    c.container_image = "img"
-    c.cwd = "/tmp"
-    c.environment = {}
-    c.mounts = {"BAR" => "FOO"}
-    c.output_path = "/tmp"
-    c.priority = 1
-    c.runtime_constraints = {}
-    c.name = "foo"
-    c.description = "bar"
-    c.save!
-
-    assert_nil c.container_uuid
-
-    check_bogus_states c
+    cr = ContainerRequest.new
+    cr.command = ["echo", "foo"]
+    cr.container_image = "img"
+    cr.cwd = "/tmp"
+    cr.environment = {}
+    cr.mounts = {"BAR" => "FOO"}
+    cr.output_path = "/tmpout"
+    cr.runtime_constraints = {}
+    cr.name = "foo"
+    cr.description = "bar"
+    cr.save!
 
-    c.reload
-    c.command = ["echo", "foo3"]
-    c.container_image = "img3"
-    c.cwd = "/tmp3"
-    c.environment = {"BUP" => "BOP"}
-    c.mounts = {"BAR" => "BAZ"}
-    c.output_path = "/tmp4"
-    c.priority = 2
-    c.runtime_constraints = {"X" => "Y"}
-    c.name = "foo3"
-    c.description = "bar3"
-    c.save!
-
-    assert_nil c.container_uuid
+    assert_nil cr.container_uuid
+    assert_nil cr.priority
+
+    check_bogus_states cr
+
+    cr.reload
+    cr.command = ["echo", "foo3"]
+    cr.container_image = "img3"
+    cr.cwd = "/tmp3"
+    cr.environment = {"BUP" => "BOP"}
+    cr.mounts = {"BAR" => "BAZ"}
+    cr.output_path = "/tmp4"
+    cr.priority = 2
+    cr.runtime_constraints = {"X" => "Y"}
+    cr.name = "foo3"
+    cr.description = "bar3"
+    cr.save!
+
+    assert_nil cr.container_uuid
+  end
+
+  test "Container request priority must be non-nil" do
+    set_user_from_auth :active_trustedclient
+    cr = ContainerRequest.new
+    cr.command = ["echo", "foo"]
+    cr.container_image = "img"
+    cr.cwd = "/tmp"
+    cr.environment = {}
+    cr.mounts = {"BAR" => "FOO"}
+    cr.output_path = "/tmpout"
+    cr.runtime_constraints = {}
+    cr.name = "foo"
+    cr.description = "bar"
+    cr.save!
+
+    cr.reload
+    cr.state = "Committed"
+    assert_raises(ActiveRecord::RecordInvalid) do
+      cr.save!
+    end
   end
 
   test "Container request commit" do
     set_user_from_auth :active_trustedclient
-    c = ContainerRequest.new
-    c.command = ["echo", "foo"]
-    c.container_image = "img"
-    c.cwd = "/tmp"
-    c.environment = {}
-    c.mounts = {"BAR" => "FOO"}
-    c.output_path = "/tmp"
-    c.priority = 1
-    c.runtime_constraints = {}
-    c.name = "foo"
-    c.description = "bar"
-    c.save!
+    cr = ContainerRequest.new
+    cr.command = ["echo", "foo"]
+    cr.container_image = "img"
+    cr.cwd = "/tmp"
+    cr.environment = {}
+    cr.mounts = {"BAR" => "FOO"}
+    cr.output_path = "/tmpout"
+    cr.priority = 1
+    cr.runtime_constraints = {}
+    cr.name = "foo"
+    cr.description = "bar"
+    cr.save!
+
+    cr.reload
+    assert_nil cr.container_uuid
+
+    cr.reload
+    cr.state = "Committed"
+    cr.save!
+
+    cr.reload
+
+    c = Container.find_by_uuid cr.container_uuid
+    assert_equal ["echo", "foo"], c.command
+    assert_equal "img", c.container_image
+    assert_equal "/tmp", c.cwd
+    assert_equal({}, c.environment)
+    assert_equal({"BAR" => "FOO"}, c.mounts)
+    assert_equal "/tmpout", c.output_path
+    assert_equal({}, c.runtime_constraints)
+    assert_equal 1, c.priority
+
+    assert_raises(ActiveRecord::RecordInvalid) do
+      cr.priority = nil
+      cr.save!
+    end
+
+    cr.priority = 0
+    cr.save!
+
+    cr.reload
+    c.reload
+    assert_equal 0, cr.priority
+    assert_equal 0, c.priority
+
+  end
+
+
+  test "Container request max priority" do
+    set_user_from_auth :active_trustedclient
+    cr = ContainerRequest.new
+    cr.state = "Committed"
+    cr.container_image = "img"
+    cr.command = ["foo", "bar"]
+    cr.output_path = "/tmp"
+    cr.cwd = "/tmp"
+    cr.priority = 5
+    cr.save!
+
+    c = Container.find_by_uuid cr.container_uuid
+    assert_equal 5, c.priority
+
+    cr2 = ContainerRequest.new
+    cr2.container_image = "img"
+    cr2.command = ["foo", "bar"]
+    cr2.output_path = "/tmp"
+    cr2.cwd = "/tmp"
+    cr2.priority = 10
+    cr2.save!
+
+    act_as_system_user do
+      cr2.state = "Committed"
+      cr2.container_uuid = cr.container_uuid
+      cr2.save!
+    end
 
     c.reload
-    assert_nil c.container_uuid
+    assert_equal 10, c.priority
+
+    cr2.reload
+    cr2.priority = 0
+    cr2.save!
 
     c.reload
-    c.state = "Committed"
-    c.save!
+    assert_equal 5, c.priority
+
+    cr.reload
+    cr.priority = 0
+    cr.save!
 
     c.reload
+    assert_equal 0, c.priority
 
-    t = Container.find_by_uuid c.container_uuid
-    assert_equal c.command, t.command
-    assert_equal c.container_image, t.container_image
-    assert_equal c.cwd, t.cwd
   end
 
 
+  test "Independent container requests" do
+    set_user_from_auth :active_trustedclient
+    cr = ContainerRequest.new
+    cr.state = "Committed"
+    cr.container_image = "img"
+    cr.command = ["foo", "bar"]
+    cr.output_path = "/tmp"
+    cr.cwd = "/tmp"
+    cr.priority = 5
+    cr.save!
+
+    cr2 = ContainerRequest.new
+    cr2.state = "Committed"
+    cr2.container_image = "img"
+    cr2.command = ["foo", "bar"]
+    cr2.output_path = "/tmp"
+    cr2.cwd = "/tmp"
+    cr2.priority = 10
+    cr2.save!
+
+    c = Container.find_by_uuid cr.container_uuid
+    assert_equal 5, c.priority
+
+    c2 = Container.find_by_uuid cr2.container_uuid
+    assert_equal 10, c2.priority
+
+    cr.priority = 0
+    cr.save!
+
+    c.reload
+    assert_equal 0, c.priority
+
+    c2.reload
+    assert_equal 10, c2.priority
+  end
+
+
+  test "Container cancelled finalizes request" do
+    set_user_from_auth :active_trustedclient
+    cr = ContainerRequest.new
+    cr.state = "Committed"
+    cr.container_image = "img"
+    cr.command = ["foo", "bar"]
+    cr.output_path = "/tmp"
+    cr.cwd = "/tmp"
+    cr.priority = 5
+    cr.save!
+
+    cr.reload
+    assert_equal "Committed", cr.state
+
+    c = Container.find_by_uuid cr.container_uuid
+    assert_equal "Queued", c.state
+
+    act_as_system_user do
+      c.state = "Cancelled"
+      c.save!
+    end
+
+    cr.reload
+    assert_equal "Final", cr.state
+
+  end
+
+
+  test "Container complete finalizes request" do
+    set_user_from_auth :active_trustedclient
+    cr = ContainerRequest.new
+    cr.state = "Committed"
+    cr.container_image = "img"
+    cr.command = ["foo", "bar"]
+    cr.output_path = "/tmp"
+    cr.cwd = "/tmp"
+    cr.priority = 5
+    cr.save!
+
+    cr.reload
+    assert_equal "Committed", cr.state
+
+    c = Container.find_by_uuid cr.container_uuid
+    assert_equal Container::Queued, c.state
+
+    act_as_system_user do
+      c.update_attributes! state: Container::Locked
+      c.update_attributes! state: Container::Running
+    end
+
+    cr.reload
+    assert_equal "Committed", cr.state
+
+    act_as_system_user do
+      c.update_attributes! state: Container::Complete
+      c.save!
+    end
+
+    cr.reload
+    assert_equal "Final", cr.state
+
+  end
+
+  test "Container makes container request, then is cancelled" do
+    set_user_from_auth :active_trustedclient
+    cr = ContainerRequest.new
+    cr.state = "Committed"
+    cr.container_image = "img"
+    cr.command = ["foo", "bar"]
+    cr.output_path = "/tmp"
+    cr.cwd = "/tmp"
+    cr.priority = 5
+    cr.save!
+
+    c = Container.find_by_uuid cr.container_uuid
+    assert_equal 5, c.priority
+
+    cr2 = ContainerRequest.new
+    cr2.state = "Committed"
+    cr2.container_image = "img"
+    cr2.command = ["foo", "bar"]
+    cr2.output_path = "/tmp"
+    cr2.cwd = "/tmp"
+    cr2.priority = 10
+    cr2.requesting_container_uuid = c.uuid
+    cr2.save!
+
+    c2 = Container.find_by_uuid cr2.container_uuid
+    assert_equal 10, c2.priority
+
+    act_as_system_user do
+      c.state = "Cancelled"
+      c.save!
+    end
+
+    cr.reload
+    assert_equal "Final", cr.state
+
+    cr2.reload
+    assert_equal 0, cr2.priority
+
+    c2.reload
+    assert_equal 0, c2.priority
+  end
+
+  [
+    ['active', 'zzzzz-dz642-runningcontainr'],
+    ['active_no_prefs', nil],
+  ].each do |token, expected|
+    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
+    end
+  end
 end