8784: Fix test for latest firefox.
[arvados.git] / sdk / cli / test / test_crunch-job.rb
1 require 'minitest/autorun'
2
3 class TestCrunchJob < Minitest::Test
4   SPECIAL_EXIT = {
5     EX_RETRY_UNLOCKED: 93,
6     EX_TEMPFAIL: 75,
7   }
8
9   JOBSPEC = {
10     grep_local: {
11       owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
12       script: 'grep',
13       script_version: 'master',
14       repository: File.absolute_path('../../../..', __FILE__),
15       script_parameters: {foo: 'bar'},
16     },
17   }
18
19   def setup
20   end
21
22   def crunchjob
23     File.absolute_path '../../bin/crunch-job', __FILE__
24   end
25
26   # Return environment suitable for running crunch-job.
27   def crunchenv opts={}
28     env = ENV.to_h
29     env['CRUNCH_REFRESH_TRIGGER'] =
30       File.absolute_path('../../../../tmp/crunch-refresh-trigger', __FILE__)
31     env
32   end
33
34   def jobspec label
35     JOBSPEC[label].dup
36   end
37
38   # Encode job record to json and run it with crunch-job.
39   #
40   # opts[:binstubs] is an array of X where ./binstub_X is added to
41   # PATH in order to mock system programs.
42   def tryjobrecord jobrecord, opts={}
43     env = crunchenv
44     (opts[:binstubs] || []).each do |binstub|
45       env['PATH'] = File.absolute_path('../binstub_'+binstub, __FILE__) + ':' + env['PATH']
46     end
47     system env, crunchjob, '--job', jobrecord.to_json
48   end
49
50   def test_bogus_json
51     out, err = capture_subprocess_io do
52       system crunchenv, crunchjob, '--job', '"}{"'
53     end
54     assert_equal false, $?.success?
55     # Must not conflict with our special exit statuses
56     assert_jobfail $?
57     assert_match /JSON/, err
58   end
59
60   def test_fail_sanity_check
61     out, err = capture_subprocess_io do
62       j = {}
63       tryjobrecord j, binstubs: ['sanity_check']
64     end
65     assert_equal 75, $?.exitstatus
66     assert_match /Sanity check failed: 7/, err
67   end
68
69   def test_fail_docker_sanity_check
70     out, err = capture_subprocess_io do
71       j = {}
72       j[:docker_image_locator] = '4d449b9d34f2e2222747ef79c53fa3ff+1234'
73       tryjobrecord j, binstubs: ['sanity_check']
74     end
75     assert_equal 75, $?.exitstatus
76     assert_match /Sanity check failed: 8/, err
77   end
78
79   def test_no_script_specified
80     out, err = capture_subprocess_io do
81       j = jobspec :grep_local
82       j.delete :script
83       tryjobrecord j
84     end
85     assert_match /No script specified/, err
86     assert_jobfail $?
87   end
88
89   def test_fail_clean_tmp
90     out, err = capture_subprocess_io do
91       j = jobspec :grep_local
92       tryjobrecord j, binstubs: ['clean_fail']
93     end
94     assert_match /Failing mount stub was called/, err
95     assert_match /clean work dirs: exit 44\n$/, err
96     assert_equal SPECIAL_EXIT[:EX_RETRY_UNLOCKED], $?.exitstatus
97   end
98
99   def test_output_collection_owner_uuid
100     j = jobspec :grep_local
101     out, err = capture_subprocess_io do
102       tryjobrecord j, binstubs: ['arv-mount', 'output_coll_owner']
103     end
104     assert_match /owner_uuid: #{j['owner_uuid']}/, err
105   end
106
107   def test_docker_image_missing
108     skip 'API bug: it refuses to create this job in Running state'
109     out, err = capture_subprocess_io do
110       j = jobspec :grep_local
111       j[:docker_image_locator] = '4d449b9d34f2e2222747ef79c53fa3ff+1234'
112       tryjobrecord j, binstubs: ['docker_noop']
113     end
114     assert_match /No Docker image hash found from locator/, err
115     assert_jobfail $?
116   end
117
118   def test_script_version_not_found_in_repository
119     bogus_version = 'f8b72707c1f5f740dbf1ed56eb429a36e0dee770'
120     out, err = capture_subprocess_io do
121       j = jobspec :grep_local
122       j[:script_version] = bogus_version
123       tryjobrecord j, binstubs: ['arv-mount']
124     end
125     assert_match /'#{bogus_version}' not found, giving up/, err
126     assert_jobfail $?
127   end
128
129   # Ensure procstatus is not interpreted as a temporary infrastructure
130   # problem. Would be assert_http_4xx if this were http.
131   def assert_jobfail procstatus
132     refute_includes SPECIAL_EXIT.values, procstatus.exitstatus
133     assert_equal false, procstatus.success?
134   end
135 end