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