9274: while creating a ContainerRequest, set requesting_container_uuid based on the...
authorradhika <radhika@curoverse.com>
Fri, 10 Jun 2016 17:43:03 +0000 (13:43 -0400)
committerradhika <radhika@curoverse.com>
Fri, 10 Jun 2016 17:43:03 +0000 (13:43 -0400)
services/api/app/models/container_request.rb
services/api/test/fixtures/containers.yml
services/api/test/unit/container_request_test.rb

index 6353132e908baa3d683ec0a9d320ff3a60d55804..83ca3346deed2f907ffabd666317c79293257596 100644 (file)
@@ -18,6 +18,7 @@ class ContainerRequest < ArvadosModel
   validate :validate_state_change
   validate :validate_change
   after_save :update_priority
+  before_create :set_requesting_container_uuid
 
   api_accessible :user, extend: :common do |t|
     t.add :command
@@ -170,4 +171,12 @@ class ContainerRequest < ArvadosModel
     end
   end
 
+  def set_requesting_container_uuid
+    return true if self.requesting_container_uuid   # already set
+
+    token_uuid = current_api_client_authorization.andand.uuid
+    container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
+    self.requesting_container_uuid = container.uuid if container
+    true
+  end
 end
index c049d8498b83386850eadedeb830d8d0f74810f8..8e2e95ce655973fd4bf7b42bbe60ae107f4dd79f 100644 (file)
@@ -131,3 +131,4 @@ requester_container:
   runtime_constraints:
     ram: 12000000000
     vcpus: 4
+  auth_uuid: zzzzz-gj3su-077z32aux8dg2s1
index 701147cf6576997c04bf633650b0770c193136ee..2c7eb76b2cb90d9284185b227905f8ee4499d909 100644 (file)
@@ -368,4 +368,15 @@ class ContainerRequestTest < ActiveSupport::TestCase
     assert_equal 0, c2.priority
   end
 
+  [
+    ['active', 'zzzzz-dz642-requestercntnr1'],
+    ['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