4 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
15 // Gocheck boilerplate
16 func Test(t *testing.T) {
20 type TestSuite struct{}
22 // Gocheck boilerplate
23 var _ = Suite(&TestSuite{})
25 type ArvTestClient struct {
31 func (t ArvTestClient) Create(resourceType string, parameters arvadosclient.Dict, output interface{}) error {
35 func (t ArvTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
36 t.c.Check(resourceType, Equals, "job_tasks")
37 t.c.Check(parameters, DeepEquals, arvadosclient.Dict{"job_task": Task{
44 func (s *TestSuite) TestSimpleRun(c *C) {
45 tmpdir, _ := ioutil.TempDir("", "")
50 err := runner(ArvTestClient{c, "", true},
52 "zzzz-8i9sb-111111111111111",
53 "zzzz-ot0gb-111111111111111",
56 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
57 Command: []string{"echo", "foo"}}}}},
62 func checkOutput(c *C, tmpdir string) {
63 file, err := os.Open(tmpdir + "/outdir/output.txt")
66 data := make([]byte, 100)
71 count, err = file.Read(data[offset:])
74 c.Assert(err, Equals, io.EOF)
75 c.Check(string(data[0:offset]), Equals, "foo\n")
78 func (s *TestSuite) TestSimpleRunSubtask(c *C) {
79 tmpdir, _ := ioutil.TempDir("", "")
84 err := runner(ArvTestClient{c,
85 ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
87 "zzzz-8i9sb-111111111111111",
88 "zzzz-ot0gb-111111111111111",
91 Job{Script_parameters: Tasks{[]TaskDef{
92 TaskDef{Command: []string{"echo", "bar"}},
93 TaskDef{Command: []string{"echo", "foo"}}}}},
94 Task{Parameters: TaskDef{
95 Command: []string{"echo", "foo"},
96 Stdout: "output.txt"},
100 checkOutput(c, tmpdir)
103 func (s *TestSuite) TestRedirect(c *C) {
104 tmpfile, _ := ioutil.TempFile("", "")
105 tmpfile.Write([]byte("foo\n"))
107 defer os.Remove(tmpfile.Name())
109 tmpdir, _ := ioutil.TempDir("", "")
114 err := runner(ArvTestClient{c,
115 ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
117 "zzzz-8i9sb-111111111111111",
118 "zzzz-ot0gb-111111111111111",
121 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
122 Command: []string{"cat"},
123 Stdout: "output.txt",
124 Stdin: tmpfile.Name()}}}},
128 checkOutput(c, tmpdir)
131 func (s *TestSuite) TestEnv(c *C) {
132 tmpdir, _ := ioutil.TempDir("", "")
137 err := runner(ArvTestClient{c, ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
139 "zzzz-8i9sb-111111111111111",
140 "zzzz-ot0gb-111111111111111",
143 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
144 Command: []string{"/bin/sh", "-c", "echo $BAR"},
145 Stdout: "output.txt",
146 Env: map[string]string{"BAR": "foo"}}}}},
149 checkOutput(c, tmpdir)
152 func (s *TestSuite) TestEnvSubstitute(c *C) {
153 tmpdir, _ := ioutil.TempDir("", "")
158 err := runner(ArvTestClient{c, ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
160 "zzzz-8i9sb-111111111111111",
161 "zzzz-ot0gb-111111111111111",
164 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
165 Command: []string{"/bin/sh", "-c", "echo $BAR"},
166 Stdout: "output.txt",
167 Env: map[string]string{"BAR": "$(task.keep)"}}}}},
170 checkOutput(c, tmpdir)
173 func (s *TestSuite) TestEnvReplace(c *C) {
174 tmpdir, _ := ioutil.TempDir("", "")
179 err := runner(ArvTestClient{c, ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
181 "zzzz-8i9sb-111111111111111",
182 "zzzz-ot0gb-111111111111111",
185 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
186 Command: []string{"/bin/sh", "-c", "echo $PATH"},
187 Stdout: "output.txt",
188 Env: map[string]string{"PATH": "foo"}}}}},
191 checkOutput(c, tmpdir)
194 type SubtaskTestClient struct {
200 func (t *SubtaskTestClient) Create(resourceType string, parameters arvadosclient.Dict, output interface{}) error {
201 t.c.Check(resourceType, Equals, "job_tasks")
202 t.c.Check(parameters, DeepEquals, arvadosclient.Dict{"job_task": t.parms[t.i]})
207 func (t SubtaskTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
211 func (s *TestSuite) TestScheduleSubtask(c *C) {
213 api := SubtaskTestClient{c, []Task{
214 Task{Job_uuid: "zzzz-8i9sb-111111111111111",
215 Created_by_job_task_uuid: "zzzz-ot0gb-111111111111111",
218 Command: []string{"echo", "bar"}}},
219 Task{Job_uuid: "zzzz-8i9sb-111111111111111",
220 Created_by_job_task_uuid: "zzzz-ot0gb-111111111111111",
223 Command: []string{"echo", "foo"}}}},
226 tmpdir, _ := ioutil.TempDir("", "")
231 err := runner(&api, KeepTestClient{},
232 "zzzz-8i9sb-111111111111111",
233 "zzzz-ot0gb-111111111111111",
236 Job{Script_parameters: Tasks{[]TaskDef{
237 TaskDef{Command: []string{"echo", "bar"}},
238 TaskDef{Command: []string{"echo", "foo"}}}}},
244 func (s *TestSuite) TestRunFail(c *C) {
245 tmpdir, _ := ioutil.TempDir("", "")
250 err := runner(ArvTestClient{c, "", false}, KeepTestClient{},
251 "zzzz-8i9sb-111111111111111",
252 "zzzz-ot0gb-111111111111111",
255 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
256 Command: []string{"/bin/sh", "-c", "exit 1"}}}}},
258 c.Check(err, FitsTypeOf, PermFail{})
261 func (s *TestSuite) TestRunSuccessCode(c *C) {
262 tmpdir, _ := ioutil.TempDir("", "")
267 err := runner(ArvTestClient{c, "", true}, KeepTestClient{},
268 "zzzz-8i9sb-111111111111111",
269 "zzzz-ot0gb-111111111111111",
272 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
273 Command: []string{"/bin/sh", "-c", "exit 1"},
274 SuccessCodes: []int{0, 1}}}}},
279 func (s *TestSuite) TestRunFailCode(c *C) {
280 tmpdir, _ := ioutil.TempDir("", "")
285 err := runner(ArvTestClient{c, "", false}, KeepTestClient{},
286 "zzzz-8i9sb-111111111111111",
287 "zzzz-ot0gb-111111111111111",
290 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
291 Command: []string{"/bin/sh", "-c", "exit 0"},
292 PermanentFailCodes: []int{0, 1}}}}},
294 c.Check(err, FitsTypeOf, PermFail{})
297 func (s *TestSuite) TestRunTempFailCode(c *C) {
298 tmpdir, _ := ioutil.TempDir("", "")
303 err := runner(ArvTestClient{c, "", false}, KeepTestClient{},
304 "zzzz-8i9sb-111111111111111",
305 "zzzz-ot0gb-111111111111111",
308 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
309 Command: []string{"/bin/sh", "-c", "exit 1"},
310 TemporaryFailCodes: []int{1}}}}},
312 c.Check(err, FitsTypeOf, TempFail{})
315 func (s *TestSuite) TestVwd(c *C) {
316 tmpfile, _ := ioutil.TempFile("", "")
317 tmpfile.Write([]byte("foo\n"))
319 defer os.Remove(tmpfile.Name())
321 tmpdir, _ := ioutil.TempDir("", "")
326 err := runner(ArvTestClient{c, ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
328 "zzzz-8i9sb-111111111111111",
329 "zzzz-ot0gb-111111111111111",
332 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
333 Command: []string{"ls", "output.txt"},
334 Vwd: map[string]string{
335 "output.txt": tmpfile.Name()}}}}},
338 checkOutput(c, tmpdir)
341 func (s *TestSuite) TestSubstitutionStdin(c *C) {
342 keepmount, _ := ioutil.TempDir("", "")
343 ioutil.WriteFile(keepmount+"/"+"file1.txt", []byte("foo\n"), 0600)
345 os.RemoveAll(keepmount)
348 log.Print("Keepmount is ", keepmount)
350 tmpdir, _ := ioutil.TempDir("", "")
355 log.Print("tmpdir is ", tmpdir)
357 err := runner(ArvTestClient{c,
358 ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
360 "zzzz-8i9sb-111111111111111",
361 "zzzz-ot0gb-111111111111111",
364 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
365 Command: []string{"cat"},
366 Stdout: "output.txt",
367 Stdin: "$(task.keep)/file1.txt"}}}},
370 checkOutput(c, tmpdir)
373 func (s *TestSuite) TestSubstitutionCommandLine(c *C) {
374 keepmount, _ := ioutil.TempDir("", "")
375 ioutil.WriteFile(keepmount+"/"+"file1.txt", []byte("foo\n"), 0600)
377 os.RemoveAll(keepmount)
380 tmpdir, _ := ioutil.TempDir("", "")
385 err := runner(ArvTestClient{c,
386 ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:output.txt\n", true},
388 "zzzz-8i9sb-111111111111111",
389 "zzzz-ot0gb-111111111111111",
392 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
393 Command: []string{"cat", "$(task.keep)/file1.txt"},
394 Stdout: "output.txt"}}}},
398 checkOutput(c, tmpdir)
401 func (s *TestSuite) TestSignal(c *C) {
402 tmpdir, _ := ioutil.TempDir("", "")
408 time.Sleep(1 * time.Second)
409 self, _ := os.FindProcess(os.Getpid())
410 self.Signal(syscall.SIGINT)
413 err := runner(ArvTestClient{c,
416 "zzzz-8i9sb-111111111111111",
417 "zzzz-ot0gb-111111111111111",
420 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
421 Command: []string{"sleep", "4"}}}}},
423 c.Check(err, FitsTypeOf, PermFail{})
427 func (s *TestSuite) TestQuoting(c *C) {
428 tmpdir, _ := ioutil.TempDir("", "")
433 err := runner(ArvTestClient{c,
434 "./s\\040ub:dir d3b07384d113edec49eaa6238ad5ff00+4 0:4::e\\040vil\n", true},
436 "zzzz-8i9sb-111111111111111",
437 "zzzz-ot0gb-111111111111111",
440 Job{Script_parameters: Tasks{[]TaskDef{TaskDef{
441 Command: []string{"echo", "foo"},
442 Stdout: "s ub:dir/:e vi\nl"}}}},