Merge branch '2936-job-progress-bar'
[arvados.git] / services / api / test / unit / job_test.rb
1 require 'test_helper'
2
3 class JobTest < ActiveSupport::TestCase
4   BAD_COLLECTION = "#{'f' * 32}+0"
5
6   setup do
7     set_user_from_auth :active
8   end
9
10   test "Job without Docker image doesn't get locator" do
11     job = Job.new
12     assert job.valid?
13     assert_nil job.docker_image_locator
14   end
15
16   { 'name' => [:links, :docker_image_collection_repository, :name],
17     'hash' => [:links, :docker_image_collection_hash, :name],
18     'locator' => [:collections, :docker_image, :uuid],
19   }.each_pair do |spec_type, (fixture_type, fixture_name, fixture_attr)|
20     test "Job initialized with Docker image #{spec_type} gets locator" do
21       image_spec = send(fixture_type, fixture_name).send(fixture_attr)
22       job = Job.new(runtime_constraints: {'docker_image' => image_spec})
23       assert(job.valid?, "Docker image #{spec_type} was invalid")
24       assert_equal(collections(:docker_image).uuid, job.docker_image_locator)
25     end
26
27     test "Job modified with Docker image #{spec_type} gets locator" do
28       job = Job.new
29       assert job.valid?
30       assert_nil job.docker_image_locator
31       image_spec = send(fixture_type, fixture_name).send(fixture_attr)
32       job.runtime_constraints['docker_image'] = image_spec
33       assert(job.valid?, "modified Docker image #{spec_type} was invalid")
34       assert_equal(collections(:docker_image).uuid, job.docker_image_locator)
35     end
36   end
37
38   test "removing a Docker runtime constraint removes the locator" do
39     image_locator = collections(:docker_image).uuid
40     job = Job.new(runtime_constraints: {'docker_image' => image_locator})
41     assert job.valid?
42     assert_equal(image_locator, job.docker_image_locator)
43     job.runtime_constraints = {}
44     assert(job.valid?, "clearing runtime constraints made the Job invalid")
45     assert_nil job.docker_image_locator
46   end
47
48   test "locate a Docker image with a repository + tag" do
49     image_repo, image_tag =
50       links(:docker_image_collection_tag2).name.split(':', 2)
51     job = Job.new(runtime_constraints:
52                   {'docker_image' => image_repo,
53                     'docker_image_tag' => image_tag})
54     assert(job.valid?, "Job with Docker tag search invalid")
55     assert_equal(collections(:docker_image).uuid, job.docker_image_locator)
56   end
57
58   test "can't locate a Docker image with a nonexistent tag" do
59     image_repo = links(:docker_image_collection_repository).name
60     image_tag = '__nonexistent tag__'
61     job = Job.new(runtime_constraints:
62                   {'docker_image' => image_repo,
63                     'docker_image_tag' => image_tag})
64     assert(job.invalid?, "Job with bad Docker tag valid")
65   end
66
67   test "locate a Docker image with a partial hash" do
68     image_hash = links(:docker_image_collection_hash).name[0..24]
69     job = Job.new(runtime_constraints: {'docker_image' => image_hash})
70     assert(job.valid?, "Job with partial Docker image hash failed")
71     assert_equal(collections(:docker_image).uuid, job.docker_image_locator)
72   end
73
74   { 'name' => 'arvados_test_nonexistent',
75     'hash' => 'f' * 64,
76     'locator' => BAD_COLLECTION,
77   }.each_pair do |spec_type, image_spec|
78     test "Job validation fails with nonexistent Docker image #{spec_type}" do
79       job = Job.new(runtime_constraints: {'docker_image' => image_spec})
80       assert(job.invalid?, "nonexistent Docker image #{spec_type} was valid")
81     end
82   end
83
84   test "Job validation fails with non-Docker Collection constraint" do
85     job = Job.new(runtime_constraints:
86                   {'docker_image' => collections(:foo_file).uuid})
87     assert(job.invalid?, "non-Docker Collection constraint was valid")
88   end
89
90   test "can't create Job with Docker image locator" do
91     begin
92       job = Job.new(docker_image_locator: BAD_COLLECTION)
93     rescue ActiveModel::MassAssignmentSecurity::Error
94       # Test passes - expected attribute protection
95     else
96       assert_nil job.docker_image_locator
97     end
98   end
99
100   test "can't assign Docker image locator to Job" do
101     job = Job.new
102     begin
103       Job.docker_image_locator = BAD_COLLECTION
104     rescue NoMethodError
105       # Test passes - expected attribute protection
106     end
107     assert_nil job.docker_image_locator
108   end
109 end