1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 "git.curoverse.com/arvados.git/sdk/go/arvados"
21 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
22 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
23 "git.curoverse.com/arvados.git/sdk/go/keepclient"
24 check "gopkg.in/check.v1"
27 var testAPIHost = os.Getenv("ARVADOS_API_HOST")
29 var _ = check.Suite(&IntegrationSuite{})
31 // IntegrationSuite tests need an API server and a keep-web server
32 type IntegrationSuite struct {
36 func (s *IntegrationSuite) TestNoToken(c *check.C) {
37 for _, token := range []string{
41 hdr, body, _ := s.runCurl(c, token, "collections.example.com", "/collections/"+arvadostest.FooCollection+"/foo")
42 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
43 c.Check(body, check.Equals, "")
46 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/collections/download/"+arvadostest.FooCollection+"/"+token+"/foo")
47 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
48 c.Check(body, check.Equals, "")
51 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/bad-route")
52 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
53 c.Check(body, check.Equals, "")
57 // TODO: Move most cases to functional tests -- at least use Go's own
58 // http client instead of forking curl. Just leave enough of an
59 // integration test to assure that the documented way of invoking curl
60 // really works against the server.
61 func (s *IntegrationSuite) Test404(c *check.C) {
62 for _, uri := range []string{
63 // Routing errors (always 404 regardless of what's stored in Keep)
68 // Implicit/generated index is not implemented yet;
69 // until then, return 404.
70 "/collections/" + arvadostest.FooCollection,
71 "/collections/" + arvadostest.FooCollection + "/",
72 "/collections/" + arvadostest.FooBarDirCollection + "/dir1",
73 "/collections/" + arvadostest.FooBarDirCollection + "/dir1/",
74 // Non-existent file in collection
75 "/collections/" + arvadostest.FooCollection + "/theperthcountyconspiracy",
76 "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
77 // Non-existent collection
78 "/collections/" + arvadostest.NonexistentCollection,
79 "/collections/" + arvadostest.NonexistentCollection + "/",
80 "/collections/" + arvadostest.NonexistentCollection + "/theperthcountyconspiracy",
81 "/collections/download/" + arvadostest.NonexistentCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
83 hdr, body, _ := s.runCurl(c, arvadostest.ActiveToken, "collections.example.com", uri)
84 c.Check(hdr, check.Matches, "(?s)HTTP/1.1 404 Not Found\r\n.*")
86 c.Check(body, check.Equals, "404 page not found\n")
91 func (s *IntegrationSuite) Test1GBFile(c *check.C) {
93 c.Skip("skipping 1GB integration test in short mode")
95 s.test100BlockFile(c, 10000000)
98 func (s *IntegrationSuite) Test100BlockFile(c *check.C) {
101 s.test100BlockFile(c, 30000)
104 s.test100BlockFile(c, 3000000)
108 func (s *IntegrationSuite) test100BlockFile(c *check.C, blocksize int) {
109 testdata := make([]byte, blocksize)
110 for i := 0; i < blocksize; i++ {
111 testdata[i] = byte(' ')
113 arv, err := arvadosclient.MakeArvadosClient()
114 c.Assert(err, check.Equals, nil)
115 arv.ApiToken = arvadostest.ActiveToken
116 kc, err := keepclient.MakeKeepClient(arv)
117 c.Assert(err, check.Equals, nil)
118 loc, _, err := kc.PutB(testdata[:])
119 c.Assert(err, check.Equals, nil)
121 for i := 0; i < 100; i++ {
122 mtext = mtext + " " + loc
124 mtext = mtext + fmt.Sprintf(" 0:%d00:testdata.bin\n", blocksize)
125 coll := map[string]interface{}{}
126 err = arv.Create("collections",
127 map[string]interface{}{
128 "collection": map[string]interface{}{
129 "name": fmt.Sprintf("testdata blocksize=%d", blocksize),
130 "manifest_text": mtext,
133 c.Assert(err, check.Equals, nil)
134 uuid := coll["uuid"].(string)
136 hdr, body, size := s.runCurl(c, arv.ApiToken, uuid+".collections.example.com", "/testdata.bin")
137 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
138 c.Check(hdr, check.Matches, `(?si).*Content-length: `+fmt.Sprintf("%d00", blocksize)+`\r\n.*`)
139 c.Check([]byte(body)[:1234], check.DeepEquals, testdata[:1234])
140 c.Check(size, check.Equals, int64(blocksize)*100)
143 type curlCase struct {
150 func (s *IntegrationSuite) Test200(c *check.C) {
151 s.testServer.Config.AnonymousTokens = []string{arvadostest.AnonymousToken}
152 for _, spec := range []curlCase{
155 auth: arvadostest.ActiveToken,
156 host: arvadostest.FooCollection + "--collections.example.com",
158 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
161 auth: arvadostest.ActiveToken,
162 host: arvadostest.FooCollection + ".collections.example.com",
164 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
167 host: strings.Replace(arvadostest.FooPdh, "+", "-", 1) + ".collections.example.com",
168 path: "/t=" + arvadostest.ActiveToken + "/foo",
169 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
172 path: "/c=" + arvadostest.FooPdh + "/t=" + arvadostest.ActiveToken + "/foo",
173 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
176 path: "/c=" + strings.Replace(arvadostest.FooPdh, "+", "-", 1) + "/t=" + arvadostest.ActiveToken + "/_/foo",
177 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
180 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
181 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
184 auth: "tokensobogus",
185 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
186 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
189 auth: arvadostest.ActiveToken,
190 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
191 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
194 auth: arvadostest.AnonymousToken,
195 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
196 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
199 // Anonymously accessible data
201 path: "/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
202 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
205 host: arvadostest.HelloWorldCollection + ".collections.example.com",
206 path: "/Hello%20world.txt",
207 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
210 host: arvadostest.HelloWorldCollection + ".collections.example.com",
211 path: "/_/Hello%20world.txt",
212 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
215 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
216 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
219 auth: arvadostest.ActiveToken,
220 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
221 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
224 auth: arvadostest.SpectatorToken,
225 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
226 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
229 auth: arvadostest.SpectatorToken,
230 host: arvadostest.HelloWorldCollection + "--collections.example.com",
231 path: "/Hello%20world.txt",
232 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
235 auth: arvadostest.SpectatorToken,
236 path: "/collections/download/" + arvadostest.HelloWorldCollection + "/" + arvadostest.SpectatorToken + "/Hello%20world.txt",
237 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
242 host = "collections.example.com"
244 hdr, body, _ := s.runCurl(c, spec.auth, host, spec.path)
245 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
246 if strings.HasSuffix(spec.path, ".txt") {
247 c.Check(hdr, check.Matches, `(?s).*\r\nContent-Type: text/plain.*`)
248 // TODO: Check some types that aren't
249 // automatically detected by Go's http server
250 // by sniffing the content.
252 c.Check(fmt.Sprintf("%x", md5.Sum([]byte(body))), check.Equals, spec.dataMD5)
256 // Return header block and body.
257 func (s *IntegrationSuite) runCurl(c *check.C, token, host, uri string, args ...string) (hdr, bodyPart string, bodySize int64) {
258 curlArgs := []string{"--silent", "--show-error", "--include"}
259 testHost, testPort, _ := net.SplitHostPort(s.testServer.Addr)
260 curlArgs = append(curlArgs, "--resolve", host+":"+testPort+":"+testHost)
262 curlArgs = append(curlArgs, "-H", "Authorization: OAuth2 "+token)
264 curlArgs = append(curlArgs, args...)
265 curlArgs = append(curlArgs, "http://"+host+":"+testPort+uri)
266 c.Log(fmt.Sprintf("curlArgs == %#v", curlArgs))
267 cmd := exec.Command("curl", curlArgs...)
268 stdout, err := cmd.StdoutPipe()
269 c.Assert(err, check.Equals, nil)
270 cmd.Stderr = cmd.Stdout
272 buf := make([]byte, 2<<27)
273 n, err := io.ReadFull(stdout, buf)
274 // Discard (but measure size of) anything past 128 MiB.
276 if err == io.ErrUnexpectedEOF {
279 c.Assert(err, check.Equals, nil)
280 discarded, err = io.Copy(ioutil.Discard, stdout)
281 c.Assert(err, check.Equals, nil)
284 // Without "-f", curl exits 0 as long as it gets a valid HTTP
285 // response from the server, even if the response status
286 // indicates that the request failed. In our test suite, we
287 // always expect a valid HTTP response, and we parse the
288 // headers ourselves. If curl exits non-zero, our testing
289 // environment is broken.
290 c.Assert(err, check.Equals, nil)
291 hdrsAndBody := strings.SplitN(string(buf), "\r\n\r\n", 2)
292 c.Assert(len(hdrsAndBody), check.Equals, 2)
294 bodyPart = hdrsAndBody[1]
295 bodySize = int64(len(bodyPart)) + discarded
299 func (s *IntegrationSuite) TestMetrics(c *check.C) {
300 origin := "http://" + s.testServer.Addr
301 req, _ := http.NewRequest("GET", origin+"/notfound", nil)
302 _, err := http.DefaultClient.Do(req)
303 c.Assert(err, check.IsNil)
304 req, _ = http.NewRequest("GET", origin+"/by_id/", nil)
305 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
306 resp, err := http.DefaultClient.Do(req)
307 c.Assert(err, check.IsNil)
308 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
309 for i := 0; i < 2; i++ {
310 req, _ = http.NewRequest("GET", origin+"/foo", nil)
311 req.Host = arvadostest.FooCollection + ".example.com"
312 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
313 resp, err = http.DefaultClient.Do(req)
314 c.Assert(err, check.IsNil)
315 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
316 buf, _ := ioutil.ReadAll(resp.Body)
317 c.Check(buf, check.DeepEquals, []byte("foo"))
321 s.testServer.Config.Cache.updateGauges()
323 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
324 resp, err = http.DefaultClient.Do(req)
325 c.Assert(err, check.IsNil)
326 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
327 type summary struct {
328 SampleCount string `json:"sample_count"`
329 SampleSum float64 `json:"sample_sum"`
335 type counter struct {
355 json.NewDecoder(resp.Body).Decode(&ents)
356 summaries := map[string]summary{}
357 gauges := map[string]gauge{}
358 counters := map[string]counter{}
359 for _, e := range ents {
360 for _, m := range e.Metric {
361 labels := map[string]string{}
362 for _, lbl := range m.Label {
363 labels[lbl.Name] = lbl.Value
365 summaries[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Summary
366 counters[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Counter
367 gauges[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Gauge
370 c.Check(summaries["request_duration_seconds/get/200"].SampleSum, check.Not(check.Equals), 0)
371 c.Check(summaries["request_duration_seconds/get/200"].SampleCount, check.Equals, "3")
372 c.Check(summaries["request_duration_seconds/get/404"].SampleCount, check.Equals, "1")
373 c.Check(summaries["time_to_status_seconds/get/404"].SampleCount, check.Equals, "1")
374 c.Check(counters["arvados_keepweb_collectioncache_requests//"].Value, check.Equals, int64(2))
375 c.Check(counters["arvados_keepweb_collectioncache_api_calls//"].Value, check.Equals, int64(1))
376 c.Check(counters["arvados_keepweb_collectioncache_hits//"].Value, check.Equals, int64(1))
377 c.Check(counters["arvados_keepweb_collectioncache_pdh_hits//"].Value, check.Equals, int64(1))
378 c.Check(counters["arvados_keepweb_collectioncache_permission_hits//"].Value, check.Equals, int64(1))
379 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifests//"].Value, check.Equals, float64(1))
380 // FooCollection's cached manifest size is 45 ("1f4b0....+45") plus one 51-byte blob signature
381 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifest_bytes//"].Value, check.Equals, float64(45+51))
383 // If the Host header indicates a collection, /metrics.json
384 // refers to a file in the collection -- the metrics handler
385 // must not intercept that route.
386 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
387 req.Host = strings.Replace(arvadostest.FooCollectionPDH, "+", "-", -1) + ".example.com"
388 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
389 resp, err = http.DefaultClient.Do(req)
390 c.Assert(err, check.IsNil)
391 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
394 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
395 arvadostest.StartAPI()
396 arvadostest.StartKeep(2, true)
398 arv, err := arvadosclient.MakeArvadosClient()
399 c.Assert(err, check.Equals, nil)
400 arv.ApiToken = arvadostest.ActiveToken
401 kc, err := keepclient.MakeKeepClient(arv)
402 c.Assert(err, check.Equals, nil)
403 kc.PutB([]byte("Hello world\n"))
404 kc.PutB([]byte("foo"))
405 kc.PutB([]byte("foobar"))
408 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
409 arvadostest.StopKeep(2)
410 arvadostest.StopAPI()
413 func (s *IntegrationSuite) SetUpTest(c *check.C) {
414 arvadostest.ResetEnv()
415 cfg := DefaultConfig()
416 cfg.Client = arvados.Client{
417 APIHost: testAPIHost,
420 cfg.Listen = "127.0.0.1:0"
421 s.testServer = &server{Config: cfg}
422 err := s.testServer.Start()
423 c.Assert(err, check.Equals, nil)
426 func (s *IntegrationSuite) TearDownTest(c *check.C) {
428 if s.testServer != nil {
429 err = s.testServer.Close()
431 c.Check(err, check.Equals, nil)
434 // Gocheck boilerplate
435 func Test(t *testing.T) {