1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 "git.arvados.org/arvados.git/lib/config"
23 "git.arvados.org/arvados.git/sdk/go/arvados"
24 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
25 "git.arvados.org/arvados.git/sdk/go/arvadostest"
26 "git.arvados.org/arvados.git/sdk/go/ctxlog"
27 "git.arvados.org/arvados.git/sdk/go/keepclient"
28 check "gopkg.in/check.v1"
31 var testAPIHost = os.Getenv("ARVADOS_API_HOST")
33 var _ = check.Suite(&IntegrationSuite{})
35 // IntegrationSuite tests need an API server and a keep-web server
36 type IntegrationSuite struct {
38 ArvConfig *arvados.Config
41 func (s *IntegrationSuite) TestNoToken(c *check.C) {
42 for _, token := range []string{
46 hdr, body, _ := s.runCurl(c, token, "collections.example.com", "/collections/"+arvadostest.FooCollection+"/foo")
47 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
48 c.Check(body, check.Equals, notFoundMessage+"\n")
51 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/collections/download/"+arvadostest.FooCollection+"/"+token+"/foo")
52 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
53 c.Check(body, check.Equals, notFoundMessage+"\n")
56 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/bad-route")
57 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
58 c.Check(body, check.Equals, notFoundMessage+"\n")
62 // TODO: Move most cases to functional tests -- at least use Go's own
63 // http client instead of forking curl. Just leave enough of an
64 // integration test to assure that the documented way of invoking curl
65 // really works against the server.
66 func (s *IntegrationSuite) Test404(c *check.C) {
67 for _, uri := range []string{
68 // Routing errors (always 404 regardless of what's stored in Keep)
73 // Implicit/generated index is not implemented yet;
74 // until then, return 404.
75 "/collections/" + arvadostest.FooCollection,
76 "/collections/" + arvadostest.FooCollection + "/",
77 "/collections/" + arvadostest.FooBarDirCollection + "/dir1",
78 "/collections/" + arvadostest.FooBarDirCollection + "/dir1/",
79 // Non-existent file in collection
80 "/collections/" + arvadostest.FooCollection + "/theperthcountyconspiracy",
81 "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
82 // Non-existent collection
83 "/collections/" + arvadostest.NonexistentCollection,
84 "/collections/" + arvadostest.NonexistentCollection + "/",
85 "/collections/" + arvadostest.NonexistentCollection + "/theperthcountyconspiracy",
86 "/collections/download/" + arvadostest.NonexistentCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
88 hdr, body, _ := s.runCurl(c, arvadostest.ActiveToken, "collections.example.com", uri)
89 c.Check(hdr, check.Matches, "(?s)HTTP/1.1 404 Not Found\r\n.*")
91 c.Check(body, check.Equals, notFoundMessage+"\n")
96 func (s *IntegrationSuite) Test1GBFile(c *check.C) {
98 c.Skip("skipping 1GB integration test in short mode")
100 s.test100BlockFile(c, 10000000)
103 func (s *IntegrationSuite) Test100BlockFile(c *check.C) {
106 s.test100BlockFile(c, 30000)
109 s.test100BlockFile(c, 3000000)
113 func (s *IntegrationSuite) test100BlockFile(c *check.C, blocksize int) {
114 testdata := make([]byte, blocksize)
115 for i := 0; i < blocksize; i++ {
116 testdata[i] = byte(' ')
118 arv, err := arvadosclient.MakeArvadosClient()
119 c.Assert(err, check.Equals, nil)
120 arv.ApiToken = arvadostest.ActiveToken
121 kc, err := keepclient.MakeKeepClient(arv)
122 c.Assert(err, check.Equals, nil)
123 loc, _, err := kc.PutB(testdata[:])
124 c.Assert(err, check.Equals, nil)
126 for i := 0; i < 100; i++ {
127 mtext = mtext + " " + loc
129 mtext = mtext + fmt.Sprintf(" 0:%d00:testdata.bin\n", blocksize)
130 coll := map[string]interface{}{}
131 err = arv.Create("collections",
132 map[string]interface{}{
133 "collection": map[string]interface{}{
134 "name": fmt.Sprintf("testdata blocksize=%d", blocksize),
135 "manifest_text": mtext,
138 c.Assert(err, check.Equals, nil)
139 uuid := coll["uuid"].(string)
141 hdr, body, size := s.runCurl(c, arv.ApiToken, uuid+".collections.example.com", "/testdata.bin")
142 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
143 c.Check(hdr, check.Matches, `(?si).*Content-length: `+fmt.Sprintf("%d00", blocksize)+`\r\n.*`)
144 c.Check([]byte(body)[:1234], check.DeepEquals, testdata[:1234])
145 c.Check(size, check.Equals, int64(blocksize)*100)
148 type curlCase struct {
155 func (s *IntegrationSuite) Test200(c *check.C) {
156 s.testServer.Config.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
157 for _, spec := range []curlCase{
160 auth: arvadostest.ActiveToken,
161 host: arvadostest.FooCollection + "--collections.example.com",
163 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
166 auth: arvadostest.ActiveToken,
167 host: arvadostest.FooCollection + ".collections.example.com",
169 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
172 host: strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + ".collections.example.com",
173 path: "/t=" + arvadostest.ActiveToken + "/foo",
174 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
177 path: "/c=" + arvadostest.FooCollectionPDH + "/t=" + arvadostest.ActiveToken + "/foo",
178 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
181 path: "/c=" + strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + "/t=" + arvadostest.ActiveToken + "/_/foo",
182 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
185 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
186 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
189 auth: "tokensobogus",
190 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
191 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
194 auth: arvadostest.ActiveToken,
195 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
196 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
199 auth: arvadostest.AnonymousToken,
200 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
201 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
204 // Anonymously accessible data
206 path: "/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
207 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
210 host: arvadostest.HelloWorldCollection + ".collections.example.com",
211 path: "/Hello%20world.txt",
212 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
215 host: arvadostest.HelloWorldCollection + ".collections.example.com",
216 path: "/_/Hello%20world.txt",
217 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
220 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
221 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
224 auth: arvadostest.ActiveToken,
225 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
226 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
229 auth: arvadostest.SpectatorToken,
230 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
231 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
234 auth: arvadostest.SpectatorToken,
235 host: arvadostest.HelloWorldCollection + "--collections.example.com",
236 path: "/Hello%20world.txt",
237 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
240 auth: arvadostest.SpectatorToken,
241 path: "/collections/download/" + arvadostest.HelloWorldCollection + "/" + arvadostest.SpectatorToken + "/Hello%20world.txt",
242 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
247 host = "collections.example.com"
249 hdr, body, _ := s.runCurl(c, spec.auth, host, spec.path)
250 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
251 if strings.HasSuffix(spec.path, ".txt") {
252 c.Check(hdr, check.Matches, `(?s).*\r\nContent-Type: text/plain.*`)
253 // TODO: Check some types that aren't
254 // automatically detected by Go's http server
255 // by sniffing the content.
257 c.Check(fmt.Sprintf("%x", md5.Sum([]byte(body))), check.Equals, spec.dataMD5)
261 // Return header block and body.
262 func (s *IntegrationSuite) runCurl(c *check.C, auth, host, uri string, args ...string) (hdr, bodyPart string, bodySize int64) {
263 curlArgs := []string{"--silent", "--show-error", "--include"}
264 testHost, testPort, _ := net.SplitHostPort(s.testServer.Addr)
265 curlArgs = append(curlArgs, "--resolve", host+":"+testPort+":"+testHost)
266 if strings.Contains(auth, " ") {
267 // caller supplied entire Authorization header value
268 curlArgs = append(curlArgs, "-H", "Authorization: "+auth)
269 } else if auth != "" {
270 // caller supplied Arvados token
271 curlArgs = append(curlArgs, "-H", "Authorization: Bearer "+auth)
273 curlArgs = append(curlArgs, args...)
274 curlArgs = append(curlArgs, "http://"+host+":"+testPort+uri)
275 c.Log(fmt.Sprintf("curlArgs == %#v", curlArgs))
276 cmd := exec.Command("curl", curlArgs...)
277 stdout, err := cmd.StdoutPipe()
278 c.Assert(err, check.IsNil)
279 cmd.Stderr = os.Stderr
281 c.Assert(err, check.IsNil)
282 buf := make([]byte, 2<<27)
283 n, err := io.ReadFull(stdout, buf)
284 // Discard (but measure size of) anything past 128 MiB.
286 if err == io.ErrUnexpectedEOF {
289 c.Assert(err, check.IsNil)
290 discarded, err = io.Copy(ioutil.Discard, stdout)
291 c.Assert(err, check.IsNil)
294 // Without "-f", curl exits 0 as long as it gets a valid HTTP
295 // response from the server, even if the response status
296 // indicates that the request failed. In our test suite, we
297 // always expect a valid HTTP response, and we parse the
298 // headers ourselves. If curl exits non-zero, our testing
299 // environment is broken.
300 c.Assert(err, check.Equals, nil)
301 hdrsAndBody := strings.SplitN(string(buf), "\r\n\r\n", 2)
302 c.Assert(len(hdrsAndBody), check.Equals, 2)
304 bodyPart = hdrsAndBody[1]
305 bodySize = int64(len(bodyPart)) + discarded
309 func (s *IntegrationSuite) TestMetrics(c *check.C) {
310 s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = s.testServer.Addr
311 origin := "http://" + s.testServer.Addr
312 req, _ := http.NewRequest("GET", origin+"/notfound", nil)
313 _, err := http.DefaultClient.Do(req)
314 c.Assert(err, check.IsNil)
315 req, _ = http.NewRequest("GET", origin+"/by_id/", nil)
316 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
317 resp, err := http.DefaultClient.Do(req)
318 c.Assert(err, check.IsNil)
319 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
320 for i := 0; i < 2; i++ {
321 req, _ = http.NewRequest("GET", origin+"/foo", nil)
322 req.Host = arvadostest.FooCollection + ".example.com"
323 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
324 resp, err = http.DefaultClient.Do(req)
325 c.Assert(err, check.IsNil)
326 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
327 buf, _ := ioutil.ReadAll(resp.Body)
328 c.Check(buf, check.DeepEquals, []byte("foo"))
332 s.testServer.Config.Cache.updateGauges()
334 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
335 resp, err = http.DefaultClient.Do(req)
336 c.Assert(err, check.IsNil)
337 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
339 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
340 req.Header.Set("Authorization", "Bearer badtoken")
341 resp, err = http.DefaultClient.Do(req)
342 c.Assert(err, check.IsNil)
343 c.Check(resp.StatusCode, check.Equals, http.StatusForbidden)
345 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
346 req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
347 resp, err = http.DefaultClient.Do(req)
348 c.Assert(err, check.IsNil)
349 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
350 type summary struct {
354 type counter struct {
374 json.NewDecoder(resp.Body).Decode(&ents)
375 summaries := map[string]summary{}
376 gauges := map[string]gauge{}
377 counters := map[string]counter{}
378 for _, e := range ents {
379 for _, m := range e.Metric {
380 labels := map[string]string{}
381 for _, lbl := range m.Label {
382 labels[lbl.Name] = lbl.Value
384 summaries[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Summary
385 counters[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Counter
386 gauges[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Gauge
389 c.Check(summaries["request_duration_seconds/get/200"].SampleSum, check.Not(check.Equals), 0)
390 c.Check(summaries["request_duration_seconds/get/200"].SampleCount, check.Equals, "3")
391 c.Check(summaries["request_duration_seconds/get/404"].SampleCount, check.Equals, "1")
392 c.Check(summaries["time_to_status_seconds/get/404"].SampleCount, check.Equals, "1")
393 c.Check(counters["arvados_keepweb_collectioncache_requests//"].Value, check.Equals, int64(2))
394 c.Check(counters["arvados_keepweb_collectioncache_api_calls//"].Value, check.Equals, int64(2))
395 c.Check(counters["arvados_keepweb_collectioncache_hits//"].Value, check.Equals, int64(1))
396 c.Check(counters["arvados_keepweb_collectioncache_pdh_hits//"].Value, check.Equals, int64(1))
397 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifests//"].Value, check.Equals, float64(1))
398 // FooCollection's cached manifest size is 45 ("1f4b0....+45")
399 // plus one 51-byte blob signature; session fs counts 3 inodes
401 c.Check(gauges["arvados_keepweb_sessions_cached_collection_bytes//"].Value, check.Equals, float64(45+51+64*3))
403 // If the Host header indicates a collection, /metrics.json
404 // refers to a file in the collection -- the metrics handler
405 // must not intercept that route.
406 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
407 req.Host = strings.Replace(arvadostest.FooCollectionPDH, "+", "-", -1) + ".example.com"
408 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
409 resp, err = http.DefaultClient.Do(req)
410 c.Assert(err, check.IsNil)
411 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
414 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
415 arvadostest.ResetDB(c)
416 arvadostest.StartKeep(2, true)
418 arv, err := arvadosclient.MakeArvadosClient()
419 c.Assert(err, check.Equals, nil)
420 arv.ApiToken = arvadostest.ActiveToken
421 kc, err := keepclient.MakeKeepClient(arv)
422 c.Assert(err, check.Equals, nil)
423 kc.PutB([]byte("Hello world\n"))
424 kc.PutB([]byte("foo"))
425 kc.PutB([]byte("foobar"))
426 kc.PutB([]byte("waz"))
429 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
430 arvadostest.StopKeep(2)
433 func (s *IntegrationSuite) SetUpTest(c *check.C) {
434 arvadostest.ResetEnv()
435 ldr := config.NewLoader(bytes.NewBufferString("Clusters: {zzzzz: {}}"), ctxlog.TestLogger(c))
437 arvCfg, err := ldr.Load()
438 c.Check(err, check.IsNil)
439 cfg := newConfig(ctxlog.TestLogger(c), arvCfg)
440 c.Assert(err, check.IsNil)
441 cfg.Client = arvados.Client{
442 APIHost: testAPIHost,
445 listen := "127.0.0.1:0"
446 cfg.cluster.Services.WebDAV.InternalURLs[arvados.URL{Host: listen}] = arvados.ServiceInstance{}
447 cfg.cluster.Services.WebDAVDownload.InternalURLs[arvados.URL{Host: listen}] = arvados.ServiceInstance{}
448 cfg.cluster.ManagementToken = arvadostest.ManagementToken
449 cfg.cluster.SystemRootToken = arvadostest.SystemRootToken
450 cfg.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
452 s.testServer = &server{Config: cfg}
453 logger := ctxlog.TestLogger(c)
454 ctx := ctxlog.Context(context.Background(), logger)
455 err = s.testServer.Start(ctx, logger)
456 c.Assert(err, check.Equals, nil)
459 func (s *IntegrationSuite) TearDownTest(c *check.C) {
461 if s.testServer != nil {
462 err = s.testServer.Close()
464 c.Check(err, check.Equals, nil)
467 // Gocheck boilerplate
468 func Test(t *testing.T) {