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) BlockRead(_ context.Context, opts arvados.BlockReadOptions) (int, error) {
353 loaded, ok := client.blocks.Load(opts.Locator)
355 return 0, os.ErrNotExist
357 n, err := io.Copy(opts.WriteTo, bytes.NewReader(loaded.([]byte)))
361 func (client *KeepTestClient) ReadAt(locator string, dst []byte, offset int) (int, error) {
362 loaded, ok := client.blocks.Load(locator)
364 return 0, os.ErrNotExist
366 data := loaded.([]byte)
367 if offset >= len(data) {
370 return copy(dst, data[offset:]), nil
373 func (client *KeepTestClient) Close() {
374 client.blocks.Range(func(locator, value interface{}) bool {
375 client.blocks.Delete(locator)
380 func (client *KeepTestClient) SetStorageClasses(sc []string) {
381 client.StorageClasses = sc
384 type FileWrapper struct {
389 func (fw FileWrapper) Readdir(n int) ([]os.FileInfo, error) {
390 return nil, errors.New("not implemented")
393 func (fw FileWrapper) Seek(int64, int) (int64, error) {
394 return 0, errors.New("not implemented")
397 func (fw FileWrapper) Size() int64 {
401 func (fw FileWrapper) Stat() (os.FileInfo, error) {
402 return nil, errors.New("not implemented")
405 func (fw FileWrapper) Truncate(int64) error {
406 return errors.New("not implemented")
409 func (fw FileWrapper) Write([]byte) (int, error) {
410 return 0, errors.New("not implemented")
413 func (fw FileWrapper) Sync() error {
414 return errors.New("not implemented")
417 func (fw FileWrapper) Snapshot() (*arvados.Subtree, error) {
418 return nil, errors.New("not implemented")
421 func (fw FileWrapper) Splice(*arvados.Subtree) error {
422 return errors.New("not implemented")
425 type apiStubServer struct {
426 server *httptest.Server
427 proxy *httputil.ReverseProxy
428 intercept func(http.ResponseWriter, *http.Request) bool
430 container arvados.Container
433 func apiStub() (*arvados.Client, *apiStubServer) {
434 client := arvados.NewClientFromEnv()
435 apistub := &apiStubServer{}
436 apistub.server = httptest.NewTLSServer(apistub)
437 apistub.proxy = httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "https", Host: client.APIHost})
439 apistub.proxy.Transport = arvados.InsecureHTTPClient.Transport
441 client.APIHost = apistub.server.Listener.Addr().String()
442 return client, apistub
445 func (apistub *apiStubServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
446 if apistub.intercept != nil && apistub.intercept(w, r) {
449 if r.Method == "GET" && r.URL.Path == "/arvados/v1/collections/"+hwPDH {
450 json.NewEncoder(w).Encode(arvados.Collection{ManifestText: hwManifest})
453 if r.Method == "GET" && r.URL.Path == "/arvados/v1/collections/"+otherPDH {
454 json.NewEncoder(w).Encode(arvados.Collection{ManifestText: otherManifest})
457 if r.Method == "GET" && r.URL.Path == "/arvados/v1/collections/"+normalizedWithSubdirsPDH {
458 json.NewEncoder(w).Encode(arvados.Collection{ManifestText: normalizedManifestWithSubdirs})
461 if r.Method == "GET" && r.URL.Path == "/arvados/v1/collections/"+denormalizedWithSubdirsPDH {
462 json.NewEncoder(w).Encode(arvados.Collection{ManifestText: denormalizedManifestWithSubdirs})
465 if r.Method == "GET" && r.URL.Path == "/arvados/v1/containers/"+apistub.container.UUID {
466 json.NewEncoder(w).Encode(apistub.container)
469 apistub.proxy.ServeHTTP(w, r)
472 func (s *TestSuite) TestLoadImage(c *C) {
473 s.runner.Container.ContainerImage = arvadostest.DockerImage112PDH
474 s.runner.Container.Mounts = map[string]arvados.Mount{
475 "/out": {Kind: "tmp", Writable: true},
477 s.runner.Container.OutputPath = "/out"
479 _, err := s.runner.SetupMounts()
482 imageID, err := s.runner.LoadImage()
484 c.Check(s.executor.loaded, Matches, ".*"+regexp.QuoteMeta(arvadostest.DockerImage112Filename))
485 c.Check(imageID, Equals, strings.TrimSuffix(arvadostest.DockerImage112Filename, ".tar"))
487 s.runner.Container.ContainerImage = arvadostest.DockerImage112PDH
488 s.executor.imageLoaded = false
489 s.executor.loaded = ""
490 s.executor.loadErr = errors.New("bork")
491 imageID, err = s.runner.LoadImage()
492 c.Check(err, ErrorMatches, ".*bork")
493 c.Check(s.executor.loaded, Matches, ".*"+regexp.QuoteMeta(arvadostest.DockerImage112Filename))
495 s.runner.Container.ContainerImage = fakeInputCollectionPDH
496 s.executor.imageLoaded = false
497 s.executor.loaded = ""
498 s.executor.loadErr = nil
499 imageID, err = s.runner.LoadImage()
500 c.Check(err, ErrorMatches, "image collection does not include a \\.tar image file")
501 c.Check(s.executor.loaded, Equals, "")
504 type ArvErrorTestClient struct{}
506 func (ArvErrorTestClient) Create(resourceType string,
507 parameters arvadosclient.Dict,
508 output interface{}) error {
512 func (ArvErrorTestClient) Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error {
513 if method == "GET" && resourceType == "containers" && action == "auth" {
516 return errors.New("ArvError")
519 func (ArvErrorTestClient) CallRaw(method, resourceType, uuid, action string,
520 parameters arvadosclient.Dict) (reader io.ReadCloser, err error) {
521 return nil, errors.New("ArvError")
524 func (ArvErrorTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
525 return errors.New("ArvError")
528 func (ArvErrorTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
532 func (ArvErrorTestClient) Discovery(key string) (interface{}, error) {
533 return discoveryMap[key], nil
536 type KeepErrorTestClient struct {
540 func (*KeepErrorTestClient) BlockWrite(context.Context, arvados.BlockWriteOptions) (arvados.BlockWriteResponse, error) {
541 return arvados.BlockWriteResponse{}, errors.New("KeepError")
544 func (*KeepErrorTestClient) LocalLocator(string) (string, error) {
545 return "", errors.New("KeepError")
548 type KeepReadErrorTestClient struct {
552 func (*KeepReadErrorTestClient) ReadAt(string, []byte, int) (int, error) {
553 return 0, errors.New("KeepError")
556 func dockerLog(fd byte, msg string) []byte {
558 header := make([]byte, 8+len(by))
560 header[7] = byte(len(by))
565 func (s *TestSuite) TestRunContainer(c *C) {
566 s.executor.runFunc = func() int {
567 fmt.Fprintf(s.executor.created.Stdout, "Hello world\n")
571 s.runner.Container.ContainerImage = arvadostest.DockerImage112PDH
572 s.runner.Container.Command = []string{"./hw"}
573 s.runner.Container.OutputStorageClasses = []string{"default"}
575 imageID, err := s.runner.LoadImage()
578 err = s.runner.CreateContainer(imageID, nil)
581 err = s.runner.StartContainer()
584 err = s.runner.WaitFinish()
587 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, `2\S+Z Hello world\n`)
588 c.Check(logFileContent(c, s.runner, "stderr.txt"), Matches, ``)
591 func (s *TestSuite) TestCommitLogs(c *C) {
592 api := &ArvTestClient{}
593 kc := &KeepTestClient{}
595 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
597 f, err := cr.openLogFile("crunch-run")
599 cr.CrunchLog = newLogWriter(newTestTimestamper(f))
601 cr.CrunchLog.Print("Hello world!")
602 cr.CrunchLog.Print("Goodbye")
603 cr.finalState = "Complete"
605 err = cr.CommitLogs()
608 c.Check(api.Calls, Equals, 1)
609 c.Check(api.Content[0]["ensure_unique_name"], Equals, true)
610 c.Check(api.Content[0]["collection"].(arvadosclient.Dict)["name"], Equals, "logs for zzzzz-zzzzz-zzzzzzzzzzzzzzz")
611 c.Check(api.Content[0]["collection"].(arvadosclient.Dict)["manifest_text"], Equals, ". 744b2e4553123b02fa7b452ec5c18993+123 0:123:crunch-run.txt\n")
612 c.Check(*cr.LogsPDH, Equals, "63da7bdacf08c40f604daad80c261e9a+60")
615 func (s *TestSuite) TestUpdateContainerRunning(c *C) {
616 api := &ArvTestClient{}
617 kc := &KeepTestClient{}
619 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
622 err = cr.UpdateContainerRunning("")
625 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Running")
628 func (s *TestSuite) TestUpdateContainerComplete(c *C) {
629 api := &ArvTestClient{}
630 kc := &KeepTestClient{}
632 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
635 cr.LogsPDH = new(string)
636 *cr.LogsPDH = "d3a229d2fe3690c2c3e75a71a153c6a3+60"
638 cr.ExitCode = new(int)
640 cr.finalState = "Complete"
642 err = cr.UpdateContainerFinal()
645 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], Equals, *cr.LogsPDH)
646 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], Equals, *cr.ExitCode)
647 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
650 func (s *TestSuite) TestUpdateContainerCancelled(c *C) {
651 api := &ArvTestClient{}
652 kc := &KeepTestClient{}
654 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
657 cr.finalState = "Cancelled"
659 err = cr.UpdateContainerFinal()
662 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["log"], IsNil)
663 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["exit_code"], IsNil)
664 c.Check(api.Content[0]["container"].(arvadosclient.Dict)["state"], Equals, "Cancelled")
667 // Used by the TestFullRun*() test below to DRY up boilerplate setup to do full
668 // dress rehearsal of the Run() function, starting from a JSON container record.
669 func (s *TestSuite) fullRunHelper(c *C, record string, extraMounts []string, fn func() int) (*ArvTestClient, *ContainerRunner, string) {
670 err := json.Unmarshal([]byte(record), &s.api.Container)
672 initialState := s.api.Container.State
675 SecretMounts map[string]arvados.Mount `json:"secret_mounts"`
677 err = json.Unmarshal([]byte(record), &sm)
679 secretMounts, err := json.Marshal(sm)
681 c.Logf("SecretMounts decoded %v json %q", sm, secretMounts)
683 s.executor.runFunc = fn
685 s.runner.statInterval = 100 * time.Millisecond
686 s.runner.containerWatchdogInterval = time.Second
688 realTemp := c.MkDir()
690 s.runner.MkTempDir = func(_, prefix string) (string, error) {
692 d := fmt.Sprintf("%s/%s%d", realTemp, prefix, tempcount)
693 err := os.Mkdir(d, os.ModePerm)
694 if err != nil && strings.Contains(err.Error(), ": file exists") {
695 // Test case must have pre-populated the tempdir
700 client, _ := apiStub()
701 s.runner.MkArvClient = func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error) {
702 return &ArvTestClient{secretMounts: secretMounts}, &s.testContainerKeepClient, client, nil
705 if extraMounts != nil && len(extraMounts) > 0 {
706 err := s.runner.SetupArvMountPoint("keep")
709 for _, m := range extraMounts {
710 os.MkdirAll(s.runner.ArvMountPoint+"/by_id/"+m, os.ModePerm)
715 if s.api.CalledWith("container.state", "Complete") != nil {
718 if s.executor.loadErr == nil && s.executor.createErr == nil && initialState != "Running" {
719 c.Check(s.api.WasSetRunning, Equals, true)
720 var lastupdate arvadosclient.Dict
721 for _, content := range s.api.Content {
722 if content["container"] != nil {
723 lastupdate = content["container"].(arvadosclient.Dict)
726 if lastupdate["log"] == nil {
727 c.Errorf("no container update with non-nil log -- updates were: %v", s.api.Content)
732 dumpAllLogFiles(c, s.runner)
735 return s.api, s.runner, realTemp
738 func (s *TestSuite) TestFullRunHello(c *C) {
739 s.runner.enableMemoryLimit = true
740 s.runner.networkMode = "default"
741 s.fullRunHelper(c, `{
742 "command": ["echo", "hello world"],
743 "container_image": "`+arvadostest.DockerImage112PDH+`",
745 "environment": {"foo":"bar","baz":"waz"},
746 "mounts": {"/tmp": {"kind": "tmp"} },
747 "output_path": "/tmp",
749 "runtime_constraints": {"vcpus":1,"ram":1000000},
751 "output_storage_classes": ["default"]
752 }`, nil, func() int {
753 c.Check(s.executor.created.Command, DeepEquals, []string{"echo", "hello world"})
754 c.Check(s.executor.created.Image, Equals, "sha256:d8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678")
755 c.Check(s.executor.created.Env, DeepEquals, map[string]string{"foo": "bar", "baz": "waz"})
756 c.Check(s.executor.created.VCPUs, Equals, 1)
757 c.Check(s.executor.created.RAM, Equals, int64(1000000))
758 c.Check(s.executor.created.NetworkMode, Equals, "default")
759 c.Check(s.executor.created.EnableNetwork, Equals, false)
760 c.Check(s.executor.created.GPUDeviceCount, Equals, 0)
761 fmt.Fprintln(s.executor.created.Stdout, "hello world")
765 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
766 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
767 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, `2\S+Z hello world\n`)
768 c.Check(s.testDispatcherKeepClient.StorageClasses, DeepEquals, []string{"default"})
769 c.Check(s.testContainerKeepClient.StorageClasses, DeepEquals, []string{"default"})
772 func (s *TestSuite) TestRunAlreadyRunning(c *C) {
774 s.fullRunHelper(c, `{
775 "command": ["sleep", "3"],
776 "container_image": "`+arvadostest.DockerImage112PDH+`",
779 "mounts": {"/tmp": {"kind": "tmp"} },
780 "output_path": "/tmp",
782 "runtime_constraints": {},
783 "scheduling_parameters":{"max_run_time": 1},
785 }`, nil, func() int {
789 c.Check(s.api.CalledWith("container.state", "Cancelled"), IsNil)
790 c.Check(s.api.CalledWith("container.state", "Complete"), IsNil)
791 c.Check(ran, Equals, false)
794 func ec2MetadataServerStub(c *C, token *string, failureRate float64, stoptime *atomic.Value) *httptest.Server {
796 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
797 if !failedOnce || rand.Float64() < failureRate {
798 w.WriteHeader(http.StatusServiceUnavailable)
803 case "/latest/api/token":
804 fmt.Fprintln(w, *token)
805 case "/latest/meta-data/spot/instance-action":
806 if r.Header.Get("X-aws-ec2-metadata-token") != *token {
807 w.WriteHeader(http.StatusUnauthorized)
808 } else if t, _ := stoptime.Load().(time.Time); t.IsZero() {
809 w.WriteHeader(http.StatusNotFound)
811 fmt.Fprintf(w, `{"action":"stop","time":"%s"}`, t.Format(time.RFC3339))
814 w.WriteHeader(http.StatusNotFound)
819 func (s *TestSuite) TestSpotInterruptionNotice(c *C) {
820 s.testSpotInterruptionNotice(c, 0.1)
823 func (s *TestSuite) TestSpotInterruptionNoticeNotAvailable(c *C) {
824 s.testSpotInterruptionNotice(c, 1)
827 func (s *TestSuite) testSpotInterruptionNotice(c *C, failureRate float64) {
828 var stoptime atomic.Value
829 token := "fake-ec2-metadata-token"
830 stub := ec2MetadataServerStub(c, &token, failureRate, &stoptime)
833 defer func(i time.Duration, u string) {
834 spotInterruptionCheckInterval = i
835 ec2MetadataBaseURL = u
836 }(spotInterruptionCheckInterval, ec2MetadataBaseURL)
837 spotInterruptionCheckInterval = time.Second / 8
838 ec2MetadataBaseURL = stub.URL
841 checkLogs := func() {
843 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Checking for spot instance interruptions every 125ms using instance metadata at http://.*`)
844 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Spot instance interruption check was inconclusive: 503 Service Unavailable -- will retry in 125ms.*`)
845 if failureRate == 1 {
846 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Spot instance interruption check was inconclusive: 503 Service Unavailable -- now giving up after too many consecutive errors.*`)
849 text := `Cloud provider scheduled instance stop at ` + stoptime.Load().(time.Time).Format(time.RFC3339)
850 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*`+text+`.*`)
851 c.Check(s.api.CalledWith("container.runtime_status.warning", "preemption notice"), NotNil)
852 c.Check(s.api.CalledWith("container.runtime_status.warningDetail", text), NotNil)
853 c.Check(s.api.CalledWith("container.runtime_status.preemptionNotice", text), NotNil)
855 // Check that the log collection was saved, and the
856 // container record updated with the new PDH,
857 // immediately after the preemption notice was
858 // received -- i.e., while the container is still
861 saved := make(map[string]string) // pdh => manifest_text
862 for _, call := range s.api.Content {
863 if ctr, ok := call["container"].(arvadosclient.Dict); ok {
864 if pdh, ok := ctr["log"].(string); ok {
868 if coll, ok := call["collection"].(arvadosclient.Dict); ok {
869 mt, _ := coll["manifest_text"].(string)
870 if strings.Contains(mt, ":crunch-run.txt") {
871 saved[arvados.PortableDataHash(mt)] = mt
875 logfs, err := (&arvados.Collection{ManifestText: saved[lastpdh]}).FileSystem(s.runner.dispatcherClient, s.runner.DispatcherKeepClient)
877 log, err := fs.ReadFile(arvados.FS(logfs), "crunch-run.txt")
879 c.Check(string(log), Matches, `(?ms).*\Q`+text+`\E.*`)
882 go s.runner.checkSpotInterruptionNotices()
883 s.fullRunHelper(c, `{
884 "command": ["sleep", "3"],
885 "container_image": "`+arvadostest.DockerImage112PDH+`",
888 "mounts": {"/tmp": {"kind": "tmp"} },
889 "output_path": "/tmp",
891 "runtime_constraints": {},
893 }`, nil, func() int {
894 time.Sleep(time.Second)
895 stoptime.Store(time.Now().Add(time.Minute).UTC())
896 token = "different-fake-ec2-metadata-token"
897 time.Sleep(time.Second)
901 c.Check(checkedLogs, Equals, true)
904 func (s *TestSuite) TestRunTimeExceeded(c *C) {
905 s.fullRunHelper(c, `{
906 "command": ["sleep", "3"],
907 "container_image": "`+arvadostest.DockerImage112PDH+`",
910 "mounts": {"/tmp": {"kind": "tmp"} },
911 "output_path": "/tmp",
913 "runtime_constraints": {},
914 "scheduling_parameters":{"max_run_time": 1},
916 }`, nil, func() int {
917 time.Sleep(3 * time.Second)
921 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
922 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, "(?ms).*maximum run time exceeded.*")
925 func (s *TestSuite) TestContainerWaitFails(c *C) {
926 s.fullRunHelper(c, `{
927 "command": ["sleep", "3"],
928 "container_image": "`+arvadostest.DockerImage112PDH+`",
930 "mounts": {"/tmp": {"kind": "tmp"} },
931 "output_path": "/tmp",
934 }`, nil, func() int {
935 s.executor.waitErr = errors.New("Container is not running")
939 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
940 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, "(?ms).*Container is not running.*")
943 func (s *TestSuite) TestCrunchstat(c *C) {
944 s.runner.crunchstatFakeFS = os.DirFS("../crunchstat/testdata/debian12")
945 s.fullRunHelper(c, `{
946 "command": ["sleep", "1"],
947 "container_image": "`+arvadostest.DockerImage112PDH+`",
950 "mounts": {"/tmp": {"kind": "tmp"} },
951 "output_path": "/tmp",
953 "runtime_constraints": {},
955 }`, nil, func() int {
956 time.Sleep(time.Second)
960 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
961 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
963 c.Check(logFileContent(c, s.runner, "crunchstat.txt"), Matches, `(?ms).*`+reTimestamp+` mem \d+ swap \d+ pgmajfault \d+ rss.*`)
964 c.Check(logFileContent(c, s.runner, "hoststat.txt"), Matches, `(?ms).*`+reTimestamp+` mem \d+ swap \d+ pgmajfault \d+ rss.*`)
966 // Check that we called (*crunchstat.Reporter)Stop().
967 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*`+reTimestamp+` Maximum crunch-run memory rss usage was \d+ bytes\n.*`)
970 func (s *TestSuite) TestNodeInfoLog(c *C) {
971 s.fullRunHelper(c, `{
973 "container_image": "`+arvadostest.DockerImage112PDH+`",
976 "mounts": {"/tmp": {"kind": "tmp"} },
977 "output_path": "/tmp",
979 "runtime_constraints": {},
981 }`, nil, func() int {
985 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
986 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
988 json := logFileContent(c, s.runner, "node.json")
989 c.Check(json, Matches, `(?ms).*"ProviderType": *"a1\.2xlarge".*`)
990 c.Check(json, Matches, `(?ms).*"Price": *1\.2.*`)
992 nodeinfo := logFileContent(c, s.runner, "node-info.txt")
993 c.Check(nodeinfo, Matches, `(?ms).*Host Information.*`)
994 c.Check(nodeinfo, Matches, `(?ms).*CPU Information.*`)
995 c.Check(nodeinfo, Matches, `(?ms).*Memory Information.*`)
996 c.Check(nodeinfo, Matches, `(?ms).*Disk Space.*`)
997 c.Check(nodeinfo, Matches, `(?ms).*Disk INodes.*`)
1000 func (s *TestSuite) TestLogVersionAndRuntime(c *C) {
1001 s.fullRunHelper(c, `{
1002 "command": ["sleep", "1"],
1003 "container_image": "`+arvadostest.DockerImage112PDH+`",
1006 "mounts": {"/tmp": {"kind": "tmp"} },
1007 "output_path": "/tmp",
1009 "runtime_constraints": {},
1011 }`, nil, func() int {
1015 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*crunch-run \S+ \(go\S+\) start.*`)
1016 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*crunch-run process has uid=\d+\(.+\) gid=\d+\(.+\) groups=\d+\(.+\)(,\d+\(.+\))*\n.*`)
1017 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Executing container: zzzzz-zzzzz-zzzzzzzzzzzzzzz.*`)
1018 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*Using container runtime: stub.*`)
1021 func (s *TestSuite) testLogRSSThresholds(c *C, ram int64, expected []int, notExpected int) {
1022 s.runner.crunchstatFakeFS = os.DirFS("../crunchstat/testdata/debian12")
1023 s.fullRunHelper(c, `{
1024 "command": ["true"],
1025 "container_image": "`+arvadostest.DockerImage112PDH+`",
1028 "mounts": {"/tmp": {"kind": "tmp"} },
1029 "output_path": "/tmp",
1031 "runtime_constraints": {"ram": `+fmt.Sprintf("%d", ram)+`},
1033 }`, nil, func() int { return 0 })
1034 logs := logFileContent(c, s.runner, "crunch-run.txt")
1035 c.Log("=== crunchstat logs")
1037 pattern := logLineStart + `Container using over %d%% of memory \(rss %d/%d bytes\)`
1039 for _, threshold = range expected {
1040 c.Check(logs, Matches, fmt.Sprintf(pattern, threshold, s.debian12MemoryCurrent, ram))
1042 if notExpected > threshold {
1043 c.Check(logs, Not(Matches), fmt.Sprintf(pattern, notExpected, s.debian12MemoryCurrent, ram))
1047 func (s *TestSuite) TestLogNoRSSThresholds(c *C) {
1048 s.testLogRSSThresholds(c, s.debian12MemoryCurrent*10, []int{}, 90)
1051 func (s *TestSuite) TestLogSomeRSSThresholds(c *C) {
1052 onePercentRSS := s.debian12MemoryCurrent / 100
1053 s.testLogRSSThresholds(c, 102*onePercentRSS, []int{90, 95}, 99)
1056 func (s *TestSuite) TestLogAllRSSThresholds(c *C) {
1057 s.testLogRSSThresholds(c, s.debian12MemoryCurrent, []int{90, 95, 99}, 0)
1060 func (s *TestSuite) TestLogMaximaAfterRun(c *C) {
1061 s.runner.crunchstatFakeFS = os.DirFS("../crunchstat/testdata/debian12")
1062 s.runner.parentTemp = c.MkDir()
1063 s.fullRunHelper(c, `{
1064 "command": ["true"],
1065 "container_image": "`+arvadostest.DockerImage112PDH+`",
1068 "mounts": {"/tmp": {"kind": "tmp"} },
1069 "output_path": "/tmp",
1071 "runtime_constraints": {"ram": `+fmt.Sprintf("%d", s.debian12MemoryCurrent*10)+`},
1073 }`, nil, func() int { return 0 })
1074 logs := logFileContent(c, s.runner, "crunch-run.txt")
1075 for _, expected := range []string{
1076 `Maximum disk usage was \d+%, \d+/\d+ bytes`,
1077 fmt.Sprintf(`Maximum container memory swap usage was %d bytes`, s.debian12SwapCurrent),
1078 `Maximum container memory pgmajfault usage was \d+ faults`,
1079 fmt.Sprintf(`Maximum container memory rss usage was 10%%, %d/%d bytes`, s.debian12MemoryCurrent, s.debian12MemoryCurrent*10),
1080 `Maximum crunch-run memory rss usage was \d+ bytes`,
1082 c.Check(logs, Matches, logLineStart+expected)
1086 func (s *TestSuite) TestCommitNodeInfoBeforeStart(c *C) {
1087 var collection_create, container_update arvadosclient.Dict
1088 s.fullRunHelper(c, `{
1089 "command": ["true"],
1090 "container_image": "`+arvadostest.DockerImage112PDH+`",
1093 "mounts": {"/tmp": {"kind": "tmp"} },
1094 "output_path": "/tmp",
1096 "runtime_constraints": {},
1098 "uuid": "zzzzz-dz642-202301121543210"
1099 }`, nil, func() int {
1100 collection_create = s.api.CalledWith("ensure_unique_name", true)
1101 container_update = s.api.CalledWith("container.state", "Running")
1105 c.Assert(collection_create, NotNil)
1106 log_collection := collection_create["collection"].(arvadosclient.Dict)
1107 c.Check(log_collection["name"], Equals, "logs for zzzzz-dz642-202301121543210")
1108 manifest_text := log_collection["manifest_text"].(string)
1109 // We check that the file size is at least two digits as an easy way to
1110 // check the file isn't empty.
1111 c.Check(manifest_text, Matches, `\. .+ \d+:\d{2,}:node-info\.txt( .+)?\n`)
1112 c.Check(manifest_text, Matches, `\. .+ \d+:\d{2,}:node\.json( .+)?\n`)
1114 c.Assert(container_update, NotNil)
1115 // As of Arvados 2.5.0, the container update must specify its log in PDH
1116 // format for the API server to propagate it to container requests, which
1117 // is what we care about for this test.
1118 expect_pdh := fmt.Sprintf("%x+%d", md5.Sum([]byte(manifest_text)), len(manifest_text))
1119 c.Check(container_update["container"].(arvadosclient.Dict)["log"], Equals, expect_pdh)
1122 func (s *TestSuite) TestContainerRecordLog(c *C) {
1123 s.fullRunHelper(c, `{
1124 "command": ["sleep", "1"],
1125 "container_image": "`+arvadostest.DockerImage112PDH+`",
1128 "mounts": {"/tmp": {"kind": "tmp"} },
1129 "output_path": "/tmp",
1131 "runtime_constraints": {},
1135 time.Sleep(time.Second)
1139 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1140 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1142 c.Check(logFileContent(c, s.runner, "container.json"), Matches, `(?ms).*container_image.*`)
1145 func (s *TestSuite) TestFullRunStderr(c *C) {
1146 s.fullRunHelper(c, `{
1147 "command": ["/bin/sh", "-c", "echo hello ; echo world 1>&2 ; exit 1"],
1148 "container_image": "`+arvadostest.DockerImage112PDH+`",
1151 "mounts": {"/tmp": {"kind": "tmp"} },
1152 "output_path": "/tmp",
1154 "runtime_constraints": {},
1156 }`, nil, func() int {
1157 fmt.Fprintln(s.executor.created.Stdout, "hello")
1158 fmt.Fprintln(s.executor.created.Stderr, "world")
1162 final := s.api.CalledWith("container.state", "Complete")
1163 c.Assert(final, NotNil)
1164 c.Check(final["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
1165 c.Check(final["container"].(arvadosclient.Dict)["log"], NotNil)
1167 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, ".*hello\n")
1168 c.Check(logFileContent(c, s.runner, "stderr.txt"), Matches, ".*world\n")
1171 func (s *TestSuite) TestFullRunDefaultCwd(c *C) {
1172 s.fullRunHelper(c, `{
1174 "container_image": "`+arvadostest.DockerImage112PDH+`",
1177 "mounts": {"/tmp": {"kind": "tmp"} },
1178 "output_path": "/tmp",
1180 "runtime_constraints": {},
1182 }`, nil, func() int {
1183 fmt.Fprintf(s.executor.created.Stdout, "workdir=%q", s.executor.created.WorkingDir)
1187 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1188 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1189 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, `.*workdir=""`)
1192 func (s *TestSuite) TestFullRunSetCwd(c *C) {
1193 s.fullRunHelper(c, `{
1195 "container_image": "`+arvadostest.DockerImage112PDH+`",
1198 "mounts": {"/tmp": {"kind": "tmp"} },
1199 "output_path": "/tmp",
1201 "runtime_constraints": {},
1203 }`, nil, func() int {
1204 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.WorkingDir)
1208 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1209 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1210 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, ".*/bin\n")
1213 func (s *TestSuite) TestFullRunSetOutputStorageClasses(c *C) {
1214 s.fullRunHelper(c, `{
1216 "container_image": "`+arvadostest.DockerImage112PDH+`",
1219 "mounts": {"/tmp": {"kind": "tmp"} },
1220 "output_path": "/tmp",
1222 "runtime_constraints": {},
1224 "output_storage_classes": ["foo", "bar"]
1225 }`, nil, func() int {
1226 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.WorkingDir)
1230 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1231 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1232 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, ".*/bin\n")
1233 c.Check(s.testDispatcherKeepClient.StorageClasses, DeepEquals, []string{"foo", "bar"})
1234 c.Check(s.testContainerKeepClient.StorageClasses, DeepEquals, []string{"foo", "bar"})
1237 func (s *TestSuite) TestEnableCUDADeviceCount(c *C) {
1238 s.fullRunHelper(c, `{
1240 "container_image": "`+arvadostest.DockerImage112PDH+`",
1243 "mounts": {"/tmp": {"kind": "tmp"} },
1244 "output_path": "/tmp",
1246 "runtime_constraints": {"gpu": {"device_count": 2, "stack": "cuda", "hardware_target": ["9.0"], "driver_version": "11.0", "vram": 8000000000}},
1248 "output_storage_classes": ["foo", "bar"]
1249 }`, nil, func() int {
1250 fmt.Fprintln(s.executor.created.Stdout, "ok")
1253 c.Check(s.executor.created.GPUDeviceCount, Equals, 2)
1254 c.Check(s.executor.created.GPUStack, Equals, "cuda")
1257 func (s *TestSuite) TestStopOnSignal(c *C) {
1258 s.executor.runFunc = func() int {
1259 s.executor.created.Stdout.Write([]byte("foo\n"))
1260 s.runner.SigChan <- syscall.SIGINT
1261 time.Sleep(10 * time.Second)
1264 s.testStopContainer(c)
1267 func (s *TestSuite) TestStopOnArvMountDeath(c *C) {
1268 s.executor.runFunc = func() int {
1269 s.executor.created.Stdout.Write([]byte("foo\n"))
1270 s.runner.ArvMountExit <- nil
1271 close(s.runner.ArvMountExit)
1272 time.Sleep(10 * time.Second)
1275 s.runner.ArvMountExit = make(chan error)
1276 s.testStopContainer(c)
1279 func (s *TestSuite) testStopContainer(c *C) {
1281 "command": ["/bin/sh", "-c", "echo foo && sleep 30 && echo bar"],
1282 "container_image": "` + arvadostest.DockerImage112PDH + `",
1285 "mounts": {"/tmp": {"kind": "tmp"} },
1286 "output_path": "/tmp",
1288 "runtime_constraints": {},
1292 err := json.Unmarshal([]byte(record), &s.api.Container)
1293 c.Assert(err, IsNil)
1295 s.runner.RunArvMount = func([]string, string) (*exec.Cmd, error) { return nil, nil }
1296 s.runner.MkArvClient = func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error) {
1297 return &ArvTestClient{}, &KeepTestClient{}, nil, nil
1300 done := make(chan error)
1302 done <- s.runner.Run()
1305 case <-time.After(20 * time.Second):
1306 pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
1307 c.Fatal("timed out")
1311 dumpAllLogFiles(c, s.runner)
1313 c.Check(s.api.CalledWith("container.log", nil), NotNil)
1314 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
1315 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, "(?ms).*foo\n$")
1318 func (s *TestSuite) TestFullRunSetEnv(c *C) {
1319 s.fullRunHelper(c, `{
1320 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1321 "container_image": "`+arvadostest.DockerImage112PDH+`",
1323 "environment": {"FROBIZ": "bilbo"},
1324 "mounts": {"/tmp": {"kind": "tmp"} },
1325 "output_path": "/tmp",
1327 "runtime_constraints": {},
1329 }`, nil, func() int {
1330 fmt.Fprintf(s.executor.created.Stdout, "%v", s.executor.created.Env)
1334 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1335 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1336 c.Check(logFileContent(c, s.runner, "stdout.txt"), Matches, `.*map\[FROBIZ:bilbo\]`)
1339 type ArvMountCmdLine struct {
1344 func (am *ArvMountCmdLine) ArvMountTest(c []string, token string) (*exec.Cmd, error) {
1350 func stubCert(c *C, temp string) string {
1351 path := temp + "/ca-certificates.crt"
1352 err := os.WriteFile(path, []byte{}, 0666)
1353 c.Assert(err, IsNil)
1354 os.Setenv("SSL_CERT_FILE", path)
1358 func (s *TestSuite) TestSetupMounts(c *C) {
1360 am := &ArvMountCmdLine{}
1361 cr.RunArvMount = am.ArvMountTest
1362 cr.containerClient, _ = apiStub()
1363 cr.ContainerArvClient = &ArvTestClient{}
1364 cr.ContainerKeepClient = &KeepTestClient{}
1365 cr.Container.OutputStorageClasses = []string{"default"}
1367 realTemp := c.MkDir()
1368 certTemp := c.MkDir()
1369 stubCertPath := stubCert(c, certTemp)
1370 cr.parentTemp = realTemp
1373 cr.MkTempDir = func(_ string, prefix string) (string, error) {
1375 d := fmt.Sprintf("%s/%s%d", realTemp, prefix, i)
1376 err := os.Mkdir(d, os.ModePerm)
1377 if err != nil && strings.Contains(err.Error(), ": file exists") {
1378 // Test case must have pre-populated the tempdir
1384 checkEmpty := func() {
1385 // Should be deleted.
1386 _, err := os.Stat(realTemp)
1387 c.Assert(os.IsNotExist(err), Equals, true)
1389 // Now recreate it for the next test.
1390 c.Assert(os.Mkdir(realTemp, 0777), IsNil)
1395 cr.ArvMountPoint = ""
1396 cr.Container.Mounts = make(map[string]arvados.Mount)
1397 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
1398 cr.Container.OutputPath = "/tmp"
1399 cr.statInterval = 5 * time.Second
1400 bindmounts, err := cr.SetupMounts()
1402 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1403 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5",
1404 "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1405 c.Check(bindmounts, DeepEquals, map[string]bindmount{"/tmp": {realTemp + "/tmp2", false}})
1406 os.RemoveAll(cr.ArvMountPoint)
1413 cr.ArvMountPoint = ""
1414 cr.Container.Mounts = make(map[string]arvados.Mount)
1415 cr.Container.Mounts["/out"] = arvados.Mount{Kind: "tmp"}
1416 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
1417 cr.Container.OutputPath = "/out"
1418 cr.Container.OutputStorageClasses = []string{"foo", "bar"}
1420 bindmounts, err := cr.SetupMounts()
1422 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1423 "--read-write", "--storage-classes", "foo,bar", "--crunchstat-interval=5",
1424 "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1425 c.Check(bindmounts, DeepEquals, map[string]bindmount{"/out": {realTemp + "/tmp2", false}, "/tmp": {realTemp + "/tmp3", false}})
1426 os.RemoveAll(cr.ArvMountPoint)
1433 cr.ArvMountPoint = ""
1434 cr.Container.Mounts = make(map[string]arvados.Mount)
1435 cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
1436 cr.Container.OutputPath = "/tmp"
1437 cr.Container.RuntimeConstraints.API = true
1438 cr.Container.OutputStorageClasses = []string{"default"}
1440 bindmounts, err := cr.SetupMounts()
1442 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1443 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5",
1444 "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1445 c.Check(bindmounts, DeepEquals, map[string]bindmount{"/tmp": {realTemp + "/tmp2", false}, "/etc/arvados/ca-certificates.crt": {stubCertPath, true}})
1446 os.RemoveAll(cr.ArvMountPoint)
1450 cr.Container.RuntimeConstraints.API = false
1455 cr.ArvMountPoint = ""
1456 cr.Container.Mounts = map[string]arvados.Mount{
1457 "/keeptmp": {Kind: "collection", Writable: true},
1459 cr.Container.OutputPath = "/keeptmp"
1461 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1463 bindmounts, err := cr.SetupMounts()
1465 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1466 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5",
1467 "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1468 c.Check(bindmounts, DeepEquals, map[string]bindmount{"/keeptmp": {realTemp + "/keep1/tmp0", false}})
1469 os.RemoveAll(cr.ArvMountPoint)
1476 cr.ArvMountPoint = ""
1477 cr.Container.Mounts = map[string]arvados.Mount{
1478 "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
1479 "/keepout": {Kind: "collection", Writable: true},
1481 cr.Container.OutputPath = "/keepout"
1483 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
1484 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1486 bindmounts, err := cr.SetupMounts()
1488 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1489 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5",
1490 "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1491 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1492 "/keepinp": {realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", true},
1493 "/keepout": {realTemp + "/keep1/tmp0", false},
1495 os.RemoveAll(cr.ArvMountPoint)
1502 cr.ArvMountPoint = ""
1503 cr.Container.RuntimeConstraints.KeepCacheRAM = 512
1504 cr.Container.Mounts = map[string]arvados.Mount{
1505 "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
1506 "/keepout": {Kind: "collection", Writable: true},
1508 cr.Container.OutputPath = "/keepout"
1510 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
1511 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1513 bindmounts, err := cr.SetupMounts()
1515 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1516 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5", "--ram-cache",
1517 "--file-cache", "512", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1518 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1519 "/keepinp": {realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", true},
1520 "/keepout": {realTemp + "/keep1/tmp0", false},
1522 os.RemoveAll(cr.ArvMountPoint)
1527 for _, test := range []struct {
1531 {in: "foo", out: `"foo"`},
1532 {in: nil, out: `null`},
1533 {in: map[string]int64{"foo": 123456789123456789}, out: `{"foo":123456789123456789}`},
1536 cr.ArvMountPoint = ""
1537 cr.Container.Mounts = map[string]arvados.Mount{
1538 "/mnt/test.json": {Kind: "json", Content: test.in},
1540 bindmounts, err := cr.SetupMounts()
1542 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1543 "/mnt/test.json": {realTemp + "/json2/mountdata.json", true},
1545 content, err := ioutil.ReadFile(realTemp + "/json2/mountdata.json")
1547 c.Check(content, DeepEquals, []byte(test.out))
1548 os.RemoveAll(cr.ArvMountPoint)
1553 for _, test := range []struct {
1557 {in: "foo", out: `foo`},
1558 {in: nil, out: "error"},
1559 {in: map[string]int64{"foo": 123456789123456789}, out: "error"},
1562 cr.ArvMountPoint = ""
1563 cr.Container.Mounts = map[string]arvados.Mount{
1564 "/mnt/test.txt": {Kind: "text", Content: test.in},
1566 bindmounts, err := cr.SetupMounts()
1567 if test.out == "error" {
1568 c.Check(err.Error(), Equals, "content for mount \"/mnt/test.txt\" must be a string")
1571 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1572 "/mnt/test.txt": {realTemp + "/text2/mountdata.text", true},
1574 content, err := ioutil.ReadFile(realTemp + "/text2/mountdata.text")
1576 c.Check(content, DeepEquals, []byte(test.out))
1578 os.RemoveAll(cr.ArvMountPoint)
1583 // Read-only mount points are allowed underneath output_dir mount point
1586 cr.ArvMountPoint = ""
1587 cr.Container.Mounts = make(map[string]arvados.Mount)
1588 cr.Container.Mounts = map[string]arvados.Mount{
1589 "/tmp": {Kind: "tmp"},
1590 "/tmp/foo": {Kind: "collection"},
1592 cr.Container.OutputPath = "/tmp"
1594 os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
1596 bindmounts, err := cr.SetupMounts()
1598 c.Check(am.Cmd, DeepEquals, []string{"arv-mount", "--foreground",
1599 "--read-write", "--storage-classes", "default", "--crunchstat-interval=5", "--ram-cache",
1600 "--file-cache", "512", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "--disable-event-listening", "--mount-by-id", "by_uuid", realTemp + "/keep1"})
1601 c.Check(bindmounts, DeepEquals, map[string]bindmount{
1602 "/tmp": {realTemp + "/tmp2", false},
1603 "/tmp/foo": {realTemp + "/keep1/tmp0", true},
1605 os.RemoveAll(cr.ArvMountPoint)
1610 // Writable mount points copied to output_dir mount point
1613 cr.ArvMountPoint = ""
1614 cr.Container.Mounts = make(map[string]arvados.Mount)
1615 cr.Container.Mounts = map[string]arvados.Mount{
1616 "/tmp": {Kind: "tmp"},
1617 "/tmp/foo": {Kind: "collection",
1618 PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53",
1620 "/tmp/bar": {Kind: "collection",
1621 PortableDataHash: "59389a8f9ee9d399be35462a0f92541d+53",
1625 cr.Container.OutputPath = "/tmp"
1627 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
1628 os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541d+53/baz", os.ModePerm)
1630 rf, _ := os.Create(realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541d+53/baz/quux")
1631 rf.Write([]byte("bar"))
1634 _, err := cr.SetupMounts()
1636 _, err = os.Stat(cr.HostOutputDir + "/foo")
1638 _, err = os.Stat(cr.HostOutputDir + "/bar/quux")
1640 os.RemoveAll(cr.ArvMountPoint)
1645 // Only mount points of kind 'collection' are allowed underneath output_dir mount point
1648 cr.ArvMountPoint = ""
1649 cr.Container.Mounts = make(map[string]arvados.Mount)
1650 cr.Container.Mounts = map[string]arvados.Mount{
1651 "/tmp": {Kind: "tmp"},
1652 "/tmp/foo": {Kind: "tmp"},
1654 cr.Container.OutputPath = "/tmp"
1656 _, err := cr.SetupMounts()
1657 c.Check(err, NotNil)
1658 c.Check(err, ErrorMatches, `only mount points of kind 'collection', 'text' or 'json' are supported underneath the output_path.*`)
1659 os.RemoveAll(cr.ArvMountPoint)
1664 // Only mount point of kind 'collection' is allowed for stdin
1667 cr.ArvMountPoint = ""
1668 cr.Container.Mounts = make(map[string]arvados.Mount)
1669 cr.Container.Mounts = map[string]arvados.Mount{
1670 "stdin": {Kind: "tmp"},
1673 _, err := cr.SetupMounts()
1674 c.Check(err, NotNil)
1675 c.Check(err, ErrorMatches, `unsupported mount kind 'tmp' for stdin.*`)
1676 os.RemoveAll(cr.ArvMountPoint)
1682 func (s *TestSuite) TestStdout(c *C) {
1684 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1685 "container_image": "` + arvadostest.DockerImage112PDH + `",
1687 "environment": {"FROBIZ": "bilbo"},
1688 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"} },
1689 "output_path": "/tmp",
1691 "runtime_constraints": {},
1695 s.fullRunHelper(c, helperRecord, nil, func() int {
1696 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
1700 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1701 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1702 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), NotNil)
1705 // Used by the TestStdoutWithWrongPath*()
1706 func (s *TestSuite) stdoutErrorRunHelper(c *C, record string, fn func() int) (*ArvTestClient, *ContainerRunner, error) {
1707 err := json.Unmarshal([]byte(record), &s.api.Container)
1708 c.Assert(err, IsNil)
1709 s.executor.runFunc = fn
1710 s.runner.RunArvMount = (&ArvMountCmdLine{}).ArvMountTest
1711 s.runner.MkArvClient = func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error) {
1712 return s.api, &KeepTestClient{}, nil, nil
1714 return s.api, s.runner, s.runner.Run()
1717 func (s *TestSuite) TestStdoutWithWrongPath(c *C) {
1718 _, _, err := s.stdoutErrorRunHelper(c, `{
1719 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path":"/tmpa.out"} },
1720 "output_path": "/tmp",
1722 }`, func() int { return 0 })
1723 c.Check(err, ErrorMatches, ".*Stdout path does not start with OutputPath.*")
1726 func (s *TestSuite) TestStdoutWithWrongKindTmp(c *C) {
1727 _, _, err := s.stdoutErrorRunHelper(c, `{
1728 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "tmp", "path":"/tmp/a.out"} },
1729 "output_path": "/tmp",
1731 }`, func() int { return 0 })
1732 c.Check(err, ErrorMatches, ".*unsupported mount kind 'tmp' for stdout.*")
1735 func (s *TestSuite) TestStdoutWithWrongKindCollection(c *C) {
1736 _, _, err := s.stdoutErrorRunHelper(c, `{
1737 "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "collection", "path":"/tmp/a.out"} },
1738 "output_path": "/tmp",
1740 }`, func() int { return 0 })
1741 c.Check(err, ErrorMatches, ".*unsupported mount kind 'collection' for stdout.*")
1744 func (s *TestSuite) TestFullRunWithAPI(c *C) {
1745 s.fullRunHelper(c, `{
1746 "command": ["/bin/sh", "-c", "true $ARVADOS_API_HOST"],
1747 "container_image": "`+arvadostest.DockerImage112PDH+`",
1750 "mounts": {"/tmp": {"kind": "tmp"} },
1751 "output_path": "/tmp",
1753 "runtime_constraints": {"API": true},
1755 }`, nil, func() int {
1756 c.Check(s.executor.created.Env["ARVADOS_API_HOST"], Equals, os.Getenv("ARVADOS_API_HOST"))
1759 c.Check(s.api.CalledWith("container.exit_code", 3), NotNil)
1760 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1761 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*`+reTimestamp+` Container exited with status code 3\n.*`)
1764 func (s *TestSuite) TestFullRunSetOutput(c *C) {
1765 defer os.Setenv("ARVADOS_API_HOST", os.Getenv("ARVADOS_API_HOST"))
1766 os.Setenv("ARVADOS_API_HOST", "test.arvados.org")
1767 s.fullRunHelper(c, `{
1768 "command": ["/bin/sh", "-c", "echo $ARVADOS_API_HOST"],
1769 "container_image": "`+arvadostest.DockerImage112PDH+`",
1772 "mounts": {"/tmp": {"kind": "tmp"} },
1773 "output_path": "/tmp",
1775 "runtime_constraints": {"API": true},
1777 }`, nil, func() int {
1778 s.api.Container.Output = arvadostest.DockerImage112PDH
1782 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1783 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1784 c.Check(s.api.CalledWith("container.output", arvadostest.DockerImage112PDH), NotNil)
1787 func (s *TestSuite) TestArvMountRuntimeStatusWarning(c *C) {
1788 s.runner.RunArvMount = func([]string, string) (*exec.Cmd, error) {
1789 os.Mkdir(s.runner.ArvMountPoint+"/by_id", 0666)
1790 ioutil.WriteFile(s.runner.ArvMountPoint+"/by_id/README", nil, 0666)
1791 return s.runner.ArvMountCmd([]string{"bash", "-c", "echo >&2 Test: Keep write error: I am a teapot; sleep 3"}, "")
1793 s.executor.runFunc = func() int {
1794 time.Sleep(time.Second)
1798 "command": ["sleep", "1"],
1799 "container_image": "` + arvadostest.DockerImage112PDH + `",
1802 "mounts": {"/tmp": {"kind": "tmp"} },
1803 "output_path": "/tmp",
1805 "runtime_constraints": {"API": true},
1808 err := json.Unmarshal([]byte(record), &s.api.Container)
1809 c.Assert(err, IsNil)
1810 err = s.runner.Run()
1811 c.Assert(err, IsNil)
1812 c.Check(s.api.CalledWith("container.exit_code", 137), NotNil)
1813 c.Check(s.api.CalledWith("container.runtime_status.warning", "arv-mount: Keep write error"), NotNil)
1814 c.Check(s.api.CalledWith("container.runtime_status.warningDetail", "Test: Keep write error: I am a teapot"), NotNil)
1815 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1816 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, `(?ms).*`+reTimestamp+` Container exited with status code 137 \(signal 9, SIGKILL\).*`)
1817 c.Check(logFileContent(c, s.runner, "arv-mount.txt"), Matches, reTimestamp+` Test: Keep write error: I am a teapot\n`)
1820 func (s *TestSuite) TestStdoutWithExcludeFromOutputMountPointUnderOutputDir(c *C) {
1822 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1823 "container_image": "` + arvadostest.DockerImage112PDH + `",
1825 "environment": {"FROBIZ": "bilbo"},
1827 "/tmp": {"kind": "tmp"},
1828 "/tmp/foo": {"kind": "collection",
1829 "portable_data_hash": "a3e8f74c6f101eae01fa08bfb4e49b3a+54",
1830 "exclude_from_output": true
1832 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1834 "output_path": "/tmp",
1836 "runtime_constraints": {},
1840 extraMounts := []string{"a3e8f74c6f101eae01fa08bfb4e49b3a+54"}
1842 s.fullRunHelper(c, helperRecord, extraMounts, func() int {
1843 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
1847 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1848 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1849 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), NotNil)
1852 func (s *TestSuite) TestStdoutWithMultipleMountPointsUnderOutputDir(c *C) {
1854 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1855 "container_image": "` + arvadostest.DockerImage112PDH + `",
1857 "environment": {"FROBIZ": "bilbo"},
1859 "/tmp": {"kind": "tmp"},
1860 "/tmp/foo/bar": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/file2_in_main.txt"},
1861 "/tmp/foo/sub1": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/subdir1"},
1862 "/tmp/foo/sub1file2": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/subdir1/file2_in_subdir1.txt"},
1863 "/tmp/foo/baz/sub2file2": {"kind": "collection", "portable_data_hash": "a0def87f80dd594d4675809e83bd4f15+367", "path":"/subdir1/subdir2/file2_in_subdir2.txt"},
1864 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1866 "output_path": "/tmp",
1868 "runtime_constraints": {},
1870 "uuid": "zzzzz-dz642-202301130848001"
1873 extraMounts := []string{
1874 "a0def87f80dd594d4675809e83bd4f15+367/file2_in_main.txt",
1875 "a0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt",
1876 "a0def87f80dd594d4675809e83bd4f15+367/subdir1/subdir2/file2_in_subdir2.txt",
1879 api, _, realtemp := s.fullRunHelper(c, helperRecord, extraMounts, func() int {
1880 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
1884 c.Check(s.executor.created.BindMounts, DeepEquals, map[string]bindmount{
1885 "/tmp": {realtemp + "/tmp1", false},
1886 "/tmp/foo/bar": {s.keepmount + "/by_id/a0def87f80dd594d4675809e83bd4f15+367/file2_in_main.txt", true},
1887 "/tmp/foo/baz/sub2file2": {s.keepmount + "/by_id/a0def87f80dd594d4675809e83bd4f15+367/subdir1/subdir2/file2_in_subdir2.txt", true},
1888 "/tmp/foo/sub1": {s.keepmount + "/by_id/a0def87f80dd594d4675809e83bd4f15+367/subdir1", true},
1889 "/tmp/foo/sub1file2": {s.keepmount + "/by_id/a0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt", true},
1892 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
1893 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
1894 output_count := uint(0)
1895 for _, v := range s.runner.ContainerArvClient.(*ArvTestClient).Content {
1896 if v["collection"] == nil {
1899 collection := v["collection"].(arvadosclient.Dict)
1900 if collection["name"].(string) != "output for zzzzz-dz642-202301130848001" {
1903 c.Check(v["ensure_unique_name"], Equals, true)
1904 c.Check(collection["manifest_text"].(string), Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
1905 ./foo 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0abcdefgh11234567890@569fa8c3 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 9:18:bar 36:18:sub1file2
1906 ./foo/baz 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0bcdefghijk544332211@569fa8c5 9:18:sub2file2
1907 ./foo/sub1 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396cabcdefghij6419876543234@569fa8c4 0:9:file1_in_subdir1.txt 9:18:file2_in_subdir1.txt
1908 ./foo/sub1/subdir2 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0bcdefghijk544332211@569fa8c5 0:9:file1_in_subdir2.txt 9:18:file2_in_subdir2.txt
1912 c.Check(output_count, Not(Equals), uint(0))
1915 func (s *TestSuite) TestStdoutWithMountPointsUnderOutputDirDenormalizedManifest(c *C) {
1917 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1918 "container_image": "` + arvadostest.DockerImage112PDH + `",
1920 "environment": {"FROBIZ": "bilbo"},
1922 "/tmp": {"kind": "tmp"},
1923 "/tmp/foo/bar": {"kind": "collection", "portable_data_hash": "b0def87f80dd594d4675809e83bd4f15+367", "path": "/subdir1/file2_in_subdir1.txt"},
1924 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1926 "output_path": "/tmp",
1928 "runtime_constraints": {},
1930 "uuid": "zzzzz-dz642-202301130848002"
1933 extraMounts := []string{
1934 "b0def87f80dd594d4675809e83bd4f15+367/subdir1/file2_in_subdir1.txt",
1937 s.fullRunHelper(c, helperRecord, extraMounts, func() int {
1938 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
1942 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
1943 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
1944 output_count := uint(0)
1945 for _, v := range s.runner.ContainerArvClient.(*ArvTestClient).Content {
1946 if v["collection"] == nil {
1949 collection := v["collection"].(arvadosclient.Dict)
1950 if collection["name"].(string) != "output for zzzzz-dz642-202301130848002" {
1953 c.Check(collection["manifest_text"].(string), Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
1954 ./foo 3e426d509afffb85e06c4c96a7c15e91+27+Aa124ac75e5168396c73c0abcdefgh11234567890@569fa8c3 10:17:bar
1958 c.Check(output_count, Not(Equals), uint(0))
1961 func (s *TestSuite) TestOutputError(c *C) {
1963 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1964 "container_image": "` + arvadostest.DockerImage112PDH + `",
1966 "environment": {"FROBIZ": "bilbo"},
1968 "/tmp": {"kind": "tmp"}
1970 "output_path": "/tmp",
1972 "runtime_constraints": {},
1975 s.fullRunHelper(c, helperRecord, nil, func() int {
1976 os.Symlink("/etc/hosts", s.runner.HostOutputDir+"/baz")
1980 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
1983 func (s *TestSuite) TestStdinCollectionMountPoint(c *C) {
1985 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
1986 "container_image": "` + arvadostest.DockerImage112PDH + `",
1988 "environment": {"FROBIZ": "bilbo"},
1990 "/tmp": {"kind": "tmp"},
1991 "stdin": {"kind": "collection", "portable_data_hash": "b0def87f80dd594d4675809e83bd4f15+367", "path": "/file1_in_main.txt"},
1992 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
1994 "output_path": "/tmp",
1996 "runtime_constraints": {},
2000 extraMounts := []string{
2001 "b0def87f80dd594d4675809e83bd4f15+367/file1_in_main.txt",
2004 api, _, _ := s.fullRunHelper(c, helperRecord, extraMounts, func() int {
2005 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
2009 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
2010 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
2011 for _, v := range api.Content {
2012 if v["collection"] != nil {
2013 collection := v["collection"].(arvadosclient.Dict)
2014 if strings.Index(collection["name"].(string), "output") == 0 {
2015 manifest := collection["manifest_text"].(string)
2016 c.Check(manifest, Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
2023 func (s *TestSuite) TestStdinJsonMountPoint(c *C) {
2025 "command": ["/bin/sh", "-c", "echo $FROBIZ"],
2026 "container_image": "` + arvadostest.DockerImage112PDH + `",
2028 "environment": {"FROBIZ": "bilbo"},
2030 "/tmp": {"kind": "tmp"},
2031 "stdin": {"kind": "json", "content": "foo"},
2032 "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"}
2034 "output_path": "/tmp",
2036 "runtime_constraints": {},
2040 api, _, _ := s.fullRunHelper(c, helperRecord, nil, func() int {
2041 fmt.Fprintln(s.executor.created.Stdout, s.executor.created.Env["FROBIZ"])
2045 c.Check(api.CalledWith("container.exit_code", 0), NotNil)
2046 c.Check(api.CalledWith("container.state", "Complete"), NotNil)
2047 for _, v := range api.Content {
2048 if v["collection"] != nil {
2049 collection := v["collection"].(arvadosclient.Dict)
2050 if strings.Index(collection["name"].(string), "output") == 0 {
2051 manifest := collection["manifest_text"].(string)
2052 c.Check(manifest, Equals, `./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out
2059 func (s *TestSuite) TestStderrMount(c *C) {
2060 api, cr, _ := s.fullRunHelper(c, `{
2061 "command": ["/bin/sh", "-c", "echo hello;exit 1"],
2062 "container_image": "`+arvadostest.DockerImage112PDH+`",
2065 "mounts": {"/tmp": {"kind": "tmp"},
2066 "stdout": {"kind": "file", "path": "/tmp/a/out.txt"},
2067 "stderr": {"kind": "file", "path": "/tmp/b/err.txt"}},
2068 "output_path": "/tmp",
2070 "runtime_constraints": {},
2072 }`, nil, func() int {
2073 fmt.Fprintln(s.executor.created.Stdout, "hello")
2074 fmt.Fprintln(s.executor.created.Stderr, "oops")
2078 final := api.CalledWith("container.state", "Complete")
2079 c.Assert(final, NotNil)
2080 c.Check(final["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
2081 c.Check(final["container"].(arvadosclient.Dict)["log"], NotNil)
2083 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)
2086 func (s *TestSuite) TestNumberRoundTrip(c *C) {
2087 s.api.callraw = true
2088 err := s.runner.fetchContainerRecord()
2089 c.Assert(err, IsNil)
2090 jsondata, err := json.Marshal(s.runner.Container.Mounts["/json"].Content)
2091 c.Logf("%#v", s.runner.Container)
2093 c.Check(string(jsondata), Equals, `{"number":123456789123456789}`)
2096 func (s *TestSuite) TestFullBrokenDocker(c *C) {
2098 for _, setup := range []func(){
2100 c.Log("// waitErr = ocl runtime error")
2101 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\\\"\""`)
2102 nextState = "Cancelled"
2105 c.Log("// loadErr = cannot connect")
2106 s.executor.loadErr = errors.New("Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?")
2107 s.runner.brokenNodeHook = c.MkDir() + "/broken-node-hook"
2108 err := ioutil.WriteFile(s.runner.brokenNodeHook, []byte("#!/bin/sh\nexec echo killme\n"), 0700)
2109 c.Assert(err, IsNil)
2110 nextState = "Queued"
2115 s.fullRunHelper(c, `{
2116 "command": ["echo", "hello world"],
2117 "container_image": "`+arvadostest.DockerImage112PDH+`",
2120 "mounts": {"/tmp": {"kind": "tmp"} },
2121 "output_path": "/tmp",
2123 "runtime_constraints": {},
2125 }`, nil, func() int { return 0 })
2126 c.Check(s.api.CalledWith("container.state", nextState), NotNil)
2127 logs := logFileContent(c, s.runner, "crunch-run.txt")
2128 c.Check(logs, Matches, "(?ms).*unable to run containers.*")
2129 if s.runner.brokenNodeHook != "" {
2130 c.Check(logs, Matches, "(?ms).*Running broken node hook.*")
2131 c.Check(logs, Matches, "(?ms).*killme.*")
2132 c.Check(logs, Not(Matches), "(?ms).*Writing /var/lock/crunch-run-broken to mark node as broken.*")
2134 c.Check(logs, Matches, "(?ms).*Writing /var/lock/crunch-run-broken to mark node as broken.*")
2139 func (s *TestSuite) TestBadCommand(c *C) {
2140 for _, startError := range []string{
2141 `panic: standard_init_linux.go:175: exec user process caused "no such file or directory"`,
2142 `Error response from daemon: Cannot start container 41f26cbc43bcc1280f4323efb1830a394ba8660c9d1c2b564ba42bf7f7694845: [8] System error: no such file or directory`,
2143 `Error response from daemon: Cannot start container 58099cd76c834f3dc2a4fb76c8028f049ae6d4fdf0ec373e1f2cfea030670c2d: [8] System error: exec: "foobar": executable file not found in $PATH`,
2146 s.executor.startErr = errors.New(startError)
2147 s.fullRunHelper(c, `{
2148 "command": ["echo", "hello world"],
2149 "container_image": "`+arvadostest.DockerImage112PDH+`",
2152 "mounts": {"/tmp": {"kind": "tmp"} },
2153 "output_path": "/tmp",
2155 "runtime_constraints": {},
2157 }`, nil, func() int { return 0 })
2158 c.Check(s.api.CalledWith("container.state", "Cancelled"), NotNil)
2159 c.Check(logFileContent(c, s.runner, "crunch-run.txt"), Matches, "(?ms).*Possible causes:.*is missing.*")
2163 func (s *TestSuite) TestSecretTextMountPoint(c *C) {
2165 "command": ["true"],
2166 "container_image": "` + arvadostest.DockerImage112PDH + `",
2169 "/tmp": {"kind": "tmp"},
2170 "/tmp/secret.conf": {"kind": "text", "content": "mypassword"}
2174 "output_path": "/tmp",
2176 "runtime_constraints": {},
2180 s.fullRunHelper(c, helperRecord, nil, func() int {
2181 content, err := ioutil.ReadFile(s.runner.HostOutputDir + "/secret.conf")
2183 c.Check(string(content), Equals, "mypassword")
2187 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
2188 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
2189 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ". 34819d7beeabb9260a5c854bc85b3e44+10 0:10:secret.conf\n"), NotNil)
2190 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ""), IsNil)
2192 // under secret mounts, not captured in output
2194 "command": ["true"],
2195 "container_image": "` + arvadostest.DockerImage112PDH + `",
2198 "/tmp": {"kind": "tmp"}
2201 "/tmp/secret.conf": {"kind": "text", "content": "mypassword"}
2203 "output_path": "/tmp",
2205 "runtime_constraints": {},
2210 s.fullRunHelper(c, helperRecord, nil, func() int {
2211 content, err := ioutil.ReadFile(s.runner.HostOutputDir + "/secret.conf")
2213 c.Check(string(content), Equals, "mypassword")
2217 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
2218 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
2219 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ". 34819d7beeabb9260a5c854bc85b3e44+10 0:10:secret.conf\n"), IsNil)
2220 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ""), NotNil)
2222 // under secret mounts, output dir is a collection, not captured in output
2224 "command": ["true"],
2225 "container_image": "` + arvadostest.DockerImage112PDH + `",
2228 "/tmp": {"kind": "collection", "writable": true}
2231 "/tmp/secret.conf": {"kind": "text", "content": "mypassword"}
2233 "output_path": "/tmp",
2235 "runtime_constraints": {},
2240 _, _, realtemp := s.fullRunHelper(c, helperRecord, nil, func() int {
2241 // secret.conf should be provisioned as a separate
2242 // bind mount, i.e., it should not appear in the
2243 // (fake) fuse filesystem as viewed from the host.
2244 content, err := ioutil.ReadFile(s.runner.HostOutputDir + "/secret.conf")
2245 if !c.Check(errors.Is(err, os.ErrNotExist), Equals, true) {
2246 c.Logf("secret.conf: content %q, err %#v", content, err)
2248 err = ioutil.WriteFile(s.runner.HostOutputDir+"/.arvados#collection", []byte(`{"manifest_text":". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo.txt\n"}`), 0700)
2253 content, err := ioutil.ReadFile(realtemp + "/text1/mountdata.text")
2255 c.Check(string(content), Equals, "mypassword")
2256 c.Check(s.executor.created.BindMounts["/tmp/secret.conf"], DeepEquals, bindmount{realtemp + "/text1/mountdata.text", true})
2257 c.Check(s.api.CalledWith("container.exit_code", 0), NotNil)
2258 c.Check(s.api.CalledWith("container.state", "Complete"), NotNil)
2259 c.Check(s.runner.ContainerArvClient.(*ArvTestClient).CalledWith("collection.manifest_text", ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo.txt\n"), NotNil)
2262 func (s *TestSuite) TestCalculateCost(c *C) {
2263 defer func(s string) { lockdir = s }(lockdir)
2267 cr.costStartTime = now.Add(-time.Hour)
2268 var logbuf bytes.Buffer
2269 cr.CrunchLog = newLogWriter(&logbuf)
2271 // if there's no InstanceType env var, cost is calculated as 0
2272 os.Unsetenv("InstanceType")
2273 cost := cr.calculateCost(now)
2274 c.Check(cost, Equals, 0.0)
2276 // with InstanceType env var and loadPrices() hasn't run (or
2277 // hasn't found any data), cost is calculated based on
2278 // InstanceType env var
2279 os.Setenv("InstanceType", `{"Price":1.2}`)
2280 cost = cr.calculateCost(now)
2281 c.Check(cost, Equals, 1.2)
2283 // first update tells us the spot price was $1/h until 30
2284 // minutes ago when it increased to $2/h
2285 j, err := json.Marshal([]cloud.InstancePrice{
2286 {StartTime: now.Add(-4 * time.Hour), Price: 1.0},
2287 {StartTime: now.Add(-time.Hour / 2), Price: 2.0},
2289 c.Assert(err, IsNil)
2290 os.WriteFile(lockdir+"/"+pricesfile, j, 0777)
2292 cost = cr.calculateCost(now)
2293 c.Check(cost, Equals, 1.5)
2295 // next update (via --list + SIGUSR2) tells us the spot price
2296 // increased to $3/h 15 minutes ago
2297 j, err = json.Marshal([]cloud.InstancePrice{
2298 {StartTime: now.Add(-time.Hour / 3), Price: 2.0}, // dup of -time.Hour/2 price
2299 {StartTime: now.Add(-time.Hour / 4), Price: 3.0},
2301 c.Assert(err, IsNil)
2302 os.WriteFile(lockdir+"/"+pricesfile, j, 0777)
2304 cost = cr.calculateCost(now)
2305 c.Check(cost, Equals, 1.0/2+2.0/4+3.0/4)
2307 cost = cr.calculateCost(now.Add(-time.Hour / 2))
2308 c.Check(cost, Equals, 0.5)
2310 c.Logf("%s", logbuf.String())
2311 c.Check(logbuf.String(), Matches, `(?ms).*Instance price changed to 1\.00 at 20.* changed to 2\.00 .* changed to 3\.00 .*`)
2312 c.Check(logbuf.String(), Not(Matches), `(?ms).*changed to 2\.00 .* changed to 2\.00 .*`)
2315 func (s *TestSuite) TestSIGUSR2CostUpdate(c *C) {
2318 pricesJSON, err := json.Marshal([]cloud.InstancePrice{
2319 {StartTime: now.Add(-4 * time.Hour), Price: 2.4},
2320 {StartTime: now.Add(-2 * time.Hour), Price: 2.6},
2322 c.Assert(err, IsNil)
2324 os.Setenv("InstanceType", `{"Price":2.2}`)
2325 defer func(s string) { lockdir = s }(lockdir)
2328 // We can't use s.api.CalledWith because timing differences will yield
2329 // different cost values across runs. getCostUpdate iterates over API
2330 // calls until it finds one that sets the cost, then writes that value
2331 // to the next index of costUpdates.
2332 deadline := now.Add(time.Second)
2333 costUpdates := make([]float64, 2)
2336 getCostUpdate := func() {
2337 for ; time.Now().Before(deadline); time.Sleep(time.Second / 10) {
2338 for apiIndex < len(s.api.Content) {
2339 update := s.api.Content[apiIndex]
2343 if update, ok = update["container"].(arvadosclient.Dict); !ok {
2346 if cost, ok = update["cost"].(float64); !ok {
2349 c.Logf("API call #%d updates cost to %v", apiIndex-1, cost)
2350 costUpdates[costIndex] = cost
2357 s.fullRunHelper(c, `{
2358 "command": ["true"],
2359 "container_image": "`+arvadostest.DockerImage112PDH+`",
2362 "mounts": {"/tmp": {"kind": "tmp"} },
2363 "output_path": "/tmp",
2365 "runtime_constraints": {},
2367 "uuid": "zzzzz-dz642-20230320101530a"
2368 }`, nil, func() int {
2369 s.runner.costStartTime = now.Add(-3 * time.Hour)
2370 err := syscall.Kill(pid, syscall.SIGUSR2)
2371 c.Check(err, IsNil, Commentf("error sending first SIGUSR2 to runner"))
2374 err = os.WriteFile(path.Join(lockdir, pricesfile), pricesJSON, 0o700)
2375 c.Check(err, IsNil, Commentf("error writing JSON prices file"))
2376 err = syscall.Kill(pid, syscall.SIGUSR2)
2377 c.Check(err, IsNil, Commentf("error sending second SIGUSR2 to runner"))
2382 // Comparing with format strings makes it easy to ignore minor variations
2383 // in cost across runs while keeping diagnostics pretty.
2384 c.Check(fmt.Sprintf("%.3f", costUpdates[0]), Equals, "6.600")
2385 c.Check(fmt.Sprintf("%.3f", costUpdates[1]), Equals, "7.600")
2388 type FakeProcess struct {
2392 func (fp FakeProcess) CmdlineSlice() ([]string, error) {
2393 return fp.cmdLine, nil
2396 func logFileContent(c *C, cr *ContainerRunner, fnm string) string {
2397 buf, err := fs.ReadFile(arvados.FS(cr.LogCollection), fnm)
2398 c.Assert(err, IsNil)
2402 func dumpAllLogFiles(c *C, cr *ContainerRunner) {
2403 d, err := cr.LogCollection.OpenFile("/", os.O_RDONLY, 0)
2404 c.Assert(err, IsNil)
2405 fis, err := d.Readdir(-1)
2406 c.Assert(err, IsNil)
2407 for _, fi := range fis {
2408 c.Logf("=== %s", fi.Name())
2409 c.Log(logFileContent(c, cr, fi.Name()))