1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.arvados.org/arvados.git/sdk/go/arvados"
20 "git.arvados.org/arvados.git/sdk/go/ctxlog"
21 "github.com/sirupsen/logrus"
28 func (diag Command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
29 f := flag.NewFlagSet(prog, flag.ContinueOnError)
30 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")
31 loglevel := f.String("log-level", "info", "logging level (debug, info, warning, error)")
32 checkInternal := f.Bool("internal-client", false, "check that this host is considered an \"internal\" client")
33 checkExternal := f.Bool("external-client", false, "check that this host is considered an \"external\" client")
34 timeout := f.Duration("timeout", 10*time.Second, "timeout for http requests")
36 if err == flag.ErrHelp {
38 } else if err != nil {
39 fmt.Fprintln(stderr, err)
43 ctx := context.Background()
45 logger := ctxlog.New(stdout, "text", *loglevel)
46 logger.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true, DisableLevelTruncation: true})
50 debugf := logger.Debugf
52 errorf := func(f string, args ...interface{}) {
53 logger.Errorf(f, args...)
54 errors = append(errors, fmt.Sprintf(f, args...))
58 logger.Info("--- no errors ---")
60 fmt.Fprint(stdout, "\n--- cut here --- error summary ---\n\n")
61 for _, e := range errors {
67 client := arvados.NewClientFromEnv()
69 var dd arvados.DiscoveryDocument
70 ddpath := "discovery/v1/apis/arvados/v1/rest"
71 testname := fmt.Sprintf("getting discovery document from https://%s/%s", client.APIHost, ddpath)
73 err = client.RequestAndDecode(&dd, "GET", ddpath, nil, nil)
75 errorf("%s: %s", testname, err)
77 infof("%s: ok, BlobSignatureTTL is %d", testname, dd.BlobSignatureTTL)
80 var cluster arvados.Cluster
81 cfgpath := "arvados/v1/config"
82 testname = fmt.Sprintf("getting exported config from https://%s/%s", client.APIHost, cfgpath)
84 err = client.RequestAndDecode(&cluster, "GET", cfgpath, nil, nil)
86 errorf("%s: %s", testname, err)
88 infof("%s: ok, Collections.BlobSigning = %v", testname, cluster.Collections.BlobSigning)
92 testname = "getting current user record"
94 err = client.RequestAndDecode(&user, "GET", "arvados/v1/users/current", nil, nil)
96 errorf("%s: %s", testname, err)
99 infof("%s: ok, uuid = %s", testname, user.UUID)
102 // uncomment to create some spurious errors
103 // cluster.Services.WebDAVDownload.ExternalURL.Host = "0.0.0.0:9"
105 // TODO: detect routing errors here, like finding wb2 at the
107 for _, svc := range []*arvados.Service{
108 &cluster.Services.Keepproxy,
109 &cluster.Services.WebDAV,
110 &cluster.Services.WebDAVDownload,
111 &cluster.Services.Websocket,
112 &cluster.Services.Workbench1,
113 &cluster.Services.Workbench2,
115 testname = fmt.Sprintf("connecting to service endpoint %s", svc.ExternalURL)
116 logger.Info(testname)
118 if strings.HasPrefix(u.Scheme, "ws") {
119 // We can do a real websocket test elsewhere,
120 // but for now we'll just check the https
122 u.Scheme = "http" + u.Scheme[2:]
124 if svc == &cluster.Services.WebDAV && strings.HasPrefix(u.Host, "*") {
125 u.Host = "d41d8cd98f00b204e9800998ecf8427e-0" + u.Host[1:]
127 req, err := http.NewRequest(http.MethodGet, u.String(), nil)
129 errorf("%s: %s", testname, err)
132 resp, err := http.DefaultClient.Do(req)
134 errorf("%s: %s", testname, err)
138 infof("%s: ok", testname)
141 for _, url := range []string{
142 cluster.Services.Controller.ExternalURL.String(),
143 cluster.Services.Keepproxy.ExternalURL.String() + "d41d8cd98f00b204e9800998ecf8427e+0",
144 cluster.Services.WebDAVDownload.ExternalURL.String(),
146 testname = fmt.Sprintf("checking CORS headers at %s", url)
147 logger.Info(testname)
148 req, err := http.NewRequest("GET", url, nil)
150 errorf("%s: %s", testname, err)
153 req.Header.Set("Origin", "https://example.com")
154 resp, err := http.DefaultClient.Do(req)
156 errorf("%s: %s", testname, err)
159 if hdr := resp.Header.Get("Access-Control-Allow-Origin"); hdr != "*" {
160 warnf("%s: expected \"Access-Control-Allow-Origin: *\", got %q", testname, hdr)
162 infof("%s: ok", testname)
166 var keeplist arvados.KeepServiceList
167 testname = "checking internal/external client detection"
168 logger.Info(testname)
169 err = client.RequestAndDecode(&keeplist, "GET", "arvados/v1/keep_services/accessible", nil, arvados.ListOptions{Limit: -1})
171 errorf("%s: error getting keep services list: %s", testname, err)
172 } else if len(keeplist.Items) == 0 {
173 errorf("%s: controller did not return any keep services", testname)
175 found := map[string]int{}
176 for _, ks := range keeplist.Items {
177 found[ks.ServiceType]++
180 isInternal := found["proxy"] == 0 && len(keeplist.Items) > 0
181 isExternal := found["proxy"] > 0 && found["proxy"] == len(keeplist.Items)
182 if (*checkInternal && !isInternal) || (*checkExternal && !isExternal) {
186 infof("%s: controller returned only proxy services, this host is considered \"external\"", testname)
187 } else if isInternal {
188 infof("%s: controller returned only non-proxy services, this host is considered \"internal\"", testname)
190 errorf("%s: controller returned both proxy and non-proxy services: %v", testname, found)
194 var project arvados.Group
195 var grplist arvados.GroupList
196 testname = fmt.Sprintf("finding/creating %q project", diag.projectName)
197 logger.Info(testname)
198 err = client.RequestAndDecode(&grplist, "GET", "arvados/v1/groups", nil, arvados.ListOptions{
199 Filters: []arvados.Filter{
200 {"name", "=", diag.projectName},
201 {"group_class", "=", "project"},
202 {"owner_uuid", "=", user.UUID}},
205 errorf("%s: list groups: %s", testname, err)
206 } else if len(grplist.Items) < 1 {
207 infof("%s: list groups: ok, no results", testname)
208 err = client.RequestAndDecode(&project, "POST", "arvados/v1/groups", nil, map[string]interface{}{"group": map[string]interface{}{
209 "name": diag.projectName,
210 "group_class": "project",
213 errorf("%s: create project: %s", testname, err)
215 infof("%s: created project, uuid = %s", testname, project.UUID)
218 project = grplist.Items[0]
219 infof("%s: ok, using existing project, uuid = %s", testname, project.UUID)
222 testname = "creating temporary collection"
223 logger.Info(testname)
224 var collection arvados.Collection
225 err = client.RequestAndDecode(&collection, "POST", "arvados/v1/collections", nil, map[string]interface{}{
226 "ensure_unique_name": true,
227 "collection": map[string]interface{}{
228 "name": "test collection",
229 "trash_at": time.Now().Add(time.Hour)}})
231 errorf("%s: %s", testname, err)
233 infof("%s: ok, uuid = %s", testname, collection.UUID)
235 testname := "deleting temporary collection"
236 logger.Info(testname)
237 err := client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+collection.UUID, nil, nil)
239 errorf("%s: %s", testname, err)
241 infof("%s: ok", testname)
246 testname = "uploading file via webdav"
247 logger.Info(testname)
249 if collection.UUID == "" {
250 infof("%s: skipping, no test collection", testname)
253 req, err := http.NewRequest("PUT", cluster.Services.WebDAVDownload.ExternalURL.String()+"c="+collection.UUID+"/testfile", bytes.NewBufferString("testfiledata"))
255 errorf("%s: BUG? http.NewRequest: %s", testname, err)
258 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
259 resp, err := http.DefaultClient.Do(req)
261 errorf("%s: error performing http request: %s", testname, err)
265 if resp.StatusCode != http.StatusCreated {
266 errorf("%s: status %s", testname, resp.Status)
269 infof("%s: ok, status %s", testname, resp.Status)
270 err = client.RequestAndDecode(&collection, "GET", "arvados/v1/collections/"+collection.UUID, nil, nil)
272 errorf("%s: get updated collection: %s", testname, err)
275 infof("%s: get updated collection: ok, pdh %s", testname, collection.PortableDataHash)
278 davurl := cluster.Services.WebDAV.ExternalURL
279 testname = fmt.Sprintf("checking WebDAV ExternalURL wildcard (%s)", davurl)
280 logger.Info(testname)
281 if strings.HasPrefix(davurl.Host, "*--") || strings.HasPrefix(davurl.Host, "*.") {
282 infof("%s: looks ok", testname)
283 } else if davurl.Host == "" {
284 warnf("%s: host missing - content previews will not work", testname)
286 warnf("%s: host has no leading wildcard - content previews will not work unless TrustAllContent==true", testname)
289 for _, trial := range []struct {
293 {http.StatusNotFound, strings.Replace(davurl.String(), "*", "d41d8cd98f00b204e9800998ecf8427e-0", 1) + "foo"},
294 {http.StatusNotFound, strings.Replace(davurl.String(), "*", "d41d8cd98f00b204e9800998ecf8427e-0", 1) + "testfile"},
295 {http.StatusNotFound, cluster.Services.WebDAVDownload.ExternalURL.String() + "c=d41d8cd98f00b204e9800998ecf8427e+0/_/foo"},
296 {http.StatusNotFound, cluster.Services.WebDAVDownload.ExternalURL.String() + "c=d41d8cd98f00b204e9800998ecf8427e+0/_/testfile"},
297 {http.StatusOK, strings.Replace(davurl.String(), "*", strings.Replace(collection.PortableDataHash, "+", "-", -1), 1) + "testfile"},
298 {http.StatusOK, cluster.Services.WebDAVDownload.ExternalURL.String() + "c=" + collection.UUID + "/_/testfile"},
301 testname := fmt.Sprintf("downloading from webdav (%s)", trial.fileurl)
302 logger.Info(testname)
303 if collection.UUID == "" {
304 errorf("%s: skipping, no test collection", testname)
307 req, err := http.NewRequest("GET", trial.fileurl, nil)
309 errorf("%s: %s", testname, err)
312 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
313 resp, err := http.DefaultClient.Do(req)
315 errorf("%s: %s", testname, err)
318 defer resp.Body.Close()
319 body, err := ioutil.ReadAll(resp.Body)
321 errorf("%s: error reading response: %s", testname, err)
323 if resp.StatusCode != trial.status {
324 errorf("%s: unexpected response status: %s", testname, resp.Status)
325 } else if trial.status == http.StatusOK && string(body) != "testfiledata" {
326 errorf("%s: unexpected response content: %q", testname, body)
328 infof("%s: ok", testname)
333 var vm arvados.VirtualMachine
334 var vmlist arvados.VirtualMachineList
335 testname = "getting list of virtual machines"
336 logger.Info(testname)
337 err = client.RequestAndDecode(&vmlist, "GET", "arvados/v1/virtual_machines", nil, arvados.ListOptions{Limit: 999999})
339 errorf("%s: %s", testname, err)
340 } else if len(vmlist.Items) < 1 {
341 errorf("%s: none found", testname)
344 infof("%s: ok", testname)
347 testname = "getting workbench1 webshell page"
348 logger.Info(testname)
351 errorf("%s: skipping, no vm available", testname)
354 webshelltermurl := cluster.Services.Workbench1.ExternalURL.String() + "virtual_machines/" + vm.UUID + "/webshell/testusername"
355 debugf("%s: url %s", testname, webshelltermurl)
356 req, err := http.NewRequest("GET", webshelltermurl, nil)
358 errorf("%s: %s", testname, err)
361 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
362 resp, err := http.DefaultClient.Do(req)
364 errorf("%s: %s", testname, err)
367 defer resp.Body.Close()
368 body, err := ioutil.ReadAll(resp.Body)
370 errorf("%s: error reading response: %s", testname, err)
372 if resp.StatusCode != http.StatusOK {
373 errorf("%s: unexpected response status: %s %q", testname, resp.Status, body)
376 infof("%s: ok", testname)
379 testname = "connecting to webshell service"
380 logger.Info(testname)
382 ctx, cancel := context.WithDeadline(ctx, time.Now().Add(*timeout))
385 errorf("%s: skipping, no vm available", testname)
388 u := cluster.Services.WebShell.ExternalURL
389 webshellurl := u.String() + vm.Hostname + "?"
390 if strings.HasPrefix(u.Host, "*") {
391 u.Host = vm.Hostname + u.Host[1:]
392 webshellurl = u.String() + "?"
394 debugf("%s: url %s", testname, webshellurl)
395 req, err := http.NewRequestWithContext(ctx, "POST", webshellurl, bytes.NewBufferString(url.Values{
398 "session": {"xyzzy"},
399 "rooturl": {webshellurl},
402 errorf("%s: %s", testname, err)
405 req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
406 resp, err := http.DefaultClient.Do(req)
408 errorf("%s: %s", testname, err)
411 defer resp.Body.Close()
412 debugf("%s: response status %s", testname, resp.Status)
413 body, err := ioutil.ReadAll(resp.Body)
415 errorf("%s: error reading response: %s", testname, err)
417 debugf("%s: response body %q", testname, body)
418 // We don't speak the protocol, so we get a 400 error
419 // from the webshell server even if everything is
420 // OK. Anything else (404, 502, ???) indicates a
422 if resp.StatusCode != http.StatusBadRequest {
423 errorf("%s: unexpected response status: %s, %q", testname, resp.Status, body)
426 infof("%s: ok", testname)