1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
29 "git.curoverse.com/arvados.git/sdk/go/arvados"
30 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
31 "git.curoverse.com/arvados.git/sdk/go/manifest"
33 dockertypes "github.com/docker/docker/api/types"
34 dockercontainer "github.com/docker/docker/api/types/container"
35 dockernetwork "github.com/docker/docker/api/types/network"
39 // Gocheck boilerplate
40 func TestCrunchExec(t *testing.T) {
44 type TestSuite struct{}
46 // Gocheck boilerplate
47 var _ = Suite(&TestSuite{})
49 type ArvTestClient struct {
52 Content []arvadosclient.Dict
54 Logs map[string]*bytes.Buffer
60 type KeepTestClient struct {
65 var hwManifest = ". 82ab40c24fc8df01798e57ba66795bb1+841216+Aa124ac75e5168396c73c0a18eda641a4f41791c0@569fa8c3 0:841216:9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7.tar\n"
66 var hwPDH = "a45557269dcb65a6b78f9ac061c0850b+120"
67 var hwImageId = "9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7"
69 var otherManifest = ". 68a84f561b1d1708c6baff5e019a9ab3+46+Ae5d0af96944a3690becb1decdf60cc1c937f556d@5693216f 0:46:md5sum.txt\n"
70 var otherPDH = "a3e8f74c6f101eae01fa08bfb4e49b3a+54"
72 var normalizedManifestWithSubdirs = `. 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0abcdefgh11234567890@569fa8c3 0:9:file1_in_main.txt 9:18:file2_in_main.txt 0:27:zzzzz-8i9sb-bcdefghijkdhvnk.log.txt
73 ./subdir1 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 0:9:file1_in_subdir1.txt 9:18:file2_in_subdir1.txt
74 ./subdir1/subdir2 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0bcdefghijk544332211@569fa8c5 0:9:file1_in_subdir2.txt 9:18:file2_in_subdir2.txt
77 var normalizedWithSubdirsPDH = "a0def87f80dd594d4675809e83bd4f15+367"
79 var denormalizedManifestWithSubdirs = ". 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0abcdefgh11234567890@569fa8c3 0:9:file1_in_main.txt 9:18:file2_in_main.txt 0:27:zzzzz-8i9sb-bcdefghijkdhvnk.log.txt 0:10:subdir1/file1_in_subdir1.txt 10:17:subdir1/file2_in_subdir1.txt\n"
80 var denormalizedWithSubdirsPDH = "b0def87f80dd594d4675809e83bd4f15+367"
82 var fakeAuthUUID = "zzzzz-gj3su-55pqoyepgi2glem"
83 var fakeAuthToken = "a3ltuwzqcu2u4sc0q7yhpc2w7s00fdcqecg5d6e0u3pfohmbjt"
85 type TestDockerClient struct {
87 logReader io.ReadCloser
88 logWriter io.WriteCloser
89 fn func(t *TestDockerClient)
98 func NewTestDockerClient(exitCode int) *TestDockerClient {
99 t := &TestDockerClient{}
100 t.logReader, t.logWriter = io.Pipe()
102 t.stop = make(chan bool, 1)
107 type MockConn struct {
111 func (m *MockConn) Write(b []byte) (int, error) {
115 func NewMockConn() *MockConn {
120 func (t *TestDockerClient) ContainerAttach(ctx context.Context, container string, options dockertypes.ContainerAttachOptions) (dockertypes.HijackedResponse, error) {
121 return dockertypes.HijackedResponse{Conn: NewMockConn(), Reader: bufio.NewReader(t.logReader)}, nil
124 func (t *TestDockerClient) ContainerCreate(ctx context.Context, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig, networkingConfig *dockernetwork.NetworkingConfig, containerName string) (dockercontainer.ContainerCreateCreatedBody, error) {
125 if config.WorkingDir != "" {
126 t.cwd = config.WorkingDir
129 return dockercontainer.ContainerCreateCreatedBody{ID: "abcde"}, nil
132 func (t *TestDockerClient) ContainerStart(ctx context.Context, container string, options dockertypes.ContainerStartOptions) error {
133 if container == "abcde" {
134 // t.fn gets executed in ContainerWait
137 return errors.New("Invalid container id")
141 func (t *TestDockerClient) ContainerStop(ctx context.Context, container string, timeout *time.Duration) error {
146 func (t *TestDockerClient) ContainerWait(ctx context.Context, container string, condition dockercontainer.WaitCondition) (<-chan dockercontainer.ContainerWaitOKBody, <-chan error) {
147 body := make(chan dockercontainer.ContainerWaitOKBody)
148 err := make(chan error)
151 body <- dockercontainer.ContainerWaitOKBody{StatusCode: int64(t.finish)}
158 func (t *TestDockerClient) ImageInspectWithRaw(ctx context.Context, image string) (dockertypes.ImageInspect, []byte, error) {
160 return dockertypes.ImageInspect{}, nil, fmt.Errorf("Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?")
163 if t.imageLoaded == image {
164 return dockertypes.ImageInspect{}, nil, nil
166 return dockertypes.ImageInspect{}, nil, errors.New("")
170 func (t *TestDockerClient) ImageLoad(ctx context.Context, input io.Reader, quiet bool) (dockertypes.ImageLoadResponse, error) {
172 return dockertypes.ImageLoadResponse{}, fmt.Errorf("Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?")
174 _, err := io.Copy(ioutil.Discard, input)
176 return dockertypes.ImageLoadResponse{}, err
178 t.imageLoaded = hwImageId
179 return dockertypes.ImageLoadResponse{Body: ioutil.NopCloser(input)}, nil
183 func (*TestDockerClient) ImageRemove(ctx context.Context, image string, options dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error) {
187 func (client *ArvTestClient) Create(resourceType string,
188 parameters arvadosclient.Dict,
189 output interface{}) error {
192 defer client.Mutex.Unlock()
195 client.Content = append(client.Content, parameters)
197 if resourceType == "logs" {
198 et := parameters["log"].(arvadosclient.Dict)["event_type"].(string)
199 if client.Logs == nil {
200 client.Logs = make(map[string]*bytes.Buffer)
202 if client.Logs[et] == nil {
203 client.Logs[et] = &bytes.Buffer{}
205 client.Logs[et].Write([]byte(parameters["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"]))
208 if resourceType == "collections" && output != nil {
209 mt := parameters["collection"].(arvadosclient.Dict)["manifest_text"].(string)
210 outmap := output.(*arvados.Collection)
211 outmap.PortableDataHash = fmt.Sprintf("%x+%d", md5.Sum([]byte(mt)), len(mt))
217 func (client *ArvTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
219 case method == "GET" && resourceType == "containers" && action == "auth":
220 return json.Unmarshal([]byte(`{
221 "kind": "arvados#api_client_authorization",
222 "uuid": "`+fakeAuthUUID+`",
223 "api_token": "`+fakeAuthToken+`"
226 return fmt.Errorf("Not found")
230 func (client *ArvTestClient) CallRaw(method, resourceType, uuid, action string,
231 parameters arvadosclient.Dict) (reader io.ReadCloser, err error) {
233 if method == "GET" && resourceType == "containers" && action == "" && !client.callraw {
234 j, err = json.Marshal(client.Container)
237 "command": ["sleep", "1"],
238 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
241 "mounts": {"/tmp": {"kind": "tmp"}, "/json": {"kind": "json", "content": {"number": 123456789123456789}}},
242 "output_path": "/tmp",
244 "runtime_constraints": {}
247 return ioutil.NopCloser(bytes.NewReader(j)), err
250 func (client *ArvTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
251 if resourceType == "collections" {
253 output.(*arvados.Collection).ManifestText = hwManifest
254 } else if uuid == otherPDH {
255 output.(*arvados.Collection).ManifestText = otherManifest
256 } else if uuid == normalizedWithSubdirsPDH {
257 output.(*arvados.Collection).ManifestText = normalizedManifestWithSubdirs
258 } else if uuid == denormalizedWithSubdirsPDH {
259 output.(*arvados.Collection).ManifestText = denormalizedManifestWithSubdirs
262 if resourceType == "containers" {
263 (*output.(*arvados.Container)) = client.Container
268 func (client *ArvTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
270 defer client.Mutex.Unlock()
272 client.Content = append(client.Content, parameters)
273 if resourceType == "containers" {
274 if parameters["container"].(arvadosclient.Dict)["state"] == "Running" {
275 client.WasSetRunning = true
281 var discoveryMap = map[string]interface{}{
282 "defaultTrashLifetime": float64(1209600),
283 "crunchLimitLogBytesPerJob": float64(67108864),
284 "crunchLogThrottleBytes": float64(65536),
285 "crunchLogThrottlePeriod": float64(60),
286 "crunchLogThrottleLines": float64(1024),
287 "crunchLogPartialLineThrottlePeriod": float64(5),
288 "crunchLogBytesPerEvent": float64(4096),
289 "crunchLogSecondsBetweenEvents": float64(1),
292 func (client *ArvTestClient) Discovery(key string) (interface{}, error) {
293 return discoveryMap[key], nil
296 // CalledWith returns the parameters from the first API call whose
297 // parameters match jpath/string. E.g., CalledWith(c, "foo.bar",
298 // "baz") returns parameters with parameters["foo"]["bar"]=="baz". If
299 // no call matches, it returns nil.
300 func (client *ArvTestClient) CalledWith(jpath string, expect interface{}) arvadosclient.Dict {
302 for _, content := range client.Content {
303 var v interface{} = content
304 for _, k := range strings.Split(jpath, ".") {
305 if dict, ok := v.(arvadosclient.Dict); !ok {
318 func (client *KeepTestClient) PutHB(hash string, buf []byte) (string, int, error) {
320 return fmt.Sprintf("%s+%d", hash, len(buf)), len(buf), nil
323 func (*KeepTestClient) ClearBlockCache() {
326 type FileWrapper struct {
331 func (fw FileWrapper) Readdir(n int) ([]os.FileInfo, error) {
332 return nil, errors.New("not implemented")
335 func (fw FileWrapper) Seek(int64, int) (int64, error) {
336 return 0, errors.New("not implemented")
339 func (fw FileWrapper) Size() int64 {
343 func (fw FileWrapper) Stat() (os.FileInfo, error) {
344 return nil, errors.New("not implemented")
347 func (fw FileWrapper) Truncate(int64) error {
348 return errors.New("not implemented")
351 func (fw FileWrapper) Write([]byte) (int, error) {
352 return 0, errors.New("not implemented")
355 func (client *KeepTestClient) ManifestFileReader(m manifest.Manifest, filename string) (arvados.File, error) {
356 if filename == hwImageId+".tar" {
357 rdr := ioutil.NopCloser(&bytes.Buffer{})
359 return FileWrapper{rdr, 1321984}, nil
360 } else if filename == "/file1_in_main.txt" {
361 rdr := ioutil.NopCloser(strings.NewReader("foo"))
363 return FileWrapper{rdr, 3}, nil
368 func (s *TestSuite) TestLoadImage(c *C) {
369 kc := &KeepTestClient{}
370 docker := NewTestDockerClient(0)
371 cr := NewContainerRunner(&ArvTestClient{}, kc, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
373 _, err := cr.Docker.ImageRemove(nil, hwImageId, dockertypes.ImageRemoveOptions{})
375 _, _, err = cr.Docker.ImageInspectWithRaw(nil, hwImageId)
378 cr.Container.ContainerImage = hwPDH
380 // (1) Test loading image from keep
381 c.Check(kc.Called, Equals, false)
382 c.Check(cr.ContainerConfig.Image, Equals, "")
388 cr.Docker.ImageRemove(nil, hwImageId, dockertypes.ImageRemoveOptions{})
391 c.Check(kc.Called, Equals, true)
392 c.Check(cr.ContainerConfig.Image, Equals, hwImageId)
394 _, _, err = cr.Docker.ImageInspectWithRaw(nil, hwImageId)
397 // (2) Test using image that's already loaded
399 cr.ContainerConfig.Image = ""
403 c.Check(kc.Called, Equals, false)
404 c.Check(cr.ContainerConfig.Image, Equals, hwImageId)
408 type ArvErrorTestClient struct{}
410 func (ArvErrorTestClient) Create(resourceType string,
411 parameters arvadosclient.Dict,
412 output interface{}) error {
416 func (ArvErrorTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
417 return errors.New("ArvError")
420 func (ArvErrorTestClient) CallRaw(method, resourceType, uuid, action string,
421 parameters arvadosclient.Dict) (reader io.ReadCloser, err error) {
422 return nil, errors.New("ArvError")
425 func (ArvErrorTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
426 return errors.New("ArvError")
429 func (ArvErrorTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
433 func (ArvErrorTestClient) Discovery(key string) (interface{}, error) {
434 return discoveryMap[key], nil
437 type KeepErrorTestClient struct{}
439 func (KeepErrorTestClient) PutHB(hash string, buf []byte) (string, int, error) {
440 return "", 0, errors.New("KeepError")
443 func (KeepErrorTestClient) ManifestFileReader(m manifest.Manifest, filename string) (arvados.File, error) {
444 return nil, errors.New("KeepError")
447 func (KeepErrorTestClient) ClearBlockCache() {
450 type KeepReadErrorTestClient struct{}
452 func (KeepReadErrorTestClient) PutHB(hash string, buf []byte) (string, int, error) {
456 func (KeepReadErrorTestClient) ClearBlockCache() {
459 type ErrorReader struct {
463 func (ErrorReader) Read(p []byte) (n int, err error) {
464 return 0, errors.New("ErrorReader")
467 func (ErrorReader) Seek(int64, int) (int64, error) {
468 return 0, errors.New("ErrorReader")
471 func (KeepReadErrorTestClient) ManifestFileReader(m manifest.Manifest, filename string) (arvados.File, error) {
472 return ErrorReader{}, nil
475 func (s *TestSuite) TestLoadImageArvError(c *C) {
477 cr := NewContainerRunner(ArvErrorTestClient{}, &KeepTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
478 cr.Container.ContainerImage = hwPDH
480 err := cr.LoadImage()
481 c.Check(err.Error(), Equals, "While getting container image collection: ArvError")
484 func (s *TestSuite) TestLoadImageKeepError(c *C) {
486 docker := NewTestDockerClient(0)
487 cr := NewContainerRunner(&ArvTestClient{}, KeepErrorTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
488 cr.Container.ContainerImage = hwPDH
490 err := cr.LoadImage()
491 c.Check(err.Error(), Equals, "While creating ManifestFileReader for container image: KeepError")
494 func (s *TestSuite) TestLoadImageCollectionError(c *C) {
495 // (3) Collection doesn't contain image
496 cr := NewContainerRunner(&ArvTestClient{}, KeepErrorTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
497 cr.Container.ContainerImage = otherPDH
499 err := cr.LoadImage()
500 c.Check(err.Error(), Equals, "First file in the container image collection does not end in .tar")
503 func (s *TestSuite) TestLoadImageKeepReadError(c *C) {
504 // (4) Collection doesn't contain image
505 docker := NewTestDockerClient(0)
506 cr := NewContainerRunner(&ArvTestClient{}, KeepReadErrorTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
507 cr.Container.ContainerImage = hwPDH
509 err := cr.LoadImage()
513 type ClosableBuffer struct {
517 func (*ClosableBuffer) Close() error {
521 type TestLogs struct {
522 Stdout ClosableBuffer
523 Stderr ClosableBuffer
526 func (tl *TestLogs) NewTestLoggingWriter(logstr string) io.WriteCloser {
527 if logstr == "stdout" {
530 if logstr == "stderr" {
536 func dockerLog(fd byte, msg string) []byte {
538 header := make([]byte, 8+len(by))
540 header[7] = byte(len(by))
545 func (s *TestSuite) TestRunContainer(c *C) {
546 docker := NewTestDockerClient(0)
547 docker.fn = func(t *TestDockerClient) {
548 t.logWriter.Write(dockerLog(1, "Hello world\n"))
551 cr := NewContainerRunner(&ArvTestClient{}, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
554 cr.NewLogWriter = logs.NewTestLoggingWriter
555 cr.Container.ContainerImage = hwPDH
556 cr.Container.Command = []string{"./hw"}
557 err := cr.LoadImage()
560 err = cr.CreateContainer()
563 err = cr.StartContainer()
566 err = cr.WaitFinish()
569 c.Check(strings.HasSuffix(logs.Stdout.String(), "Hello world\n"), Equals, true)
570 c.Check(logs.Stderr.String(), Equals, "")
573 func (s *TestSuite) TestCommitLogs(c *C) {
574 api := &ArvTestClient{}
575 kc := &KeepTestClient{}
576 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
577 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
579 cr.CrunchLog.Print("Hello world!")
580 cr.CrunchLog.Print("Goodbye")
581 cr.finalState = "Complete"
583 err := cr.CommitLogs()
586 c.Check(api.Calls, Equals, 2)
587 c.Check(api.Content[1]["ensure_unique_name"], Equals, true)
588 c.Check(api.Content[1]["collection"].(arvadosclient.Dict)["name"], Equals, "logs for zzzzz-zzzzz-zzzzzzzzzzzzzzz")
589 c.Check(api.Content[1]["collection"].(arvadosclient.Dict)["manifest_text"], Equals, ". 744b2e4553123b02fa7b452ec5c18993+123 0:123:crunch-run.txt\n")
590 c.Check(*cr.LogsPDH, Equals, "63da7bdacf08c40f604daad80c261e9a+60")
593 func (s *TestSuite) TestUpdateContainerRunning(c *C) {
594 api := &ArvTestClient{}
595 kc := &KeepTestClient{}
596 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
598 err := cr.UpdateContainerRunning()
601 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Running")
604 func (s *TestSuite) TestUpdateContainerComplete(c *C) {
605 api := &ArvTestClient{}
606 kc := &KeepTestClient{}
607 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
609 cr.LogsPDH = new(string)
610 *cr.LogsPDH = "d3a229d2fe3690c2c3e75a71a153c6a3+60"
612 cr.ExitCode = new(int)
614 cr.finalState = "Complete"
616 err := cr.UpdateContainerFinal()
619 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], Equals, *cr.LogsPDH)
620 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], Equals, *cr.ExitCode)
621 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
624 func (s *TestSuite) TestUpdateContainerCancelled(c *C) {
625 api := &ArvTestClient{}
626 kc := &KeepTestClient{}
627 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
629 cr.finalState = "Cancelled"
631 err := cr.UpdateContainerFinal()
634 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], IsNil)
635 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], IsNil)
636 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Cancelled")
639 // Used by the TestFullRun*() test below to DRY up boilerplate setup to do full
640 // dress rehearsal of the Run() function, starting from a JSON container record.
641 func FullRunHelper(c *C, record string, extraMounts []string, exitCode int, fn func(t *TestDockerClient)) (api *ArvTestClient, cr *ContainerRunner, realTemp string) {
642 rec := arvados.Container{}
643 err := json.Unmarshal([]byte(record), &rec)
646 docker := NewTestDockerClient(exitCode)
648 docker.ImageRemove(nil, hwImageId, dockertypes.ImageRemoveOptions{})
650 api = &ArvTestClient{Container: rec}
652 cr = NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
653 cr.statInterval = 100 * time.Millisecond
654 am := &ArvMountCmdLine{}
655 cr.RunArvMount = am.ArvMountTest
657 realTemp, err = ioutil.TempDir("", "crunchrun_test1-")
659 defer os.RemoveAll(realTemp)
661 docker.realTemp = realTemp
664 cr.MkTempDir = func(_ string, prefix string) (string, error) {
666 d := fmt.Sprintf("%s/%s%d", realTemp, prefix, tempcount)
667 err := os.Mkdir(d, os.ModePerm)
668 if err != nil && strings.Contains(err.Error(), ": file exists") {
669 // Test case must have pre-populated the tempdir
675 if extraMounts != nil && len(extraMounts) > 0 {
676 err := cr.SetupArvMountPoint("keep")
679 for _, m := range extraMounts {
680 os.MkdirAll(cr.ArvMountPoint+"/by_id/"+m, os.ModePerm)
685 if api.CalledWith("container.state", "Complete") != nil {
689 c.Check(api.WasSetRunning, Equals, true)
690 c.Check(api.Content[api.Calls-2]["container"].(arvadosclient.Dict)["log"], NotNil)
694 for k, v := range api.Logs {
703 func (s *TestSuite) TestFullRunHello(c *C) {
704 api, _, _ := FullRunHelper(c, `{
705 "command": ["echo", "hello world"],
706 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
709 "mounts": {"/tmp": {"kind": "tmp"} },
710 "output_path": "/tmp",
712 "runtime_constraints": {}
713 }`, nil, 0, func(t *TestDockerClient) {
714 t.logWriter.Write(dockerLog(1, "hello world\n"))
718 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
719 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
720 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello world\n"), Equals, true)
724 func (s *TestSuite) TestCrunchstat(c *C) {
725 api, _, _ := FullRunHelper(c, `{
726 "command": ["sleep", "1"],
727 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
730 "mounts": {"/tmp": {"kind": "tmp"} },
731 "output_path": "/tmp",
733 "runtime_constraints": {}
734 }`, nil, 0, func(t *TestDockerClient) {
735 time.Sleep(time.Second)
739 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
740 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
742 // We didn't actually start a container, so crunchstat didn't
743 // find accounting files and therefore didn't log any stats.
744 // It should have logged a "can't find accounting files"
745 // message after one poll interval, though, so we can confirm
747 c.Assert(api.Logs["crunchstat"], NotNil)
748 c.Check(api.Logs["crunchstat"].String(), Matches, `(?ms).*cgroup stats files have not appeared after 100ms.*`)
750 // The "files never appeared" log assures us that we called
751 // (*crunchstat.Reporter)Stop(), and that we set it up with
752 // the correct container ID "abcde":
753 c.Check(api.Logs["crunchstat"].String(), Matches, `(?ms).*cgroup stats files never appeared for abcde\n`)
756 func (s *TestSuite) TestNodeInfoLog(c *C) {
757 api, _, _ := FullRunHelper(c, `{
758 "command": ["sleep", "1"],
759 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
762 "mounts": {"/tmp": {"kind": "tmp"} },
763 "output_path": "/tmp",
765 "runtime_constraints": {}
767 func(t *TestDockerClient) {
768 time.Sleep(time.Second)
772 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
773 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
775 c.Assert(api.Logs["node-info"], NotNil)
776 c.Check(api.Logs["node-info"].String(), Matches, `(?ms).*Host Information.*`)
777 c.Check(api.Logs["node-info"].String(), Matches, `(?ms).*CPU Information.*`)
778 c.Check(api.Logs["node-info"].String(), Matches, `(?ms).*Memory Information.*`)
779 c.Check(api.Logs["node-info"].String(), Matches, `(?ms).*Disk Space.*`)
780 c.Check(api.Logs["node-info"].String(), Matches, `(?ms).*Disk INodes.*`)
783 func (s *TestSuite) TestContainerRecordLog(c *C) {
784 api, _, _ := FullRunHelper(c, `{
785 "command": ["sleep", "1"],
786 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
789 "mounts": {"/tmp": {"kind": "tmp"} },
790 "output_path": "/tmp",
792 "runtime_constraints": {}
794 func(t *TestDockerClient) {
795 time.Sleep(time.Second)
799 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
800 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
802 c.Assert(api.Logs["container"], NotNil)
803 c.Check(api.Logs["container"].String(), Matches, `(?ms).*container_image.*`)
806 func (s *TestSuite) TestFullRunStderr(c *C) {
807 api, _, _ := FullRunHelper(c, `{
808 "command": ["/bin/sh", "-c", "echo hello ; echo world 1>&2 ; exit 1"],
809 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
812 "mounts": {"/tmp": {"kind": "tmp"} },
813 "output_path": "/tmp",
815 "runtime_constraints": {}
816 }`, nil, 1, func(t *TestDockerClient) {
817 t.logWriter.Write(dockerLog(1, "hello\n"))
818 t.logWriter.Write(dockerLog(2, "world\n"))
822 final := api.CalledWith("container.state", "Complete")
823 c.Assert(final, NotNil)
824 c.Check(final["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
825 c.Check(final["container"].(arvadosclient.Dict)["log"], NotNil)
827 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello\n"), Equals, true)
828 c.Check(strings.HasSuffix(api.Logs["stderr"].String(), "world\n"), Equals, true)
831 func (s *TestSuite) TestFullRunDefaultCwd(c *C) {
832 api, _, _ := FullRunHelper(c, `{
834 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
837 "mounts": {"/tmp": {"kind": "tmp"} },
838 "output_path": "/tmp",
840 "runtime_constraints": {}
841 }`, nil, 0, func(t *TestDockerClient) {
842 t.logWriter.Write(dockerLog(1, t.cwd+"\n"))
846 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
847 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
848 c.Log(api.Logs["stdout"])
849 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/\n"), Equals, true)
852 func (s *TestSuite) TestFullRunSetCwd(c *C) {
853 api, _, _ := FullRunHelper(c, `{
855 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
858 "mounts": {"/tmp": {"kind": "tmp"} },
859 "output_path": "/tmp",
861 "runtime_constraints": {}
862 }`, nil, 0, func(t *TestDockerClient) {
863 t.logWriter.Write(dockerLog(1, t.cwd+"\n"))
867 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
868 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
869 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/bin\n"), Equals, true)
872 func (s *TestSuite) TestStopOnSignal(c *C) {
873 s.testStopContainer(c, func(cr *ContainerRunner) {
876 time.Sleep(time.Millisecond)
878 cr.SigChan <- syscall.SIGINT
883 func (s *TestSuite) TestStopOnArvMountDeath(c *C) {
884 s.testStopContainer(c, func(cr *ContainerRunner) {
885 cr.ArvMountExit = make(chan error)
887 cr.ArvMountExit <- exec.Command("true").Run()
888 close(cr.ArvMountExit)
893 func (s *TestSuite) testStopContainer(c *C, setup func(cr *ContainerRunner)) {
895 "command": ["/bin/sh", "-c", "echo foo && sleep 30 && echo bar"],
896 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
899 "mounts": {"/tmp": {"kind": "tmp"} },
900 "output_path": "/tmp",
902 "runtime_constraints": {}
905 rec := arvados.Container{}
906 err := json.Unmarshal([]byte(record), &rec)
909 docker := NewTestDockerClient(0)
910 docker.fn = func(t *TestDockerClient) {
912 t.logWriter.Write(dockerLog(1, "foo\n"))
915 docker.ImageRemove(nil, hwImageId, dockertypes.ImageRemoveOptions{})
917 api := &ArvTestClient{Container: rec}
918 cr := NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
919 cr.RunArvMount = func([]string, string) (*exec.Cmd, error) { return nil, nil }
922 done := make(chan error)
927 case <-time.After(20 * time.Second):
928 pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
933 for k, v := range api.Logs {
938 c.Check(api.CalledWith("container.log", nil), NotNil)
939 c.Check(api.CalledWith("container.state", "Cancelled"), NotNil)
940 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "foo\n"), Equals, true)
943 func (s *TestSuite) TestFullRunSetEnv(c *C) {
944 api, _, _ := FullRunHelper(c, `{
945 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
946 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
948 "environment": {"FROBIZ": "bilbo"},
949 "mounts": {"/tmp": {"kind": "tmp"} },
950 "output_path": "/tmp",
952 "runtime_constraints": {}
953 }`, nil, 0, func(t *TestDockerClient) {
954 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
958 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
959 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
960 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "bilbo\n"), Equals, true)
963 type ArvMountCmdLine struct {
968 func (am *ArvMountCmdLine) ArvMountTest(c []string, token string) (*exec.Cmd, error) {
974 func stubCert(temp string) string {
975 path := temp + "/ca-certificates.crt"
976 crt, _ := os.Create(path)
978 arvadosclient.CertFiles = []string{path}
982 func (s *TestSuite) TestSetupMounts(c *C) {
983 api := &ArvTestClient{}
984 kc := &KeepTestClient{}
985 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
986 am := &ArvMountCmdLine{}
987 cr.RunArvMount = am.ArvMountTest
989 realTemp, err := ioutil.TempDir("", "crunchrun_test1-")
991 certTemp, err := ioutil.TempDir("", "crunchrun_test2-")
993 stubCertPath := stubCert(certTemp)
995 defer os.RemoveAll(realTemp)
996 defer os.RemoveAll(certTemp)
999 cr.MkTempDir = func(_ string, prefix string) (string, error) {
1001 d := fmt.Sprintf("%s/%s%d", realTemp, prefix, i)
1002 err := os.Mkdir(d, os.ModePerm)
1003 if err != nil && strings.Contains(err.Error(), ": file exists") {
1004 // Test case must have pre-populated the tempdir
1010 checkEmpty := func() {
1011 filepath.Walk(realTemp, func(path string, _ os.FileInfo, err error) error {
1012 c.Check(path, Equals, realTemp)
1020 cr.ArvMountPoint = ""
1021 cr.Container.Mounts = make(map[string]arvados.Mount)
1022 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
1023 cr.OutputPath = "/tmp"
1024 cr.statInterval = 5 * time.Second
1025 err := cr.SetupMounts()
1027 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other",
1028 "--read-write", "--crunchstat-interval=5",
1029 "--mount-by-pdh", "by_id", realTemp + "/keep1"})
1030 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2:/tmp"})
1031 os.RemoveAll(cr.ArvMountPoint)
1038 cr.ArvMountPoint = ""
1039 cr.Container.Mounts = make(map[string]arvados.Mount)
1040 cr.Container.Mounts["/out"] = arvados.Mount{Kind: "tmp"}
1041 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
1042 cr.OutputPath = "/out"
1044 err := cr.SetupMounts()
1046 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other",
1047 "--read-write", "--crunchstat-interval=5",
1048 "--mount-by-pdh", "by_id", realTemp + "/keep1"})
1049 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2:/out", realTemp + "/3:/tmp"})
1050 os.RemoveAll(cr.ArvMountPoint)
1057 cr.ArvMountPoint = ""
1058 cr.Container.Mounts = make(map[string]arvados.Mount)
1059 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
1060 cr.OutputPath = "/tmp"
1063 cr.Container.RuntimeConstraints.API = &apiflag
1065 err := cr.SetupMounts()
1067 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other",
1068 "--read-write", "--crunchstat-interval=5",
1069 "--mount-by-pdh", "by_id", realTemp + "/keep1"})
1070 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2:/tmp", stubCertPath + ":/etc/arvados/ca-certificates.crt:ro"})
1071 os.RemoveAll(cr.ArvMountPoint)
1080 cr.ArvMountPoint = ""
1081 cr.Container.Mounts = map[string]arvados.Mount{
1082 "/keeptmp": {Kind: "collection", Writable: true},
1084 cr.OutputPath = "/keeptmp"
1086 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1088 err := cr.SetupMounts()
1090 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other",
1091 "--read-write", "--crunchstat-interval=5",
1092 "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
1093 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/keep1/tmp0:/keeptmp"})
1094 os.RemoveAll(cr.ArvMountPoint)
1101 cr.ArvMountPoint = ""
1102 cr.Container.Mounts = map[string]arvados.Mount{
1103 "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
1104 "/keepout": {Kind: "collection", Writable: true},
1106 cr.OutputPath = "/keepout"
1108 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
1109 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1111 err := cr.SetupMounts()
1113 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other",
1114 "--read-write", "--crunchstat-interval=5",
1115 "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
1116 sort.StringSlice(cr.Binds).Sort()
1117 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53:/keepinp:ro",
1118 realTemp + "/keep1/tmp0:/keepout"})
1119 os.RemoveAll(cr.ArvMountPoint)
1126 cr.ArvMountPoint = ""
1127 cr.Container.RuntimeConstraints.KeepCacheRAM = 512
1128 cr.Container.Mounts = map[string]arvados.Mount{
1129 "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
1130 "/keepout": {Kind: "collection", Writable: true},
1132 cr.OutputPath = "/keepout"
1134 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
1135 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1137 err := cr.SetupMounts()
1139 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other",
1140 "--read-write", "--crunchstat-interval=5",
1141 "--file-cache", "512", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
1142 sort.StringSlice(cr.Binds).Sort()
1143 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53:/keepinp:ro",
1144 realTemp + "/keep1/tmp0:/keepout"})
1145 os.RemoveAll(cr.ArvMountPoint)
1150 for _, test := range []struct {
1154 {in: "foo", out: `"foo"`},
1155 {in: nil, out: `null`},
1156 {in: map[string]int64{"foo": 123456789123456789}, out: `{"foo":123456789123456789}`},
1159 cr.ArvMountPoint = ""
1160 cr.Container.Mounts = map[string]arvados.Mount{
1161 "/mnt/test.json": {Kind: "json", Content: test.in},
1163 err := cr.SetupMounts()
1165 sort.StringSlice(cr.Binds).Sort()
1166 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2/mountdata.json:/mnt/test.json:ro"})
1167 content, err := ioutil.ReadFile(realTemp + "/2/mountdata.json")
1169 c.Check(content, DeepEquals, []byte(test.out))
1170 os.RemoveAll(cr.ArvMountPoint)
1175 // Read-only mount points are allowed underneath output_dir mount point
1178 cr.ArvMountPoint = ""
1179 cr.Container.Mounts = make(map[string]arvados.Mount)
1180 cr.Container.Mounts = map[string]arvados.Mount{
1181 "/tmp": {Kind: "tmp"},
1182 "/tmp/foo": {Kind: "collection"},
1184 cr.OutputPath = "/tmp"
1186 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1188 err := cr.SetupMounts()
1190 c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other",
1191 "--read-write", "--crunchstat-interval=5",
1192 "--file-cache", "512", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
1193 c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2:/tmp", realTemp + "/keep1/tmp0:/tmp/foo:ro"})
1194 os.RemoveAll(cr.ArvMountPoint)
1199 // Writable mount points are not allowed underneath output_dir mount point
1202 cr.ArvMountPoint = ""
1203 cr.Container.Mounts = make(map[string]arvados.Mount)
1204 cr.Container.Mounts = map[string]arvados.Mount{
1205 "/tmp": {Kind: "tmp"},
1206 "/tmp/foo": {Kind: "collection", Writable: true},
1208 cr.OutputPath = "/tmp"
1210 err := cr.SetupMounts()
1211 c.Check(err, NotNil)
1212 c.Check(err, ErrorMatches, `Writable mount points are not permitted underneath the output_path.*`)
1213 os.RemoveAll(cr.ArvMountPoint)
1218 // Only mount points of kind 'collection' are allowed underneath output_dir mount point
1221 cr.ArvMountPoint = ""
1222 cr.Container.Mounts = make(map[string]arvados.Mount)
1223 cr.Container.Mounts = map[string]arvados.Mount{
1224 "/tmp": {Kind: "tmp"},
1225 "/tmp/foo": {Kind: "json"},
1227 cr.OutputPath = "/tmp"
1229 err := cr.SetupMounts()
1230 c.Check(err, NotNil)
1231 c.Check(err, ErrorMatches, `Only mount points of kind 'collection' are supported underneath the output_path.*`)
1232 os.RemoveAll(cr.ArvMountPoint)
1237 // Only mount point of kind 'collection' is allowed for stdin
1240 cr.ArvMountPoint = ""
1241 cr.Container.Mounts = make(map[string]arvados.Mount)
1242 cr.Container.Mounts = map[string]arvados.Mount{
1243 "stdin": {Kind: "tmp"},
1246 err := cr.SetupMounts()
1247 c.Check(err, NotNil)
1248 c.Check(err, ErrorMatches, `Unsupported mount kind 'tmp' for stdin.*`)
1249 os.RemoveAll(cr.ArvMountPoint)
1255 func (s *TestSuite) TestStdout(c *C) {
1257 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1258 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1260 "environment": {"FROBIZ": "bilbo"},
1261 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"} },
1262 "output_path": "/tmp",
1264 "runtime_constraints": {}
1267 api, _, _ := FullRunHelper(c, helperRecord, nil, 0, func(t *TestDockerClient) {
1268 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
1272 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1273 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1274 c.Check(api.CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), NotNil)
1277 // Used by the TestStdoutWithWrongPath*()
1278 func StdoutErrorRunHelper(c *C, record string, fn func(t *TestDockerClient)) (api *ArvTestClient, cr *ContainerRunner, err error) {
1279 rec := arvados.Container{}
1280 err = json.Unmarshal([]byte(record), &rec)
1283 docker := NewTestDockerClient(0)
1285 docker.ImageRemove(nil, hwImageId, dockertypes.ImageRemoveOptions{})
1287 api = &ArvTestClient{Container: rec}
1288 cr = NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
1289 am := &ArvMountCmdLine{}
1290 cr.RunArvMount = am.ArvMountTest
1296 func (s *TestSuite) TestStdoutWithWrongPath(c *C) {
1297 _, _, err := StdoutErrorRunHelper(c, `{
1298 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path":"/tmpa.out"} },
1299 "output_path": "/tmp"
1300 }`, func(t *TestDockerClient) {})
1302 c.Check(err, NotNil)
1303 c.Check(strings.Contains(err.Error(), "Stdout path does not start with OutputPath"), Equals, true)
1306 func (s *TestSuite) TestStdoutWithWrongKindTmp(c *C) {
1307 _, _, err := StdoutErrorRunHelper(c, `{
1308 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "tmp", "path":"/tmp/a.out"} },
1309 "output_path": "/tmp"
1310 }`, func(t *TestDockerClient) {})
1312 c.Check(err, NotNil)
1313 c.Check(strings.Contains(err.Error(), "Unsupported mount kind 'tmp' for stdout"), Equals, true)
1316 func (s *TestSuite) TestStdoutWithWrongKindCollection(c *C) {
1317 _, _, err := StdoutErrorRunHelper(c, `{
1318 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "collection", "path":"/tmp/a.out"} },
1319 "output_path": "/tmp"
1320 }`, func(t *TestDockerClient) {})
1322 c.Check(err, NotNil)
1323 c.Check(strings.Contains(err.Error(), "Unsupported mount kind 'collection' for stdout"), Equals, true)
1326 func (s *TestSuite) TestFullRunWithAPI(c *C) {
1327 os.Setenv("ARVADOS_API_HOST", "test.arvados.org")
1328 defer os.Unsetenv("ARVADOS_API_HOST")
1329 api, _, _ := FullRunHelper(c, `{
1330 "command": ["/bin/sh", "-c", "echo $ARVADOS_API_HOST"],
1331 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1334 "mounts": {"/tmp": {"kind": "tmp"} },
1335 "output_path": "/tmp",
1337 "runtime_constraints": {"API": true}
1338 }`, nil, 0, func(t *TestDockerClient) {
1339 t.logWriter.Write(dockerLog(1, t.env[1][17:]+"\n"))
1343 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1344 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1345 c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "test.arvados.org\n"), Equals, true)
1346 c.Check(api.CalledWith("container.output", "d41d8cd98f00b204e9800998ecf8427e+0"), NotNil)
1349 func (s *TestSuite) TestFullRunSetOutput(c *C) {
1350 os.Setenv("ARVADOS_API_HOST", "test.arvados.org")
1351 defer os.Unsetenv("ARVADOS_API_HOST")
1352 api, _, _ := FullRunHelper(c, `{
1353 "command": ["/bin/sh", "-c", "echo $ARVADOS_API_HOST"],
1354 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1357 "mounts": {"/tmp": {"kind": "tmp"} },
1358 "output_path": "/tmp",
1360 "runtime_constraints": {"API": true}
1361 }`, nil, 0, func(t *TestDockerClient) {
1362 t.api.Container.Output = "d4ab34d3d4f8a72f5c4973051ae69fab+122"
1366 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1367 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1368 c.Check(api.CalledWith("container.output", "d4ab34d3d4f8a72f5c4973051ae69fab+122"), NotNil)
1371 func (s *TestSuite) TestStdoutWithExcludeFromOutputMountPointUnderOutputDir(c *C) {
1373 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1374 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1376 "environment": {"FROBIZ": "bilbo"},
1378 "/tmp": {"kind": "tmp"},
1379 "/tmp/foo": {"kind": "collection",
1380 "portable_data_hash": "a3e8f74c6f101eae01fa08bfb4e49b3a+54",
1381 "exclude_from_output": true
1383 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1385 "output_path": "/tmp",
1387 "runtime_constraints": {}
1390 extraMounts := []string{"a3e8f74c6f101eae01fa08bfb4e49b3a+54"}
1392 api, _, _ := FullRunHelper(c, helperRecord, extraMounts, 0, func(t *TestDockerClient) {
1393 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
1397 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1398 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1399 c.Check(api.CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), NotNil)
1402 func (s *TestSuite) TestStdoutWithMultipleMountPointsUnderOutputDir(c *C) {
1404 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1405 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1407 "environment": {"FROBIZ": "bilbo"},
1409 "/tmp": {"kind": "tmp"},
1410 "/tmp/foo/bar": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/file2_in_main.txt"},
1411 "/tmp/foo/sub1": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/subdir1"},
1412 "/tmp/foo/sub1file2": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/subdir1/file2_in_subdir1.txt"},
1413 "/tmp/foo/baz/sub2file2": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/subdir1/subdir2/file2_in_subdir2.txt"},
1414 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1416 "output_path": "/tmp",
1418 "runtime_constraints": {}
1421 extraMounts := []string{
1422 "a0def87f80dd594d4675809e83bd4f15+367/file2_in_main.txt",
1423 "a0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt",
1424 "a0def87f80dd594d4675809e83bd4f15+367/subdir1/subdir2/file2_in_subdir2.txt",
1427 api, runner, realtemp := FullRunHelper(c, helperRecord, extraMounts, 0, func(t *TestDockerClient) {
1428 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
1432 c.Check(runner.Binds, DeepEquals, []string{realtemp + "/2:/tmp",
1433 realtemp + "/keep1/by_id/a0def87f80dd594d4675809e83bd4f15+367/file2_in_main.txt:/tmp/foo/bar:ro",
1434 realtemp + "/keep1/by_id/a0def87f80dd594d4675809e83bd4f15+367/subdir1/subdir2/file2_in_subdir2.txt:/tmp/foo/baz/sub2file2:ro",
1435 realtemp + "/keep1/by_id/a0def87f80dd594d4675809e83bd4f15+367/subdir1:/tmp/foo/sub1:ro",
1436 realtemp + "/keep1/by_id/a0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt:/tmp/foo/sub1file2:ro",
1439 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1440 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1441 for _, v := range api.Content {
1442 if v["collection"] != nil {
1443 c.Check(v["ensure_unique_name"], Equals, true)
1444 collection := v["collection"].(arvadosclient.Dict)
1445 if strings.Index(collection["name"].(string), "output") == 0 {
1446 manifest := collection["manifest_text"].(string)
1448 c.Check(manifest, Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
1449 ./foo 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0abcdefgh11234567890@569fa8c3 9:18:bar 9:18:sub1file2
1450 ./foo/baz 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0bcdefghijk544332211@569fa8c5 9:18:sub2file2
1451 ./foo/sub1 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 0:9:file1_in_subdir1.txt 9:18:file2_in_subdir1.txt
1452 ./foo/sub1/subdir2 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0bcdefghijk544332211@569fa8c5 0:9:file1_in_subdir2.txt 9:18:file2_in_subdir2.txt
1459 func (s *TestSuite) TestStdoutWithMountPointsUnderOutputDirDenormalizedManifest(c *C) {
1461 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1462 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1464 "environment": {"FROBIZ": "bilbo"},
1466 "/tmp": {"kind": "tmp"},
1467 "/tmp/foo/bar": {"kind": "collection", "portable_data_hash": "b0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt"},
1468 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1470 "output_path": "/tmp",
1472 "runtime_constraints": {}
1475 extraMounts := []string{
1476 "b0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt",
1479 api, _, _ := FullRunHelper(c, helperRecord, extraMounts, 0, func(t *TestDockerClient) {
1480 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
1484 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1485 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1486 for _, v := range api.Content {
1487 if v["collection"] != nil {
1488 collection := v["collection"].(arvadosclient.Dict)
1489 if strings.Index(collection["name"].(string), "output") == 0 {
1490 manifest := collection["manifest_text"].(string)
1492 c.Check(manifest, Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
1493 ./foo 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0abcdefgh11234567890@569fa8c3 10:17:bar
1500 func (s *TestSuite) TestOutputSymlinkToInput(c *C) {
1502 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1503 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1505 "environment": {"FROBIZ": "bilbo"},
1507 "/tmp": {"kind": "tmp"},
1508 "/keep/foo/sub1file2": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path": "/subdir1/file2_in_subdir1.txt"},
1509 "/keep/foo2": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367"}
1511 "output_path": "/tmp",
1513 "runtime_constraints": {}
1516 extraMounts := []string{
1517 "a0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt",
1520 api, _, _ := FullRunHelper(c, helperRecord, extraMounts, 0, func(t *TestDockerClient) {
1521 os.Symlink("/keep/foo/sub1file2", t.realTemp+"/2/baz")
1522 os.Symlink("/keep/foo2/subdir1/file2_in_subdir1.txt", t.realTemp+"/2/baz2")
1523 os.Symlink("/keep/foo2/subdir1", t.realTemp+"/2/baz3")
1524 os.Mkdir(t.realTemp+"/2/baz4", 0700)
1525 os.Symlink("/keep/foo2/subdir1/file2_in_subdir1.txt", t.realTemp+"/2/baz4/baz5")
1529 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1530 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1531 for _, v := range api.Content {
1532 if v["collection"] != nil {
1533 collection := v["collection"].(arvadosclient.Dict)
1534 if strings.Index(collection["name"].(string), "output") == 0 {
1535 manifest := collection["manifest_text"].(string)
1536 c.Check(manifest, Equals, `. 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 9:18:baz 9:18:baz2
1537 ./baz3 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 0:9:file1_in_subdir1.txt 9:18:file2_in_subdir1.txt
1538 ./baz3/subdir2 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0bcdefghijk544332211@569fa8c5 0:9:file1_in_subdir2.txt 9:18:file2_in_subdir2.txt
1539 ./baz4 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 9:18:baz5
1546 func (s *TestSuite) TestOutputError(c *C) {
1548 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1549 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1551 "environment": {"FROBIZ": "bilbo"},
1553 "/tmp": {"kind": "tmp"}
1555 "output_path": "/tmp",
1557 "runtime_constraints": {}
1560 extraMounts := []string{}
1562 api, _, _ := FullRunHelper(c, helperRecord, extraMounts, 0, func(t *TestDockerClient) {
1563 os.Symlink("/etc/hosts", t.realTemp+"/2/baz")
1567 c.Check(api.CalledWith("container.state", "Cancelled"), NotNil)
1570 func (s *TestSuite) TestOutputSymlinkToOutput(c *C) {
1572 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1573 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1575 "environment": {"FROBIZ": "bilbo"},
1577 "/tmp": {"kind": "tmp"}
1579 "output_path": "/tmp",
1581 "runtime_constraints": {}
1584 extraMounts := []string{}
1586 api, _, _ := FullRunHelper(c, helperRecord, extraMounts, 0, func(t *TestDockerClient) {
1587 rf, _ := os.Create(t.realTemp + "/2/realfile")
1588 rf.Write([]byte("foo"))
1591 os.Mkdir(t.realTemp+"/2/realdir", 0700)
1592 rf, _ = os.Create(t.realTemp + "/2/realdir/subfile")
1593 rf.Write([]byte("bar"))
1596 os.Symlink("/tmp/realfile", t.realTemp+"/2/file1")
1597 os.Symlink("realfile", t.realTemp+"/2/file2")
1598 os.Symlink("/tmp/file1", t.realTemp+"/2/file3")
1599 os.Symlink("file2", t.realTemp+"/2/file4")
1600 os.Symlink("realdir", t.realTemp+"/2/dir1")
1601 os.Symlink("/tmp/realdir", t.realTemp+"/2/dir2")
1605 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1606 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1607 for _, v := range api.Content {
1608 if v["collection"] != nil {
1609 collection := v["collection"].(arvadosclient.Dict)
1610 if strings.Index(collection["name"].(string), "output") == 0 {
1611 manifest := collection["manifest_text"].(string)
1612 c.Check(manifest, Equals,
1613 `. 7a2c86e102dcc231bd232aad99686dfa+15 0:3:file1 3:3:file2 6:3:file3 9:3:file4 12:3:realfile
1614 ./dir1 37b51d194a7513e45b56f6524f2d51f2+3 0:3:subfile
1615 ./dir2 37b51d194a7513e45b56f6524f2d51f2+3 0:3:subfile
1616 ./realdir 37b51d194a7513e45b56f6524f2d51f2+3 0:3:subfile
1623 func (s *TestSuite) TestStdinCollectionMountPoint(c *C) {
1625 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1626 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1628 "environment": {"FROBIZ": "bilbo"},
1630 "/tmp": {"kind": "tmp"},
1631 "stdin": {"kind": "collection", "portable_data_hash": "b0def87f80dd594d4675809e83bd4f15+367", "path": "/file1_in_main.txt"},
1632 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1634 "output_path": "/tmp",
1636 "runtime_constraints": {}
1639 extraMounts := []string{
1640 "b0def87f80dd594d4675809e83bd4f15+367/file1_in_main.txt",
1643 api, _, _ := FullRunHelper(c, helperRecord, extraMounts, 0, func(t *TestDockerClient) {
1644 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
1648 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1649 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1650 for _, v := range api.Content {
1651 if v["collection"] != nil {
1652 collection := v["collection"].(arvadosclient.Dict)
1653 if strings.Index(collection["name"].(string), "output") == 0 {
1654 manifest := collection["manifest_text"].(string)
1655 c.Check(manifest, Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
1662 func (s *TestSuite) TestStdinJsonMountPoint(c *C) {
1664 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1665 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1667 "environment": {"FROBIZ": "bilbo"},
1669 "/tmp": {"kind": "tmp"},
1670 "stdin": {"kind": "json", "content": "foo"},
1671 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1673 "output_path": "/tmp",
1675 "runtime_constraints": {}
1678 api, _, _ := FullRunHelper(c, helperRecord, nil, 0, func(t *TestDockerClient) {
1679 t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
1683 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1684 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1685 for _, v := range api.Content {
1686 if v["collection"] != nil {
1687 collection := v["collection"].(arvadosclient.Dict)
1688 if strings.Index(collection["name"].(string), "output") == 0 {
1689 manifest := collection["manifest_text"].(string)
1690 c.Check(manifest, Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
1697 func (s *TestSuite) TestStderrMount(c *C) {
1698 api, _, _ := FullRunHelper(c, `{
1699 "command": ["/bin/sh", "-c", "echo hello;exit 1"],
1700 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1703 "mounts": {"/tmp": {"kind": "tmp"},
1704 "stdout": {"kind": "file", "path": "/tmp/a/out.txt"},
1705 "stderr": {"kind": "file", "path": "/tmp/b/err.txt"}},
1706 "output_path": "/tmp",
1708 "runtime_constraints": {}
1709 }`, nil, 1, func(t *TestDockerClient) {
1710 t.logWriter.Write(dockerLog(1, "hello\n"))
1711 t.logWriter.Write(dockerLog(2, "oops\n"))
1715 final := api.CalledWith("container.state", "Complete")
1716 c.Assert(final, NotNil)
1717 c.Check(final["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
1718 c.Check(final["container"].(arvadosclient.Dict)["log"], NotNil)
1720 c.Check(api.CalledWith("collection.manifest_text", "./a b1946ac92492d2347c6235b4d2611184+6 0:6:out.txt\n./b 38af5c54926b620264ab1501150cf189+5 0:5:err.txt\n"), NotNil)
1723 func (s *TestSuite) TestNumberRoundTrip(c *C) {
1724 cr := NewContainerRunner(&ArvTestClient{callraw: true}, &KeepTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
1725 cr.fetchContainerRecord()
1727 jsondata, err := json.Marshal(cr.Container.Mounts["/json"].Content)
1730 c.Check(string(jsondata), Equals, `{"number":123456789123456789}`)
1733 func (s *TestSuite) TestEvalSymlinks(c *C) {
1734 cr := NewContainerRunner(&ArvTestClient{callraw: true}, &KeepTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
1736 realTemp, err := ioutil.TempDir("", "crunchrun_test-")
1737 c.Assert(err, IsNil)
1738 defer os.RemoveAll(realTemp)
1740 cr.HostOutputDir = realTemp
1742 // Absolute path outside output dir
1743 os.Symlink("/etc/passwd", realTemp+"/p1")
1745 // Relative outside output dir
1746 os.Symlink("../zip", realTemp+"/p2")
1748 // Circular references
1749 os.Symlink("p4", realTemp+"/p3")
1750 os.Symlink("p5", realTemp+"/p4")
1751 os.Symlink("p3", realTemp+"/p5")
1753 // Target doesn't exist
1754 os.Symlink("p99", realTemp+"/p6")
1756 for _, v := range []string{"p1", "p2", "p3", "p4", "p5"} {
1757 info, err := os.Lstat(realTemp + "/" + v)
1758 _, _, _, err = cr.derefOutputSymlink(realTemp+"/"+v, info)
1759 c.Assert(err, NotNil)
1763 func (s *TestSuite) TestEvalSymlinkDir(c *C) {
1764 cr := NewContainerRunner(&ArvTestClient{callraw: true}, &KeepTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
1766 realTemp, err := ioutil.TempDir("", "crunchrun_test-")
1767 c.Assert(err, IsNil)
1768 defer os.RemoveAll(realTemp)
1770 cr.HostOutputDir = realTemp
1772 // Absolute path outside output dir
1773 os.Symlink(".", realTemp+"/loop")
1776 info, err := os.Lstat(realTemp + "/" + v)
1777 _, err = cr.UploadOutputFile(realTemp+"/"+v, info, err, []string{}, nil, "", "", 0)
1778 c.Assert(err, NotNil)
1781 func (s *TestSuite) TestFullBrokenDocker1(c *C) {
1782 tf, err := ioutil.TempFile("", "brokenNodeHook-")
1783 c.Assert(err, IsNil)
1784 defer os.Remove(tf.Name())
1786 tf.Write([]byte(`#!/bin/sh
1790 os.Chmod(tf.Name(), 0700)
1793 brokenNodeHook = &ech
1795 api, _, _ := FullRunHelper(c, `{
1796 "command": ["echo", "hello world"],
1797 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1800 "mounts": {"/tmp": {"kind": "tmp"} },
1801 "output_path": "/tmp",
1803 "runtime_constraints": {}
1804 }`, nil, 2, func(t *TestDockerClient) {
1805 t.logWriter.Write(dockerLog(1, "hello world\n"))
1809 c.Check(api.CalledWith("container.state", "Queued"), NotNil)
1810 c.Check(api.Logs["crunch-run"].String(), Matches, "(?ms).*unable to run containers.*")
1811 c.Check(api.Logs["crunch-run"].String(), Matches, "(?ms).*Running broken node hook.*")
1812 c.Check(api.Logs["crunch-run"].String(), Matches, "(?ms).*killme.*")
1816 func (s *TestSuite) TestFullBrokenDocker2(c *C) {
1818 brokenNodeHook = &ech
1820 api, _, _ := FullRunHelper(c, `{
1821 "command": ["echo", "hello world"],
1822 "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
1825 "mounts": {"/tmp": {"kind": "tmp"} },
1826 "output_path": "/tmp",
1828 "runtime_constraints": {}
1829 }`, nil, 2, func(t *TestDockerClient) {
1830 t.logWriter.Write(dockerLog(1, "hello world\n"))
1834 c.Check(api.CalledWith("container.state", "Queued"), NotNil)
1835 c.Check(api.Logs["crunch-run"].String(), Matches, "(?ms).*unable to run containers.*")
1836 c.Check(api.Logs["crunch-run"].String(), Matches, "(?ms).*No broken node hook.*")