1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
34 "git.arvados.org/arvados.git/lib/cloud"
35 "git.arvados.org/arvados.git/lib/cmd"
36 "git.arvados.org/arvados.git/sdk/go/arvados"
37 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
38 "git.arvados.org/arvados.git/sdk/go/arvadostest"
43 // Gocheck boilerplate
44 func TestCrunchExec(t *testing.T) {
48 const logLineStart = `(?m)(.*\n)*\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d+Z `
50 var _ = Suite(&TestSuite{})
52 type TestSuite struct {
53 client *arvados.Client
55 runner *ContainerRunner
56 executor *stubExecutor
59 testDispatcherKeepClient KeepTestClient
60 testContainerKeepClient KeepTestClient
61 debian12MemoryCurrent int64
62 debian12SwapCurrent int64
65 func (s *TestSuite) SetUpSuite(c *C) {
66 buf, err := os.ReadFile("../crunchstat/testdata/debian12/sys/fs/cgroup/user.slice/user-1000.slice/session-4.scope/memory.current")
68 _, err = fmt.Sscanf(string(buf), "%d", &s.debian12MemoryCurrent)
71 buf, err = os.ReadFile("../crunchstat/testdata/debian12/sys/fs/cgroup/user.slice/user-1000.slice/session-4.scope/memory.swap.current")
73 _, err = fmt.Sscanf(string(buf), "%d", &s.debian12SwapCurrent)
77 func (s *TestSuite) SetUpTest(c *C) {
78 s.client = arvados.NewClientFromEnv()
79 s.executor = &stubExecutor{}
81 s.api = &ArvTestClient{}
82 s.runner, err = NewContainerRunner(s.client, s.api, &s.testDispatcherKeepClient, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
84 s.runner.executor = s.executor
85 s.runner.MkArvClient = func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error) {
86 return s.api, &s.testContainerKeepClient, s.client, nil
88 s.runner.RunArvMount = func(cmd []string, tok string) (*exec.Cmd, error) {
89 s.runner.ArvMountPoint = s.keepmount
90 for i, opt := range cmd {
91 if opt == "--mount-tmp" {
92 err := os.Mkdir(s.keepmount+"/"+cmd[i+1], 0700)
96 s.keepmountTmp = append(s.keepmountTmp, cmd[i+1])
101 s.keepmount = c.MkDir()
102 err = os.Mkdir(s.keepmount+"/by_id", 0755)
105 err = os.Mkdir(s.keepmount+"/by_id/"+arvadostest.DockerImage112PDH, 0755)
107 err = ioutil.WriteFile(s.keepmount+"/by_id/"+arvadostest.DockerImage112PDH+"/"+arvadostest.DockerImage112Filename, []byte("#notarealtarball"), 0644)
108 err = os.Mkdir(s.keepmount+"/by_id/"+fakeInputCollectionPDH, 0755)
110 err = ioutil.WriteFile(s.keepmount+"/by_id/"+fakeInputCollectionPDH+"/input.json", []byte(`{"input":true}`), 0644)
112 s.runner.ArvMountPoint = s.keepmount
113 os.Setenv("InstanceType", `{"ProviderType":"a1.2xlarge","Price":1.2}`)
116 type ArvTestClient struct {
119 Content []arvadosclient.Dict
127 type KeepTestClient struct {
129 StorageClasses []string
133 type stubExecutor struct {
139 created containerSpec
141 waitSleep time.Duration
150 func (e *stubExecutor) LoadImage(imageId string, tarball string, container arvados.Container, keepMount string,
151 containerClient *arvados.Client) error {
155 func (e *stubExecutor) Runtime() string { return "stub" }
156 func (e *stubExecutor) Version() string { return "stub " + cmd.Version.String() }
157 func (e *stubExecutor) Create(spec containerSpec) error { e.created = spec; return e.createErr }
158 func (e *stubExecutor) Start() error {
159 e.exit = make(chan int, 1)
160 go func() { e.exit <- e.runFunc() }()
163 func (e *stubExecutor) Pid() int { return 1115883 } // matches pid in ../crunchstat/testdata/debian12/proc/
164 func (e *stubExecutor) Stop() error { e.stopped = true; go func() { e.exit <- -1 }(); return e.stopErr }
165 func (e *stubExecutor) Close() { e.closed = true }
166 func (e *stubExecutor) Wait(context.Context) (int, error) {
167 return <-e.exit, e.waitErr
169 func (e *stubExecutor) InjectCommand(ctx context.Context, _, _ string, _ bool, _ []string) (*exec.Cmd, error) {
170 return nil, errors.New("unimplemented")
172 func (e *stubExecutor) IPAddress() (string, error) { return "", errors.New("unimplemented") }
174 const fakeInputCollectionPDH = "ffffffffaaaaaaaa88888888eeeeeeee+1234"
176 var hwManifest = ". 82ab40c24fc8df01798e57ba66795bb1+841216+Aa124ac75e5168396c73c0a18eda641a4f41791c0@569fa8c3 0:841216:9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7.tar\n"
177 var hwPDH = "a45557269dcb65a6b78f9ac061c0850b+120"
178 var hwImageID = "9c31ee32b3d15268a0754e8edc74d4f815ee014b693bc5109058e431dd5caea7"
180 var otherManifest = ". 68a84f561b1d1708c6baff5e019a9ab3+46+Ae5d0af96944a3690becb1decdf60cc1c937f556d@5693216f 0:46:md5sum.txt\n"
181 var otherPDH = "a3e8f74c6f101eae01fa08bfb4e49b3a+54"
183 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
184 ./subdir1 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 0:9:file1_in_subdir1.txt 9:18:file2_in_subdir1.txt
185 ./subdir1/subdir2 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0bcdefghijk544332211@569fa8c5 0:9:file1_in_subdir2.txt 9:18:file2_in_subdir2.txt
188 var normalizedWithSubdirsPDH = "a0def87f80dd594d4675809e83bd4f15+367"
190 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"
191 var denormalizedWithSubdirsPDH = "b0def87f80dd594d4675809e83bd4f15+367"
193 var fakeAuthUUID = "zzzzz-gj3su-55pqoyepgi2glem"
194 var fakeAuthToken = "a3ltuwzqcu2u4sc0q7yhpc2w7s00fdcqecg5d6e0u3pfohmbjt"
196 func (client *ArvTestClient) Create(resourceType string,
197 parameters arvadosclient.Dict,
198 output interface{}) error {
201 defer client.Mutex.Unlock()
204 client.Content = append(client.Content, parameters)
206 if resourceType == "logs" {
207 panic("logs.create called")
210 if resourceType == "collections" && output != nil {
211 mt := parameters["collection"].(arvadosclient.Dict)["manifest_text"].(string)
212 md5sum := md5.Sum([]byte(mt))
213 outmap := output.(*arvados.Collection)
214 outmap.PortableDataHash = fmt.Sprintf("%x+%d", md5sum, len(mt))
215 outmap.UUID = fmt.Sprintf("zzzzz-4zz18-%015x", md5sum[:7])
221 func (client *ArvTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
223 case method == "GET" && resourceType == "containers" && action == "auth":
224 return json.Unmarshal([]byte(`{
225 "kind": "arvados#api_client_authorization",
226 "uuid": "`+fakeAuthUUID+`",
227 "api_token": "`+fakeAuthToken+`"
229 case method == "GET" && resourceType == "containers" && action == "secret_mounts":
230 if client.secretMounts != nil {
231 return json.Unmarshal(client.secretMounts, output)
233 return json.Unmarshal([]byte(`{"secret_mounts":{}}`), output)
235 return fmt.Errorf("Not found")
239 func (client *ArvTestClient) CallRaw(method, resourceType, uuid, action string,
240 parameters arvadosclient.Dict) (reader io.ReadCloser, err error) {
242 if method == "GET" && resourceType == "nodes" && uuid == "" && action == "" {
244 "kind": "arvados#nodeList",
246 "uuid": "zzzzz-7ekkf-2z3mc76g2q73aio",
247 "hostname": "compute2",
248 "properties": {"total_cpu_cores": 16}
250 } else if method == "GET" && resourceType == "containers" && action == "" && !client.callraw {
252 j, err = json.Marshal(map[string]interface{}{
253 "items": []interface{}{client.Container},
254 "kind": "arvados#nodeList",
257 j, err = json.Marshal(client.Container)
261 "command": ["sleep", "1"],
262 "container_image": "` + arvadostest.DockerImage112PDH + `",
265 "mounts": {"/tmp": {"kind": "tmp"}, "/json": {"kind": "json", "content": {"number": 123456789123456789}}},
266 "output_path": "/tmp",
268 "runtime_constraints": {}
271 return ioutil.NopCloser(bytes.NewReader(j)), err
274 func (client *ArvTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
275 if resourceType == "collections" {
277 output.(*arvados.Collection).ManifestText = hwManifest
278 } else if uuid == otherPDH {
279 output.(*arvados.Collection).ManifestText = otherManifest
280 } else if uuid == normalizedWithSubdirsPDH {
281 output.(*arvados.Collection).ManifestText = normalizedManifestWithSubdirs
282 } else if uuid == denormalizedWithSubdirsPDH {
283 output.(*arvados.Collection).ManifestText = denormalizedManifestWithSubdirs
286 if resourceType == "containers" {
287 (*output.(*arvados.Container)) = client.Container
292 func (client *ArvTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
294 defer client.Mutex.Unlock()
296 client.Content = append(client.Content, parameters)
297 if resourceType == "containers" {
298 if parameters["container"].(arvadosclient.Dict)["state"] == "Running" {
299 client.WasSetRunning = true
301 } else if resourceType == "collections" && output != nil {
302 mt := parameters["collection"].(arvadosclient.Dict)["manifest_text"].(string)
303 output.(*arvados.Collection).UUID = uuid
304 output.(*arvados.Collection).PortableDataHash = arvados.PortableDataHash(mt)
309 var discoveryMap = map[string]interface{}{
310 "crunchLogUpdateSize": float64(crunchLogUpdateSize),
311 "crunchLogUpdatePeriod": float64(crunchLogUpdatePeriod.Seconds()),
314 func (client *ArvTestClient) Discovery(key string) (interface{}, error) {
315 return discoveryMap[key], nil
318 // CalledWith returns the parameters from the first API call whose
319 // parameters match jpath/string. E.g., CalledWith(c, "foo.bar",
320 // "baz") returns parameters with parameters["foo"]["bar"]=="baz". If
321 // no call matches, it returns nil.
322 func (client *ArvTestClient) CalledWith(jpath string, expect interface{}) arvadosclient.Dict {
324 for _, content := range client.Content {
325 var v interface{} = content
326 for _, k := range strings.Split(jpath, ".") {
327 if dict, ok := v.(arvadosclient.Dict); !ok {
340 func (client *KeepTestClient) LocalLocator(locator string) (string, error) {
344 func (client *KeepTestClient) BlockWrite(_ context.Context, opts arvados.BlockWriteOptions) (arvados.BlockWriteResponse, error) {
345 locator := fmt.Sprintf("%x+%d", md5.Sum(opts.Data), len(opts.Data))
346 client.blocks.Store(locator, append([]byte(nil), opts.Data...))
347 return arvados.BlockWriteResponse{
352 func (client *KeepTestClient) ReadAt(locator string, dst []byte, offset int) (int, error) {
353 loaded, ok := client.blocks.Load(locator)
355 return 0, os.ErrNotExist
357 data := loaded.([]byte)
358 if offset >= len(data) {
361 return copy(dst, data[offset:]), nil
364 func (client *KeepTestClient) Close() {
365 client.blocks.Range(func(locator, value interface{}) bool {
366 client.blocks.Delete(locator)
371 func (client *KeepTestClient) SetStorageClasses(sc []string) {
372 client.StorageClasses = sc
375 type FileWrapper struct {
380 func (fw FileWrapper) Readdir(n int) ([]os.FileInfo, error) {
381 return nil, errors.New("not implemented")
384 func (fw FileWrapper) Seek(int64, int) (int64, error) {
385 return 0, errors.New("not implemented")
388 func (fw FileWrapper) Size() int64 {
392 func (fw FileWrapper) Stat() (os.FileInfo, error) {
393 return nil, errors.New("not implemented")
396 func (fw FileWrapper) Truncate(int64) error {
397 return errors.New("not implemented")
400 func (fw FileWrapper) Write([]byte) (int, error) {
401 return 0, errors.New("not implemented")
404 func (fw FileWrapper) Sync() error {
405 return errors.New("not implemented")
408 func (fw FileWrapper) Snapshot() (*arvados.Subtree, error) {
409 return nil, errors.New("not implemented")
412 func (fw FileWrapper) Splice(*arvados.Subtree) error {
413 return errors.New("not implemented")
416 type apiStubServer struct {
417 server *httptest.Server
418 proxy *httputil.ReverseProxy
419 intercept func(http.ResponseWriter, *http.Request) bool
421 container arvados.Container
424 func apiStub() (*arvados.Client, *apiStubServer) {
425 client := arvados.NewClientFromEnv()
426 apistub := &apiStubServer{}
427 apistub.server = httptest.NewTLSServer(apistub)
428 apistub.proxy = httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "https", Host: client.APIHost})
430 apistub.proxy.Transport = arvados.InsecureHTTPClient.Transport
432 client.APIHost = apistub.server.Listener.Addr().String()
433 return client, apistub
436 func (apistub *apiStubServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
437 if apistub.intercept != nil && apistub.intercept(w, r) {
440 if r.Method == "GET" && r.URL.Path == "/arvados/v1/collections/"+hwPDH {
441 json.NewEncoder(w).Encode(arvados.Collection{ManifestText: hwManifest})
444 if r.Method == "GET" && r.URL.Path == "/arvados/v1/collections/"+otherPDH {
445 json.NewEncoder(w).Encode(arvados.Collection{ManifestText: otherManifest})
448 if r.Method == "GET" && r.URL.Path == "/arvados/v1/collections/"+normalizedWithSubdirsPDH {
449 json.NewEncoder(w).Encode(arvados.Collection{ManifestText: normalizedManifestWithSubdirs})
452 if r.Method == "GET" && r.URL.Path == "/arvados/v1/collections/"+denormalizedWithSubdirsPDH {
453 json.NewEncoder(w).Encode(arvados.Collection{ManifestText: denormalizedManifestWithSubdirs})
456 if r.Method == "GET" && r.URL.Path == "/arvados/v1/containers/"+apistub.container.UUID {
457 json.NewEncoder(w).Encode(apistub.container)
460 apistub.proxy.ServeHTTP(w, r)
463 func (s *TestSuite) TestLoadImage(c *C) {
464 s.runner.Container.ContainerImage = arvadostest.DockerImage112PDH
465 s.runner.Container.Mounts = map[string]arvados.Mount{
466 "/out": {Kind: "tmp", Writable: true},
468 s.runner.Container.OutputPath = "/out"
470 _, err := s.runner.SetupMounts()
473 imageID, err := s.runner.LoadImage()
475 c.Check(s.executor.loaded, Matches, ".*"+regexp.QuoteMeta(arvadostest.DockerImage112Filename))
476 c.Check(imageID, Equals, strings.TrimSuffix(arvadostest.DockerImage112Filename, ".tar"))
478 s.runner.Container.ContainerImage = arvadostest.DockerImage112PDH
479 s.executor.imageLoaded = false
480 s.executor.loaded = ""
481 s.executor.loadErr = errors.New("bork")
482 imageID, err = s.runner.LoadImage()
483 c.Check(err, ErrorMatches, ".*bork")
484 c.Check(s.executor.loaded, Matches, ".*"+regexp.QuoteMeta(arvadostest.DockerImage112Filename))
486 s.runner.Container.ContainerImage = fakeInputCollectionPDH
487 s.executor.imageLoaded = false
488 s.executor.loaded = ""
489 s.executor.loadErr = nil
490 imageID, err = s.runner.LoadImage()
491 c.Check(err, ErrorMatches, "image collection does not include a \\.tar image file")
492 c.Check(s.executor.loaded, Equals, "")
495 type ArvErrorTestClient struct{}
497 func (ArvErrorTestClient) Create(resourceType string,
498 parameters arvadosclient.Dict,
499 output interface{}) error {
503 func (ArvErrorTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
504 if method == "GET" && resourceType == "containers" && action == "auth" {
507 return errors.New("ArvError")
510 func (ArvErrorTestClient) CallRaw(method, resourceType, uuid, action string,
511 parameters arvadosclient.Dict) (reader io.ReadCloser, err error) {
512 return nil, errors.New("ArvError")
515 func (ArvErrorTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
516 return errors.New("ArvError")
519 func (ArvErrorTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
523 func (ArvErrorTestClient) Discovery(key string) (interface{}, error) {
524 return discoveryMap[key], nil
527 type KeepErrorTestClient struct {
531 func (*KeepErrorTestClient) BlockWrite(context.Context, arvados.BlockWriteOptions) (arvados.BlockWriteResponse, error) {
532 return arvados.BlockWriteResponse{}, errors.New("KeepError")
535 func (*KeepErrorTestClient) LocalLocator(string) (string, error) {
536 return "", errors.New("KeepError")
539 type KeepReadErrorTestClient struct {
543 func (*KeepReadErrorTestClient) ReadAt(string, []byte, int) (int, error) {
544 return 0, errors.New("KeepError")
547 func dockerLog(fd byte, msg string) []byte {
549 header := make([]byte, 8+len(by))
551 header[7] = byte(len(by))
556 func (s *TestSuite) TestRunContainer(c *C) {
557 s.executor.runFunc = func() int {
558 fmt.Fprintf(s.executor.created.Stdout, "Hello world\n")
562 s.runner.Container.ContainerImage = arvadostest.DockerImage112PDH
563 s.runner.Container.Command = []string{"./hw"}
564 s.runner.Container.OutputStorageClasses = []string{"default"}
566 imageID, err := s.runner.LoadImage()
569 err = s.runner.CreateContainer(imageID, nil)
572 err = s.runner.StartContainer()
575 err = s.runner.WaitFinish()
578 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, `2\S+Z Hello world\n`)
579 c.Check(logFileContent(c, s.runner, "stderr.txt"), Matches, ``)
582 func (s *TestSuite) TestCommitLogs(c *C) {
583 api := &ArvTestClient{}
584 kc := &KeepTestClient{}
586 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
588 f, err := cr.openLogFile("crunch-run")
590 cr.CrunchLog = newLogWriter(newTestTimestamper(f))
592 cr.CrunchLog.Print("Hello world!")
593 cr.CrunchLog.Print("Goodbye")
594 cr.finalState = "Complete"
596 err = cr.CommitLogs()
599 c.Check(api.Calls, Equals, 1)
600 c.Check(api.Content[0]["ensure_unique_name"], Equals, true)
601 c.Check(api.Content[0]["collection"].(arvadosclient.Dict)["name"], Equals, "logs for zzzzz-zzzzz-zzzzzzzzzzzzzzz")
602 c.Check(api.Content[0]["collection"].(arvadosclient.Dict)["manifest_text"], Equals, ". 744b2e4553123b02fa7b452ec5c18993+123 0:123:crunch-run.txt\n")
603 c.Check(*cr.LogsPDH, Equals, "63da7bdacf08c40f604daad80c261e9a+60")
606 func (s *TestSuite) TestUpdateContainerRunning(c *C) {
607 api := &ArvTestClient{}
608 kc := &KeepTestClient{}
610 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
613 err = cr.UpdateContainerRunning("")
616 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Running")
619 func (s *TestSuite) TestUpdateContainerComplete(c *C) {
620 api := &ArvTestClient{}
621 kc := &KeepTestClient{}
623 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
626 cr.LogsPDH = new(string)
627 *cr.LogsPDH = "d3a229d2fe3690c2c3e75a71a153c6a3+60"
629 cr.ExitCode = new(int)
631 cr.finalState = "Complete"
633 err = cr.UpdateContainerFinal()
636 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], Equals, *cr.LogsPDH)
637 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], Equals, *cr.ExitCode)
638 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
641 func (s *TestSuite) TestUpdateContainerCancelled(c *C) {
642 api := &ArvTestClient{}
643 kc := &KeepTestClient{}
645 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
648 cr.finalState = "Cancelled"
650 err = cr.UpdateContainerFinal()
653 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], IsNil)
654 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], IsNil)
655 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Cancelled")
658 // Used by the TestFullRun*() test below to DRY up boilerplate setup to do full
659 // dress rehearsal of the Run() function, starting from a JSON container record.
660 func (s *TestSuite) fullRunHelper(c *C, record string, extraMounts []string, fn func() int) (*ArvTestClient, *ContainerRunner, string) {
661 err := json.Unmarshal([]byte(record), &s.api.Container)
663 initialState := s.api.Container.State
666 SecretMounts map[string]arvados.Mount `json:"secret_mounts"`
668 err = json.Unmarshal([]byte(record), &sm)
670 secretMounts, err := json.Marshal(sm)
672 c.Logf("SecretMounts decoded %v json %q", sm, secretMounts)
674 s.executor.runFunc = fn
676 s.runner.statInterval = 100 * time.Millisecond
677 s.runner.containerWatchdogInterval = time.Second
679 realTemp := c.MkDir()
681 s.runner.MkTempDir = func(_, prefix string) (string, error) {
683 d := fmt.Sprintf("%s/%s%d", realTemp, prefix, tempcount)
684 err := os.Mkdir(d, os.ModePerm)
685 if err != nil && strings.Contains(err.Error(), ": file exists") {
686 // Test case must have pre-populated the tempdir
691 client, _ := apiStub()
692 s.runner.MkArvClient = func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error) {
693 return &ArvTestClient{secretMounts: secretMounts}, &s.testContainerKeepClient, client, nil
696 if extraMounts != nil && len(extraMounts) > 0 {
697 err := s.runner.SetupArvMountPoint("keep")
700 for _, m := range extraMounts {
701 os.MkdirAll(s.runner.ArvMountPoint+"/by_id/"+m, os.ModePerm)
706 if s.api.CalledWith("container.state", "Complete") != nil {
709 if s.executor.loadErr == nil && s.executor.createErr == nil && initialState != "Running" {
710 c.Check(s.api.WasSetRunning, Equals, true)
711 var lastupdate arvadosclient.Dict
712 for _, content := range s.api.Content {
713 if content["container"] != nil {
714 lastupdate = content["container"].(arvadosclient.Dict)
717 if lastupdate["log"] == nil {
718 c.Errorf("no container update with non-nil log -- updates were: %v", s.api.Content)
723 dumpAllLogFiles(c, s.runner)
726 return s.api, s.runner, realTemp
729 func (s *TestSuite) TestFullRunHello(c *C) {
730 s.runner.enableMemoryLimit = true
731 s.runner.networkMode = "default"
732 s.fullRunHelper(c, `{
733 "command": ["echo", "hello world"],
734 "container_image": "`+arvadostest.DockerImage112PDH+`",
736 "environment": {"foo":"bar","baz":"waz"},
737 "mounts": {"/tmp": {"kind": "tmp"} },
738 "output_path": "/tmp",
740 "runtime_constraints": {"vcpus":1,"ram":1000000},
742 "output_storage_classes": ["default"]
743 }`, nil, func() int {
744 c.Check(s.executor.created.Command, DeepEquals, []string{"echo", "hello world"})
745 c.Check(s.executor.created.Image, Equals, "sha256:d8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678")
746 c.Check(s.executor.created.Env, DeepEquals, map[string]string{"foo": "bar", "baz": "waz"})
747 c.Check(s.executor.created.VCPUs, Equals, 1)
748 c.Check(s.executor.created.RAM, Equals, int64(1000000))
749 c.Check(s.executor.created.NetworkMode, Equals, "default")
750 c.Check(s.executor.created.EnableNetwork, Equals, false)
751 c.Check(s.executor.created.CUDADeviceCount, Equals, 0)
752 fmt.Fprintln(s.executor.created.Stdout, "hello world")
756 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
757 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
758 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, `2\S+Z hello world\n`)
759 c.Check(s.testDispatcherKeepClient.StorageClasses, DeepEquals, []string{"default"})
760 c.Check(s.testContainerKeepClient.StorageClasses, DeepEquals, []string{"default"})
763 func (s *TestSuite) TestRunAlreadyRunning(c *C) {
765 s.fullRunHelper(c, `{
766 "command": ["sleep", "3"],
767 "container_image": "`+arvadostest.DockerImage112PDH+`",
770 "mounts": {"/tmp": {"kind": "tmp"} },
771 "output_path": "/tmp",
773 "runtime_constraints": {},
774 "scheduling_parameters":{"max_run_time": 1},
776 }`, nil, func() int {
780 c.Check(s.api.CalledWith("container.state", "Cancelled"), IsNil)
781 c.Check(s.api.CalledWith("container.state", "Complete"), IsNil)
782 c.Check(ran, Equals, false)
785 func ec2MetadataServerStub(c *C, token *string, failureRate float64, stoptime *atomic.Value) *httptest.Server {
787 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
788 if !failedOnce || rand.Float64() < failureRate {
789 w.WriteHeader(http.StatusServiceUnavailable)
794 case "/latest/api/token":
795 fmt.Fprintln(w, *token)
796 case "/latest/meta-data/spot/instance-action":
797 if r.Header.Get("X-aws-ec2-metadata-token") != *token {
798 w.WriteHeader(http.StatusUnauthorized)
799 } else if t, _ := stoptime.Load().(time.Time); t.IsZero() {
800 w.WriteHeader(http.StatusNotFound)
802 fmt.Fprintf(w, `{"action":"stop","time":"%s"}`, t.Format(time.RFC3339))
805 w.WriteHeader(http.StatusNotFound)
810 func (s *TestSuite) TestSpotInterruptionNotice(c *C) {
811 s.testSpotInterruptionNotice(c, 0.1)
814 func (s *TestSuite) TestSpotInterruptionNoticeNotAvailable(c *C) {
815 s.testSpotInterruptionNotice(c, 1)
818 func (s *TestSuite) testSpotInterruptionNotice(c *C, failureRate float64) {
819 var stoptime atomic.Value
820 token := "fake-ec2-metadata-token"
821 stub := ec2MetadataServerStub(c, &token, failureRate, &stoptime)
824 defer func(i time.Duration, u string) {
825 spotInterruptionCheckInterval = i
826 ec2MetadataBaseURL = u
827 }(spotInterruptionCheckInterval, ec2MetadataBaseURL)
828 spotInterruptionCheckInterval = time.Second / 8
829 ec2MetadataBaseURL = stub.URL
832 checkLogs := func() {
834 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Checking for spot interruptions every 125ms using instance metadata at http://.*`)
835 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Error checking spot interruptions: 503 Service Unavailable.*`)
836 if failureRate == 1 {
837 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Giving up on checking spot interruptions after too many consecutive failures.*`)
840 text := `Cloud provider scheduled instance stop at ` + stoptime.Load().(time.Time).Format(time.RFC3339)
841 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*`+text+`.*`)
842 c.Check(s.api.CalledWith("container.runtime_status.warning", "preemption notice"), NotNil)
843 c.Check(s.api.CalledWith("container.runtime_status.warningDetail", text), NotNil)
844 c.Check(s.api.CalledWith("container.runtime_status.preemptionNotice", text), NotNil)
846 // Check that the log collection was saved, and the
847 // container record updated with the new PDH,
848 // immediately after the preemption notice was
849 // received -- i.e., while the container is still
852 saved := make(map[string]string) // pdh => manifest_text
853 for _, call := range s.api.Content {
854 if ctr, ok := call["container"].(arvadosclient.Dict); ok {
855 if pdh, ok := ctr["log"].(string); ok {
859 if coll, ok := call["collection"].(arvadosclient.Dict); ok {
860 mt, _ := coll["manifest_text"].(string)
861 if strings.Contains(mt, ":crunch-run.txt") {
862 saved[arvados.PortableDataHash(mt)] = mt
866 logfs, err := (&arvados.Collection{ManifestText: saved[lastpdh]}).FileSystem(s.runner.dispatcherClient, s.runner.DispatcherKeepClient)
868 log, err := fs.ReadFile(arvados.FS(logfs), "crunch-run.txt")
870 c.Check(string(log), Matches, `(?ms).*\Q`+text+`\E.*`)
873 go s.runner.checkSpotInterruptionNotices()
874 s.fullRunHelper(c, `{
875 "command": ["sleep", "3"],
876 "container_image": "`+arvadostest.DockerImage112PDH+`",
879 "mounts": {"/tmp": {"kind": "tmp"} },
880 "output_path": "/tmp",
882 "runtime_constraints": {},
884 }`, nil, func() int {
885 time.Sleep(time.Second)
886 stoptime.Store(time.Now().Add(time.Minute).UTC())
887 token = "different-fake-ec2-metadata-token"
888 time.Sleep(time.Second)
892 c.Check(checkedLogs, Equals, true)
895 func (s *TestSuite) TestRunTimeExceeded(c *C) {
896 s.fullRunHelper(c, `{
897 "command": ["sleep", "3"],
898 "container_image": "`+arvadostest.DockerImage112PDH+`",
901 "mounts": {"/tmp": {"kind": "tmp"} },
902 "output_path": "/tmp",
904 "runtime_constraints": {},
905 "scheduling_parameters":{"max_run_time": 1},
907 }`, nil, func() int {
908 time.Sleep(3 * time.Second)
912 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
913 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, "(?ms).*maximum run time exceeded.*")
916 func (s *TestSuite) TestContainerWaitFails(c *C) {
917 s.fullRunHelper(c, `{
918 "command": ["sleep", "3"],
919 "container_image": "`+arvadostest.DockerImage112PDH+`",
921 "mounts": {"/tmp": {"kind": "tmp"} },
922 "output_path": "/tmp",
925 }`, nil, func() int {
926 s.executor.waitErr = errors.New("Container is not running")
930 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
931 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, "(?ms).*Container is not running.*")
934 func (s *TestSuite) TestCrunchstat(c *C) {
935 s.runner.crunchstatFakeFS = os.DirFS("../crunchstat/testdata/debian12")
936 s.fullRunHelper(c, `{
937 "command": ["sleep", "1"],
938 "container_image": "`+arvadostest.DockerImage112PDH+`",
941 "mounts": {"/tmp": {"kind": "tmp"} },
942 "output_path": "/tmp",
944 "runtime_constraints": {},
946 }`, nil, func() int {
947 time.Sleep(time.Second)
951 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
952 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
954 c.Check(logFileContent(c, s.runner, "crunchstat.txt"), Matches, `(?ms).*`+reTimestamp+` mem \d+ swap \d+ pgmajfault \d+ rss.*`)
955 c.Check(logFileContent(c, s.runner, "hoststat.txt"), Matches, `(?ms).*`+reTimestamp+` mem \d+ swap \d+ pgmajfault \d+ rss.*`)
957 // Check that we called (*crunchstat.Reporter)Stop().
958 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*`+reTimestamp+` Maximum crunch-run memory rss usage was \d+ bytes\n.*`)
961 func (s *TestSuite) TestNodeInfoLog(c *C) {
962 s.fullRunHelper(c, `{
964 "container_image": "`+arvadostest.DockerImage112PDH+`",
967 "mounts": {"/tmp": {"kind": "tmp"} },
968 "output_path": "/tmp",
970 "runtime_constraints": {},
972 }`, nil, func() int {
976 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
977 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
979 json := logFileContent(c, s.runner, "node.json")
980 c.Check(json, Matches, `(?ms).*"ProviderType": *"a1\.2xlarge".*`)
981 c.Check(json, Matches, `(?ms).*"Price": *1\.2.*`)
983 nodeinfo := logFileContent(c, s.runner, "node-info.txt")
984 c.Check(nodeinfo, Matches, `(?ms).*Host Information.*`)
985 c.Check(nodeinfo, Matches, `(?ms).*CPU Information.*`)
986 c.Check(nodeinfo, Matches, `(?ms).*Memory Information.*`)
987 c.Check(nodeinfo, Matches, `(?ms).*Disk Space.*`)
988 c.Check(nodeinfo, Matches, `(?ms).*Disk INodes.*`)
991 func (s *TestSuite) TestLogVersionAndRuntime(c *C) {
992 s.fullRunHelper(c, `{
993 "command": ["sleep", "1"],
994 "container_image": "`+arvadostest.DockerImage112PDH+`",
997 "mounts": {"/tmp": {"kind": "tmp"} },
998 "output_path": "/tmp",
1000 "runtime_constraints": {},
1002 }`, nil, func() int {
1006 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*crunch-run \S+ \(go\S+\) start.*`)
1007 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*crunch-run process has uid=\d+\(.+\) gid=\d+\(.+\) groups=\d+\(.+\)(,\d+\(.+\))*\n.*`)
1008 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Executing container: zzzzz-zzzzz-zzzzzzzzzzzzzzz.*`)
1009 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Using container runtime: stub.*`)
1012 func (s *TestSuite) testLogRSSThresholds(c *C, ram int64, expected []int, notExpected int) {
1013 s.runner.crunchstatFakeFS = os.DirFS("../crunchstat/testdata/debian12")
1014 s.fullRunHelper(c, `{
1015 "command": ["true"],
1016 "container_image": "`+arvadostest.DockerImage112PDH+`",
1019 "mounts": {"/tmp": {"kind": "tmp"} },
1020 "output_path": "/tmp",
1022 "runtime_constraints": {"ram": `+fmt.Sprintf("%d", ram)+`},
1024 }`, nil, func() int { return 0 })
1025 logs := logFileContent(c, s.runner, "crunch-run.txt")
1026 c.Log("=== crunchstat logs")
1028 pattern := logLineStart + `Container using over %d%% of memory \(rss %d/%d bytes\)`
1030 for _, threshold = range expected {
1031 c.Check(logs, Matches, fmt.Sprintf(pattern, threshold, s.debian12MemoryCurrent, ram))
1033 if notExpected > threshold {
1034 c.Check(logs, Not(Matches), fmt.Sprintf(pattern, notExpected, s.debian12MemoryCurrent, ram))
1038 func (s *TestSuite) TestLogNoRSSThresholds(c *C) {
1039 s.testLogRSSThresholds(c, s.debian12MemoryCurrent*10, []int{}, 90)
1042 func (s *TestSuite) TestLogSomeRSSThresholds(c *C) {
1043 onePercentRSS := s.debian12MemoryCurrent / 100
1044 s.testLogRSSThresholds(c, 102*onePercentRSS, []int{90, 95}, 99)
1047 func (s *TestSuite) TestLogAllRSSThresholds(c *C) {
1048 s.testLogRSSThresholds(c, s.debian12MemoryCurrent, []int{90, 95, 99}, 0)
1051 func (s *TestSuite) TestLogMaximaAfterRun(c *C) {
1052 s.runner.crunchstatFakeFS = os.DirFS("../crunchstat/testdata/debian12")
1053 s.runner.parentTemp = c.MkDir()
1054 s.fullRunHelper(c, `{
1055 "command": ["true"],
1056 "container_image": "`+arvadostest.DockerImage112PDH+`",
1059 "mounts": {"/tmp": {"kind": "tmp"} },
1060 "output_path": "/tmp",
1062 "runtime_constraints": {"ram": `+fmt.Sprintf("%d", s.debian12MemoryCurrent*10)+`},
1064 }`, nil, func() int { return 0 })
1065 logs := logFileContent(c, s.runner, "crunch-run.txt")
1066 for _, expected := range []string{
1067 `Maximum disk usage was \d+%, \d+/\d+ bytes`,
1068 fmt.Sprintf(`Maximum container memory swap usage was %d bytes`, s.debian12SwapCurrent),
1069 `Maximum container memory pgmajfault usage was \d+ faults`,
1070 fmt.Sprintf(`Maximum container memory rss usage was 10%%, %d/%d bytes`, s.debian12MemoryCurrent, s.debian12MemoryCurrent*10),
1071 `Maximum crunch-run memory rss usage was \d+ bytes`,
1073 c.Check(logs, Matches, logLineStart+expected)
1077 func (s *TestSuite) TestCommitNodeInfoBeforeStart(c *C) {
1078 var collection_create, container_update arvadosclient.Dict
1079 s.fullRunHelper(c, `{
1080 "command": ["true"],
1081 "container_image": "`+arvadostest.DockerImage112PDH+`",
1084 "mounts": {"/tmp": {"kind": "tmp"} },
1085 "output_path": "/tmp",
1087 "runtime_constraints": {},
1089 "uuid": "zzzzz-dz642-202301121543210"
1090 }`, nil, func() int {
1091 collection_create = s.api.CalledWith("ensure_unique_name", true)
1092 container_update = s.api.CalledWith("container.state", "Running")
1096 c.Assert(collection_create, NotNil)
1097 log_collection := collection_create["collection"].(arvadosclient.Dict)
1098 c.Check(log_collection["name"], Equals, "logs for zzzzz-dz642-202301121543210")
1099 manifest_text := log_collection["manifest_text"].(string)
1100 // We check that the file size is at least two digits as an easy way to
1101 // check the file isn't empty.
1102 c.Check(manifest_text, Matches, `\. .+ \d+:\d{2,}:node-info\.txt( .+)?\n`)
1103 c.Check(manifest_text, Matches, `\. .+ \d+:\d{2,}:node\.json( .+)?\n`)
1105 c.Assert(container_update, NotNil)
1106 // As of Arvados 2.5.0, the container update must specify its log in PDH
1107 // format for the API server to propagate it to container requests, which
1108 // is what we care about for this test.
1109 expect_pdh := fmt.Sprintf("%x+%d", md5.Sum([]byte(manifest_text)), len(manifest_text))
1110 c.Check(container_update["container"].(arvadosclient.Dict)["log"], Equals, expect_pdh)
1113 func (s *TestSuite) TestContainerRecordLog(c *C) {
1114 s.fullRunHelper(c, `{
1115 "command": ["sleep", "1"],
1116 "container_image": "`+arvadostest.DockerImage112PDH+`",
1119 "mounts": {"/tmp": {"kind": "tmp"} },
1120 "output_path": "/tmp",
1122 "runtime_constraints": {},
1126 time.Sleep(time.Second)
1130 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1131 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1133 c.Check(logFileContent(c, s.runner, "container.json"), Matches, `(?ms).*container_image.*`)
1136 func (s *TestSuite) TestFullRunStderr(c *C) {
1137 s.fullRunHelper(c, `{
1138 "command": ["/bin/sh", "-c", "echo hello ; echo world 1>&2 ; exit 1"],
1139 "container_image": "`+arvadostest.DockerImage112PDH+`",
1142 "mounts": {"/tmp": {"kind": "tmp"} },
1143 "output_path": "/tmp",
1145 "runtime_constraints": {},
1147 }`, nil, func() int {
1148 fmt.Fprintln(s.executor.created.Stdout, "hello")
1149 fmt.Fprintln(s.executor.created.Stderr, "world")
1153 final := s.api.CalledWith("container.state", "Complete")
1154 c.Assert(final, NotNil)
1155 c.Check(final["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
1156 c.Check(final["container"].(arvadosclient.Dict)["log"], NotNil)
1158 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, ".*hello\n")
1159 c.Check(logFileContent(c, s.runner, "stderr.txt"), Matches, ".*world\n")
1162 func (s *TestSuite) TestFullRunDefaultCwd(c *C) {
1163 s.fullRunHelper(c, `{
1165 "container_image": "`+arvadostest.DockerImage112PDH+`",
1168 "mounts": {"/tmp": {"kind": "tmp"} },
1169 "output_path": "/tmp",
1171 "runtime_constraints": {},
1173 }`, nil, func() int {
1174 fmt.Fprintf(s.executor.created.Stdout, "workdir=%q", s.executor.created.WorkingDir)
1178 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1179 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1180 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, `.*workdir=""`)
1183 func (s *TestSuite) TestFullRunSetCwd(c *C) {
1184 s.fullRunHelper(c, `{
1186 "container_image": "`+arvadostest.DockerImage112PDH+`",
1189 "mounts": {"/tmp": {"kind": "tmp"} },
1190 "output_path": "/tmp",
1192 "runtime_constraints": {},
1194 }`, nil, func() int {
1195 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.WorkingDir)
1199 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1200 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1201 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, ".*/bin\n")
1204 func (s *TestSuite) TestFullRunSetOutputStorageClasses(c *C) {
1205 s.fullRunHelper(c, `{
1207 "container_image": "`+arvadostest.DockerImage112PDH+`",
1210 "mounts": {"/tmp": {"kind": "tmp"} },
1211 "output_path": "/tmp",
1213 "runtime_constraints": {},
1215 "output_storage_classes": ["foo", "bar"]
1216 }`, nil, func() int {
1217 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.WorkingDir)
1221 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1222 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1223 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, ".*/bin\n")
1224 c.Check(s.testDispatcherKeepClient.StorageClasses, DeepEquals, []string{"foo", "bar"})
1225 c.Check(s.testContainerKeepClient.StorageClasses, DeepEquals, []string{"foo", "bar"})
1228 func (s *TestSuite) TestEnableCUDADeviceCount(c *C) {
1229 s.fullRunHelper(c, `{
1231 "container_image": "`+arvadostest.DockerImage112PDH+`",
1234 "mounts": {"/tmp": {"kind": "tmp"} },
1235 "output_path": "/tmp",
1237 "runtime_constraints": {"cuda": {"device_count": 2}},
1239 "output_storage_classes": ["foo", "bar"]
1240 }`, nil, func() int {
1241 fmt.Fprintln(s.executor.created.Stdout, "ok")
1244 c.Check(s.executor.created.CUDADeviceCount, Equals, 2)
1247 func (s *TestSuite) TestEnableCUDAHardwareCapability(c *C) {
1248 s.fullRunHelper(c, `{
1250 "container_image": "`+arvadostest.DockerImage112PDH+`",
1253 "mounts": {"/tmp": {"kind": "tmp"} },
1254 "output_path": "/tmp",
1256 "runtime_constraints": {"cuda": {"hardware_capability": "foo"}},
1258 "output_storage_classes": ["foo", "bar"]
1259 }`, nil, func() int {
1260 fmt.Fprintln(s.executor.created.Stdout, "ok")
1263 c.Check(s.executor.created.CUDADeviceCount, Equals, 0)
1266 func (s *TestSuite) TestStopOnSignal(c *C) {
1267 s.executor.runFunc = func() int {
1268 s.executor.created.Stdout.Write([]byte("foo\n"))
1269 s.runner.SigChan <- syscall.SIGINT
1270 time.Sleep(10 * time.Second)
1273 s.testStopContainer(c)
1276 func (s *TestSuite) TestStopOnArvMountDeath(c *C) {
1277 s.executor.runFunc = func() int {
1278 s.executor.created.Stdout.Write([]byte("foo\n"))
1279 s.runner.ArvMountExit <- nil
1280 close(s.runner.ArvMountExit)
1281 time.Sleep(10 * time.Second)
1284 s.runner.ArvMountExit = make(chan error)
1285 s.testStopContainer(c)
1288 func (s *TestSuite) testStopContainer(c *C) {
1290 "command": ["/bin/sh", "-c", "echo foo && sleep 30 && echo bar"],
1291 "container_image": "` + arvadostest.DockerImage112PDH + `",
1294 "mounts": {"/tmp": {"kind": "tmp"} },
1295 "output_path": "/tmp",
1297 "runtime_constraints": {},
1301 err := json.Unmarshal([]byte(record), &s.api.Container)
1302 c.Assert(err, IsNil)
1304 s.runner.RunArvMount = func([]string, string) (*exec.Cmd, error) { return nil, nil }
1305 s.runner.MkArvClient = func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error) {
1306 return &ArvTestClient{}, &KeepTestClient{}, nil, nil
1309 done := make(chan error)
1311 done <- s.runner.Run()
1314 case <-time.After(20 * time.Second):
1315 pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
1316 c.Fatal("timed out")
1320 dumpAllLogFiles(c, s.runner)
1322 c.Check(s.api.CalledWith("container.log", nil), NotNil)
1323 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
1324 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, "(?ms).*foo\n$")
1327 func (s *TestSuite) TestFullRunSetEnv(c *C) {
1328 s.fullRunHelper(c, `{
1329 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1330 "container_image": "`+arvadostest.DockerImage112PDH+`",
1332 "environment": {"FROBIZ": "bilbo"},
1333 "mounts": {"/tmp": {"kind": "tmp"} },
1334 "output_path": "/tmp",
1336 "runtime_constraints": {},
1338 }`, nil, func() int {
1339 fmt.Fprintf(s.executor.created.Stdout, "%v", s.executor.created.Env)
1343 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1344 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1345 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, `.*map\[FROBIZ:bilbo\]`)
1348 type ArvMountCmdLine struct {
1353 func (am *ArvMountCmdLine) ArvMountTest(c []string, token string) (*exec.Cmd, error) {
1359 func stubCert(c *C, temp string) string {
1360 path := temp + "/ca-certificates.crt"
1361 err := os.WriteFile(path, []byte{}, 0666)
1362 c.Assert(err, IsNil)
1363 os.Setenv("SSL_CERT_FILE", path)
1367 func (s *TestSuite) TestSetupMounts(c *C) {
1369 am := &ArvMountCmdLine{}
1370 cr.RunArvMount = am.ArvMountTest
1371 cr.containerClient, _ = apiStub()
1372 cr.ContainerArvClient = &ArvTestClient{}
1373 cr.ContainerKeepClient = &KeepTestClient{}
1374 cr.Container.OutputStorageClasses = []string{"default"}
1376 realTemp := c.MkDir()
1377 certTemp := c.MkDir()
1378 stubCertPath := stubCert(c, certTemp)
1379 cr.parentTemp = realTemp
1382 cr.MkTempDir = func(_ string, prefix string) (string, error) {
1384 d := fmt.Sprintf("%s/%s%d", realTemp, prefix, i)
1385 err := os.Mkdir(d, os.ModePerm)
1386 if err != nil && strings.Contains(err.Error(), ": file exists") {
1387 // Test case must have pre-populated the tempdir
1393 checkEmpty := func() {
1394 // Should be deleted.
1395 _, err := os.Stat(realTemp)
1396 c.Assert(os.IsNotExist(err), Equals, true)
1398 // Now recreate it for the next test.
1399 c.Assert(os.Mkdir(realTemp, 0777), IsNil)
1404 cr.ArvMountPoint = ""
1405 cr.Container.Mounts = make(map[string]arvados.Mount)
1406 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
1407 cr.Container.OutputPath = "/tmp"
1408 cr.statInterval = 5 * time.Second
1409 bindmounts, err := cr.SetupMounts()
1411 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1412 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5",
1413 "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1414 c.Check(bindmounts, DeepEquals, map[string]bindmount{"/tmp": {realTemp + "/tmp2", false}})
1415 os.RemoveAll(cr.ArvMountPoint)
1422 cr.ArvMountPoint = ""
1423 cr.Container.Mounts = make(map[string]arvados.Mount)
1424 cr.Container.Mounts["/out"] = arvados.Mount{Kind: "tmp"}
1425 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
1426 cr.Container.OutputPath = "/out"
1427 cr.Container.OutputStorageClasses = []string{"foo", "bar"}
1429 bindmounts, err := cr.SetupMounts()
1431 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1432 "--read-write", "--storage-classes", "foo,bar", "--crunchstat-interval=5",
1433 "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1434 c.Check(bindmounts, DeepEquals, map[string]bindmount{"/out": {realTemp + "/tmp2", false}, "/tmp": {realTemp + "/tmp3", false}})
1435 os.RemoveAll(cr.ArvMountPoint)
1442 cr.ArvMountPoint = ""
1443 cr.Container.Mounts = make(map[string]arvados.Mount)
1444 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
1445 cr.Container.OutputPath = "/tmp"
1446 cr.Container.RuntimeConstraints.API = true
1447 cr.Container.OutputStorageClasses = []string{"default"}
1449 bindmounts, err := cr.SetupMounts()
1451 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1452 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5",
1453 "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1454 c.Check(bindmounts, DeepEquals, map[string]bindmount{"/tmp": {realTemp + "/tmp2", false}, "/etc/arvados/ca-certificates.crt": {stubCertPath, true}})
1455 os.RemoveAll(cr.ArvMountPoint)
1459 cr.Container.RuntimeConstraints.API = false
1464 cr.ArvMountPoint = ""
1465 cr.Container.Mounts = map[string]arvados.Mount{
1466 "/keeptmp": {Kind: "collection", Writable: true},
1468 cr.Container.OutputPath = "/keeptmp"
1470 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1472 bindmounts, err := cr.SetupMounts()
1474 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1475 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5",
1476 "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1477 c.Check(bindmounts, DeepEquals, map[string]bindmount{"/keeptmp": {realTemp + "/keep1/tmp0", false}})
1478 os.RemoveAll(cr.ArvMountPoint)
1485 cr.ArvMountPoint = ""
1486 cr.Container.Mounts = map[string]arvados.Mount{
1487 "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
1488 "/keepout": {Kind: "collection", Writable: true},
1490 cr.Container.OutputPath = "/keepout"
1492 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
1493 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1495 bindmounts, err := cr.SetupMounts()
1497 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1498 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5",
1499 "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1500 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1501 "/keepinp": {realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", true},
1502 "/keepout": {realTemp + "/keep1/tmp0", false},
1504 os.RemoveAll(cr.ArvMountPoint)
1511 cr.ArvMountPoint = ""
1512 cr.Container.RuntimeConstraints.KeepCacheRAM = 512
1513 cr.Container.Mounts = map[string]arvados.Mount{
1514 "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
1515 "/keepout": {Kind: "collection", Writable: true},
1517 cr.Container.OutputPath = "/keepout"
1519 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
1520 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1522 bindmounts, err := cr.SetupMounts()
1524 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1525 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5", "--ram-cache",
1526 "--file-cache", "512", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1527 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1528 "/keepinp": {realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", true},
1529 "/keepout": {realTemp + "/keep1/tmp0", false},
1531 os.RemoveAll(cr.ArvMountPoint)
1536 for _, test := range []struct {
1540 {in: "foo", out: `"foo"`},
1541 {in: nil, out: `null`},
1542 {in: map[string]int64{"foo": 123456789123456789}, out: `{"foo":123456789123456789}`},
1545 cr.ArvMountPoint = ""
1546 cr.Container.Mounts = map[string]arvados.Mount{
1547 "/mnt/test.json": {Kind: "json", Content: test.in},
1549 bindmounts, err := cr.SetupMounts()
1551 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1552 "/mnt/test.json": {realTemp + "/json2/mountdata.json", true},
1554 content, err := ioutil.ReadFile(realTemp + "/json2/mountdata.json")
1556 c.Check(content, DeepEquals, []byte(test.out))
1557 os.RemoveAll(cr.ArvMountPoint)
1562 for _, test := range []struct {
1566 {in: "foo", out: `foo`},
1567 {in: nil, out: "error"},
1568 {in: map[string]int64{"foo": 123456789123456789}, out: "error"},
1571 cr.ArvMountPoint = ""
1572 cr.Container.Mounts = map[string]arvados.Mount{
1573 "/mnt/test.txt": {Kind: "text", Content: test.in},
1575 bindmounts, err := cr.SetupMounts()
1576 if test.out == "error" {
1577 c.Check(err.Error(), Equals, "content for mount \"/mnt/test.txt\" must be a string")
1580 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1581 "/mnt/test.txt": {realTemp + "/text2/mountdata.text", true},
1583 content, err := ioutil.ReadFile(realTemp + "/text2/mountdata.text")
1585 c.Check(content, DeepEquals, []byte(test.out))
1587 os.RemoveAll(cr.ArvMountPoint)
1592 // Read-only mount points are allowed underneath output_dir mount point
1595 cr.ArvMountPoint = ""
1596 cr.Container.Mounts = make(map[string]arvados.Mount)
1597 cr.Container.Mounts = map[string]arvados.Mount{
1598 "/tmp": {Kind: "tmp"},
1599 "/tmp/foo": {Kind: "collection"},
1601 cr.Container.OutputPath = "/tmp"
1603 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1605 bindmounts, err := cr.SetupMounts()
1607 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1608 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5", "--ram-cache",
1609 "--file-cache", "512", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1610 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1611 "/tmp": {realTemp + "/tmp2", false},
1612 "/tmp/foo": {realTemp + "/keep1/tmp0", true},
1614 os.RemoveAll(cr.ArvMountPoint)
1619 // Writable mount points copied to output_dir mount point
1622 cr.ArvMountPoint = ""
1623 cr.Container.Mounts = make(map[string]arvados.Mount)
1624 cr.Container.Mounts = map[string]arvados.Mount{
1625 "/tmp": {Kind: "tmp"},
1626 "/tmp/foo": {Kind: "collection",
1627 PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53",
1629 "/tmp/bar": {Kind: "collection",
1630 PortableDataHash: "59389a8f9ee9d399be35462a0f92541d+53",
1634 cr.Container.OutputPath = "/tmp"
1636 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
1637 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541d+53/baz", os.ModePerm)
1639 rf, _ := os.Create(realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541d+53/baz/quux")
1640 rf.Write([]byte("bar"))
1643 _, err := cr.SetupMounts()
1645 _, err = os.Stat(cr.HostOutputDir + "/foo")
1647 _, err = os.Stat(cr.HostOutputDir + "/bar/quux")
1649 os.RemoveAll(cr.ArvMountPoint)
1654 // Only mount points of kind 'collection' are allowed underneath output_dir mount point
1657 cr.ArvMountPoint = ""
1658 cr.Container.Mounts = make(map[string]arvados.Mount)
1659 cr.Container.Mounts = map[string]arvados.Mount{
1660 "/tmp": {Kind: "tmp"},
1661 "/tmp/foo": {Kind: "tmp"},
1663 cr.Container.OutputPath = "/tmp"
1665 _, err := cr.SetupMounts()
1666 c.Check(err, NotNil)
1667 c.Check(err, ErrorMatches, `only mount points of kind 'collection', 'text' or 'json' are supported underneath the output_path.*`)
1668 os.RemoveAll(cr.ArvMountPoint)
1673 // Only mount point of kind 'collection' is allowed for stdin
1676 cr.ArvMountPoint = ""
1677 cr.Container.Mounts = make(map[string]arvados.Mount)
1678 cr.Container.Mounts = map[string]arvados.Mount{
1679 "stdin": {Kind: "tmp"},
1682 _, err := cr.SetupMounts()
1683 c.Check(err, NotNil)
1684 c.Check(err, ErrorMatches, `unsupported mount kind 'tmp' for stdin.*`)
1685 os.RemoveAll(cr.ArvMountPoint)
1691 func (s *TestSuite) TestStdout(c *C) {
1693 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1694 "container_image": "` + arvadostest.DockerImage112PDH + `",
1696 "environment": {"FROBIZ": "bilbo"},
1697 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"} },
1698 "output_path": "/tmp",
1700 "runtime_constraints": {},
1704 s.fullRunHelper(c, helperRecord, nil, func() int {
1705 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
1709 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1710 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1711 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), NotNil)
1714 // Used by the TestStdoutWithWrongPath*()
1715 func (s *TestSuite) stdoutErrorRunHelper(c *C, record string, fn func() int) (*ArvTestClient, *ContainerRunner, error) {
1716 err := json.Unmarshal([]byte(record), &s.api.Container)
1717 c.Assert(err, IsNil)
1718 s.executor.runFunc = fn
1719 s.runner.RunArvMount = (&ArvMountCmdLine{}).ArvMountTest
1720 s.runner.MkArvClient = func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error) {
1721 return s.api, &KeepTestClient{}, nil, nil
1723 return s.api, s.runner, s.runner.Run()
1726 func (s *TestSuite) TestStdoutWithWrongPath(c *C) {
1727 _, _, err := s.stdoutErrorRunHelper(c, `{
1728 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path":"/tmpa.out"} },
1729 "output_path": "/tmp",
1731 }`, func() int { return 0 })
1732 c.Check(err, ErrorMatches, ".*Stdout path does not start with OutputPath.*")
1735 func (s *TestSuite) TestStdoutWithWrongKindTmp(c *C) {
1736 _, _, err := s.stdoutErrorRunHelper(c, `{
1737 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "tmp", "path":"/tmp/a.out"} },
1738 "output_path": "/tmp",
1740 }`, func() int { return 0 })
1741 c.Check(err, ErrorMatches, ".*unsupported mount kind 'tmp' for stdout.*")
1744 func (s *TestSuite) TestStdoutWithWrongKindCollection(c *C) {
1745 _, _, err := s.stdoutErrorRunHelper(c, `{
1746 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "collection", "path":"/tmp/a.out"} },
1747 "output_path": "/tmp",
1749 }`, func() int { return 0 })
1750 c.Check(err, ErrorMatches, ".*unsupported mount kind 'collection' for stdout.*")
1753 func (s *TestSuite) TestFullRunWithAPI(c *C) {
1754 s.fullRunHelper(c, `{
1755 "command": ["/bin/sh", "-c", "true $ARVADOS_API_HOST"],
1756 "container_image": "`+arvadostest.DockerImage112PDH+`",
1759 "mounts": {"/tmp": {"kind": "tmp"} },
1760 "output_path": "/tmp",
1762 "runtime_constraints": {"API": true},
1764 }`, nil, func() int {
1765 c.Check(s.executor.created.Env["ARVADOS_API_HOST"], Equals, os.Getenv("ARVADOS_API_HOST"))
1768 c.Check(s.api.CalledWith("container.exit_code", 3), NotNil)
1769 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1770 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*`+reTimestamp+` Container exited with status code 3\n.*`)
1773 func (s *TestSuite) TestFullRunSetOutput(c *C) {
1774 defer os.Setenv("ARVADOS_API_HOST", os.Getenv("ARVADOS_API_HOST"))
1775 os.Setenv("ARVADOS_API_HOST", "test.arvados.org")
1776 s.fullRunHelper(c, `{
1777 "command": ["/bin/sh", "-c", "echo $ARVADOS_API_HOST"],
1778 "container_image": "`+arvadostest.DockerImage112PDH+`",
1781 "mounts": {"/tmp": {"kind": "tmp"} },
1782 "output_path": "/tmp",
1784 "runtime_constraints": {"API": true},
1786 }`, nil, func() int {
1787 s.api.Container.Output = arvadostest.DockerImage112PDH
1791 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1792 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1793 c.Check(s.api.CalledWith("container.output", arvadostest.DockerImage112PDH), NotNil)
1796 func (s *TestSuite) TestArvMountRuntimeStatusWarning(c *C) {
1797 s.runner.RunArvMount = func([]string, string) (*exec.Cmd, error) {
1798 os.Mkdir(s.runner.ArvMountPoint+"/by_id", 0666)
1799 ioutil.WriteFile(s.runner.ArvMountPoint+"/by_id/README", nil, 0666)
1800 return s.runner.ArvMountCmd([]string{"bash", "-c", "echo >&2 Test: Keep write error: I am a teapot; sleep 3"}, "")
1802 s.executor.runFunc = func() int {
1803 time.Sleep(time.Second)
1807 "command": ["sleep", "1"],
1808 "container_image": "` + arvadostest.DockerImage112PDH + `",
1811 "mounts": {"/tmp": {"kind": "tmp"} },
1812 "output_path": "/tmp",
1814 "runtime_constraints": {"API": true},
1817 err := json.Unmarshal([]byte(record), &s.api.Container)
1818 c.Assert(err, IsNil)
1819 err = s.runner.Run()
1820 c.Assert(err, IsNil)
1821 c.Check(s.api.CalledWith("container.exit_code", 137), NotNil)
1822 c.Check(s.api.CalledWith("container.runtime_status.warning", "arv-mount: Keep write error"), NotNil)
1823 c.Check(s.api.CalledWith("container.runtime_status.warningDetail", "Test: Keep write error: I am a teapot"), NotNil)
1824 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1825 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*`+reTimestamp+` Container exited with status code 137 \(signal 9, SIGKILL\).*`)
1826 c.Check(logFileContent(c, s.runner, "arv-mount.txt"), Matches, reTimestamp+` Test: Keep write error: I am a teapot\n`)
1829 func (s *TestSuite) TestStdoutWithExcludeFromOutputMountPointUnderOutputDir(c *C) {
1831 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1832 "container_image": "` + arvadostest.DockerImage112PDH + `",
1834 "environment": {"FROBIZ": "bilbo"},
1836 "/tmp": {"kind": "tmp"},
1837 "/tmp/foo": {"kind": "collection",
1838 "portable_data_hash": "a3e8f74c6f101eae01fa08bfb4e49b3a+54",
1839 "exclude_from_output": true
1841 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1843 "output_path": "/tmp",
1845 "runtime_constraints": {},
1849 extraMounts := []string{"a3e8f74c6f101eae01fa08bfb4e49b3a+54"}
1851 s.fullRunHelper(c, helperRecord, extraMounts, func() int {
1852 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
1856 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1857 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1858 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), NotNil)
1861 func (s *TestSuite) TestStdoutWithMultipleMountPointsUnderOutputDir(c *C) {
1863 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1864 "container_image": "` + arvadostest.DockerImage112PDH + `",
1866 "environment": {"FROBIZ": "bilbo"},
1868 "/tmp": {"kind": "tmp"},
1869 "/tmp/foo/bar": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/file2_in_main.txt"},
1870 "/tmp/foo/sub1": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/subdir1"},
1871 "/tmp/foo/sub1file2": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/subdir1/file2_in_subdir1.txt"},
1872 "/tmp/foo/baz/sub2file2": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/subdir1/subdir2/file2_in_subdir2.txt"},
1873 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1875 "output_path": "/tmp",
1877 "runtime_constraints": {},
1879 "uuid": "zzzzz-dz642-202301130848001"
1882 extraMounts := []string{
1883 "a0def87f80dd594d4675809e83bd4f15+367/file2_in_main.txt",
1884 "a0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt",
1885 "a0def87f80dd594d4675809e83bd4f15+367/subdir1/subdir2/file2_in_subdir2.txt",
1888 api, _, realtemp := s.fullRunHelper(c, helperRecord, extraMounts, func() int {
1889 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
1893 c.Check(s.executor.created.BindMounts, DeepEquals, map[string]bindmount{
1894 "/tmp": {realtemp + "/tmp1", false},
1895 "/tmp/foo/bar": {s.keepmount + "/by_id/a0def87f80dd594d4675809e83bd4f15+367/file2_in_main.txt", true},
1896 "/tmp/foo/baz/sub2file2": {s.keepmount + "/by_id/a0def87f80dd594d4675809e83bd4f15+367/subdir1/subdir2/file2_in_subdir2.txt", true},
1897 "/tmp/foo/sub1": {s.keepmount + "/by_id/a0def87f80dd594d4675809e83bd4f15+367/subdir1", true},
1898 "/tmp/foo/sub1file2": {s.keepmount + "/by_id/a0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt", true},
1901 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1902 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1903 output_count := uint(0)
1904 for _, v := range s.runner.ContainerArvClient.(*ArvTestClient).Content {
1905 if v["collection"] == nil {
1908 collection := v["collection"].(arvadosclient.Dict)
1909 if collection["name"].(string) != "output for zzzzz-dz642-202301130848001" {
1912 c.Check(v["ensure_unique_name"], Equals, true)
1913 c.Check(collection["manifest_text"].(string), Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
1914 ./foo 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0abcdefgh11234567890@569fa8c3 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 9:18:bar 36:18:sub1file2
1915 ./foo/baz 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0bcdefghijk544332211@569fa8c5 9:18:sub2file2
1916 ./foo/sub1 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 0:9:file1_in_subdir1.txt 9:18:file2_in_subdir1.txt
1917 ./foo/sub1/subdir2 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0bcdefghijk544332211@569fa8c5 0:9:file1_in_subdir2.txt 9:18:file2_in_subdir2.txt
1921 c.Check(output_count, Not(Equals), uint(0))
1924 func (s *TestSuite) TestStdoutWithMountPointsUnderOutputDirDenormalizedManifest(c *C) {
1926 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1927 "container_image": "` + arvadostest.DockerImage112PDH + `",
1929 "environment": {"FROBIZ": "bilbo"},
1931 "/tmp": {"kind": "tmp"},
1932 "/tmp/foo/bar": {"kind": "collection", "portable_data_hash": "b0def87f80dd594d4675809e83bd4f15+367", "path": "/subdir1/file2_in_subdir1.txt"},
1933 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1935 "output_path": "/tmp",
1937 "runtime_constraints": {},
1939 "uuid": "zzzzz-dz642-202301130848002"
1942 extraMounts := []string{
1943 "b0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt",
1946 s.fullRunHelper(c, helperRecord, extraMounts, func() int {
1947 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
1951 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1952 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1953 output_count := uint(0)
1954 for _, v := range s.runner.ContainerArvClient.(*ArvTestClient).Content {
1955 if v["collection"] == nil {
1958 collection := v["collection"].(arvadosclient.Dict)
1959 if collection["name"].(string) != "output for zzzzz-dz642-202301130848002" {
1962 c.Check(collection["manifest_text"].(string), Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
1963 ./foo 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0abcdefgh11234567890@569fa8c3 10:17:bar
1967 c.Check(output_count, Not(Equals), uint(0))
1970 func (s *TestSuite) TestOutputError(c *C) {
1972 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1973 "container_image": "` + arvadostest.DockerImage112PDH + `",
1975 "environment": {"FROBIZ": "bilbo"},
1977 "/tmp": {"kind": "tmp"}
1979 "output_path": "/tmp",
1981 "runtime_constraints": {},
1984 s.fullRunHelper(c, helperRecord, nil, func() int {
1985 os.Symlink("/etc/hosts", s.runner.HostOutputDir+"/baz")
1989 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
1992 func (s *TestSuite) TestStdinCollectionMountPoint(c *C) {
1994 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1995 "container_image": "` + arvadostest.DockerImage112PDH + `",
1997 "environment": {"FROBIZ": "bilbo"},
1999 "/tmp": {"kind": "tmp"},
2000 "stdin": {"kind": "collection", "portable_data_hash": "b0def87f80dd594d4675809e83bd4f15+367", "path": "/file1_in_main.txt"},
2001 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
2003 "output_path": "/tmp",
2005 "runtime_constraints": {},
2009 extraMounts := []string{
2010 "b0def87f80dd594d4675809e83bd4f15+367/file1_in_main.txt",
2013 api, _, _ := s.fullRunHelper(c, helperRecord, extraMounts, func() int {
2014 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
2018 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
2019 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
2020 for _, v := range api.Content {
2021 if v["collection"] != nil {
2022 collection := v["collection"].(arvadosclient.Dict)
2023 if strings.Index(collection["name"].(string), "output") == 0 {
2024 manifest := collection["manifest_text"].(string)
2025 c.Check(manifest, Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
2032 func (s *TestSuite) TestStdinJsonMountPoint(c *C) {
2034 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
2035 "container_image": "` + arvadostest.DockerImage112PDH + `",
2037 "environment": {"FROBIZ": "bilbo"},
2039 "/tmp": {"kind": "tmp"},
2040 "stdin": {"kind": "json", "content": "foo"},
2041 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
2043 "output_path": "/tmp",
2045 "runtime_constraints": {},
2049 api, _, _ := s.fullRunHelper(c, helperRecord, nil, func() int {
2050 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
2054 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
2055 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
2056 for _, v := range api.Content {
2057 if v["collection"] != nil {
2058 collection := v["collection"].(arvadosclient.Dict)
2059 if strings.Index(collection["name"].(string), "output") == 0 {
2060 manifest := collection["manifest_text"].(string)
2061 c.Check(manifest, Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
2068 func (s *TestSuite) TestStderrMount(c *C) {
2069 api, cr, _ := s.fullRunHelper(c, `{
2070 "command": ["/bin/sh", "-c", "echo hello;exit 1"],
2071 "container_image": "`+arvadostest.DockerImage112PDH+`",
2074 "mounts": {"/tmp": {"kind": "tmp"},
2075 "stdout": {"kind": "file", "path": "/tmp/a/out.txt"},
2076 "stderr": {"kind": "file", "path": "/tmp/b/err.txt"}},
2077 "output_path": "/tmp",
2079 "runtime_constraints": {},
2081 }`, nil, func() int {
2082 fmt.Fprintln(s.executor.created.Stdout, "hello")
2083 fmt.Fprintln(s.executor.created.Stderr, "oops")
2087 final := api.CalledWith("container.state", "Complete")
2088 c.Assert(final, NotNil)
2089 c.Check(final["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
2090 c.Check(final["container"].(arvadosclient.Dict)["log"], NotNil)
2092 c.Check(cr.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", "./a b1946ac92492d2347c6235b4d2611184+6 0:6:out.txt\n./b 38af5c54926b620264ab1501150cf189+5 0:5:err.txt\n"), NotNil)
2095 func (s *TestSuite) TestNumberRoundTrip(c *C) {
2096 s.api.callraw = true
2097 err := s.runner.fetchContainerRecord()
2098 c.Assert(err, IsNil)
2099 jsondata, err := json.Marshal(s.runner.Container.Mounts["/json"].Content)
2100 c.Logf("%#v", s.runner.Container)
2102 c.Check(string(jsondata), Equals, `{"number":123456789123456789}`)
2105 func (s *TestSuite) TestFullBrokenDocker(c *C) {
2107 for _, setup := range []func(){
2109 c.Log("// waitErr = ocl runtime error")
2110 s.executor.waitErr = errors.New(`Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "process_linux.go:359: container init caused \"rootfs_linux.go:54: mounting \\\"/tmp/keep453790790/by_id/99999999999999999999999999999999+99999/myGenome\\\" to rootfs \\\"/tmp/docker/overlay2/9999999999999999999999999999999999999999999999999999999999999999/merged\\\" at \\\"/tmp/docker/overlay2/9999999999999999999999999999999999999999999999999999999999999999/merged/keep/99999999999999999999999999999999+99999/myGenome\\\" caused \\\"no such file or directory\\\"\""`)
2111 nextState = "Cancelled"
2114 c.Log("// loadErr = cannot connect")
2115 s.executor.loadErr = errors.New("Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?")
2116 s.runner.brokenNodeHook = c.MkDir() + "/broken-node-hook"
2117 err := ioutil.WriteFile(s.runner.brokenNodeHook, []byte("#!/bin/sh\nexec echo killme\n"), 0700)
2118 c.Assert(err, IsNil)
2119 nextState = "Queued"
2124 s.fullRunHelper(c, `{
2125 "command": ["echo", "hello world"],
2126 "container_image": "`+arvadostest.DockerImage112PDH+`",
2129 "mounts": {"/tmp": {"kind": "tmp"} },
2130 "output_path": "/tmp",
2132 "runtime_constraints": {},
2134 }`, nil, func() int { return 0 })
2135 c.Check(s.api.CalledWith("container.state", nextState), NotNil)
2136 logs := logFileContent(c, s.runner, "crunch-run.txt")
2137 c.Check(logs, Matches, "(?ms).*unable to run containers.*")
2138 if s.runner.brokenNodeHook != "" {
2139 c.Check(logs, Matches, "(?ms).*Running broken node hook.*")
2140 c.Check(logs, Matches, "(?ms).*killme.*")
2141 c.Check(logs, Not(Matches), "(?ms).*Writing /var/lock/crunch-run-broken to mark node as broken.*")
2143 c.Check(logs, Matches, "(?ms).*Writing /var/lock/crunch-run-broken to mark node as broken.*")
2148 func (s *TestSuite) TestBadCommand(c *C) {
2149 for _, startError := range []string{
2150 `panic: standard_init_linux.go:175: exec user process caused "no such file or directory"`,
2151 `Error response from daemon: Cannot start container 41f26cbc43bcc1280f4323efb1830a394ba8660c9d1c2b564ba42bf7f7694845: [8] System error: no such file or directory`,
2152 `Error response from daemon: Cannot start container 58099cd76c834f3dc2a4fb76c8028f049ae6d4fdf0ec373e1f2cfea030670c2d: [8] System error: exec: "foobar": executable file not found in $PATH`,
2155 s.executor.startErr = errors.New(startError)
2156 s.fullRunHelper(c, `{
2157 "command": ["echo", "hello world"],
2158 "container_image": "`+arvadostest.DockerImage112PDH+`",
2161 "mounts": {"/tmp": {"kind": "tmp"} },
2162 "output_path": "/tmp",
2164 "runtime_constraints": {},
2166 }`, nil, func() int { return 0 })
2167 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
2168 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, "(?ms).*Possible causes:.*is missing.*")
2172 func (s *TestSuite) TestSecretTextMountPoint(c *C) {
2174 "command": ["true"],
2175 "container_image": "` + arvadostest.DockerImage112PDH + `",
2178 "/tmp": {"kind": "tmp"},
2179 "/tmp/secret.conf": {"kind": "text", "content": "mypassword"}
2183 "output_path": "/tmp",
2185 "runtime_constraints": {},
2189 s.fullRunHelper(c, helperRecord, nil, func() int {
2190 content, err := ioutil.ReadFile(s.runner.HostOutputDir + "/secret.conf")
2192 c.Check(string(content), Equals, "mypassword")
2196 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
2197 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
2198 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ". 34819d7beeabb9260a5c854bc85b3e44+10 0:10:secret.conf\n"), NotNil)
2199 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ""), IsNil)
2201 // under secret mounts, not captured in output
2203 "command": ["true"],
2204 "container_image": "` + arvadostest.DockerImage112PDH + `",
2207 "/tmp": {"kind": "tmp"}
2210 "/tmp/secret.conf": {"kind": "text", "content": "mypassword"}
2212 "output_path": "/tmp",
2214 "runtime_constraints": {},
2219 s.fullRunHelper(c, helperRecord, nil, func() int {
2220 content, err := ioutil.ReadFile(s.runner.HostOutputDir + "/secret.conf")
2222 c.Check(string(content), Equals, "mypassword")
2226 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
2227 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
2228 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ". 34819d7beeabb9260a5c854bc85b3e44+10 0:10:secret.conf\n"), IsNil)
2229 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ""), NotNil)
2231 // under secret mounts, output dir is a collection, not captured in output
2233 "command": ["true"],
2234 "container_image": "` + arvadostest.DockerImage112PDH + `",
2237 "/tmp": {"kind": "collection", "writable": true}
2240 "/tmp/secret.conf": {"kind": "text", "content": "mypassword"}
2242 "output_path": "/tmp",
2244 "runtime_constraints": {},
2249 _, _, realtemp := s.fullRunHelper(c, helperRecord, nil, func() int {
2250 // secret.conf should be provisioned as a separate
2251 // bind mount, i.e., it should not appear in the
2252 // (fake) fuse filesystem as viewed from the host.
2253 content, err := ioutil.ReadFile(s.runner.HostOutputDir + "/secret.conf")
2254 if !c.Check(errors.Is(err, os.ErrNotExist), Equals, true) {
2255 c.Logf("secret.conf: content %q, err %#v", content, err)
2257 err = ioutil.WriteFile(s.runner.HostOutputDir+"/.arvados#collection", []byte(`{"manifest_text":". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo.txt\n"}`), 0700)
2262 content, err := ioutil.ReadFile(realtemp + "/text1/mountdata.text")
2264 c.Check(string(content), Equals, "mypassword")
2265 c.Check(s.executor.created.BindMounts["/tmp/secret.conf"], DeepEquals, bindmount{realtemp + "/text1/mountdata.text", true})
2266 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
2267 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
2268 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo.txt\n"), NotNil)
2271 func (s *TestSuite) TestCalculateCost(c *C) {
2272 defer func(s string) { lockdir = s }(lockdir)
2276 cr.costStartTime = now.Add(-time.Hour)
2277 var logbuf bytes.Buffer
2278 cr.CrunchLog = newLogWriter(&logbuf)
2280 // if there's no InstanceType env var, cost is calculated as 0
2281 os.Unsetenv("InstanceType")
2282 cost := cr.calculateCost(now)
2283 c.Check(cost, Equals, 0.0)
2285 // with InstanceType env var and loadPrices() hasn't run (or
2286 // hasn't found any data), cost is calculated based on
2287 // InstanceType env var
2288 os.Setenv("InstanceType", `{"Price":1.2}`)
2289 cost = cr.calculateCost(now)
2290 c.Check(cost, Equals, 1.2)
2292 // first update tells us the spot price was $1/h until 30
2293 // minutes ago when it increased to $2/h
2294 j, err := json.Marshal([]cloud.InstancePrice{
2295 {StartTime: now.Add(-4 * time.Hour), Price: 1.0},
2296 {StartTime: now.Add(-time.Hour / 2), Price: 2.0},
2298 c.Assert(err, IsNil)
2299 os.WriteFile(lockdir+"/"+pricesfile, j, 0777)
2301 cost = cr.calculateCost(now)
2302 c.Check(cost, Equals, 1.5)
2304 // next update (via --list + SIGUSR2) tells us the spot price
2305 // increased to $3/h 15 minutes ago
2306 j, err = json.Marshal([]cloud.InstancePrice{
2307 {StartTime: now.Add(-time.Hour / 3), Price: 2.0}, // dup of -time.Hour/2 price
2308 {StartTime: now.Add(-time.Hour / 4), Price: 3.0},
2310 c.Assert(err, IsNil)
2311 os.WriteFile(lockdir+"/"+pricesfile, j, 0777)
2313 cost = cr.calculateCost(now)
2314 c.Check(cost, Equals, 1.0/2+2.0/4+3.0/4)
2316 cost = cr.calculateCost(now.Add(-time.Hour / 2))
2317 c.Check(cost, Equals, 0.5)
2319 c.Logf("%s", logbuf.String())
2320 c.Check(logbuf.String(), Matches, `(?ms).*Instance price changed to 1\.00 at 20.* changed to 2\.00 .* changed to 3\.00 .*`)
2321 c.Check(logbuf.String(), Not(Matches), `(?ms).*changed to 2\.00 .* changed to 2\.00 .*`)
2324 func (s *TestSuite) TestSIGUSR2CostUpdate(c *C) {
2327 pricesJSON, err := json.Marshal([]cloud.InstancePrice{
2328 {StartTime: now.Add(-4 * time.Hour), Price: 2.4},
2329 {StartTime: now.Add(-2 * time.Hour), Price: 2.6},
2331 c.Assert(err, IsNil)
2333 os.Setenv("InstanceType", `{"Price":2.2}`)
2334 defer func(s string) { lockdir = s }(lockdir)
2337 // We can't use s.api.CalledWith because timing differences will yield
2338 // different cost values across runs. getCostUpdate iterates over API
2339 // calls until it finds one that sets the cost, then writes that value
2340 // to the next index of costUpdates.
2341 deadline := now.Add(time.Second)
2342 costUpdates := make([]float64, 2)
2345 getCostUpdate := func() {
2346 for ; time.Now().Before(deadline); time.Sleep(time.Second / 10) {
2347 for apiIndex < len(s.api.Content) {
2348 update := s.api.Content[apiIndex]
2352 if update, ok = update["container"].(arvadosclient.Dict); !ok {
2355 if cost, ok = update["cost"].(float64); !ok {
2358 c.Logf("API call #%d updates cost to %v", apiIndex-1, cost)
2359 costUpdates[costIndex] = cost
2366 s.fullRunHelper(c, `{
2367 "command": ["true"],
2368 "container_image": "`+arvadostest.DockerImage112PDH+`",
2371 "mounts": {"/tmp": {"kind": "tmp"} },
2372 "output_path": "/tmp",
2374 "runtime_constraints": {},
2376 "uuid": "zzzzz-dz642-20230320101530a"
2377 }`, nil, func() int {
2378 s.runner.costStartTime = now.Add(-3 * time.Hour)
2379 err := syscall.Kill(pid, syscall.SIGUSR2)
2380 c.Check(err, IsNil, Commentf("error sending first SIGUSR2 to runner"))
2383 err = os.WriteFile(path.Join(lockdir, pricesfile), pricesJSON, 0o700)
2384 c.Check(err, IsNil, Commentf("error writing JSON prices file"))
2385 err = syscall.Kill(pid, syscall.SIGUSR2)
2386 c.Check(err, IsNil, Commentf("error sending second SIGUSR2 to runner"))
2391 // Comparing with format strings makes it easy to ignore minor variations
2392 // in cost across runs while keeping diagnostics pretty.
2393 c.Check(fmt.Sprintf("%.3f", costUpdates[0]), Equals, "6.600")
2394 c.Check(fmt.Sprintf("%.3f", costUpdates[1]), Equals, "7.600")
2397 type FakeProcess struct {
2401 func (fp FakeProcess) CmdlineSlice() ([]string, error) {
2402 return fp.cmdLine, nil
2405 func logFileContent(c *C, cr *ContainerRunner, fnm string) string {
2406 buf, err := fs.ReadFile(arvados.FS(cr.LogCollection), fnm)
2407 c.Assert(err, IsNil)
2411 func dumpAllLogFiles(c *C, cr *ContainerRunner) {
2412 d, err := cr.LogCollection.OpenFile("/", os.O_RDONLY, 0)
2413 c.Assert(err, IsNil)
2414 fis, err := d.Readdir(-1)
2415 c.Assert(err, IsNil)
2416 for _, fi := range fis {
2417 c.Logf("=== %s", fi.Name())
2418 c.Log(logFileContent(c, cr, fi.Name()))