1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
16 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
25 type TestSuite struct{}
27 // Gocheck boilerplate
28 var _ = Suite(&TestSuite{})
30 type ArvTestClient struct {
36 func (t ArvTestClient) Create(resourceType string, parameters arvadosclient.Dict, output interface{}) error {
40 func (t ArvTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
41 t.c.Check(resourceType, Equals, "job_tasks")
42 t.c.Check(parameters, DeepEquals, arvadosclient.Dict{"job_task": Task{
49 func (s *TestSuite) TestSimpleRun(c *C) {
50 tmpdir, _ := ioutil.TempDir("", "")
55 err := runner(ArvTestClient{c, "", true},
57 "zzzz-8i9sb-111111111111111",
58 "zzzz-ot0gb-111111111111111",
61 Job{ScriptParameters: Tasks{[]TaskDef{{
62 Command: []string{"echo", "foo"}}}}},
67 func checkOutput(c *C, tmpdir string) {
68 file, err := os.Open(tmpdir + "/outdir/output.txt")
71 data := make([]byte, 100)
76 count, err = file.Read(data[offset:])
79 c.Assert(err, Equals, io.EOF)
80 c.Check(string(data[0:offset]), Equals, "foo\n")
83 func (s *TestSuite) TestSimpleRunSubtask(c *C) {
84 tmpdir, _ := ioutil.TempDir("", "")
89 err := runner(ArvTestClient{c,
90 ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
92 "zzzz-8i9sb-111111111111111",
93 "zzzz-ot0gb-111111111111111",
96 Job{ScriptParameters: Tasks{[]TaskDef{
97 {Command: []string{"echo", "bar"}},
98 {Command: []string{"echo", "foo"}}}}},
99 Task{Parameters: TaskDef{
100 Command: []string{"echo", "foo"},
101 Stdout: "output.txt"},
105 checkOutput(c, tmpdir)
108 func (s *TestSuite) TestRedirect(c *C) {
109 tmpfile, _ := ioutil.TempFile("", "")
110 tmpfile.Write([]byte("foo\n"))
112 defer os.Remove(tmpfile.Name())
114 tmpdir, _ := ioutil.TempDir("", "")
119 err := runner(ArvTestClient{c,
120 ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
122 "zzzz-8i9sb-111111111111111",
123 "zzzz-ot0gb-111111111111111",
126 Job{ScriptParameters: Tasks{[]TaskDef{{
127 Command: []string{"cat"},
128 Stdout: "output.txt",
129 Stdin: tmpfile.Name()}}}},
133 checkOutput(c, tmpdir)
136 func (s *TestSuite) TestEnv(c *C) {
137 tmpdir, _ := ioutil.TempDir("", "")
142 err := runner(ArvTestClient{c, ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
144 "zzzz-8i9sb-111111111111111",
145 "zzzz-ot0gb-111111111111111",
148 Job{ScriptParameters: Tasks{[]TaskDef{{
149 Command: []string{"/bin/sh", "-c", "echo $BAR"},
150 Stdout: "output.txt",
151 Env: map[string]string{"BAR": "foo"}}}}},
154 checkOutput(c, tmpdir)
157 func (s *TestSuite) TestEnvSubstitute(c *C) {
158 tmpdir, _ := ioutil.TempDir("", "")
163 err := runner(ArvTestClient{c, ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
165 "zzzz-8i9sb-111111111111111",
166 "zzzz-ot0gb-111111111111111",
169 Job{ScriptParameters: Tasks{[]TaskDef{{
170 Command: []string{"/bin/sh", "-c", "echo $BAR"},
171 Stdout: "output.txt",
172 Env: map[string]string{"BAR": "$(task.keep)"}}}}},
175 checkOutput(c, tmpdir)
178 func (s *TestSuite) TestEnvReplace(c *C) {
179 tmpdir, _ := ioutil.TempDir("", "")
184 err := runner(ArvTestClient{c, ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
186 "zzzz-8i9sb-111111111111111",
187 "zzzz-ot0gb-111111111111111",
190 Job{ScriptParameters: Tasks{[]TaskDef{{
191 Command: []string{"/bin/sh", "-c", "echo $PATH"},
192 Stdout: "output.txt",
193 Env: map[string]string{"PATH": "foo"}}}}},
196 checkOutput(c, tmpdir)
199 type SubtaskTestClient struct {
205 func (t *SubtaskTestClient) Create(resourceType string, parameters arvadosclient.Dict, output interface{}) error {
206 t.c.Check(resourceType, Equals, "job_tasks")
207 t.c.Check(parameters, DeepEquals, arvadosclient.Dict{"job_task": t.parms[t.i]})
212 func (t SubtaskTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
216 func (s *TestSuite) TestScheduleSubtask(c *C) {
218 api := SubtaskTestClient{c, []Task{
219 {JobUUID: "zzzz-8i9sb-111111111111111",
220 CreatedByJobTaskUUID: "zzzz-ot0gb-111111111111111",
223 Command: []string{"echo", "bar"}}},
224 {JobUUID: "zzzz-8i9sb-111111111111111",
225 CreatedByJobTaskUUID: "zzzz-ot0gb-111111111111111",
228 Command: []string{"echo", "foo"}}}},
231 tmpdir, _ := ioutil.TempDir("", "")
236 err := runner(&api, KeepTestClient{},
237 "zzzz-8i9sb-111111111111111",
238 "zzzz-ot0gb-111111111111111",
241 Job{ScriptParameters: Tasks{[]TaskDef{
242 {Command: []string{"echo", "bar"}},
243 {Command: []string{"echo", "foo"}}}}},
249 func (s *TestSuite) TestRunFail(c *C) {
250 tmpdir, _ := ioutil.TempDir("", "")
255 err := runner(ArvTestClient{c, "", false}, KeepTestClient{},
256 "zzzz-8i9sb-111111111111111",
257 "zzzz-ot0gb-111111111111111",
260 Job{ScriptParameters: Tasks{[]TaskDef{{
261 Command: []string{"/bin/sh", "-c", "exit 1"}}}}},
263 c.Check(err, FitsTypeOf, PermFail{})
266 func (s *TestSuite) TestRunSuccessCode(c *C) {
267 tmpdir, _ := ioutil.TempDir("", "")
272 err := runner(ArvTestClient{c, "", true}, KeepTestClient{},
273 "zzzz-8i9sb-111111111111111",
274 "zzzz-ot0gb-111111111111111",
277 Job{ScriptParameters: Tasks{[]TaskDef{{
278 Command: []string{"/bin/sh", "-c", "exit 1"},
279 SuccessCodes: []int{0, 1}}}}},
284 func (s *TestSuite) TestRunFailCode(c *C) {
285 tmpdir, _ := ioutil.TempDir("", "")
290 err := runner(ArvTestClient{c, "", false}, KeepTestClient{},
291 "zzzz-8i9sb-111111111111111",
292 "zzzz-ot0gb-111111111111111",
295 Job{ScriptParameters: Tasks{[]TaskDef{{
296 Command: []string{"/bin/sh", "-c", "exit 0"},
297 PermanentFailCodes: []int{0, 1}}}}},
299 c.Check(err, FitsTypeOf, PermFail{})
302 func (s *TestSuite) TestRunTempFailCode(c *C) {
303 tmpdir, _ := ioutil.TempDir("", "")
308 err := runner(ArvTestClient{c, "", false}, KeepTestClient{},
309 "zzzz-8i9sb-111111111111111",
310 "zzzz-ot0gb-111111111111111",
313 Job{ScriptParameters: Tasks{[]TaskDef{{
314 Command: []string{"/bin/sh", "-c", "exit 1"},
315 TemporaryFailCodes: []int{1}}}}},
317 c.Check(err, FitsTypeOf, TempFail{})
320 func (s *TestSuite) TestVwd(c *C) {
321 tmpfile, _ := ioutil.TempFile("", "")
322 tmpfile.Write([]byte("foo\n"))
324 defer os.Remove(tmpfile.Name())
326 tmpdir, _ := ioutil.TempDir("", "")
331 err := runner(ArvTestClient{c, ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
333 "zzzz-8i9sb-111111111111111",
334 "zzzz-ot0gb-111111111111111",
337 Job{ScriptParameters: Tasks{[]TaskDef{{
338 Command: []string{"ls", "output.txt"},
339 Vwd: map[string]string{
340 "output.txt": tmpfile.Name()}}}}},
343 checkOutput(c, tmpdir)
346 func (s *TestSuite) TestSubstitutionStdin(c *C) {
347 keepmount, _ := ioutil.TempDir("", "")
348 ioutil.WriteFile(keepmount+"/"+"file1.txt", []byte("foo\n"), 0600)
350 os.RemoveAll(keepmount)
353 log.Print("Keepmount is ", keepmount)
355 tmpdir, _ := ioutil.TempDir("", "")
360 log.Print("tmpdir is ", tmpdir)
362 err := runner(ArvTestClient{c,
363 ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
365 "zzzz-8i9sb-111111111111111",
366 "zzzz-ot0gb-111111111111111",
369 Job{ScriptParameters: Tasks{[]TaskDef{{
370 Command: []string{"cat"},
371 Stdout: "output.txt",
372 Stdin: "$(task.keep)/file1.txt"}}}},
375 checkOutput(c, tmpdir)
378 func (s *TestSuite) TestSubstitutionCommandLine(c *C) {
379 keepmount, _ := ioutil.TempDir("", "")
380 ioutil.WriteFile(keepmount+"/"+"file1.txt", []byte("foo\n"), 0600)
382 os.RemoveAll(keepmount)
385 tmpdir, _ := ioutil.TempDir("", "")
390 err := runner(ArvTestClient{c,
391 ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
393 "zzzz-8i9sb-111111111111111",
394 "zzzz-ot0gb-111111111111111",
397 Job{ScriptParameters: Tasks{[]TaskDef{{
398 Command: []string{"cat", "$(task.keep)/file1.txt"},
399 Stdout: "output.txt"}}}},
403 checkOutput(c, tmpdir)
406 func (s *TestSuite) TestSignal(c *C) {
407 tmpdir, _ := ioutil.TempDir("", "")
413 time.Sleep(1 * time.Second)
414 self, _ := os.FindProcess(os.Getpid())
415 self.Signal(syscall.SIGINT)
418 err := runner(ArvTestClient{c,
421 "zzzz-8i9sb-111111111111111",
422 "zzzz-ot0gb-111111111111111",
425 Job{ScriptParameters: Tasks{[]TaskDef{{
426 Command: []string{"sleep", "4"}}}}},
428 c.Check(err, FitsTypeOf, PermFail{})
432 func (s *TestSuite) TestQuoting(c *C) {
433 tmpdir, _ := ioutil.TempDir("", "")
438 err := runner(ArvTestClient{c,
439 "./s\\040ub:dir d3b07384d113edec49eaa6238ad5ff00+4 0:4::e\\040vil\n", true},
441 "zzzz-8i9sb-111111111111111",
442 "zzzz-ot0gb-111111111111111",
445 Job{ScriptParameters: Tasks{[]TaskDef{{
446 Command: []string{"echo", "foo"},
447 Stdout: "s ub:dir/:e vi\nl"}}}},
452 func (s *TestSuite) TestKeepTmp(c *C) {
453 tmpdir, _ := ioutil.TempDir("", "")
458 os.Setenv("TASK_KEEPMOUNT_TMP", tmpdir)
459 defer os.Setenv("TASK_KEEPMOUNT_TMP", "")
461 fn, err := os.Create(tmpdir + "/.arvados#collection")
462 fn.Write([]byte("{\"manifest_text\":\". unparsed 0:3:foo\\n\",\"uuid\":null}"))
465 err = runner(ArvTestClient{c,
466 ". unparsed 0:3:foo\n", true},
468 "zzzz-8i9sb-111111111111111",
469 "zzzz-ot0gb-111111111111111",
472 Job{ScriptParameters: Tasks{[]TaskDef{{
473 Command: []string{"echo", "foo"},
474 KeepTmpOutput: true}}}},