9 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
10 "git.curoverse.com/arvados.git/sdk/go/keepclient"
11 "git.curoverse.com/arvados.git/sdk/go/manifest"
12 "github.com/curoverse/dockerclient"
26 // Gocheck boilerplate
27 func TestCrunchExec(t *testing.T) {
31 type TestSuite struct{}
33 // Gocheck boilerplate
34 var _ = Suite(&TestSuite{})
36 type ArvTestClient struct {
39 Content []arvadosclient.Dict
41 Logs map[string]*bytes.Buffer
45 type KeepTestClient struct {
50 var hwManifest = ". 82ab40c24fc8df01798e57ba66795bb1+841216+Aa124ac75e5168396c73c0a18eda641a4f41791c0@569fa8c3 0:841216:9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7.tar\n"
51 var hwPDH = "a45557269dcb65a6b78f9ac061c0850b+120"
52 var hwImageId = "9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7"
54 var otherManifest = ". 68a84f561b1d1708c6baff5e019a9ab3+46+Ae5d0af96944a3690becb1decdf60cc1c937f556d@5693216f 0:46:md5sum.txt\n"
55 var otherPDH = "a3e8f74c6f101eae01fa08bfb4e49b3a+54"
57 var fakeAuthUUID = "zzzzz-gj3su-55pqoyepgi2glem"
58 var fakeAuthToken = "a3ltuwzqcu2u4sc0q7yhpc2w7s00fdcqecg5d6e0u3pfohmbjt"
60 type TestDockerClient struct {
62 logReader io.ReadCloser
63 logWriter io.WriteCloser
64 fn func(t *TestDockerClient)
65 finish chan dockerclient.WaitResult
71 func NewTestDockerClient() *TestDockerClient {
72 t := &TestDockerClient{}
73 t.logReader, t.logWriter = io.Pipe()
74 t.finish = make(chan dockerclient.WaitResult)
75 t.stop = make(chan bool)
80 func (t *TestDockerClient) StopContainer(id string, timeout int) error {
85 func (t *TestDockerClient) InspectImage(id string) (*dockerclient.ImageInfo, error) {
86 if t.imageLoaded == id {
87 return &dockerclient.ImageInfo{}, nil
89 return nil, errors.New("")
93 func (t *TestDockerClient) LoadImage(reader io.Reader) error {
94 _, err := io.Copy(ioutil.Discard, reader)
98 t.imageLoaded = hwImageId
103 func (t *TestDockerClient) CreateContainer(config *dockerclient.ContainerConfig, name string, authConfig *dockerclient.AuthConfig) (string, error) {
104 if config.WorkingDir != "" {
105 t.cwd = config.WorkingDir
111 func (t *TestDockerClient) StartContainer(id string, config *dockerclient.HostConfig) error {
116 return errors.New("Invalid container id")
120 func (t *TestDockerClient) AttachContainer(id string, options *dockerclient.AttachOptions) (io.ReadCloser, error) {
121 return t.logReader, nil
124 func (t *TestDockerClient) Wait(id string) <-chan dockerclient.WaitResult {
128 func (*TestDockerClient) RemoveImage(name string, force bool) ([]*dockerclient.ImageDelete, error) {
132 func (this *ArvTestClient) Create(resourceType string,
133 parameters arvadosclient.Dict,
134 output interface{}) error {
137 this.Content = append(this.Content, parameters)
139 if resourceType == "logs" {
140 et := parameters["log"].(arvadosclient.Dict)["event_type"].(string)
141 if this.Logs == nil {
142 this.Logs = make(map[string]*bytes.Buffer)
144 if this.Logs[et] == nil {
145 this.Logs[et] = &bytes.Buffer{}
147 this.Logs[et].Write([]byte(parameters["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"]))
150 if resourceType == "collections" && output != nil {
151 mt := parameters["collection"].(arvadosclient.Dict)["manifest_text"].(string)
152 outmap := output.(*CollectionRecord)
153 outmap.PortableDataHash = fmt.Sprintf("%x+%d", md5.Sum([]byte(mt)), len(mt))
159 func (this *ArvTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
161 case method == "GET" && resourceType == "containers" && action == "auth":
162 return json.Unmarshal([]byte(`{
163 "kind": "arvados#api_client_authorization",
164 "uuid": "`+fakeAuthUUID+`",
165 "api_token": "`+fakeAuthToken+`"
168 return fmt.Errorf("Not found")
172 func (this *ArvTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
173 if resourceType == "collections" {
175 output.(*CollectionRecord).ManifestText = hwManifest
176 } else if uuid == otherPDH {
177 output.(*CollectionRecord).ManifestText = otherManifest
180 if resourceType == "containers" {
181 (*output.(*ContainerRecord)) = this.ContainerRecord
186 func (this *ArvTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
188 this.Content = append(this.Content, parameters)
189 if resourceType == "containers" {
190 if parameters["container"].(arvadosclient.Dict)["state"] == "Running" {
191 this.WasSetRunning = true
198 func (this *KeepTestClient) PutHB(hash string, buf []byte) (string, int, error) {
200 return fmt.Sprintf("%s+%d", hash, len(buf)), len(buf), nil
203 type FileWrapper struct {
208 func (this FileWrapper) Len() uint64 {
212 func (this *KeepTestClient) ManifestFileReader(m manifest.Manifest, filename string) (keepclient.ReadCloserWithLen, error) {
213 if filename == hwImageId+".tar" {
214 rdr := ioutil.NopCloser(&bytes.Buffer{})
216 return FileWrapper{rdr, 1321984}, nil
221 func (s *TestSuite) TestLoadImage(c *C) {
222 kc := &KeepTestClient{}
223 docker := NewTestDockerClient()
224 cr := NewContainerRunner(&ArvTestClient{}, kc, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
226 _, err := cr.Docker.RemoveImage(hwImageId, true)
228 _, err = cr.Docker.InspectImage(hwImageId)
231 cr.ContainerRecord.ContainerImage = hwPDH
233 // (1) Test loading image from keep
234 c.Check(kc.Called, Equals, false)
235 c.Check(cr.ContainerConfig.Image, Equals, "")
241 cr.Docker.RemoveImage(hwImageId, true)
244 c.Check(kc.Called, Equals, true)
245 c.Check(cr.ContainerConfig.Image, Equals, hwImageId)
247 _, err = cr.Docker.InspectImage(hwImageId)
250 // (2) Test using image that's already loaded
252 cr.ContainerConfig.Image = ""
256 c.Check(kc.Called, Equals, false)
257 c.Check(cr.ContainerConfig.Image, Equals, hwImageId)
261 type ArvErrorTestClient struct{}
262 type KeepErrorTestClient struct{}
263 type KeepReadErrorTestClient struct{}
265 func (this ArvErrorTestClient) Create(resourceType string,
266 parameters arvadosclient.Dict,
267 output interface{}) error {
271 func (this ArvErrorTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
272 return errors.New("ArvError")
275 func (this ArvErrorTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
276 return errors.New("ArvError")
279 func (this ArvErrorTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
283 func (this KeepErrorTestClient) PutHB(hash string, buf []byte) (string, int, error) {
284 return "", 0, errors.New("KeepError")
287 func (this KeepErrorTestClient) ManifestFileReader(m manifest.Manifest, filename string) (keepclient.ReadCloserWithLen, error) {
288 return nil, errors.New("KeepError")
291 func (this KeepReadErrorTestClient) PutHB(hash string, buf []byte) (string, int, error) {
295 type ErrorReader struct{}
297 func (this ErrorReader) Read(p []byte) (n int, err error) {
298 return 0, errors.New("ErrorReader")
301 func (this ErrorReader) Close() error {
305 func (this ErrorReader) Len() uint64 {
309 func (this KeepReadErrorTestClient) ManifestFileReader(m manifest.Manifest, filename string) (keepclient.ReadCloserWithLen, error) {
310 return ErrorReader{}, nil
313 func (s *TestSuite) TestLoadImageArvError(c *C) {
315 cr := NewContainerRunner(ArvErrorTestClient{}, &KeepTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
316 cr.ContainerRecord.ContainerImage = hwPDH
318 err := cr.LoadImage()
319 c.Check(err.Error(), Equals, "While getting container image collection: ArvError")
322 func (s *TestSuite) TestLoadImageKeepError(c *C) {
324 docker := NewTestDockerClient()
325 cr := NewContainerRunner(&ArvTestClient{}, KeepErrorTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
326 cr.ContainerRecord.ContainerImage = hwPDH
328 err := cr.LoadImage()
329 c.Check(err.Error(), Equals, "While creating ManifestFileReader for container image: KeepError")
332 func (s *TestSuite) TestLoadImageCollectionError(c *C) {
333 // (3) Collection doesn't contain image
334 cr := NewContainerRunner(&ArvTestClient{}, KeepErrorTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
335 cr.ContainerRecord.ContainerImage = otherPDH
337 err := cr.LoadImage()
338 c.Check(err.Error(), Equals, "First file in the container image collection does not end in .tar")
341 func (s *TestSuite) TestLoadImageKeepReadError(c *C) {
342 // (4) Collection doesn't contain image
343 docker := NewTestDockerClient()
344 cr := NewContainerRunner(&ArvTestClient{}, KeepReadErrorTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
345 cr.ContainerRecord.ContainerImage = hwPDH
347 err := cr.LoadImage()
351 type ClosableBuffer struct {
355 type TestLogs struct {
356 Stdout ClosableBuffer
357 Stderr ClosableBuffer
360 func (this *ClosableBuffer) Close() error {
364 func (this *TestLogs) NewTestLoggingWriter(logstr string) io.WriteCloser {
365 if logstr == "stdout" {
368 if logstr == "stderr" {
374 func dockerLog(fd byte, msg string) []byte {
376 header := make([]byte, 8+len(by))
378 header[7] = byte(len(by))
383 func (s *TestSuite) TestRunContainer(c *C) {
384 docker := NewTestDockerClient()
385 docker.fn = func(t *TestDockerClient) {
386 t.logWriter.Write(dockerLog(1, "Hello world\n"))
388 t.finish <- dockerclient.WaitResult{}
390 cr := NewContainerRunner(&ArvTestClient{}, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
393 cr.NewLogWriter = logs.NewTestLoggingWriter
394 cr.ContainerRecord.ContainerImage = hwPDH
395 cr.ContainerRecord.Command = []string{"./hw"}
396 err := cr.LoadImage()
399 err = cr.CreateContainer()
402 err = cr.StartContainer()
405 err = cr.WaitFinish()
408 c.Check(strings.HasSuffix(logs.Stdout.String(), "Hello world\n"), Equals, true)
409 c.Check(logs.Stderr.String(), Equals, "")
412 func (s *TestSuite) TestCommitLogs(c *C) {
413 api := &ArvTestClient{}
414 kc := &KeepTestClient{}
415 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
416 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
418 cr.CrunchLog.Print("Hello world!")
419 cr.CrunchLog.Print("Goodbye")
420 cr.finalState = "Complete"
422 err := cr.CommitLogs()
425 c.Check(api.Calls, Equals, 2)
426 c.Check(api.Content[1]["collection"].(arvadosclient.Dict)["name"], Equals, "logs for zzzzz-zzzzz-zzzzzzzzzzzzzzz")
427 c.Check(api.Content[1]["collection"].(arvadosclient.Dict)["manifest_text"], Equals, ". 744b2e4553123b02fa7b452ec5c18993+123 0:123:crunch-run.txt\n")
428 c.Check(*cr.LogsPDH, Equals, "63da7bdacf08c40f604daad80c261e9a+60")
431 func (s *TestSuite) TestUpdateContainerRecordRunning(c *C) {
432 api := &ArvTestClient{}
433 kc := &KeepTestClient{}
434 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
436 err := cr.UpdateContainerRecordRunning()
439 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Running")
442 func (s *TestSuite) TestUpdateContainerRecordComplete(c *C) {
443 api := &ArvTestClient{}
444 kc := &KeepTestClient{}
445 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
447 cr.LogsPDH = new(string)
448 *cr.LogsPDH = "d3a229d2fe3690c2c3e75a71a153c6a3+60"
450 cr.ExitCode = new(int)
452 cr.finalState = "Complete"
454 err := cr.UpdateContainerRecordFinal()
457 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], Equals, *cr.LogsPDH)
458 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], Equals, *cr.ExitCode)
459 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
462 func (s *TestSuite) TestUpdateContainerRecordCancelled(c *C) {
463 api := &ArvTestClient{}
464 kc := &KeepTestClient{}
465 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
467 cr.finalState = "Cancelled"
469 err := cr.UpdateContainerRecordFinal()
472 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], IsNil)
473 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], IsNil)
474 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Cancelled")
477 // Used by the TestFullRun*() test below to DRY up boilerplate setup to do full
478 // dress rehearsal of the Run() function, starting from a JSON container record.
479 func FullRunHelper(c *C, record string, fn func(t *TestDockerClient)) (api *ArvTestClient, cr *ContainerRunner) {
480 rec := ContainerRecord{}
481 err := json.NewDecoder(strings.NewReader(record)).Decode(&rec)
484 docker := NewTestDockerClient()
486 docker.RemoveImage(hwImageId, true)
488 api = &ArvTestClient{ContainerRecord: rec}
489 cr = NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
490 am := &ArvMountCmdLine{}
491 cr.RunArvMount = am.ArvMountTest
495 c.Check(api.WasSetRunning, Equals, true)
497 c.Check(api.Content[api.Calls-1]["container"].(arvadosclient.Dict)["log"], NotNil)
500 for k, v := range api.Logs {
509 func (s *TestSuite) TestFullRunHello(c *C) {
510 api, _ := FullRunHelper(c, `{
511 "command": ["echo", "hello world"],
512 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
515 "mounts": {"/tmp": {"kind": "tmp"} },
516 "output_path": "/tmp",
518 "runtime_constraints": {}
519 }`, func(t *TestDockerClient) {
520 t.logWriter.Write(dockerLog(1, "hello world\n"))
522 t.finish <- dockerclient.WaitResult{}
525 c.Check(api.Calls, Equals, 7)
526 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
527 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
529 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello world\n"), Equals, true)
533 func (s *TestSuite) TestFullRunStderr(c *C) {
534 api, _ := FullRunHelper(c, `{
535 "command": ["/bin/sh", "-c", "echo hello ; echo world 1>&2 ; exit 1"],
536 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
539 "mounts": {"/tmp": {"kind": "tmp"} },
540 "output_path": "/tmp",
542 "runtime_constraints": {}
543 }`, func(t *TestDockerClient) {
544 t.logWriter.Write(dockerLog(1, "hello\n"))
545 t.logWriter.Write(dockerLog(2, "world\n"))
547 t.finish <- dockerclient.WaitResult{ExitCode: 1}
550 c.Assert(api.Calls, Equals, 8)
551 c.Check(api.Content[7]["container"].(arvadosclient.Dict)["log"], NotNil)
552 c.Check(api.Content[7]["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
553 c.Check(api.Content[7]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
555 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello\n"), Equals, true)
556 c.Check(strings.HasSuffix(api.Logs["stderr"].String(), "world\n"), Equals, true)
559 func (s *TestSuite) TestFullRunDefaultCwd(c *C) {
560 api, _ := FullRunHelper(c, `{
562 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
565 "mounts": {"/tmp": {"kind": "tmp"} },
566 "output_path": "/tmp",
568 "runtime_constraints": {}
569 }`, func(t *TestDockerClient) {
570 t.logWriter.Write(dockerLog(1, t.cwd+"\n"))
572 t.finish <- dockerclient.WaitResult{ExitCode: 0}
575 c.Check(api.Calls, Equals, 7)
576 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
577 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
579 log.Print(api.Logs["stdout"].String())
581 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/\n"), Equals, true)
584 func (s *TestSuite) TestFullRunSetCwd(c *C) {
585 api, _ := FullRunHelper(c, `{
587 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
590 "mounts": {"/tmp": {"kind": "tmp"} },
591 "output_path": "/tmp",
593 "runtime_constraints": {}
594 }`, func(t *TestDockerClient) {
595 t.logWriter.Write(dockerLog(1, t.cwd+"\n"))
597 t.finish <- dockerclient.WaitResult{ExitCode: 0}
600 c.Check(api.Calls, Equals, 7)
601 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
602 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
604 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/bin\n"), Equals, true)
607 func (s *TestSuite) TestCancel(c *C) {
609 "command": ["/bin/sh", "-c", "echo foo && sleep 30 && echo bar"],
610 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
613 "mounts": {"/tmp": {"kind": "tmp"} },
614 "output_path": "/tmp",
616 "runtime_constraints": {}
619 rec := ContainerRecord{}
620 err := json.NewDecoder(strings.NewReader(record)).Decode(&rec)
623 docker := NewTestDockerClient()
624 docker.fn = func(t *TestDockerClient) {
626 t.logWriter.Write(dockerLog(1, "foo\n"))
628 t.finish <- dockerclient.WaitResult{ExitCode: 0}
630 docker.RemoveImage(hwImageId, true)
632 api := &ArvTestClient{ContainerRecord: rec}
633 cr := NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
634 am := &ArvMountCmdLine{}
635 cr.RunArvMount = am.ArvMountTest
638 for cr.ContainerID == "" {
639 time.Sleep(1 * time.Second)
641 cr.SigChan <- syscall.SIGINT
648 for k, v := range api.Logs {
654 c.Assert(api.Calls, Equals, 6)
655 c.Check(api.Content[5]["container"].(arvadosclient.Dict)["log"], IsNil)
656 c.Check(api.Content[5]["container"].(arvadosclient.Dict)["state"], Equals, "Cancelled")
657 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "foo\n"), Equals, true)
661 func (s *TestSuite) TestFullRunSetEnv(c *C) {
662 api, _ := FullRunHelper(c, `{
663 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
664 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
666 "environment": {"FROBIZ": "bilbo"},
667 "mounts": {"/tmp": {"kind": "tmp"} },
668 "output_path": "/tmp",
670 "runtime_constraints": {}
671 }`, func(t *TestDockerClient) {
672 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
674 t.finish <- dockerclient.WaitResult{ExitCode: 0}
677 c.Check(api.Calls, Equals, 7)
678 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
679 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
681 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "bilbo\n"), Equals, true)
684 type ArvMountCmdLine struct {
689 func (am *ArvMountCmdLine) ArvMountTest(c []string, token string) (*exec.Cmd, error) {
695 func (s *TestSuite) TestSetupMounts(c *C) {
696 api := &ArvTestClient{}
697 kc := &KeepTestClient{}
698 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
699 am := &ArvMountCmdLine{}
700 cr.RunArvMount = am.ArvMountTest
703 cr.MkTempDir = func(string, string) (string, error) {
705 d := fmt.Sprintf("/tmp/mktmpdir%d", i)
706 os.Mkdir(d, os.ModePerm)
711 cr.ContainerRecord.Mounts = make(map[string]Mount)
712 cr.ContainerRecord.Mounts["/tmp"] = Mount{Kind: "tmp"}
713 cr.OutputPath = "/tmp"
715 err := cr.SetupMounts()
717 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-by-pdh", "by_id", "/tmp/mktmpdir1"})
718 c.Check(cr.Binds, DeepEquals, []string{"/tmp/mktmpdir2:/tmp"})
724 cr.ContainerRecord.Mounts = make(map[string]Mount)
725 cr.ContainerRecord.Mounts["/keeptmp"] = Mount{Kind: "collection", Writable: true}
726 cr.OutputPath = "/keeptmp"
728 os.MkdirAll("/tmp/mktmpdir1/tmp0", os.ModePerm)
730 err := cr.SetupMounts()
732 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "/tmp/mktmpdir1"})
733 c.Check(cr.Binds, DeepEquals, []string{"/tmp/mktmpdir1/tmp0:/keeptmp"})
739 cr.ContainerRecord.Mounts = make(map[string]Mount)
740 cr.ContainerRecord.Mounts["/keepinp"] = Mount{Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"}
741 cr.ContainerRecord.Mounts["/keepout"] = Mount{Kind: "collection", Writable: true}
742 cr.OutputPath = "/keepout"
744 os.MkdirAll("/tmp/mktmpdir1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
745 os.MkdirAll("/tmp/mktmpdir1/tmp0", os.ModePerm)
747 err := cr.SetupMounts()
749 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "/tmp/mktmpdir1"})
750 var ss sort.StringSlice = cr.Binds
752 c.Check(cr.Binds, DeepEquals, []string{"/tmp/mktmpdir1/by_id/59389a8f9ee9d399be35462a0f92541c+53:/keepinp:ro",
753 "/tmp/mktmpdir1/tmp0:/keepout"})
758 func (s *TestSuite) TestStdout(c *C) {
760 helperRecord += `"command": ["/bin/sh", "-c", "echo $FROBIZ"],`
761 helperRecord += `"container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",`
762 helperRecord += `"cwd": "/bin",`
763 helperRecord += `"environment": {"FROBIZ": "bilbo"},`
764 helperRecord += `"mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"} },`
765 helperRecord += `"output_path": "/tmp",`
766 helperRecord += `"priority": 1,`
767 helperRecord += `"runtime_constraints": {}`
770 api, _ := FullRunHelper(c, helperRecord, func(t *TestDockerClient) {
771 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
773 t.finish <- dockerclient.WaitResult{ExitCode: 0}
776 c.Check(api.Calls, Equals, 6)
777 c.Check(api.Content[5]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
778 c.Check(api.Content[5]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
779 c.Check(api.Content[2]["collection"].(arvadosclient.Dict)["manifest_text"], Equals, "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n")
782 // Used by the TestStdoutWithWrongPath*()
783 func StdoutErrorRunHelper(c *C, record string, fn func(t *TestDockerClient)) (api *ArvTestClient, cr *ContainerRunner, err error) {
784 rec := ContainerRecord{}
785 err = json.NewDecoder(strings.NewReader(record)).Decode(&rec)
788 docker := NewTestDockerClient()
790 docker.RemoveImage(hwImageId, true)
792 api = &ArvTestClient{ContainerRecord: rec}
793 cr = NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
794 am := &ArvMountCmdLine{}
795 cr.RunArvMount = am.ArvMountTest
801 func (s *TestSuite) TestStdoutWithWrongPath(c *C) {
802 _, _, err := StdoutErrorRunHelper(c, `{
803 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path":"/tmpa.out"} },
804 "output_path": "/tmp"
805 }`, func(t *TestDockerClient) {})
808 c.Check(strings.Contains(err.Error(), "Stdout path does not start with OutputPath"), Equals, true)
811 func (s *TestSuite) TestStdoutWithWrongKindTmp(c *C) {
812 _, _, err := StdoutErrorRunHelper(c, `{
813 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "tmp", "path":"/tmp/a.out"} },
814 "output_path": "/tmp"
815 }`, func(t *TestDockerClient) {})
818 c.Check(strings.Contains(err.Error(), "Unsupported mount kind 'tmp' for stdout"), Equals, true)
821 func (s *TestSuite) TestStdoutWithWrongKindCollection(c *C) {
822 _, _, err := StdoutErrorRunHelper(c, `{
823 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "collection", "path":"/tmp/a.out"} },
824 "output_path": "/tmp"
825 }`, func(t *TestDockerClient) {})
828 c.Check(strings.Contains(err.Error(), "Unsupported mount kind 'collection' for stdout"), Equals, true)