18948: Update test.
[arvados.git] / services / api / test / unit / job_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6 require 'helpers/git_test_helper'
7 require 'helpers/docker_migration_helper'
8
9 class JobTest < ActiveSupport::TestCase
10   include DockerMigrationHelper
11   include GitTestHelper
12
13   BAD_COLLECTION = "#{'f' * 32}+0"
14
15   setup do
16     set_user_from_auth :active
17   end
18
19   def job_attrs merge_me={}
20     # Default (valid) set of attributes, with given overrides
21     {
22       script: "hash",
23       script_version: "main",
24       repository: "active/foo",
25     }.merge(merge_me)
26   end
27
28   test "Job without Docker image doesn't get locator" do
29     job = Job.new job_attrs
30     assert job.valid?, job.errors.full_messages.to_s
31     assert_nil job.docker_image_locator
32   end
33
34   { 'name' => [:links, :docker_image_collection_tag, :name],
35     'hash' => [:links, :docker_image_collection_hash, :name],
36     'locator' => [:collections, :docker_image, :portable_data_hash],
37   }.each_pair do |spec_type, (fixture_type, fixture_name, fixture_attr)|
38     test "Job initialized with Docker image #{spec_type} gets locator" do
39       image_spec = send(fixture_type, fixture_name).send(fixture_attr)
40       job = Job.new job_attrs(runtime_constraints:
41                               {'docker_image' => image_spec})
42       assert job.valid?, job.errors.full_messages.to_s
43       assert_equal(collections(:docker_image).portable_data_hash, job.docker_image_locator)
44     end
45
46     test "Job modified with Docker image #{spec_type} gets locator" do
47       job = Job.new job_attrs
48       assert job.valid?, job.errors.full_messages.to_s
49       assert_nil job.docker_image_locator
50       image_spec = send(fixture_type, fixture_name).send(fixture_attr)
51       job.runtime_constraints['docker_image'] = image_spec
52       assert job.valid?, job.errors.full_messages.to_s
53       assert_equal(collections(:docker_image).portable_data_hash, job.docker_image_locator)
54     end
55   end
56
57   test "removing a Docker runtime constraint removes the locator" do
58     image_locator = collections(:docker_image).portable_data_hash
59     job = Job.new job_attrs(runtime_constraints:
60                             {'docker_image' => image_locator})
61     assert job.valid?, job.errors.full_messages.to_s
62     assert_equal(image_locator, job.docker_image_locator)
63     job.runtime_constraints = {}
64     assert job.valid?, job.errors.full_messages.to_s + "after clearing runtime constraints"
65     assert_nil job.docker_image_locator
66   end
67
68   test "locate a Docker image with a repository + tag" do
69     image_repo, image_tag =
70       links(:docker_image_collection_tag2).name.split(':', 2)
71     job = Job.new job_attrs(runtime_constraints:
72                             {'docker_image' => image_repo,
73                               'docker_image_tag' => image_tag})
74     assert job.valid?, job.errors.full_messages.to_s
75     assert_equal(collections(:docker_image).portable_data_hash, job.docker_image_locator)
76   end
77
78   test "can't locate a Docker image with a nonexistent tag" do
79     image_repo = links(:docker_image_collection_tag).name
80     image_tag = '__nonexistent tag__'
81     job = Job.new job_attrs(runtime_constraints:
82                             {'docker_image' => image_repo,
83                               'docker_image_tag' => image_tag})
84     assert(job.invalid?, "Job with bad Docker tag valid")
85   end
86
87   [
88     false,
89     true
90   ].each do |use_config|
91     test "Job with no Docker image uses default docker image when configuration is set #{use_config}" do
92       default_docker_image = collections(:docker_image)[:portable_data_hash]
93       Rails.configuration.Containers.JobsAPI.DefaultDockerImage = default_docker_image if use_config
94
95       job = Job.new job_attrs
96       assert job.valid?, job.errors.full_messages.to_s
97
98       if use_config
99         refute_nil job.docker_image_locator
100         assert_equal default_docker_image, job.docker_image_locator
101       else
102         assert_nil job.docker_image_locator
103       end
104     end
105   end
106
107   test "locate a Docker image with a partial hash" do
108     image_hash = links(:docker_image_collection_hash).name[0..24]
109     job = Job.new job_attrs(runtime_constraints:
110                             {'docker_image' => image_hash})
111     assert job.valid?, job.errors.full_messages.to_s + " with partial hash #{image_hash}"
112     assert_equal(collections(:docker_image).portable_data_hash, job.docker_image_locator)
113   end
114
115   { 'name' => 'arvados_test_nonexistent',
116     'hash' => 'f' * 64,
117     'locator' => BAD_COLLECTION,
118   }.each_pair do |spec_type, image_spec|
119     test "Job validation fails with nonexistent Docker image #{spec_type}" do
120       Rails.configuration.RemoteClusters = ConfigLoader.to_OrderedOptions({})
121       job = Job.new job_attrs(runtime_constraints:
122                               {'docker_image' => image_spec})
123       assert(job.invalid?, "nonexistent Docker image #{spec_type} #{image_spec} was valid")
124     end
125   end
126
127   test "Job validation fails with non-Docker Collection constraint" do
128     job = Job.new job_attrs(runtime_constraints:
129                             {'docker_image' => collections(:foo_file).uuid})
130     assert(job.invalid?, "non-Docker Collection constraint was valid")
131   end
132
133   test "can create Job with Docker image Collection without Docker links" do
134     image_uuid = collections(:unlinked_docker_image).portable_data_hash
135     job = Job.new job_attrs(runtime_constraints: {"docker_image" => image_uuid})
136     assert(job.valid?, "Job created with unlinked Docker image was invalid")
137     assert_equal(image_uuid, job.docker_image_locator)
138   end
139
140   def check_attrs_unset(job, attrs)
141     assert_empty(attrs.each_key.map { |key| job.send(key) }.compact,
142                  "job has values for #{attrs.keys}")
143   end
144
145   def check_creation_prohibited(attrs)
146     begin
147       job = Job.new(job_attrs(attrs))
148     rescue ActiveModel::MassAssignmentSecurity::Error
149       # Test passes - expected attribute protection
150     else
151       check_attrs_unset(job, attrs)
152     end
153   end
154
155   def check_modification_prohibited(attrs)
156     job = Job.new(job_attrs)
157     attrs.each_pair do |key, value|
158       assert_raises(NoMethodError) { job.send("{key}=".to_sym, value) }
159     end
160     check_attrs_unset(job, attrs)
161   end
162
163   test "can't create Job with Docker image locator" do
164     check_creation_prohibited(docker_image_locator: BAD_COLLECTION)
165   end
166
167   test "can't assign Docker image locator to Job" do
168     check_modification_prohibited(docker_image_locator: BAD_COLLECTION)
169   end
170
171   SDK_MASTER = "ca68b24e51992e790f29df5cc4bc54ce1da4a1c2"
172   SDK_TAGGED = "00634b2b8a492d6f121e3cf1d6587b821136a9a7"
173
174   def sdk_constraint(version)
175     {runtime_constraints: {
176         "arvados_sdk_version" => version,
177         "docker_image" => links(:docker_image_collection_tag).name,
178       }}
179   end
180
181   def check_job_sdk_version(expected)
182     job = yield
183     if expected.nil?
184       refute(job.valid?, "job valid with bad Arvados SDK version")
185     else
186       assert(job.valid?, "job not valid with good Arvados SDK version")
187       assert_equal(expected, job.arvados_sdk_version)
188     end
189   end
190
191   test "can't create job with SDK version assigned directly" do
192     check_creation_prohibited(arvados_sdk_version: SDK_MASTER)
193   end
194
195   test "can't modify job to assign SDK version directly" do
196     check_modification_prohibited(arvados_sdk_version: SDK_MASTER)
197   end
198
199   test 'script_parameters_digest is independent of key order' do
200     j1 = Job.new(job_attrs(script_parameters: {'a' => 'a', 'ddee' => {'d' => 'd', 'e' => 'e'}}))
201     j2 = Job.new(job_attrs(script_parameters: {'ddee' => {'e' => 'e', 'd' => 'd'}, 'a' => 'a'}))
202     assert j1.valid?
203     assert j2.valid?
204     assert_equal(j1.script_parameters_digest, j2.script_parameters_digest)
205   end
206
207   test 'job fixtures have correct script_parameters_digest' do
208     Job.all.each do |j|
209       d = j.script_parameters_digest
210       assert_equal(j.update_script_parameters_digest, d,
211                    "wrong script_parameters_digest for #{j.uuid}")
212     end
213   end
214
215   test 'deep_sort_hash on array of hashes' do
216     a = {'z' => [[{'a' => 'a', 'b' => 'b'}]]}
217     b = {'z' => [[{'b' => 'b', 'a' => 'a'}]]}
218     assert_equal Job.deep_sort_hash(a).to_json, Job.deep_sort_hash(b).to_json
219   end
220
221   def try_find_reusable
222     foobar = jobs(:foobar)
223     example_attrs = {
224       script_version: foobar.script_version,
225       script: foobar.script,
226       script_parameters: foobar.script_parameters,
227       repository: foobar.repository,
228     }
229
230     # Two matching jobs exist with identical outputs. The older one
231     # should be reused.
232     j = Job.find_reusable(example_attrs, {}, [], [users(:active)])
233     assert j
234     assert_equal foobar.uuid, j.uuid
235
236     # Two matching jobs exist with different outputs. Neither should
237     # be reused.
238     Job.where(uuid: jobs(:job_with_latest_version).uuid).
239       update_all(output: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+1')
240     assert_nil Job.find_reusable(example_attrs, {}, [], [users(:active)])
241
242     # ...unless config says to reuse the earlier job in such cases.
243     Rails.configuration.Containers.JobsAPI.ReuseJobIfOutputsDiffer = true
244     j = Job.find_reusable(example_attrs, {}, [], [users(:active)])
245     assert_equal foobar.uuid, j.uuid
246   end
247
248   test 'enable legacy api configuration option = true' do
249     Rails.configuration.Containers.JobsAPI.Enable = "true"
250     check_enable_legacy_jobs_api
251     assert_equal(Disable_update_jobs_api_method_list, Rails.configuration.API.DisabledAPIs)
252   end
253
254   test 'enable legacy api configuration option = false' do
255     Rails.configuration.Containers.JobsAPI.Enable = "false"
256     check_enable_legacy_jobs_api
257     assert_equal Disable_jobs_api_method_list, Rails.configuration.API.DisabledAPIs
258   end
259
260   test 'enable legacy api configuration option = auto, has jobs' do
261     Rails.configuration.Containers.JobsAPI.Enable = "auto"
262     assert Job.count > 0
263     check_enable_legacy_jobs_api
264     assert_equal(Disable_update_jobs_api_method_list, Rails.configuration.API.DisabledAPIs)
265   end
266
267   test 'enable legacy api configuration option = auto, no jobs' do
268     Rails.configuration.Containers.JobsAPI.Enable = "auto"
269     act_as_system_user do
270       Job.destroy_all
271     end
272     assert_equal 0, Job.count
273     assert_equal({}, Rails.configuration.API.DisabledAPIs)
274     check_enable_legacy_jobs_api
275     assert_equal Disable_jobs_api_method_list, Rails.configuration.API.DisabledAPIs
276   end
277 end