The bleeding edge branch is now called 'main'.
[arvados.git] / services / api / test / functional / arvados / v1 / job_reuse_controller_test.rb
index 16916b7313f1ad1b448875ec864e5fa37f27ff8f..46cfac5c9a841899f3267788141bb8c2163f12a0 100644 (file)
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 require 'test_helper'
-load 'test/functional/arvados/v1/git_setup.rb'
+require 'helpers/git_test_helper'
 
 class Arvados::V1::JobReuseControllerTest < ActionController::TestCase
   fixtures :repositories, :users, :jobs, :links, :collections
 
-  # See git_setup.rb for the commit log for test.git.tar
-  include GitSetup
-
-  test "test_reuse_job" do
+  setup do
     @controller = Arvados::V1::JobsController.new
     authorize_with :active
-    post :create, job: {
-      script: "hash",
-      script_version: "4fe459abe02d9b365932b8f5dc419439ab4e2577",
-      repository: "foo",
-      script_parameters: {
-        input: 'fa7aeb5140e2848d39b416daeef4ffc5+45',
-        an_integer: '1'
-      }
-    }
-    assert_response :success
-    assert_not_nil assigns(:object)
-    new_job = JSON.parse(@response.body)
-    assert_equal 'zzzzz-8i9sb-cjs4pklxxjykqqq', new_job['uuid']
-    assert_equal '4fe459abe02d9b365932b8f5dc419439ab4e2577', new_job['script_version']
   end
 
-  test "test_cannot_reuse_job_no_output" do
-    @controller = Arvados::V1::JobsController.new
-    authorize_with :active
-    post :create, job: {
-      script: "hash",
-      script_version: "4fe459abe02d9b365932b8f5dc419439ab4e2577",
-      repository: "foo",
-      script_parameters: {
-        input: 'fa7aeb5140e2848d39b416daeef4ffc5+45',
-        an_integer: '2'
-      }
-    }
-    assert_response :success
-    assert_not_nil assigns(:object)
-    new_job = JSON.parse(@response.body)
-    assert_not_equal 'zzzzz-8i9sb-cjs4pklxxjykppp', new_job['uuid']
-  end
+  BASE_FILTERS = {
+    'repository' => ['=', 'active/foo'],
+    'script' => ['=', 'hash'],
+    'script_version' => ['in git', 'main'],
+    'docker_image_locator' => ['=', nil],
+    'arvados_sdk_version' => ['=', nil],
+  }
 
-  test "test_reuse_job_range" do
-    @controller = Arvados::V1::JobsController.new
-    authorize_with :active
-    post :create, job: {
-      script: "hash",
-      minimum_script_version: "tag1",
-      script_version: "master",
-      repository: "foo",
-      script_parameters: {
-        input: 'fa7aeb5140e2848d39b416daeef4ffc5+45',
-        an_integer: '1'
-      }
-    }
-    assert_response :success
-    assert_not_nil assigns(:object)
-    new_job = JSON.parse(@response.body)
-    assert_equal 'zzzzz-8i9sb-cjs4pklxxjykqqq', new_job['uuid']
-    assert_equal '4fe459abe02d9b365932b8f5dc419439ab4e2577', new_job['script_version']
+  def filters_from_hash(hash)
+    hash.each_pair.map { |name, filter| [name] + filter }
   end
 
-  test "test_cannot_reuse_job_different_input" do
-    @controller = Arvados::V1::JobsController.new
-    authorize_with :active
-    post :create, job: {
-      script: "hash",
-      script_version: "4fe459abe02d9b365932b8f5dc419439ab4e2577",
-      repository: "foo",
-      script_parameters: {
-        input: 'fa7aeb5140e2848d39b416daeef4ffc5+45',
-        an_integer: '2'
-      }
+  test "find Job with script version range" do
+    get :index, params: {
+      filters: [["repository", "=", "active/foo"],
+                ["script", "=", "hash"],
+                ["script_version", "in git", "tag1"]]
     }
     assert_response :success
-    assert_not_nil assigns(:object)
-    new_job = JSON.parse(@response.body)
-    assert_not_equal 'zzzzz-8i9sb-cjs4pklxxjykqqq', new_job['uuid']
-    assert_equal '4fe459abe02d9b365932b8f5dc419439ab4e2577', new_job['script_version']
+    assert_not_nil assigns(:objects)
+    assert_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_job_run).uuid)
   end
 
-  test "test_cannot_reuse_job_different_version" do
-    @controller = Arvados::V1::JobsController.new
-    authorize_with :active
-    post :create, job: {
-      script: "hash",
-      script_version: "master",
-      repository: "foo",
-      script_parameters: {
-        input: 'fa7aeb5140e2848d39b416daeef4ffc5+45',
-        an_integer: '2'
-      }
+  test "find Job with script version range exclusions" do
+    get :index, params: {
+      filters: [["repository", "=", "active/foo"],
+                ["script", "=", "hash"],
+                ["script_version", "not in git", "tag1"]]
     }
     assert_response :success
-    assert_not_nil assigns(:object)
-    new_job = JSON.parse(@response.body)
-    assert_not_equal 'zzzzz-8i9sb-cjs4pklxxjykqqq', new_job['uuid']
-    assert_equal '077ba2ad3ea24a929091a9e6ce545c93199b8e57', new_job['script_version']
+    assert_not_nil assigns(:objects)
+    refute_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_job_run).uuid)
   end
 
-  test "test_cannot_reuse_job_submitted_nondeterministic" do
-    @controller = Arvados::V1::JobsController.new
-    authorize_with :active
-    post :create, job: {
-      script: "hash",
-      script_version: "4fe459abe02d9b365932b8f5dc419439ab4e2577",
-      repository: "foo",
-      script_parameters: {
-        input: 'fa7aeb5140e2848d39b416daeef4ffc5+45',
-        an_integer: '1'
-      },
-      nondeterministic: true
+  test "find Job with Docker image range" do
+    get :index, params: {
+      filters: [["docker_image_locator", "in docker",
+                 "arvados/apitestfixture"]]
     }
     assert_response :success
-    assert_not_nil assigns(:object)
-    new_job = JSON.parse(@response.body)
-    assert_not_equal 'zzzzz-8i9sb-cjs4pklxxjykqqq', new_job['uuid']
-    assert_equal '4fe459abe02d9b365932b8f5dc419439ab4e2577', new_job['script_version']
+    assert_not_nil assigns(:objects)
+    assert_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_docker_job_run).uuid)
+    refute_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_job_run).uuid)
   end
 
-  test "test_cannot_reuse_job_past_nondeterministic" do
-    @controller = Arvados::V1::JobsController.new
-    authorize_with :active
-    post :create, job: {
-      script: "hash2",
-      script_version: "4fe459abe02d9b365932b8f5dc419439ab4e2577",
-      repository: "foo",
-      script_parameters: {
-        input: 'fa7aeb5140e2848d39b416daeef4ffc5+45',
-        an_integer: '1'
-      }
-    }
+  test "find Job with Docker image using reader tokens" do
+    authorize_with :inactive
+    get(:index, params: {
+          filters: [["docker_image_locator", "in docker",
+                     "arvados/apitestfixture"]],
+          reader_tokens: [api_token(:active)],
+        })
     assert_response :success
-    assert_not_nil assigns(:object)
-    new_job = JSON.parse(@response.body)
-    assert_not_equal 'zzzzz-8i9sb-cjs4pklxxjykyyy', new_job['uuid']
-    assert_equal '4fe459abe02d9b365932b8f5dc419439ab4e2577', new_job['script_version']
+    assert_not_nil assigns(:objects)
+    assert_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_docker_job_run).uuid)
+    refute_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_job_run).uuid)
   end
 
-  test "test_cannot_reuse_job_no_permission" do
-    @controller = Arvados::V1::JobsController.new
-    authorize_with :spectator
-    post :create, job: {
-      script: "hash",
-      script_version: "4fe459abe02d9b365932b8f5dc419439ab4e2577",
-      repository: "foo",
-      script_parameters: {
-        input: 'fa7aeb5140e2848d39b416daeef4ffc5+45',
-        an_integer: '1'
-      }
+  test "'in docker' filter accepts arrays" do
+    get :index, params: {
+      filters: [["docker_image_locator", "in docker",
+                ["_nonesuchname_", "arvados/apitestfixture"]]]
     }
     assert_response :success
-    assert_not_nil assigns(:object)
-    new_job = JSON.parse(@response.body)
-    assert_not_equal 'zzzzz-8i9sb-cjs4pklxxjykqqq', new_job['uuid']
-    assert_equal '4fe459abe02d9b365932b8f5dc419439ab4e2577', new_job['script_version']
+    assert_not_nil assigns(:objects)
+    assert_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_docker_job_run).uuid)
+    refute_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_job_run).uuid)
   end
 
-  test "test_cannot_reuse_job_excluded" do
-    @controller = Arvados::V1::JobsController.new
-    authorize_with :active
-    post :create, job: {
-      script: "hash",
-      minimum_script_version: "31ce37fe365b3dc204300a3e4c396ad333ed0556",
-      script_version: "master",
-      repository: "foo",
-      exclude_script_versions: ["tag1"],
-      script_parameters: {
-        input: 'fa7aeb5140e2848d39b416daeef4ffc5+45',
-        an_integer: '1'
-      }
+  test "'not in docker' filter accepts arrays" do
+    get :index, params: {
+      filters: [["docker_image_locator", "not in docker",
+                ["_nonesuchname_", "arvados/apitestfixture"]]]
     }
     assert_response :success
-    assert_not_nil assigns(:object)
-    new_job = JSON.parse(@response.body)
-    assert_not_equal 'zzzzz-8i9sb-cjs4pklxxjykqqq', new_job['uuid']
-    assert_equal '077ba2ad3ea24a929091a9e6ce545c93199b8e57', new_job['script_version']
+    assert_not_nil assigns(:objects)
+    assert_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_job_run).uuid)
+    refute_includes(assigns(:objects).map { |job| job.uuid },
+                    jobs(:previous_docker_job_run).uuid)
   end
 
-
 end