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"
27 // Gocheck boilerplate
28 func TestCrunchExec(t *testing.T) {
32 type TestSuite struct{}
34 // Gocheck boilerplate
35 var _ = Suite(&TestSuite{})
37 type ArvTestClient struct {
40 Content []arvadosclient.Dict
42 Logs map[string]*bytes.Buffer
47 type KeepTestClient struct {
52 var hwManifest = ". 82ab40c24fc8df01798e57ba66795bb1+841216+Aa124ac75e5168396c73c0a18eda641a4f41791c0@569fa8c3 0:841216:9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7.tar\n"
53 var hwPDH = "a45557269dcb65a6b78f9ac061c0850b+120"
54 var hwImageId = "9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7"
56 var otherManifest = ". 68a84f561b1d1708c6baff5e019a9ab3+46+Ae5d0af96944a3690becb1decdf60cc1c937f556d@5693216f 0:46:md5sum.txt\n"
57 var otherPDH = "a3e8f74c6f101eae01fa08bfb4e49b3a+54"
59 var fakeAuthUUID = "zzzzz-gj3su-55pqoyepgi2glem"
60 var fakeAuthToken = "a3ltuwzqcu2u4sc0q7yhpc2w7s00fdcqecg5d6e0u3pfohmbjt"
62 type TestDockerClient struct {
64 logReader io.ReadCloser
65 logWriter io.WriteCloser
66 fn func(t *TestDockerClient)
67 finish chan dockerclient.WaitResult
73 func NewTestDockerClient() *TestDockerClient {
74 t := &TestDockerClient{}
75 t.logReader, t.logWriter = io.Pipe()
76 t.finish = make(chan dockerclient.WaitResult)
77 t.stop = make(chan bool)
82 func (t *TestDockerClient) StopContainer(id string, timeout int) error {
87 func (t *TestDockerClient) InspectImage(id string) (*dockerclient.ImageInfo, error) {
88 if t.imageLoaded == id {
89 return &dockerclient.ImageInfo{}, nil
91 return nil, errors.New("")
95 func (t *TestDockerClient) LoadImage(reader io.Reader) error {
96 _, err := io.Copy(ioutil.Discard, reader)
100 t.imageLoaded = hwImageId
105 func (t *TestDockerClient) CreateContainer(config *dockerclient.ContainerConfig, name string, authConfig *dockerclient.AuthConfig) (string, error) {
106 if config.WorkingDir != "" {
107 t.cwd = config.WorkingDir
113 func (t *TestDockerClient) StartContainer(id string, config *dockerclient.HostConfig) error {
118 return errors.New("Invalid container id")
122 func (t *TestDockerClient) AttachContainer(id string, options *dockerclient.AttachOptions) (io.ReadCloser, error) {
123 return t.logReader, nil
126 func (t *TestDockerClient) Wait(id string) <-chan dockerclient.WaitResult {
130 func (*TestDockerClient) RemoveImage(name string, force bool) ([]*dockerclient.ImageDelete, error) {
134 func (this *ArvTestClient) Create(resourceType string,
135 parameters arvadosclient.Dict,
136 output interface{}) error {
139 defer this.Mutex.Unlock()
142 this.Content = append(this.Content, parameters)
144 if resourceType == "logs" {
145 et := parameters["log"].(arvadosclient.Dict)["event_type"].(string)
146 if this.Logs == nil {
147 this.Logs = make(map[string]*bytes.Buffer)
149 if this.Logs[et] == nil {
150 this.Logs[et] = &bytes.Buffer{}
152 this.Logs[et].Write([]byte(parameters["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"]))
155 if resourceType == "collections" && output != nil {
156 mt := parameters["collection"].(arvadosclient.Dict)["manifest_text"].(string)
157 outmap := output.(*CollectionRecord)
158 outmap.PortableDataHash = fmt.Sprintf("%x+%d", md5.Sum([]byte(mt)), len(mt))
164 func (this *ArvTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
166 case method == "GET" && resourceType == "containers" && action == "auth":
167 return json.Unmarshal([]byte(`{
168 "kind": "arvados#api_client_authorization",
169 "uuid": "`+fakeAuthUUID+`",
170 "api_token": "`+fakeAuthToken+`"
173 return fmt.Errorf("Not found")
177 func (this *ArvTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
178 if resourceType == "collections" {
180 output.(*CollectionRecord).ManifestText = hwManifest
181 } else if uuid == otherPDH {
182 output.(*CollectionRecord).ManifestText = otherManifest
185 if resourceType == "containers" {
186 (*output.(*ContainerRecord)) = this.ContainerRecord
191 func (this *ArvTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
193 defer this.Mutex.Unlock()
195 this.Content = append(this.Content, parameters)
196 if resourceType == "containers" {
197 if parameters["container"].(arvadosclient.Dict)["state"] == "Running" {
198 this.WasSetRunning = true
204 // CalledWith returns the parameters from the first API call whose
205 // parameters match jpath/string. E.g., CalledWith(c, "foo.bar",
206 // "baz") returns parameters with parameters["foo"]["bar"]=="baz". If
207 // no call matches, it returns nil.
208 func (this *ArvTestClient) CalledWith(jpath, expect string) arvadosclient.Dict {
209 call: for _, content := range this.Content {
210 var v interface{} = content
211 for _, k := range strings.Split(jpath, ".") {
212 if dict, ok := v.(arvadosclient.Dict); !ok {
218 if v, ok := v.(string); ok && v == expect {
225 func (this *KeepTestClient) PutHB(hash string, buf []byte) (string, int, error) {
227 return fmt.Sprintf("%s+%d", hash, len(buf)), len(buf), nil
230 type FileWrapper struct {
235 func (this FileWrapper) Len() uint64 {
239 func (this *KeepTestClient) ManifestFileReader(m manifest.Manifest, filename string) (keepclient.ReadCloserWithLen, error) {
240 if filename == hwImageId+".tar" {
241 rdr := ioutil.NopCloser(&bytes.Buffer{})
243 return FileWrapper{rdr, 1321984}, nil
248 func (s *TestSuite) TestLoadImage(c *C) {
249 kc := &KeepTestClient{}
250 docker := NewTestDockerClient()
251 cr := NewContainerRunner(&ArvTestClient{}, kc, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
253 _, err := cr.Docker.RemoveImage(hwImageId, true)
255 _, err = cr.Docker.InspectImage(hwImageId)
258 cr.ContainerRecord.ContainerImage = hwPDH
260 // (1) Test loading image from keep
261 c.Check(kc.Called, Equals, false)
262 c.Check(cr.ContainerConfig.Image, Equals, "")
268 cr.Docker.RemoveImage(hwImageId, true)
271 c.Check(kc.Called, Equals, true)
272 c.Check(cr.ContainerConfig.Image, Equals, hwImageId)
274 _, err = cr.Docker.InspectImage(hwImageId)
277 // (2) Test using image that's already loaded
279 cr.ContainerConfig.Image = ""
283 c.Check(kc.Called, Equals, false)
284 c.Check(cr.ContainerConfig.Image, Equals, hwImageId)
288 type ArvErrorTestClient struct{}
289 type KeepErrorTestClient struct{}
290 type KeepReadErrorTestClient struct{}
292 func (this ArvErrorTestClient) Create(resourceType string,
293 parameters arvadosclient.Dict,
294 output interface{}) error {
298 func (this ArvErrorTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
299 return errors.New("ArvError")
302 func (this ArvErrorTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
303 return errors.New("ArvError")
306 func (this ArvErrorTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
310 func (this KeepErrorTestClient) PutHB(hash string, buf []byte) (string, int, error) {
311 return "", 0, errors.New("KeepError")
314 func (this KeepErrorTestClient) ManifestFileReader(m manifest.Manifest, filename string) (keepclient.ReadCloserWithLen, error) {
315 return nil, errors.New("KeepError")
318 func (this KeepReadErrorTestClient) PutHB(hash string, buf []byte) (string, int, error) {
322 type ErrorReader struct{}
324 func (this ErrorReader) Read(p []byte) (n int, err error) {
325 return 0, errors.New("ErrorReader")
328 func (this ErrorReader) Close() error {
332 func (this ErrorReader) Len() uint64 {
336 func (this KeepReadErrorTestClient) ManifestFileReader(m manifest.Manifest, filename string) (keepclient.ReadCloserWithLen, error) {
337 return ErrorReader{}, nil
340 func (s *TestSuite) TestLoadImageArvError(c *C) {
342 cr := NewContainerRunner(ArvErrorTestClient{}, &KeepTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
343 cr.ContainerRecord.ContainerImage = hwPDH
345 err := cr.LoadImage()
346 c.Check(err.Error(), Equals, "While getting container image collection: ArvError")
349 func (s *TestSuite) TestLoadImageKeepError(c *C) {
351 docker := NewTestDockerClient()
352 cr := NewContainerRunner(&ArvTestClient{}, KeepErrorTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
353 cr.ContainerRecord.ContainerImage = hwPDH
355 err := cr.LoadImage()
356 c.Check(err.Error(), Equals, "While creating ManifestFileReader for container image: KeepError")
359 func (s *TestSuite) TestLoadImageCollectionError(c *C) {
360 // (3) Collection doesn't contain image
361 cr := NewContainerRunner(&ArvTestClient{}, KeepErrorTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
362 cr.ContainerRecord.ContainerImage = otherPDH
364 err := cr.LoadImage()
365 c.Check(err.Error(), Equals, "First file in the container image collection does not end in .tar")
368 func (s *TestSuite) TestLoadImageKeepReadError(c *C) {
369 // (4) Collection doesn't contain image
370 docker := NewTestDockerClient()
371 cr := NewContainerRunner(&ArvTestClient{}, KeepReadErrorTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
372 cr.ContainerRecord.ContainerImage = hwPDH
374 err := cr.LoadImage()
378 type ClosableBuffer struct {
382 type TestLogs struct {
383 Stdout ClosableBuffer
384 Stderr ClosableBuffer
387 func (this *ClosableBuffer) Close() error {
391 func (this *TestLogs) NewTestLoggingWriter(logstr string) io.WriteCloser {
392 if logstr == "stdout" {
395 if logstr == "stderr" {
401 func dockerLog(fd byte, msg string) []byte {
403 header := make([]byte, 8+len(by))
405 header[7] = byte(len(by))
410 func (s *TestSuite) TestRunContainer(c *C) {
411 docker := NewTestDockerClient()
412 docker.fn = func(t *TestDockerClient) {
413 t.logWriter.Write(dockerLog(1, "Hello world\n"))
415 t.finish <- dockerclient.WaitResult{}
417 cr := NewContainerRunner(&ArvTestClient{}, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
420 cr.NewLogWriter = logs.NewTestLoggingWriter
421 cr.ContainerRecord.ContainerImage = hwPDH
422 cr.ContainerRecord.Command = []string{"./hw"}
423 err := cr.LoadImage()
426 err = cr.CreateContainer()
429 err = cr.StartContainer()
432 err = cr.WaitFinish()
435 c.Check(strings.HasSuffix(logs.Stdout.String(), "Hello world\n"), Equals, true)
436 c.Check(logs.Stderr.String(), Equals, "")
439 func (s *TestSuite) TestCommitLogs(c *C) {
440 api := &ArvTestClient{}
441 kc := &KeepTestClient{}
442 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
443 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
445 cr.CrunchLog.Print("Hello world!")
446 cr.CrunchLog.Print("Goodbye")
447 cr.finalState = "Complete"
449 err := cr.CommitLogs()
452 c.Check(api.Calls, Equals, 2)
453 c.Check(api.Content[1]["collection"].(arvadosclient.Dict)["name"], Equals, "logs for zzzzz-zzzzz-zzzzzzzzzzzzzzz")
454 c.Check(api.Content[1]["collection"].(arvadosclient.Dict)["manifest_text"], Equals, ". 744b2e4553123b02fa7b452ec5c18993+123 0:123:crunch-run.txt\n")
455 c.Check(*cr.LogsPDH, Equals, "63da7bdacf08c40f604daad80c261e9a+60")
458 func (s *TestSuite) TestUpdateContainerRecordRunning(c *C) {
459 api := &ArvTestClient{}
460 kc := &KeepTestClient{}
461 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
463 err := cr.UpdateContainerRecordRunning()
466 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Running")
469 func (s *TestSuite) TestUpdateContainerRecordComplete(c *C) {
470 api := &ArvTestClient{}
471 kc := &KeepTestClient{}
472 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
474 cr.LogsPDH = new(string)
475 *cr.LogsPDH = "d3a229d2fe3690c2c3e75a71a153c6a3+60"
477 cr.ExitCode = new(int)
479 cr.finalState = "Complete"
481 err := cr.UpdateContainerRecordFinal()
484 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], Equals, *cr.LogsPDH)
485 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], Equals, *cr.ExitCode)
486 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
489 func (s *TestSuite) TestUpdateContainerRecordCancelled(c *C) {
490 api := &ArvTestClient{}
491 kc := &KeepTestClient{}
492 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
494 cr.finalState = "Cancelled"
496 err := cr.UpdateContainerRecordFinal()
499 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], IsNil)
500 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], IsNil)
501 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Cancelled")
504 // Used by the TestFullRun*() test below to DRY up boilerplate setup to do full
505 // dress rehearsal of the Run() function, starting from a JSON container record.
506 func FullRunHelper(c *C, record string, fn func(t *TestDockerClient)) (api *ArvTestClient, cr *ContainerRunner) {
507 rec := ContainerRecord{}
508 err := json.Unmarshal([]byte(record), &rec)
511 docker := NewTestDockerClient()
513 docker.RemoveImage(hwImageId, true)
515 api = &ArvTestClient{ContainerRecord: rec}
516 cr = NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
517 am := &ArvMountCmdLine{}
518 cr.RunArvMount = am.ArvMountTest
522 c.Check(api.WasSetRunning, Equals, true)
524 c.Check(api.Content[api.Calls-1]["container"].(arvadosclient.Dict)["log"], NotNil)
527 for k, v := range api.Logs {
536 func (s *TestSuite) TestFullRunHello(c *C) {
537 api, _ := FullRunHelper(c, `{
538 "command": ["echo", "hello world"],
539 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
542 "mounts": {"/tmp": {"kind": "tmp"} },
543 "output_path": "/tmp",
545 "runtime_constraints": {}
546 }`, func(t *TestDockerClient) {
547 t.logWriter.Write(dockerLog(1, "hello world\n"))
549 t.finish <- dockerclient.WaitResult{}
552 c.Check(api.Calls, Equals, 7)
553 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
554 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
556 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello world\n"), Equals, true)
560 func (s *TestSuite) TestFullRunStderr(c *C) {
561 api, _ := FullRunHelper(c, `{
562 "command": ["/bin/sh", "-c", "echo hello ; echo world 1>&2 ; exit 1"],
563 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
566 "mounts": {"/tmp": {"kind": "tmp"} },
567 "output_path": "/tmp",
569 "runtime_constraints": {}
570 }`, func(t *TestDockerClient) {
571 t.logWriter.Write(dockerLog(1, "hello\n"))
572 t.logWriter.Write(dockerLog(2, "world\n"))
574 t.finish <- dockerclient.WaitResult{ExitCode: 1}
577 c.Assert(api.Calls, Equals, 8)
578 c.Check(api.Content[7]["container"].(arvadosclient.Dict)["log"], NotNil)
579 c.Check(api.Content[7]["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
580 c.Check(api.Content[7]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
582 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello\n"), Equals, true)
583 c.Check(strings.HasSuffix(api.Logs["stderr"].String(), "world\n"), Equals, true)
586 func (s *TestSuite) TestFullRunDefaultCwd(c *C) {
587 api, _ := FullRunHelper(c, `{
589 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
592 "mounts": {"/tmp": {"kind": "tmp"} },
593 "output_path": "/tmp",
595 "runtime_constraints": {}
596 }`, func(t *TestDockerClient) {
597 t.logWriter.Write(dockerLog(1, t.cwd+"\n"))
599 t.finish <- dockerclient.WaitResult{ExitCode: 0}
602 c.Check(api.Calls, Equals, 7)
603 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
604 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
606 log.Print(api.Logs["stdout"].String())
608 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/\n"), Equals, true)
611 func (s *TestSuite) TestFullRunSetCwd(c *C) {
612 api, _ := FullRunHelper(c, `{
614 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
617 "mounts": {"/tmp": {"kind": "tmp"} },
618 "output_path": "/tmp",
620 "runtime_constraints": {}
621 }`, func(t *TestDockerClient) {
622 t.logWriter.Write(dockerLog(1, t.cwd+"\n"))
624 t.finish <- dockerclient.WaitResult{ExitCode: 0}
627 c.Check(api.Calls, Equals, 7)
628 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
629 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
631 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/bin\n"), Equals, true)
634 func (s *TestSuite) TestCancel(c *C) {
636 "command": ["/bin/sh", "-c", "echo foo && sleep 30 && echo bar"],
637 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
640 "mounts": {"/tmp": {"kind": "tmp"} },
641 "output_path": "/tmp",
643 "runtime_constraints": {}
646 rec := ContainerRecord{}
647 err := json.Unmarshal([]byte(record), &rec)
650 docker := NewTestDockerClient()
651 docker.fn = func(t *TestDockerClient) {
653 t.logWriter.Write(dockerLog(1, "foo\n"))
655 t.finish <- dockerclient.WaitResult{ExitCode: 0}
657 docker.RemoveImage(hwImageId, true)
659 api := &ArvTestClient{ContainerRecord: rec}
660 cr := NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
661 am := &ArvMountCmdLine{}
662 cr.RunArvMount = am.ArvMountTest
665 for cr.ContainerID == "" {
666 time.Sleep(time.Millisecond)
668 cr.SigChan <- syscall.SIGINT
675 for k, v := range api.Logs {
681 c.Assert(api.Calls, Equals, 6)
682 c.Check(api.Content[5]["container"].(arvadosclient.Dict)["log"], IsNil)
683 c.Check(api.Content[5]["container"].(arvadosclient.Dict)["state"], Equals, "Cancelled")
684 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "foo\n"), Equals, true)
688 func (s *TestSuite) TestFullRunSetEnv(c *C) {
689 api, _ := FullRunHelper(c, `{
690 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
691 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
693 "environment": {"FROBIZ": "bilbo"},
694 "mounts": {"/tmp": {"kind": "tmp"} },
695 "output_path": "/tmp",
697 "runtime_constraints": {}
698 }`, func(t *TestDockerClient) {
699 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
701 t.finish <- dockerclient.WaitResult{ExitCode: 0}
704 c.Check(api.Calls, Equals, 7)
705 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
706 c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
708 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "bilbo\n"), Equals, true)
711 type ArvMountCmdLine struct {
716 func (am *ArvMountCmdLine) ArvMountTest(c []string, token string) (*exec.Cmd, error) {
722 func (s *TestSuite) TestSetupMounts(c *C) {
723 api := &ArvTestClient{}
724 kc := &KeepTestClient{}
725 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
726 am := &ArvMountCmdLine{}
727 cr.RunArvMount = am.ArvMountTest
730 cr.MkTempDir = func(string, string) (string, error) {
732 d := fmt.Sprintf("/tmp/mktmpdir%d", i)
733 os.Mkdir(d, os.ModePerm)
738 cr.ContainerRecord.Mounts = make(map[string]Mount)
739 cr.ContainerRecord.Mounts["/tmp"] = Mount{Kind: "tmp"}
740 cr.OutputPath = "/tmp"
742 err := cr.SetupMounts()
744 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-by-pdh", "by_id", "/tmp/mktmpdir1"})
745 c.Check(cr.Binds, DeepEquals, []string{"/tmp/mktmpdir2:/tmp"})
751 cr.ContainerRecord.Mounts = make(map[string]Mount)
752 cr.ContainerRecord.Mounts["/keeptmp"] = Mount{Kind: "collection", Writable: true}
753 cr.OutputPath = "/keeptmp"
755 os.MkdirAll("/tmp/mktmpdir1/tmp0", os.ModePerm)
757 err := cr.SetupMounts()
759 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "/tmp/mktmpdir1"})
760 c.Check(cr.Binds, DeepEquals, []string{"/tmp/mktmpdir1/tmp0:/keeptmp"})
766 cr.ContainerRecord.Mounts = make(map[string]Mount)
767 cr.ContainerRecord.Mounts["/keepinp"] = Mount{Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"}
768 cr.ContainerRecord.Mounts["/keepout"] = Mount{Kind: "collection", Writable: true}
769 cr.OutputPath = "/keepout"
771 os.MkdirAll("/tmp/mktmpdir1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
772 os.MkdirAll("/tmp/mktmpdir1/tmp0", os.ModePerm)
774 err := cr.SetupMounts()
776 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "/tmp/mktmpdir1"})
777 var ss sort.StringSlice = cr.Binds
779 c.Check(cr.Binds, DeepEquals, []string{"/tmp/mktmpdir1/by_id/59389a8f9ee9d399be35462a0f92541c+53:/keepinp:ro",
780 "/tmp/mktmpdir1/tmp0:/keepout"})
785 func (s *TestSuite) TestStdout(c *C) {
787 helperRecord += `"command": ["/bin/sh", "-c", "echo $FROBIZ"],`
788 helperRecord += `"container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",`
789 helperRecord += `"cwd": "/bin",`
790 helperRecord += `"environment": {"FROBIZ": "bilbo"},`
791 helperRecord += `"mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"} },`
792 helperRecord += `"output_path": "/tmp",`
793 helperRecord += `"priority": 1,`
794 helperRecord += `"runtime_constraints": {}`
797 api, _ := FullRunHelper(c, helperRecord, func(t *TestDockerClient) {
798 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
800 t.finish <- dockerclient.WaitResult{ExitCode: 0}
803 c.Assert(api.Calls, Equals, 6)
804 c.Check(api.Content[5]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
805 c.Check(api.Content[5]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
806 c.Check(api.CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), Not(IsNil))
809 // Used by the TestStdoutWithWrongPath*()
810 func StdoutErrorRunHelper(c *C, record string, fn func(t *TestDockerClient)) (api *ArvTestClient, cr *ContainerRunner, err error) {
811 rec := ContainerRecord{}
812 err = json.Unmarshal([]byte(record), &rec)
815 docker := NewTestDockerClient()
817 docker.RemoveImage(hwImageId, true)
819 api = &ArvTestClient{ContainerRecord: rec}
820 cr = NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
821 am := &ArvMountCmdLine{}
822 cr.RunArvMount = am.ArvMountTest
828 func (s *TestSuite) TestStdoutWithWrongPath(c *C) {
829 _, _, err := StdoutErrorRunHelper(c, `{
830 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path":"/tmpa.out"} },
831 "output_path": "/tmp"
832 }`, func(t *TestDockerClient) {})
835 c.Check(strings.Contains(err.Error(), "Stdout path does not start with OutputPath"), Equals, true)
838 func (s *TestSuite) TestStdoutWithWrongKindTmp(c *C) {
839 _, _, err := StdoutErrorRunHelper(c, `{
840 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "tmp", "path":"/tmp/a.out"} },
841 "output_path": "/tmp"
842 }`, func(t *TestDockerClient) {})
845 c.Check(strings.Contains(err.Error(), "Unsupported mount kind 'tmp' for stdout"), Equals, true)
848 func (s *TestSuite) TestStdoutWithWrongKindCollection(c *C) {
849 _, _, err := StdoutErrorRunHelper(c, `{
850 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "collection", "path":"/tmp/a.out"} },
851 "output_path": "/tmp"
852 }`, func(t *TestDockerClient) {})
855 c.Check(strings.Contains(err.Error(), "Unsupported mount kind 'collection' for stdout"), Equals, true)