13996: Migrate tests to new config
[arvados.git] / services / api / test / functional / arvados / v1 / jobs_controller_test.rb
index 1e1425e92b7d27057e89a335c2480b8024b0c444..8bbe96687f72a409e164d03eb3f297ccc5496c6a 100644 (file)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 require 'test_helper'
 require 'helpers/git_test_helper'
 
@@ -7,11 +11,13 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "submit a job" do
     authorize_with :active
-    post :create, job: {
-      script: "hash",
-      script_version: "master",
-      repository: "active/foo",
-      script_parameters: {}
+    post :create, params: {
+      job: {
+        script: "hash",
+        script_version: "master",
+        repository: "active/foo",
+        script_parameters: {}
+      }
     }
     assert_response :success
     assert_not_nil assigns(:object)
@@ -23,17 +29,19 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "normalize output and log uuids when creating job" do
     authorize_with :active
-    post :create, job: {
-      script: "hash",
-      script_version: "master",
-      script_parameters: {},
-      repository: "active/foo",
-      started_at: Time.now,
-      finished_at: Time.now,
-      running: false,
-      success: true,
-      output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
-      log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
+    post :create, params: {
+      job: {
+        script: "hash",
+        script_version: "master",
+        script_parameters: {},
+        repository: "active/foo",
+        started_at: Time.now,
+        finished_at: Time.now,
+        running: false,
+        success: true,
+        output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
+        log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
+      }
     }
     assert_response :success
     assert_not_nil assigns(:object)
@@ -54,7 +62,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
     new_output = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
     new_log = 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
-    put :update, {
+    put :update, params: {
       id: foobar_job['uuid'],
       job: {
         output: new_output,
@@ -75,12 +83,12 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
     # We need to verify that "cancel" creates a trigger file, so first
     # let's make sure there is no stale trigger file.
     begin
-      File.unlink(Rails.configuration.crunch_refresh_trigger)
+      File.unlink(Rails.configuration.Containers["JobsAPI"]["CrunchRefreshTrigger"])
     rescue Errno::ENOENT
     end
 
     authorize_with :active
-    put :update, {
+    put :update, params: {
       id: jobs(:running).uuid,
       job: {
         cancelled_at: 4.day.ago
@@ -97,7 +105,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
                  'server should correct bogus cancelled_at ' +
                  job['cancelled_at'])
     assert_equal(true,
-                 File.exists?(Rails.configuration.crunch_refresh_trigger),
+                 File.exist?(Rails.configuration.Containers["JobsAPI"]["CrunchRefreshTrigger"]),
                  'trigger file should be created when job is cancelled')
   end
 
@@ -115,12 +123,12 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
       # We need to verify that "cancel" creates a trigger file, so first
       # let's make sure there is no stale trigger file.
       begin
-        File.unlink(Rails.configuration.crunch_refresh_trigger)
+        File.unlink(Rails.configuration.Containers["JobsAPI"]["CrunchRefreshTrigger"])
       rescue Errno::ENOENT
       end
 
       authorize_with :active
-      self.send http_method, action, { id: jobs(:cancelled).uuid }.merge(params)
+      self.send http_method, action, params: { id: jobs(:cancelled).uuid }.merge(params)
       assert_response expected_response
       if expected_response == :success
         job = json_response
@@ -136,12 +144,12 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
     # We need to verify that "cancel" creates a trigger file, so first
     # let's make sure there is no stale trigger file.
     begin
-      File.unlink(Rails.configuration.crunch_refresh_trigger)
+      File.unlink(Rails.configuration.Containers["JobsAPI"]["CrunchRefreshTrigger"])
     rescue Errno::ENOENT
     end
 
     authorize_with :active
-    put :update, {
+    put :update, params: {
       id: jobs(:running_cancelled).uuid,
       job: {
         cancelled_at: nil
@@ -153,7 +161,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
   ['abc.py', 'hash.py'].each do |script|
     test "update job script attribute to #{script} without failing script_version check" do
       authorize_with :admin
-      put :update, {
+      put :update, params: {
         id: jobs(:uses_nonexistent_script_version).uuid,
         job: {
           script: script
@@ -167,7 +175,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by uuid with >= query" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
     }
     assert_response :success
@@ -178,7 +186,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by uuid with <= query" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
     }
     assert_response :success
@@ -189,7 +197,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by uuid with >= and <= query" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
               ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
     }
@@ -200,7 +208,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by uuid with < query" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
     }
     assert_response :success
@@ -211,7 +219,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by uuid with like query" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['uuid', 'like', '%hmckwoma9pl%']]
     }
     assert_response :success
@@ -221,7 +229,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by uuid with 'in' query" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
                                 'zzzzz-8i9sb-pshmckwoma9plh7']]]
     }
@@ -235,7 +243,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
     exclude_uuids = [jobs(:running).uuid,
                      jobs(:running_cancelled).uuid]
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['uuid', 'not in', exclude_uuids]]
     }
     assert_response :success
@@ -250,7 +258,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
      ['output', nil]].each do |attr, operand|
       test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
         authorize_with :active
-        get :index, {
+        get :index, params: {
           filters: [[attr, operator, operand]]
         }
         assert_response :success
@@ -267,7 +275,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by started_at with < query" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['started_at', '<', Time.now.to_s]]
     }
     assert_response :success
@@ -277,7 +285,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by started_at with > query" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['started_at', '>', Time.now.to_s]]
     }
     assert_response :success
@@ -286,7 +294,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by started_at with >= query on metric date" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['started_at', '>=', '2014-01-01']]
     }
     assert_response :success
@@ -296,7 +304,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by started_at with >= query on metric date and time" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['started_at', '>=', '2014-01-01 01:23:45']]
     }
     assert_response :success
@@ -306,7 +314,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs with 'any' operator" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       where: { any: ['contains', 'pshmckw'] }
     }
     assert_response :success
@@ -317,7 +325,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "search jobs by nonexistent column with < query" do
     authorize_with :active
-    get :index, {
+    get :index, params: {
       filters: [['is_borked', '<', 'fizzbuzz']]
     }
     assert_response 422
@@ -325,7 +333,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "finish a job" do
     authorize_with :active
-    put :update, {
+    put :update, params: {
       id: jobs(:nearly_finished_job).uuid,
       job: {
         output: '551392cc37a317abf865b95f66f4ef94+101',
@@ -350,7 +358,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "get job queue as with a = filter" do
     authorize_with :admin
-    get :queue, { filters: [['script','=','foo']] }
+    get :queue, params: { filters: [['script','=','foo']] }
     assert_response :success
     assert_equal ['foo'], assigns(:objects).collect(&:script).uniq
     assert_equal 0, assigns(:objects)[0].queue_position
@@ -358,7 +366,7 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "get job queue as with a != filter" do
     authorize_with :admin
-    get :queue, { filters: [['script','!=','foo']] }
+    get :queue, params: { filters: [['script','!=','foo']] }
     assert_response :success
     assert_equal 0, assigns(:objects).count
   end
@@ -374,14 +382,14 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "job includes assigned nodes" do
     authorize_with :active
-    get :show, {id: jobs(:nearly_finished_job).uuid}
+    get :show, params: {id: jobs(:nearly_finished_job).uuid}
     assert_response :success
     assert_equal([nodes(:busy).uuid], json_response["node_uuids"])
   end
 
   test "job lock success" do
     authorize_with :active
-    post :lock, {id: jobs(:queued).uuid}
+    post :lock, params: {id: jobs(:queued).uuid}
     assert_response :success
     job = Job.where(uuid: jobs(:queued).uuid).first
     assert_equal "Running", job.state
@@ -389,19 +397,21 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test "job lock conflict" do
     authorize_with :active
-    post :lock, {id: jobs(:running).uuid}
-    assert_response 403 # forbidden
+    post :lock, params: {id: jobs(:running).uuid}
+    assert_response 422 # invalid state transition
   end
 
   test 'reject invalid commit in remote repository' do
     authorize_with :active
     url = "http://localhost:1/fake/fake.git"
     fetch_remote_from_local_repo url, :foo
-    post :create, job: {
-      script: "hash",
-      script_version: "abc123",
-      repository: url,
-      script_parameters: {}
+    post :create, params: {
+      job: {
+        script: "hash",
+        script_version: "abc123",
+        repository: url,
+        script_parameters: {}
+      }
     }
     assert_response 422
   end
@@ -410,11 +420,13 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
     authorize_with :active
     url = "http://localhost:1/fake/fake.git"
     fetch_remote_from_local_repo url, :foo
-    post :create, job: {
-      script: "hash",
-      script_version: "master",
-      repository: url,
-      script_parameters: {}
+    post :create, params: {
+      job: {
+        script: "hash",
+        script_version: "master",
+        repository: url,
+        script_parameters: {}
+      }
     }
     assert_response :success
     assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
@@ -423,14 +435,62 @@ class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
   test 'tag local commit in internal repository' do
     authorize_with :active
-    post :create, job: {
-      script: "hash",
-      script_version: "master",
-      repository: "active/foo",
-      script_parameters: {}
+    post :create, params: {
+      job: {
+        script: "hash",
+        script_version: "master",
+        repository: "active/foo",
+        script_parameters: {}
+      }
     }
     assert_response :success
     assert_equal('077ba2ad3ea24a929091a9e6ce545c93199b8e57',
                  internal_tag(json_response['uuid']))
   end
+
+  test 'get job with components' do
+    authorize_with :active
+    get :show, params: {id: jobs(:running_job_with_components).uuid}
+    assert_response :success
+    assert_not_nil json_response["components"]
+    assert_equal ["component1", "component2"], json_response["components"].keys
+  end
+
+  [
+    [:active, :success],
+    [:system_user, :success],
+    [:admin, 403],
+  ].each do |user, expected|
+    test "add components to job locked by active user as #{user} user and expect #{expected}" do
+      authorize_with user
+      put :update, params: {
+        id: jobs(:running).uuid,
+        job: {
+          components: {"component1" => "value1", "component2" => "value2"}
+        }
+      }
+      assert_response expected
+      if expected == :success
+        assert_not_nil json_response["components"]
+        keys = json_response["components"].keys
+        assert_equal ["component1", "component2"], keys
+        assert_equal "value1", json_response["components"][keys[0]]
+      end
+    end
+  end
+
+  test 'jobs.create disabled in config' do
+    Rails.configuration.API["DisabledAPIs"] = ["jobs.create",
+                                               "pipeline_instances.create"]
+    authorize_with :active
+    post :create, params: {
+      job: {
+        script: "hash",
+        script_version: "master",
+        repository: "active/foo",
+        script_parameters: {}
+      }
+    }
+    assert_response 404
+  end
 end