9701: Merge branch '9463-change-arvput-use-collection-class' into 9701-collection...
[arvados.git] / services / api / test / functional / arvados / v1 / containers_controller_test.rb
index 208c1ccfdc0c0b1bfae719791205af90f738a10f..cf1f5765b4d2460ae854356088d15bc7c8061add 100644 (file)
@@ -24,7 +24,7 @@ class Arvados::V1::ContainersControllerTest < ActionController::TestCase
   test 'cannot get auth with wrong token' do
     authorize_with :dispatch1
     c = containers(:queued)
-    assert c.update_attributes(state: Container::Locked), show_errors(c)
+    assert c.lock, show_errors(c)
 
     authorize_with :system_user
     get :auth, id: c.uuid
@@ -34,7 +34,7 @@ class Arvados::V1::ContainersControllerTest < ActionController::TestCase
   test 'get auth' do
     authorize_with :dispatch1
     c = containers(:queued)
-    assert c.update_attributes(state: Container::Locked), show_errors(c)
+    assert c.lock, show_errors(c)
     get :auth, id: c.uuid
     assert_response :success
     assert_operator 32, :<, json_response['api_token'].length
@@ -44,80 +44,47 @@ class Arvados::V1::ContainersControllerTest < ActionController::TestCase
   test 'no auth in container response' do
     authorize_with :dispatch1
     c = containers(:queued)
-    assert c.update_attributes(state: Container::Locked), show_errors(c)
+    assert c.lock, show_errors(c)
     get :show, id: c.uuid
     assert_response :success
     assert_nil json_response['auth']
   end
 
-  test "lock and unlock container" do
-    # lock container
+  test "lock container" do
     authorize_with :dispatch1
-    post :lock, {id: containers(:queued).uuid}
+    uuid = containers(:queued).uuid
+    post :lock, {id: uuid}
     assert_response :success
-    container = Container.where(uuid: containers(:queued).uuid).first
+    container = Container.where(uuid: uuid).first
     assert_equal 'Locked', container.state
     assert_not_nil container.locked_by_uuid
     assert_not_nil container.auth_uuid
+  end
 
-    # unlock container
-    @test_counter = 0  # Reset executed action counter
-    @controller = Arvados::V1::ContainersController.new
+  test "unlock container" do
     authorize_with :dispatch1
-    post :unlock, {id: container.uuid}
+    uuid = containers(:locked).uuid
+    post :unlock, {id: uuid}
     assert_response :success
-    container = Container.where(uuid: container.uuid).first
+    container = Container.where(uuid: uuid).first
     assert_equal 'Queued', container.state
     assert_nil container.locked_by_uuid
     assert_nil container.auth_uuid
   end
 
-  def create_new_container attrs={}
-    attrs = {
-      command: ['echo', 'foo'],
-      container_image: 'img',
-      output_path: '/tmp',
-      priority: 1,
-      runtime_constraints: {"vcpus" => 1, "ram" => 1},
-    }
-    c = Container.new attrs.merge(attrs)
-    c.save!
-    cr = ContainerRequest.new attrs.merge(attrs)
-    cr.save!
-    assert cr.update_attributes(container_uuid: c.uuid,
-                                state: ContainerRequest::Committed,
-                               ), show_errors(cr)
-    return c
-  end
-
   [
-    [['Queued', :success], ['Locked', :success]],
-    [['Locked', :success], ['Locked', 422]],
-    [['Locked', :success], ['Queued', :success]],
-    [['Locked', :success], ['Running', :success], ['Queued', 422]],
-  ].each do |transitions|
-    test "lock and unlock state transitions #{transitions}" do
+    [:queued, :lock, :success, 'Locked'],
+    [:queued, :unlock, 422, 'Queued'],
+    [:locked, :lock, 422, 'Locked'],
+    [:running, :lock, 422, 'Running'],
+    [:running, :unlock, 422, 'Running'],
+  ].each do |fixture, action, response, state|
+    test "state transitions from #{fixture } to #{action}" do
       authorize_with :dispatch1
-
-      container = create_new_container()
-
-      transitions.each do |state, status|
-        @test_counter = 0  # Reset executed action counter
-        @controller = Arvados::V1::ContainersController.new
-        authorize_with :dispatch1
-
-        if state == 'Locked'
-          post :lock, {id: container.uuid}
-        elsif state == 'Queued'
-          post :unlock, {id: container.uuid}
-        else
-          container.update_attributes!(state: state)
-        end
-        assert_response status
-
-        container = Container.where(uuid: container['uuid']).first
-        assert_equal state, container.state if status == :success
-      end
+      uuid = containers(fixture).uuid
+      post action, {id: uuid}
+      assert_response response
+      assert_equal state, Container.where(uuid: uuid).first.state
     end
   end
 end