9 "git.curoverse.com/arvados.git/sdk/go/arvados"
10 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
11 "git.curoverse.com/arvados.git/sdk/go/keepclient"
12 "git.curoverse.com/arvados.git/sdk/go/manifest"
13 "github.com/curoverse/dockerclient"
28 // Gocheck boilerplate
29 func TestCrunchExec(t *testing.T) {
33 type TestSuite struct{}
35 // Gocheck boilerplate
36 var _ = Suite(&TestSuite{})
38 type ArvTestClient struct {
41 Content []arvadosclient.Dict
43 Logs map[string]*bytes.Buffer
48 type KeepTestClient struct {
53 var hwManifest = ". 82ab40c24fc8df01798e57ba66795bb1+841216+Aa124ac75e5168396c73c0a18eda641a4f41791c0@569fa8c3 0:841216:9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7.tar\n"
54 var hwPDH = "a45557269dcb65a6b78f9ac061c0850b+120"
55 var hwImageId = "9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7"
57 var otherManifest = ". 68a84f561b1d1708c6baff5e019a9ab3+46+Ae5d0af96944a3690becb1decdf60cc1c937f556d@5693216f 0:46:md5sum.txt\n"
58 var otherPDH = "a3e8f74c6f101eae01fa08bfb4e49b3a+54"
60 var fakeAuthUUID = "zzzzz-gj3su-55pqoyepgi2glem"
61 var fakeAuthToken = "a3ltuwzqcu2u4sc0q7yhpc2w7s00fdcqecg5d6e0u3pfohmbjt"
63 type TestDockerClient struct {
65 logReader io.ReadCloser
66 logWriter io.WriteCloser
67 fn func(t *TestDockerClient)
68 finish chan dockerclient.WaitResult
75 func NewTestDockerClient() *TestDockerClient {
76 t := &TestDockerClient{}
77 t.logReader, t.logWriter = io.Pipe()
78 t.finish = make(chan dockerclient.WaitResult)
79 t.stop = make(chan bool)
84 func (t *TestDockerClient) StopContainer(id string, timeout int) error {
89 func (t *TestDockerClient) InspectImage(id string) (*dockerclient.ImageInfo, error) {
90 if t.imageLoaded == id {
91 return &dockerclient.ImageInfo{}, nil
93 return nil, errors.New("")
97 func (t *TestDockerClient) LoadImage(reader io.Reader) error {
98 _, err := io.Copy(ioutil.Discard, reader)
102 t.imageLoaded = hwImageId
107 func (t *TestDockerClient) CreateContainer(config *dockerclient.ContainerConfig, name string, authConfig *dockerclient.AuthConfig) (string, error) {
108 if config.WorkingDir != "" {
109 t.cwd = config.WorkingDir
115 func (t *TestDockerClient) StartContainer(id string, config *dockerclient.HostConfig) error {
120 return errors.New("Invalid container id")
124 func (t *TestDockerClient) AttachContainer(id string, options *dockerclient.AttachOptions) (io.ReadCloser, error) {
125 return t.logReader, nil
128 func (t *TestDockerClient) Wait(id string) <-chan dockerclient.WaitResult {
132 func (*TestDockerClient) RemoveImage(name string, force bool) ([]*dockerclient.ImageDelete, error) {
136 func (client *ArvTestClient) Create(resourceType string,
137 parameters arvadosclient.Dict,
138 output interface{}) error {
141 defer client.Mutex.Unlock()
144 client.Content = append(client.Content, parameters)
146 if resourceType == "logs" {
147 et := parameters["log"].(arvadosclient.Dict)["event_type"].(string)
148 if client.Logs == nil {
149 client.Logs = make(map[string]*bytes.Buffer)
151 if client.Logs[et] == nil {
152 client.Logs[et] = &bytes.Buffer{}
154 client.Logs[et].Write([]byte(parameters["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"]))
157 if resourceType == "collections" && output != nil {
158 mt := parameters["collection"].(arvadosclient.Dict)["manifest_text"].(string)
159 outmap := output.(*arvados.Collection)
160 outmap.PortableDataHash = fmt.Sprintf("%x+%d", md5.Sum([]byte(mt)), len(mt))
166 func (client *ArvTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
168 case method == "GET" && resourceType == "containers" && action == "auth":
169 return json.Unmarshal([]byte(`{
170 "kind": "arvados#api_client_authorization",
171 "uuid": "`+fakeAuthUUID+`",
172 "api_token": "`+fakeAuthToken+`"
175 return fmt.Errorf("Not found")
179 func (client *ArvTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
180 if resourceType == "collections" {
182 output.(*arvados.Collection).ManifestText = hwManifest
183 } else if uuid == otherPDH {
184 output.(*arvados.Collection).ManifestText = otherManifest
187 if resourceType == "containers" {
188 (*output.(*arvados.Container)) = client.Container
193 func (client *ArvTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
195 defer client.Mutex.Unlock()
197 client.Content = append(client.Content, parameters)
198 if resourceType == "containers" {
199 if parameters["container"].(arvadosclient.Dict)["state"] == "Running" {
200 client.WasSetRunning = true
206 var discoveryMap = map[string]interface{}{"defaultTrashLifetime": float64(1209600)}
208 func (client *ArvTestClient) Discovery(key string) (interface{}, error) {
209 return discoveryMap[key], nil
212 // CalledWith returns the parameters from the first API call whose
213 // parameters match jpath/string. E.g., CalledWith(c, "foo.bar",
214 // "baz") returns parameters with parameters["foo"]["bar"]=="baz". If
215 // no call matches, it returns nil.
216 func (client *ArvTestClient) CalledWith(jpath string, expect interface{}) arvadosclient.Dict {
218 for _, content := range client.Content {
219 var v interface{} = content
220 for _, k := range strings.Split(jpath, ".") {
221 if dict, ok := v.(arvadosclient.Dict); !ok {
234 func (client *KeepTestClient) PutHB(hash string, buf []byte) (string, int, error) {
236 return fmt.Sprintf("%s+%d", hash, len(buf)), len(buf), nil
239 type FileWrapper struct {
244 func (fw FileWrapper) Len() uint64 {
248 func (fw FileWrapper) Seek(int64, int) (int64, error) {
249 return 0, errors.New("not implemented")
252 func (client *KeepTestClient) ManifestFileReader(m manifest.Manifest, filename string) (keepclient.Reader, error) {
253 if filename == hwImageId+".tar" {
254 rdr := ioutil.NopCloser(&bytes.Buffer{})
256 return FileWrapper{rdr, 1321984}, nil
261 func (s *TestSuite) TestLoadImage(c *C) {
262 kc := &KeepTestClient{}
263 docker := NewTestDockerClient()
264 cr := NewContainerRunner(&ArvTestClient{}, kc, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
266 _, err := cr.Docker.RemoveImage(hwImageId, true)
268 _, err = cr.Docker.InspectImage(hwImageId)
271 cr.Container.ContainerImage = hwPDH
273 // (1) Test loading image from keep
274 c.Check(kc.Called, Equals, false)
275 c.Check(cr.ContainerConfig.Image, Equals, "")
281 cr.Docker.RemoveImage(hwImageId, true)
284 c.Check(kc.Called, Equals, true)
285 c.Check(cr.ContainerConfig.Image, Equals, hwImageId)
287 _, err = cr.Docker.InspectImage(hwImageId)
290 // (2) Test using image that's already loaded
292 cr.ContainerConfig.Image = ""
296 c.Check(kc.Called, Equals, false)
297 c.Check(cr.ContainerConfig.Image, Equals, hwImageId)
301 type ArvErrorTestClient struct{}
303 func (ArvErrorTestClient) Create(resourceType string,
304 parameters arvadosclient.Dict,
305 output interface{}) error {
309 func (ArvErrorTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
310 return errors.New("ArvError")
313 func (ArvErrorTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
314 return errors.New("ArvError")
317 func (ArvErrorTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
321 func (ArvErrorTestClient) Discovery(key string) (interface{}, error) {
322 return discoveryMap[key], nil
325 type KeepErrorTestClient struct{}
327 func (KeepErrorTestClient) PutHB(hash string, buf []byte) (string, int, error) {
328 return "", 0, errors.New("KeepError")
331 func (KeepErrorTestClient) ManifestFileReader(m manifest.Manifest, filename string) (keepclient.Reader, error) {
332 return nil, errors.New("KeepError")
335 type KeepReadErrorTestClient struct{}
337 func (KeepReadErrorTestClient) PutHB(hash string, buf []byte) (string, int, error) {
341 type ErrorReader struct{}
343 func (ErrorReader) Read(p []byte) (n int, err error) {
344 return 0, errors.New("ErrorReader")
347 func (ErrorReader) Close() error {
351 func (ErrorReader) Len() uint64 {
355 func (ErrorReader) Seek(int64, int) (int64, error) {
356 return 0, errors.New("ErrorReader")
359 func (KeepReadErrorTestClient) ManifestFileReader(m manifest.Manifest, filename string) (keepclient.Reader, error) {
360 return ErrorReader{}, nil
363 func (s *TestSuite) TestLoadImageArvError(c *C) {
365 cr := NewContainerRunner(ArvErrorTestClient{}, &KeepTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
366 cr.Container.ContainerImage = hwPDH
368 err := cr.LoadImage()
369 c.Check(err.Error(), Equals, "While getting container image collection: ArvError")
372 func (s *TestSuite) TestLoadImageKeepError(c *C) {
374 docker := NewTestDockerClient()
375 cr := NewContainerRunner(&ArvTestClient{}, KeepErrorTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
376 cr.Container.ContainerImage = hwPDH
378 err := cr.LoadImage()
379 c.Check(err.Error(), Equals, "While creating ManifestFileReader for container image: KeepError")
382 func (s *TestSuite) TestLoadImageCollectionError(c *C) {
383 // (3) Collection doesn't contain image
384 cr := NewContainerRunner(&ArvTestClient{}, KeepErrorTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
385 cr.Container.ContainerImage = otherPDH
387 err := cr.LoadImage()
388 c.Check(err.Error(), Equals, "First file in the container image collection does not end in .tar")
391 func (s *TestSuite) TestLoadImageKeepReadError(c *C) {
392 // (4) Collection doesn't contain image
393 docker := NewTestDockerClient()
394 cr := NewContainerRunner(&ArvTestClient{}, KeepReadErrorTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
395 cr.Container.ContainerImage = hwPDH
397 err := cr.LoadImage()
401 type ClosableBuffer struct {
405 func (*ClosableBuffer) Close() error {
409 type TestLogs struct {
410 Stdout ClosableBuffer
411 Stderr ClosableBuffer
414 func (tl *TestLogs) NewTestLoggingWriter(logstr string) io.WriteCloser {
415 if logstr == "stdout" {
418 if logstr == "stderr" {
424 func dockerLog(fd byte, msg string) []byte {
426 header := make([]byte, 8+len(by))
428 header[7] = byte(len(by))
433 func (s *TestSuite) TestRunContainer(c *C) {
434 docker := NewTestDockerClient()
435 docker.fn = func(t *TestDockerClient) {
436 t.logWriter.Write(dockerLog(1, "Hello world\n"))
438 t.finish <- dockerclient.WaitResult{}
440 cr := NewContainerRunner(&ArvTestClient{}, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
443 cr.NewLogWriter = logs.NewTestLoggingWriter
444 cr.Container.ContainerImage = hwPDH
445 cr.Container.Command = []string{"./hw"}
446 err := cr.LoadImage()
449 err = cr.CreateContainer()
452 err = cr.StartContainer()
455 err = cr.WaitFinish()
458 c.Check(strings.HasSuffix(logs.Stdout.String(), "Hello world\n"), Equals, true)
459 c.Check(logs.Stderr.String(), Equals, "")
462 func (s *TestSuite) TestCommitLogs(c *C) {
463 api := &ArvTestClient{}
464 kc := &KeepTestClient{}
465 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
466 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
468 cr.CrunchLog.Print("Hello world!")
469 cr.CrunchLog.Print("Goodbye")
470 cr.finalState = "Complete"
472 err := cr.CommitLogs()
475 c.Check(api.Calls, Equals, 2)
476 c.Check(api.Content[1]["collection"].(arvadosclient.Dict)["name"], Equals, "logs for zzzzz-zzzzz-zzzzzzzzzzzzzzz")
477 c.Check(api.Content[1]["collection"].(arvadosclient.Dict)["manifest_text"], Equals, ". 744b2e4553123b02fa7b452ec5c18993+123 0:123:crunch-run.txt\n")
478 c.Check(*cr.LogsPDH, Equals, "63da7bdacf08c40f604daad80c261e9a+60")
481 func (s *TestSuite) TestUpdateContainerRunning(c *C) {
482 api := &ArvTestClient{}
483 kc := &KeepTestClient{}
484 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
486 err := cr.UpdateContainerRunning()
489 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Running")
492 func (s *TestSuite) TestUpdateContainerComplete(c *C) {
493 api := &ArvTestClient{}
494 kc := &KeepTestClient{}
495 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
497 cr.LogsPDH = new(string)
498 *cr.LogsPDH = "d3a229d2fe3690c2c3e75a71a153c6a3+60"
500 cr.ExitCode = new(int)
502 cr.finalState = "Complete"
504 err := cr.UpdateContainerFinal()
507 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], Equals, *cr.LogsPDH)
508 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], Equals, *cr.ExitCode)
509 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
512 func (s *TestSuite) TestUpdateContainerCancelled(c *C) {
513 api := &ArvTestClient{}
514 kc := &KeepTestClient{}
515 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
517 cr.finalState = "Cancelled"
519 err := cr.UpdateContainerFinal()
522 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], IsNil)
523 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], IsNil)
524 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Cancelled")
527 // Used by the TestFullRun*() test below to DRY up boilerplate setup to do full
528 // dress rehearsal of the Run() function, starting from a JSON container record.
529 func FullRunHelper(c *C, record string, fn func(t *TestDockerClient)) (api *ArvTestClient, cr *ContainerRunner) {
530 rec := arvados.Container{}
531 err := json.Unmarshal([]byte(record), &rec)
534 docker := NewTestDockerClient()
536 docker.RemoveImage(hwImageId, true)
538 api = &ArvTestClient{Container: rec}
540 cr = NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
541 cr.statInterval = 100 * time.Millisecond
542 am := &ArvMountCmdLine{}
543 cr.RunArvMount = am.ArvMountTest
547 c.Check(api.WasSetRunning, Equals, true)
549 c.Check(api.Content[api.Calls-1]["container"].(arvadosclient.Dict)["log"], NotNil)
552 for k, v := range api.Logs {
561 func (s *TestSuite) TestFullRunHello(c *C) {
562 api, _ := FullRunHelper(c, `{
563 "command": ["echo", "hello world"],
564 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
567 "mounts": {"/tmp": {"kind": "tmp"} },
568 "output_path": "/tmp",
570 "runtime_constraints": {}
571 }`, func(t *TestDockerClient) {
572 t.logWriter.Write(dockerLog(1, "hello world\n"))
574 t.finish <- dockerclient.WaitResult{}
577 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
578 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
579 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello world\n"), Equals, true)
583 func (s *TestSuite) TestCrunchstat(c *C) {
584 api, _ := FullRunHelper(c, `{
585 "command": ["sleep", "1"],
586 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
589 "mounts": {"/tmp": {"kind": "tmp"} },
590 "output_path": "/tmp",
592 "runtime_constraints": {}
593 }`, func(t *TestDockerClient) {
594 time.Sleep(time.Second)
596 t.finish <- dockerclient.WaitResult{}
599 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
600 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
602 // We didn't actually start a container, so crunchstat didn't
603 // find accounting files and therefore didn't log any stats.
604 // It should have logged a "can't find accounting files"
605 // message after one poll interval, though, so we can confirm
607 c.Assert(api.Logs["crunchstat"], NotNil)
608 c.Check(api.Logs["crunchstat"].String(), Matches, `(?ms).*cgroup stats files have not appeared after 100ms.*`)
610 // The "files never appeared" log assures us that we called
611 // (*crunchstat.Reporter)Stop(), and that we set it up with
612 // the correct container ID "abcde":
613 c.Check(api.Logs["crunchstat"].String(), Matches, `(?ms).*cgroup stats files never appeared for abcde\n`)
616 func (s *TestSuite) TestFullRunStderr(c *C) {
617 api, _ := FullRunHelper(c, `{
618 "command": ["/bin/sh", "-c", "echo hello ; echo world 1>&2 ; exit 1"],
619 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
622 "mounts": {"/tmp": {"kind": "tmp"} },
623 "output_path": "/tmp",
625 "runtime_constraints": {}
626 }`, func(t *TestDockerClient) {
627 t.logWriter.Write(dockerLog(1, "hello\n"))
628 t.logWriter.Write(dockerLog(2, "world\n"))
630 t.finish <- dockerclient.WaitResult{ExitCode: 1}
633 final := api.CalledWith("container.state", "Complete")
634 c.Assert(final, NotNil)
635 c.Check(final["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
636 c.Check(final["container"].(arvadosclient.Dict)["log"], NotNil)
638 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello\n"), Equals, true)
639 c.Check(strings.HasSuffix(api.Logs["stderr"].String(), "world\n"), Equals, true)
642 func (s *TestSuite) TestFullRunDefaultCwd(c *C) {
643 api, _ := FullRunHelper(c, `{
645 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
648 "mounts": {"/tmp": {"kind": "tmp"} },
649 "output_path": "/tmp",
651 "runtime_constraints": {}
652 }`, func(t *TestDockerClient) {
653 t.logWriter.Write(dockerLog(1, t.cwd+"\n"))
655 t.finish <- dockerclient.WaitResult{ExitCode: 0}
658 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
659 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
660 c.Log(api.Logs["stdout"])
661 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/\n"), Equals, true)
664 func (s *TestSuite) TestFullRunSetCwd(c *C) {
665 api, _ := FullRunHelper(c, `{
667 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
670 "mounts": {"/tmp": {"kind": "tmp"} },
671 "output_path": "/tmp",
673 "runtime_constraints": {}
674 }`, func(t *TestDockerClient) {
675 t.logWriter.Write(dockerLog(1, t.cwd+"\n"))
677 t.finish <- dockerclient.WaitResult{ExitCode: 0}
680 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
681 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
682 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/bin\n"), Equals, true)
685 func (s *TestSuite) TestCancel(c *C) {
687 "command": ["/bin/sh", "-c", "echo foo && sleep 30 && echo bar"],
688 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
691 "mounts": {"/tmp": {"kind": "tmp"} },
692 "output_path": "/tmp",
694 "runtime_constraints": {}
697 rec := arvados.Container{}
698 err := json.Unmarshal([]byte(record), &rec)
701 docker := NewTestDockerClient()
702 docker.fn = func(t *TestDockerClient) {
704 t.logWriter.Write(dockerLog(1, "foo\n"))
706 t.finish <- dockerclient.WaitResult{ExitCode: 0}
708 docker.RemoveImage(hwImageId, true)
710 api := &ArvTestClient{Container: rec}
711 cr := NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
712 am := &ArvMountCmdLine{}
713 cr.RunArvMount = am.ArvMountTest
716 for cr.ContainerID == "" {
717 time.Sleep(time.Millisecond)
719 cr.SigChan <- syscall.SIGINT
726 for k, v := range api.Logs {
732 c.Check(api.CalledWith("container.log", nil), NotNil)
733 c.Check(api.CalledWith("container.state", "Cancelled"), NotNil)
734 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "foo\n"), Equals, true)
738 func (s *TestSuite) TestFullRunSetEnv(c *C) {
739 api, _ := FullRunHelper(c, `{
740 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
741 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
743 "environment": {"FROBIZ": "bilbo"},
744 "mounts": {"/tmp": {"kind": "tmp"} },
745 "output_path": "/tmp",
747 "runtime_constraints": {}
748 }`, func(t *TestDockerClient) {
749 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
751 t.finish <- dockerclient.WaitResult{ExitCode: 0}
754 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
755 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
756 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "bilbo\n"), Equals, true)
759 type ArvMountCmdLine struct {
764 func (am *ArvMountCmdLine) ArvMountTest(c []string, token string) (*exec.Cmd, error) {
770 func stubCert(temp string) string {
771 path := temp + "/ca-certificates.crt"
772 crt, _ := os.Create(path)
774 arvadosclient.CertFiles = []string{path}
778 func (s *TestSuite) TestSetupMounts(c *C) {
779 api := &ArvTestClient{}
780 kc := &KeepTestClient{}
781 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
782 am := &ArvMountCmdLine{}
783 cr.RunArvMount = am.ArvMountTest
785 realTemp, err := ioutil.TempDir("", "crunchrun_test1-")
787 certTemp, err := ioutil.TempDir("", "crunchrun_test2-")
789 stubCertPath := stubCert(certTemp)
791 defer os.RemoveAll(realTemp)
792 defer os.RemoveAll(certTemp)
795 cr.MkTempDir = func(_ string, prefix string) (string, error) {
797 d := fmt.Sprintf("%s/%s%d", realTemp, prefix, i)
798 err := os.Mkdir(d, os.ModePerm)
799 if err != nil && strings.Contains(err.Error(), ": file exists") {
800 // Test case must have pre-populated the tempdir
806 checkEmpty := func() {
807 filepath.Walk(realTemp, func(path string, _ os.FileInfo, err error) error {
808 c.Check(path, Equals, realTemp)
816 cr.Container.Mounts = make(map[string]arvados.Mount)
817 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
818 cr.OutputPath = "/tmp"
820 err := cr.SetupMounts()
822 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
823 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2:/tmp"})
830 cr.Container.Mounts = make(map[string]arvados.Mount)
831 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
832 cr.OutputPath = "/tmp"
835 cr.Container.RuntimeConstraints.API = &apiflag
837 err := cr.SetupMounts()
839 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
840 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2:/tmp", stubCertPath + ":/etc/arvados/ca-certificates.crt:ro"})
849 cr.Container.Mounts = map[string]arvados.Mount{
850 "/keeptmp": {Kind: "collection", Writable: true},
852 cr.OutputPath = "/keeptmp"
854 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
856 err := cr.SetupMounts()
858 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
859 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/keep1/tmp0:/keeptmp"})
866 cr.Container.Mounts = map[string]arvados.Mount{
867 "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
868 "/keepout": {Kind: "collection", Writable: true},
870 cr.OutputPath = "/keepout"
872 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
873 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
875 err := cr.SetupMounts()
877 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
878 sort.StringSlice(cr.Binds).Sort()
879 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53:/keepinp:ro",
880 realTemp + "/keep1/tmp0:/keepout"})
887 cr.Container.RuntimeConstraints.KeepCacheRAM = 512
888 cr.Container.Mounts = map[string]arvados.Mount{
889 "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
890 "/keepout": {Kind: "collection", Writable: true},
892 cr.OutputPath = "/keepout"
894 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
895 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
897 err := cr.SetupMounts()
899 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--file-cache", "512", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
900 sort.StringSlice(cr.Binds).Sort()
901 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53:/keepinp:ro",
902 realTemp + "/keep1/tmp0:/keepout"})
907 for _, test := range []struct {
911 {in: "foo", out: `"foo"`},
912 {in: nil, out: `null`},
913 {in: map[string]int{"foo": 123}, out: `{"foo":123}`},
916 cr.Container.Mounts = map[string]arvados.Mount{
917 "/mnt/test.json": {Kind: "json", Content: test.in},
919 err := cr.SetupMounts()
921 sort.StringSlice(cr.Binds).Sort()
922 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2/mountdata.json:/mnt/test.json:ro"})
923 content, err := ioutil.ReadFile(realTemp + "/2/mountdata.json")
925 c.Check(content, DeepEquals, []byte(test.out))
931 func (s *TestSuite) TestStdout(c *C) {
933 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
934 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
936 "environment": {"FROBIZ": "bilbo"},
937 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"} },
938 "output_path": "/tmp",
940 "runtime_constraints": {}
943 api, _ := FullRunHelper(c, helperRecord, func(t *TestDockerClient) {
944 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
946 t.finish <- dockerclient.WaitResult{ExitCode: 0}
949 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
950 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
951 c.Check(api.CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), NotNil)
954 // Used by the TestStdoutWithWrongPath*()
955 func StdoutErrorRunHelper(c *C, record string, fn func(t *TestDockerClient)) (api *ArvTestClient, cr *ContainerRunner, err error) {
956 rec := arvados.Container{}
957 err = json.Unmarshal([]byte(record), &rec)
960 docker := NewTestDockerClient()
962 docker.RemoveImage(hwImageId, true)
964 api = &ArvTestClient{Container: rec}
965 cr = NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
966 am := &ArvMountCmdLine{}
967 cr.RunArvMount = am.ArvMountTest
973 func (s *TestSuite) TestStdoutWithWrongPath(c *C) {
974 _, _, err := StdoutErrorRunHelper(c, `{
975 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path":"/tmpa.out"} },
976 "output_path": "/tmp"
977 }`, func(t *TestDockerClient) {})
980 c.Check(strings.Contains(err.Error(), "Stdout path does not start with OutputPath"), Equals, true)
983 func (s *TestSuite) TestStdoutWithWrongKindTmp(c *C) {
984 _, _, err := StdoutErrorRunHelper(c, `{
985 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "tmp", "path":"/tmp/a.out"} },
986 "output_path": "/tmp"
987 }`, func(t *TestDockerClient) {})
990 c.Check(strings.Contains(err.Error(), "Unsupported mount kind 'tmp' for stdout"), Equals, true)
993 func (s *TestSuite) TestStdoutWithWrongKindCollection(c *C) {
994 _, _, err := StdoutErrorRunHelper(c, `{
995 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "collection", "path":"/tmp/a.out"} },
996 "output_path": "/tmp"
997 }`, func(t *TestDockerClient) {})
1000 c.Check(strings.Contains(err.Error(), "Unsupported mount kind 'collection' for stdout"), Equals, true)
1003 func (s *TestSuite) TestFullRunWithAPI(c *C) {
1004 os.Setenv("ARVADOS_API_HOST", "test.arvados.org")
1005 defer os.Unsetenv("ARVADOS_API_HOST")
1006 api, _ := FullRunHelper(c, `{
1007 "command": ["/bin/sh", "-c", "echo $ARVADOS_API_HOST"],
1008 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1011 "mounts": {"/tmp": {"kind": "tmp"} },
1012 "output_path": "/tmp",
1014 "runtime_constraints": {"API": true}
1015 }`, func(t *TestDockerClient) {
1016 t.logWriter.Write(dockerLog(1, t.env[1][17:]+"\n"))
1018 t.finish <- dockerclient.WaitResult{ExitCode: 0}
1021 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1022 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1023 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "test.arvados.org\n"), Equals, true)
1024 c.Check(api.CalledWith("container.output", "d41d8cd98f00b204e9800998ecf8427e+0"), NotNil)
1027 func (s *TestSuite) TestFullRunSetOutput(c *C) {
1028 os.Setenv("ARVADOS_API_HOST", "test.arvados.org")
1029 defer os.Unsetenv("ARVADOS_API_HOST")
1030 api, _ := FullRunHelper(c, `{
1031 "command": ["/bin/sh", "-c", "echo $ARVADOS_API_HOST"],
1032 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1035 "mounts": {"/tmp": {"kind": "tmp"} },
1036 "output_path": "/tmp",
1038 "runtime_constraints": {"API": true}
1039 }`, func(t *TestDockerClient) {
1040 t.api.Container.Output = "d4ab34d3d4f8a72f5c4973051ae69fab+122"
1042 t.finish <- dockerclient.WaitResult{ExitCode: 0}
1045 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1046 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1047 c.Check(api.CalledWith("container.output", "d4ab34d3d4f8a72f5c4973051ae69fab+122"), NotNil)