8470: Resolve docker image hash or tag to collection PDH when creating a Container.
authorTom Clegg <tom@curoverse.com>
Fri, 24 Jun 2016 17:55:08 +0000 (13:55 -0400)
committerTom Clegg <tom@curoverse.com>
Mon, 27 Jun 2016 19:06:58 +0000 (15:06 -0400)
services/api/app/models/container_request.rb
services/api/test/unit/container_request_test.rb

index 15418f237fd25f9c68b06647836252dbdca3dbf8..e84026a369120d8f9001cc60ec45865716ec4083 100644 (file)
@@ -82,16 +82,16 @@ class ContainerRequest < ArvadosModel
   # Create a new container (or find an existing one) to satisfy this
   # request.
   def resolve
-    # TODO: resolve container_image to a content address.
     c_mounts = mounts_for_container
     c_runtime_constraints = runtime_constraints_for_container
+    c_container_image = container_image_for_container
     c = act_as_system_user do
       Container.create!(command: self.command,
-                        container_image: self.container_image,
                         cwd: self.cwd,
                         environment: self.environment,
-                        mounts: c_mounts,
                         output_path: self.output_path,
+                        container_image: c_container_image,
+                        mounts: c_mounts,
                         runtime_constraints: c_runtime_constraints)
     end
     self.container_uuid = c.uuid
@@ -149,6 +149,15 @@ class ContainerRequest < ArvadosModel
     return c_mounts
   end
 
+  # Return a container_image PDH suitable for a Container.
+  def container_image_for_container
+    coll = Collection.for_latest_docker_image(container_image)
+    if !coll
+      raise ActiveRecord::RecordNotFound.new "docker image #{container_image.inspect} not found"
+    end
+    return coll.portable_data_hash
+  end
+
   def set_container
     if (container_uuid_changed? and
         not current_user.andand.is_admin and
index d8aed7698deaa823bc307e54d53d5f8ce1814b0e..8621daaaf087fdf83fa6fac634b96f8d96422578 100644 (file)
@@ -4,7 +4,7 @@ class ContainerRequestTest < ActiveSupport::TestCase
   def create_minimal_req! attrs={}
     defaults = {
       command: ["echo", "foo"],
-      container_image: "img",
+      container_image: links(:docker_image_collection_tag).name,
       cwd: "/tmp",
       environment: {},
       mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}},
@@ -79,7 +79,7 @@ class ContainerRequestTest < ActiveSupport::TestCase
     c = Container.find_by_uuid cr.container_uuid
     assert_not_nil c
     assert_equal ["echo", "foo"], c.command
-    assert_equal "img", c.container_image
+    assert_equal collections(:docker_image).portable_data_hash, c.container_image
     assert_equal "/tmp", c.cwd
     assert_equal({}, c.environment)
     assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts)
@@ -311,4 +311,37 @@ class ContainerRequestTest < ActiveSupport::TestCase
       cr.send :mounts_for_container
     end
   end
+
+  ['arvados/apitestfixture:latest',
+   'arvados/apitestfixture',
+   'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
+  ].each do |tag|
+    test "container_image_for_container(#{tag.inspect})" do
+      set_user_from_auth :active
+      cr = ContainerRequest.new(container_image: tag)
+      resolved = cr.send :container_image_for_container
+      assert_equal resolved, collections(:docker_image).portable_data_hash
+    end
+  end
+
+  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
+  end
+
+  ['acbd18db4cc2f85cedef654fccc4a4d8+3',
+   'ENOEXIST',
+   'arvados/apitestfixture:ENOEXIST',
+  ].each do |img|
+    test "container_image_for_container(#{img.inspect}) => 404" do
+      set_user_from_auth :active
+      cr = ContainerRequest.new(container_image: img)
+      assert_raises(ActiveRecord::RecordNotFound) do
+        cr.send :container_image_for_container
+      end
+    end
+  end
 end