1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
25 "git.arvados.org/arvados.git/lib/cmd"
26 "git.arvados.org/arvados.git/lib/config"
27 "git.arvados.org/arvados.git/sdk/go/arvados"
28 "git.arvados.org/arvados.git/sdk/go/ctxlog"
29 "git.arvados.org/arvados.git/sdk/go/health"
30 "github.com/sirupsen/logrus"
35 func (Command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
37 f := flag.NewFlagSet(prog, flag.ContinueOnError)
38 f.StringVar(&diag.projectName, "project-name", "scratch area for diagnostics", "`name` of project to find/create in home project and use for temporary/test objects")
39 f.StringVar(&diag.logLevel, "log-level", "info", "logging `level` (debug, info, warning, error)")
40 f.StringVar(&diag.dockerImage, "docker-image", "", "`image` (tag or portable data hash) to use when running a test container, or \"hello-world\" to use embedded hello-world image (default: build a custom image containing this executable, and run diagnostics inside the container too)")
41 f.StringVar(&diag.dockerImageFrom, "docker-image-from", "debian:stable-slim", "`base` image to use when building a custom image (use a debian-based image similar this host's OS for best results)")
42 f.BoolVar(&diag.checkInternal, "internal-client", false, "check that this host is considered an \"internal\" client")
43 f.BoolVar(&diag.checkExternal, "external-client", false, "check that this host is considered an \"external\" client")
44 f.BoolVar(&diag.verbose, "v", false, "verbose: include more information in report")
45 f.IntVar(&diag.priority, "priority", 500, "priority for test container (1..1000, or 0 to skip)")
46 f.DurationVar(&diag.timeout, "timeout", 10*time.Second, "timeout for http requests")
47 if ok, code := cmd.ParseFlags(f, prog, args, "", stderr); !ok {
52 diag.logger = ctxlog.New(stdout, "text", diag.logLevel)
53 diag.logger.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true, DisableLevelTruncation: true, PadLevelText: true})
55 if len(diag.errors) == 0 {
56 diag.logger.Info("--- no errors ---")
59 if diag.logger.Level > logrus.ErrorLevel {
60 fmt.Fprint(stdout, "\n--- cut here --- error summary ---\n\n")
61 for _, e := range diag.errors {
69 // docker save hello-world > hello-world.tar
71 //go:embed hello-world.tar
72 var HelloWorldDockerImage []byte
74 type diagnoser struct {
81 dockerImageFrom string
91 func (diag *diagnoser) debugf(f string, args ...interface{}) {
92 diag.logger.Debugf(" ... "+f, args...)
95 func (diag *diagnoser) infof(f string, args ...interface{}) {
96 diag.logger.Infof(" ... "+f, args...)
99 func (diag *diagnoser) verbosef(f string, args ...interface{}) {
101 diag.logger.Infof(" ... "+f, args...)
105 func (diag *diagnoser) warnf(f string, args ...interface{}) {
106 diag.logger.Warnf(" ... "+f, args...)
109 func (diag *diagnoser) errorf(f string, args ...interface{}) {
110 diag.logger.Errorf(f, args...)
111 diag.errors = append(diag.errors, fmt.Sprintf(f, args...))
114 // Run the given func, logging appropriate messages before and after,
115 // adding timing info, etc.
117 // The id argument should be unique among tests, and shouldn't change
118 // when other tests are added/removed.
119 func (diag *diagnoser) dotest(id int, title string, fn func() error) {
120 if diag.done == nil {
121 diag.done = map[int]bool{}
122 } else if diag.done[id] {
123 diag.errorf("(bug) reused test id %d", id)
127 diag.logger.Infof("%4d: %s", id, title)
130 elapsed := fmt.Sprintf("%d ms", time.Now().Sub(t0)/time.Millisecond)
132 diag.errorf("%4d: %s (%s): %s", id, title, elapsed, err)
134 diag.logger.Debugf("%4d: %s (%s): ok", id, title, elapsed)
138 func (diag *diagnoser) runtests() {
139 client := arvados.NewClientFromEnv()
140 // Disable auto-retry, use context instead
143 if client.APIHost == "" || client.AuthToken == "" {
144 diag.errorf("ARVADOS_API_HOST and ARVADOS_API_TOKEN environment variables are not set -- aborting without running any tests")
148 hostname, err := os.Hostname()
150 diag.warnf("error getting hostname: %s")
152 diag.verbosef("hostname = %s", hostname)
155 diag.dotest(5, "running health check (same as `arvados-server check`)", func() error {
156 ldr := config.NewLoader(&bytes.Buffer{}, ctxlog.New(&bytes.Buffer{}, "text", "info"))
157 ldr.SetupFlags(flag.NewFlagSet("diagnostics", flag.ContinueOnError))
158 cfg, err := ldr.Load()
160 diag.infof("skipping because config could not be loaded: %s", err)
163 cluster, err := cfg.GetCluster("")
167 if cluster.SystemRootToken != os.Getenv("ARVADOS_API_TOKEN") {
168 return fmt.Errorf("diagnostics usage error: %s is readable but SystemRootToken does not match $ARVADOS_API_TOKEN (to fix, either run 'arvados-client sudo diagnostics' to load everything from config file, or set ARVADOS_CONFIG=- to load nothing from config file)", ldr.Path)
170 agg := &health.Aggregator{Cluster: cluster}
171 resp := agg.ClusterHealth()
172 for _, e := range resp.Errors {
173 diag.errorf("health check: %s", e)
175 if len(resp.Errors) > 0 {
176 diag.infof("consider running `arvados-server check -yaml` for a comprehensive report")
178 diag.verbosef("reported clock skew = %v", resp.ClockSkew)
179 reported := map[string]bool{}
180 for _, result := range resp.Checks {
181 version := strings.SplitN(result.Metrics.Version, " (go", 2)[0]
182 if version != "" && !reported[version] {
183 diag.verbosef("arvados version = %s", version)
184 reported[version] = true
187 reported = map[string]bool{}
188 for _, result := range resp.Checks {
189 if result.Server != "" && !reported[result.Server] {
190 diag.verbosef("http frontend version = %s", result.Server)
191 reported[result.Server] = true
194 reported = map[string]bool{}
195 for _, result := range resp.Checks {
196 if sha := result.ConfigSourceSHA256; sha != "" && !reported[sha] {
197 diag.verbosef("config file sha256 = %s", sha)
204 var dd arvados.DiscoveryDocument
205 ddpath := "discovery/v1/apis/arvados/v1/rest"
206 diag.dotest(10, fmt.Sprintf("getting discovery document from https://%s/%s", client.APIHost, ddpath), func() error {
207 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
209 err := client.RequestAndDecodeContext(ctx, &dd, "GET", ddpath, nil, nil)
213 diag.verbosef("BlobSignatureTTL = %d", dd.BlobSignatureTTL)
217 var cluster arvados.Cluster
218 cfgpath := "arvados/v1/config"
220 diag.dotest(20, fmt.Sprintf("getting exported config from https://%s/%s", client.APIHost, cfgpath), func() error {
221 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
223 err := client.RequestAndDecodeContext(ctx, &cluster, "GET", cfgpath, nil, nil)
227 diag.verbosef("Collections.BlobSigning = %v", cluster.Collections.BlobSigning)
232 var user arvados.User
233 diag.dotest(30, "getting current user record", func() error {
234 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
236 err := client.RequestAndDecodeContext(ctx, &user, "GET", "arvados/v1/users/current", nil, nil)
240 diag.verbosef("user uuid = %s", user.UUID)
245 diag.errorf("cannot proceed without cluster config -- aborting without running any further tests")
249 // uncomment to create some spurious errors
250 // cluster.Services.WebDAVDownload.ExternalURL.Host = "0.0.0.0:9"
252 // TODO: detect routing errors here, like finding wb2 at the
254 for i, svc := range []*arvados.Service{
255 &cluster.Services.Keepproxy,
256 &cluster.Services.WebDAV,
257 &cluster.Services.WebDAVDownload,
258 &cluster.Services.Websocket,
259 &cluster.Services.Workbench1,
260 &cluster.Services.Workbench2,
262 diag.dotest(40+i, fmt.Sprintf("connecting to service endpoint %s", svc.ExternalURL), func() error {
263 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
266 if strings.HasPrefix(u.Scheme, "ws") {
267 // We can do a real websocket test elsewhere,
268 // but for now we'll just check the https
270 u.Scheme = "http" + u.Scheme[2:]
272 if svc == &cluster.Services.WebDAV && strings.HasPrefix(u.Host, "*") {
273 u.Host = "d41d8cd98f00b204e9800998ecf8427e-0" + u.Host[1:]
275 req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
279 resp, err := http.DefaultClient.Do(req)
288 for i, url := range []string{
289 cluster.Services.Controller.ExternalURL.String(),
290 cluster.Services.Keepproxy.ExternalURL.String() + "d41d8cd98f00b204e9800998ecf8427e+0",
291 cluster.Services.WebDAVDownload.ExternalURL.String(),
293 diag.dotest(50+i, fmt.Sprintf("checking CORS headers at %s", url), func() error {
294 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
296 req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
300 req.Header.Set("Origin", "https://example.com")
301 resp, err := http.DefaultClient.Do(req)
305 if hdr := resp.Header.Get("Access-Control-Allow-Origin"); hdr != "*" {
306 return fmt.Errorf("expected \"Access-Control-Allow-Origin: *\", got %q", hdr)
312 var keeplist arvados.KeepServiceList
313 diag.dotest(60, "checking internal/external client detection", func() error {
314 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
316 err := client.RequestAndDecodeContext(ctx, &keeplist, "GET", "arvados/v1/keep_services/accessible", nil, arvados.ListOptions{Limit: 999999})
318 return fmt.Errorf("error getting keep services list: %s", err)
319 } else if len(keeplist.Items) == 0 {
320 return fmt.Errorf("controller did not return any keep services")
322 found := map[string]int{}
323 for _, ks := range keeplist.Items {
324 found[ks.ServiceType]++
326 isInternal := found["proxy"] == 0 && len(keeplist.Items) > 0
327 isExternal := found["proxy"] > 0 && found["proxy"] == len(keeplist.Items)
329 diag.infof("controller returned only proxy services, this host is treated as \"external\"")
330 } else if isInternal {
331 diag.infof("controller returned only non-proxy services, this host is treated as \"internal\"")
333 if (diag.checkInternal && !isInternal) || (diag.checkExternal && !isExternal) {
334 return fmt.Errorf("expecting internal=%v external=%v, but found internal=%v external=%v", diag.checkInternal, diag.checkExternal, isInternal, isExternal)
339 for i, ks := range keeplist.Items {
342 Host: net.JoinHostPort(ks.ServiceHost, fmt.Sprintf("%d", ks.ServicePort)),
345 if ks.ServiceSSLFlag {
348 diag.dotest(61+i, fmt.Sprintf("reading+writing via keep service at %s", u.String()), func() error {
349 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
351 req, err := http.NewRequestWithContext(ctx, "PUT", u.String()+"d41d8cd98f00b204e9800998ecf8427e", nil)
355 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
356 resp, err := http.DefaultClient.Do(req)
360 defer resp.Body.Close()
361 body, err := ioutil.ReadAll(resp.Body)
363 return fmt.Errorf("reading response body: %s", err)
365 loc := strings.TrimSpace(string(body))
366 if !strings.HasPrefix(loc, "d41d8") {
367 return fmt.Errorf("unexpected response from write: %q", body)
370 req, err = http.NewRequestWithContext(ctx, "GET", u.String()+loc, nil)
374 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
375 resp, err = http.DefaultClient.Do(req)
379 defer resp.Body.Close()
380 body, err = ioutil.ReadAll(resp.Body)
382 return fmt.Errorf("reading response body: %s", err)
385 return fmt.Errorf("unexpected response from read: %q", body)
392 var project arvados.Group
393 diag.dotest(80, fmt.Sprintf("finding/creating %q project", diag.projectName), func() error {
394 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
396 var grplist arvados.GroupList
397 err := client.RequestAndDecodeContext(ctx, &grplist, "GET", "arvados/v1/groups", nil, arvados.ListOptions{
398 Filters: []arvados.Filter{
399 {"name", "=", diag.projectName},
400 {"group_class", "=", "project"},
401 {"owner_uuid", "=", user.UUID}},
404 return fmt.Errorf("list groups: %s", err)
406 if len(grplist.Items) > 0 {
407 project = grplist.Items[0]
408 diag.verbosef("using existing project, uuid = %s", project.UUID)
411 diag.debugf("list groups: ok, no results")
412 err = client.RequestAndDecodeContext(ctx, &project, "POST", "arvados/v1/groups", nil, map[string]interface{}{"group": map[string]interface{}{
413 "name": diag.projectName,
414 "group_class": "project",
417 return fmt.Errorf("create project: %s", err)
419 diag.verbosef("created project, uuid = %s", project.UUID)
423 var collection arvados.Collection
424 diag.dotest(90, "creating temporary collection", func() error {
425 if project.UUID == "" {
426 return fmt.Errorf("skipping, no project to work in")
428 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
430 err := client.RequestAndDecodeContext(ctx, &collection, "POST", "arvados/v1/collections", nil, map[string]interface{}{
431 "ensure_unique_name": true,
432 "collection": map[string]interface{}{
433 "owner_uuid": project.UUID,
434 "name": "test collection",
435 "trash_at": time.Now().Add(time.Hour)}})
439 diag.verbosef("ok, uuid = %s", collection.UUID)
443 if collection.UUID != "" {
445 diag.dotest(9990, "deleting temporary collection", func() error {
446 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
448 return client.RequestAndDecodeContext(ctx, nil, "DELETE", "arvados/v1/collections/"+collection.UUID, nil, nil)
453 tempdir, err := ioutil.TempDir("", "arvados-diagnostics")
455 diag.errorf("error creating temp dir: %s", err)
458 defer os.RemoveAll(tempdir)
460 var dockerImageData []byte
461 if diag.dockerImage != "" || diag.priority < 1 {
462 // We won't be using the self-built docker image, so
463 // don't build it. But we will write the embedded
464 // "hello-world" image to our test collection to test
465 // upload/download, whether or not we're using it as a
467 dockerImageData = HelloWorldDockerImage
468 } else if selfbin, err := os.Readlink("/proc/self/exe"); err != nil {
469 diag.errorf("readlink /proc/self/exe: %s", err)
471 } else if selfbindata, err := os.ReadFile(selfbin); err != nil {
472 diag.errorf("error reading %s: %s", selfbin, err)
475 selfbinSha := fmt.Sprintf("%x", sha256.Sum256(selfbindata))
476 tag := "arvados-client-diagnostics:" + selfbinSha[:9]
477 err := os.WriteFile(tempdir+"/arvados-client", selfbindata, 0777)
479 diag.errorf("error writing %s: %s", tempdir+"/arvados-client", err)
483 dockerfile := "FROM " + diag.dockerImageFrom + "\n"
484 dockerfile += "RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --yes --no-install-recommends libfuse2 ca-certificates && apt-get clean\n"
485 dockerfile += "COPY /arvados-client /arvados-client\n"
486 cmd := exec.Command("docker", "build", "--tag", tag, "-f", "-", tempdir)
487 cmd.Stdin = strings.NewReader(dockerfile)
488 cmd.Stdout = diag.stderr
489 cmd.Stderr = diag.stderr
492 diag.errorf("error building docker image: %s", err)
495 checkversion, err := exec.Command("docker", "run", tag, "/arvados-client", "version").CombinedOutput()
497 diag.errorf("docker image does not seem to work: %s", err)
500 diag.infof("arvados-client version: %s", checkversion)
502 buf, err := exec.Command("docker", "save", tag).Output()
504 diag.errorf("docker save %s: %s", tag, err)
507 diag.infof("docker image size is %d", len(buf))
508 dockerImageData = buf
511 // Read image tarball to find image ID, so we can upload it as
512 // "sha256:{...}.tar"
515 tr := tar.NewReader(bytes.NewReader(dockerImageData))
517 hdr, err := tr.Next()
522 diag.errorf("internal error/bug: cannot read docker image tar file: %s", err)
525 if s := strings.TrimSuffix(hdr.Name, ".json"); len(s) == 64 && s != hdr.Name {
530 diag.errorf("internal error/bug: cannot find {sha256}.json file in docker image tar file")
534 tarfilename := "sha256:" + imageSHA2 + ".tar"
536 diag.dotest(100, "uploading file via webdav", func() error {
537 timeout := diag.timeout
538 if len(dockerImageData) > 10<<20 && timeout < time.Minute {
539 // Extend the normal http timeout if we're
540 // uploading a substantial docker image.
541 timeout = time.Minute
543 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(timeout))
545 if collection.UUID == "" {
546 return fmt.Errorf("skipping, no test collection")
549 req, err := http.NewRequestWithContext(ctx, "PUT", cluster.Services.WebDAVDownload.ExternalURL.String()+"c="+collection.UUID+"/"+tarfilename, bytes.NewReader(dockerImageData))
551 return fmt.Errorf("BUG? http.NewRequest: %s", err)
553 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
554 resp, err := http.DefaultClient.Do(req)
556 return fmt.Errorf("error performing http request: %s", err)
559 if resp.StatusCode != http.StatusCreated {
560 return fmt.Errorf("status %s", resp.Status)
562 diag.verbosef("upload ok, status %s, %f MB/s", resp.Status, float64(len(dockerImageData))/time.Since(t0).Seconds()/1000000)
563 err = client.RequestAndDecodeContext(ctx, &collection, "GET", "arvados/v1/collections/"+collection.UUID, nil, nil)
565 return fmt.Errorf("get updated collection: %s", err)
567 diag.verbosef("upload pdh %s", collection.PortableDataHash)
571 davurl := cluster.Services.WebDAV.ExternalURL
572 davWildcard := strings.HasPrefix(davurl.Host, "*--") || strings.HasPrefix(davurl.Host, "*.")
573 diag.dotest(110, fmt.Sprintf("checking WebDAV ExternalURL wildcard (%s)", davurl), func() error {
574 if davurl.Host == "" {
575 return fmt.Errorf("host missing - content previews will not work")
577 if !davWildcard && !cluster.Collections.TrustAllContent {
578 diag.warnf("WebDAV ExternalURL has no leading wildcard and TrustAllContent==false - content previews will not work")
583 for i, trial := range []struct {
589 {false, false, http.StatusNotFound, strings.Replace(davurl.String(), "*", "d41d8cd98f00b204e9800998ecf8427e-0", 1) + "foo"},
590 {false, false, http.StatusNotFound, strings.Replace(davurl.String(), "*", "d41d8cd98f00b204e9800998ecf8427e-0", 1) + tarfilename},
591 {false, false, http.StatusNotFound, cluster.Services.WebDAVDownload.ExternalURL.String() + "c=d41d8cd98f00b204e9800998ecf8427e+0/_/foo"},
592 {false, false, http.StatusNotFound, cluster.Services.WebDAVDownload.ExternalURL.String() + "c=d41d8cd98f00b204e9800998ecf8427e+0/_/" + tarfilename},
593 {true, true, http.StatusOK, strings.Replace(davurl.String(), "*", strings.Replace(collection.PortableDataHash, "+", "-", -1), 1) + tarfilename},
594 {true, false, http.StatusOK, cluster.Services.WebDAVDownload.ExternalURL.String() + "c=" + collection.UUID + "/_/" + tarfilename},
596 diag.dotest(120+i, fmt.Sprintf("downloading from webdav (%s)", trial.fileurl), func() error {
597 if trial.needWildcard && !davWildcard {
598 diag.warnf("skipping collection-id-in-vhost test because WebDAV ExternalURL has no leading wildcard")
601 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
603 if trial.needcoll && collection.UUID == "" {
604 return fmt.Errorf("skipping, no test collection")
606 req, err := http.NewRequestWithContext(ctx, "GET", trial.fileurl, nil)
610 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
611 resp, err := http.DefaultClient.Do(req)
615 defer resp.Body.Close()
616 body, err := ioutil.ReadAll(resp.Body)
618 return fmt.Errorf("reading response: %s", err)
620 if resp.StatusCode != trial.status {
621 return fmt.Errorf("unexpected response status: %s", resp.Status)
623 if trial.status == http.StatusOK && !bytes.Equal(body, dockerImageData) {
625 if len(excerpt) > 128 {
626 excerpt = append([]byte(nil), body[:128]...)
627 excerpt = append(excerpt, []byte("[...]")...)
629 return fmt.Errorf("unexpected response content: len %d, %q", len(body), excerpt)
635 var vm arvados.VirtualMachine
636 diag.dotest(130, "getting list of virtual machines", func() error {
637 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
639 var vmlist arvados.VirtualMachineList
640 err := client.RequestAndDecodeContext(ctx, &vmlist, "GET", "arvados/v1/virtual_machines", nil, arvados.ListOptions{Limit: 999999})
644 if len(vmlist.Items) < 1 {
645 diag.warnf("no VMs found")
652 diag.dotest(140, "getting workbench1 webshell page", func() error {
653 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
656 diag.warnf("skipping, no vm available")
659 webshelltermurl := cluster.Services.Workbench1.ExternalURL.String() + "virtual_machines/" + vm.UUID + "/webshell/testusername"
660 diag.debugf("url %s", webshelltermurl)
661 req, err := http.NewRequestWithContext(ctx, "GET", webshelltermurl, nil)
665 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
666 resp, err := http.DefaultClient.Do(req)
670 defer resp.Body.Close()
671 body, err := ioutil.ReadAll(resp.Body)
673 return fmt.Errorf("reading response: %s", err)
675 if resp.StatusCode != http.StatusOK {
676 return fmt.Errorf("unexpected response status: %s %q", resp.Status, body)
681 diag.dotest(150, "connecting to webshell service", func() error {
682 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
685 diag.warnf("skipping, no vm available")
688 u := cluster.Services.WebShell.ExternalURL
689 webshellurl := u.String() + vm.Hostname + "?"
690 if strings.HasPrefix(u.Host, "*") {
691 u.Host = vm.Hostname + u.Host[1:]
692 webshellurl = u.String() + "?"
694 diag.debugf("url %s", webshellurl)
695 req, err := http.NewRequestWithContext(ctx, "POST", webshellurl, bytes.NewBufferString(url.Values{
698 "session": {"xyzzy"},
699 "rooturl": {webshellurl},
704 req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
705 resp, err := http.DefaultClient.Do(req)
709 defer resp.Body.Close()
710 diag.debugf("response status %s", resp.Status)
711 body, err := ioutil.ReadAll(resp.Body)
713 return fmt.Errorf("reading response: %s", err)
715 diag.debugf("response body %q", body)
716 // We don't speak the protocol, so we get a 400 error
717 // from the webshell server even if everything is
718 // OK. Anything else (404, 502, ???) indicates a
720 if resp.StatusCode != http.StatusBadRequest {
721 return fmt.Errorf("unexpected response status: %s, %q", resp.Status, body)
726 diag.dotest(160, "running a container", func() error {
727 if diag.priority < 1 {
728 diag.infof("skipping (use priority > 0 if you want to run a container)")
731 if project.UUID == "" {
732 return fmt.Errorf("skipping, no project to work in")
735 timestamp := time.Now().Format(time.RFC3339)
737 var ctrCommand []string
738 switch diag.dockerImage {
740 if collection.UUID == "" {
741 return fmt.Errorf("skipping, no test collection to use as docker image")
743 diag.dockerImage = collection.PortableDataHash
744 ctrCommand = []string{"/arvados-client", "diagnostics",
745 "-priority=0", // don't run a container
746 "-log-level=" + diag.logLevel,
747 "-internal-client=true"}
749 if collection.UUID == "" {
750 return fmt.Errorf("skipping, no test collection to use as docker image")
752 diag.dockerImage = collection.PortableDataHash
753 ctrCommand = []string{"/hello"}
755 ctrCommand = []string{"echo", timestamp}
758 var cr arvados.ContainerRequest
759 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
762 err := client.RequestAndDecodeContext(ctx, &cr, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{"container_request": map[string]interface{}{
763 "owner_uuid": project.UUID,
764 "name": fmt.Sprintf("diagnostics container request %s", timestamp),
765 "container_image": diag.dockerImage,
766 "command": ctrCommand,
767 "use_existing": false,
768 "output_path": "/mnt/output",
769 "output_name": fmt.Sprintf("diagnostics output %s", timestamp),
770 "priority": diag.priority,
771 "state": arvados.ContainerRequestStateCommitted,
772 "mounts": map[string]map[string]interface{}{
774 "kind": "collection",
778 "runtime_constraints": arvados.RuntimeConstraints{
782 KeepCacheRAM: 64 << 20,
788 diag.infof("container request uuid = %s", cr.UUID)
789 diag.verbosef("container uuid = %s", cr.ContainerUUID)
791 timeout := 10 * time.Minute
792 diag.infof("container request submitted, waiting up to %v for container to run", arvados.Duration(timeout))
793 deadline := time.Now().Add(timeout)
795 var c arvados.Container
796 for ; cr.State != arvados.ContainerRequestStateFinal && time.Now().Before(deadline); time.Sleep(2 * time.Second) {
797 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(diag.timeout))
800 crStateWas := cr.State
801 err := client.RequestAndDecodeContext(ctx, &cr, "GET", "arvados/v1/container_requests/"+cr.UUID, nil, nil)
805 if cr.State != crStateWas {
806 diag.debugf("container request state = %s", cr.State)
810 err = client.RequestAndDecodeContext(ctx, &c, "GET", "arvados/v1/containers/"+cr.ContainerUUID, nil, nil)
814 if c.State != cStateWas {
815 diag.debugf("container state = %s", c.State)
821 if cr.State != arvados.ContainerRequestStateFinal {
822 err := client.RequestAndDecodeContext(context.Background(), &cr, "PATCH", "arvados/v1/container_requests/"+cr.UUID, nil, map[string]interface{}{
823 "container_request": map[string]interface{}{
827 diag.infof("error canceling container request %s: %s", cr.UUID, err)
829 diag.debugf("canceled container request %s", cr.UUID)
831 return fmt.Errorf("timed out waiting for container to finish; container request %s state was %q, container %s state was %q", cr.UUID, cr.State, c.UUID, c.State)
833 if c.State != arvados.ContainerStateComplete {
834 return fmt.Errorf("container request %s is final but container %s did not complete: container state = %q", cr.UUID, cr.ContainerUUID, c.State)
837 return fmt.Errorf("container exited %d", c.ExitCode)