9898: add lock and unlock endpoints to containers_controller.
authorradhika <radhika@curoverse.com>
Fri, 2 Sep 2016 00:42:16 +0000 (20:42 -0400)
committerradhika <radhika@curoverse.com>
Fri, 2 Sep 2016 00:42:16 +0000 (20:42 -0400)
services/api/app/controllers/arvados/v1/containers_controller.rb
services/api/app/models/container.rb
services/api/config/routes.rb
services/api/test/functional/arvados/v1/containers_controller_test.rb
services/api/test/unit/container_test.rb

index 21ee7efa53b5d4008c0f42717095194b9b0c39c6..0da228f081eac9f3c1935c3f3f7e7001b975b8e9 100644 (file)
@@ -19,4 +19,15 @@ class Arvados::V1::ContainersController < ApplicationController
       super
     end
   end
+
+  def lock
+    @object.lock
+    show
+  end
+
+  def unlock
+    reload_object_before_update
+    @object.update_attributes! state: Container::Queued
+    show
+  end
 end
index 4c770083786934abdafe9461f51ee03646396415..a57fbf075418cfcb79981382839a5f756efd4c87 100644 (file)
@@ -76,6 +76,15 @@ class Container < ArvadosModel
     end
   end
 
+  def lock
+    with_lock do
+      if self.state == Queued
+        self.state = Locked
+        self.save!
+      end
+    end
+  end
+
   protected
 
   def fill_field_defaults
index 7bf75800b4af909e9537e845a1d40a43187fea9b..3638c726e9bf1118540243476169db913dbc7e58 100644 (file)
@@ -31,6 +31,8 @@ Server::Application.routes.draw do
       resources :job_tasks
       resources :containers do
         get 'auth', on: :member
+        post 'lock', on: :member
+        post 'unlock', on: :member
       end
       resources :container_requests
       resources :jobs do
index d9f7d96225a651c825544c4ded685d57fcad6777..28ffbbc05a9fdb000bd064462bb31248c5d146d6 100644 (file)
@@ -49,4 +49,26 @@ class Arvados::V1::ContainersControllerTest < ActionController::TestCase
     assert_response :success
     assert_nil json_response['auth']
   end
+
+  test "lock and unlock container" do
+    # lock container
+    authorize_with :dispatch1
+    post :lock, {id: containers(:queued).uuid}
+    assert_response :success
+    container = Container.where(uuid: containers(:queued).uuid).first
+    assert_equal 'Locked', container.state
+    assert_not_nil container.locked_by_uuid
+    assert_not_nil container.auth_uuid
+
+    # unlock container
+    @test_counter = 0  # Reset executed action counter
+    @controller = Arvados::V1::ContainersController.new
+    authorize_with :dispatch1
+    post :unlock, {id: container.uuid}
+    assert_response :success
+    container = Container.where(uuid: container.uuid).first
+    assert_equal 'Queued', container.state
+    assert_nil container.locked_by_uuid
+    assert_nil container.auth_uuid
+  end
 end
index 24186e8ff3c17874e6df39b874531a8090172b80..c0b95f3d258acfda0a9200a1ca37c3ed229626c3 100644 (file)
@@ -112,10 +112,13 @@ class ContainerTest < ActiveSupport::TestCase
     refute c.update_attributes(state: Container::Complete), "not locked"
     c.reload
 
-    assert c.update_attributes(state: Container::Locked), show_errors(c)
+    assert c.lock, show_errors(c)
     assert c.locked_by_uuid
     assert c.auth_uuid
 
+    refute c.lock, "already locked"
+    c.reload
+
     assert c.update_attributes(state: Container::Queued), show_errors(c)
     refute c.locked_by_uuid
     refute c.auth_uuid