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.StatusUnauthorized)
328 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
329 req.Header.Set("Authorization", "Bearer badtoken")
330 resp, err = http.DefaultClient.Do(req)
331 c.Assert(err, check.IsNil)
332 c.Check(resp.StatusCode, check.Equals, http.StatusForbidden)
334 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
335 req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
336 resp, err = http.DefaultClient.Do(req)
337 c.Assert(err, check.IsNil)
338 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
339 type summary struct {
340 SampleCount string `json:"sample_count"`
341 SampleSum float64 `json:"sample_sum"`
347 type counter struct {
367 json.NewDecoder(resp.Body).Decode(&ents)
368 summaries := map[string]summary{}
369 gauges := map[string]gauge{}
370 counters := map[string]counter{}
371 for _, e := range ents {
372 for _, m := range e.Metric {
373 labels := map[string]string{}
374 for _, lbl := range m.Label {
375 labels[lbl.Name] = lbl.Value
377 summaries[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Summary
378 counters[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Counter
379 gauges[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Gauge
382 c.Check(summaries["request_duration_seconds/get/200"].SampleSum, check.Not(check.Equals), 0)
383 c.Check(summaries["request_duration_seconds/get/200"].SampleCount, check.Equals, "3")
384 c.Check(summaries["request_duration_seconds/get/404"].SampleCount, check.Equals, "1")
385 c.Check(summaries["time_to_status_seconds/get/404"].SampleCount, check.Equals, "1")
386 c.Check(counters["arvados_keepweb_collectioncache_requests//"].Value, check.Equals, int64(2))
387 c.Check(counters["arvados_keepweb_collectioncache_api_calls//"].Value, check.Equals, int64(1))
388 c.Check(counters["arvados_keepweb_collectioncache_hits//"].Value, check.Equals, int64(1))
389 c.Check(counters["arvados_keepweb_collectioncache_pdh_hits//"].Value, check.Equals, int64(1))
390 c.Check(counters["arvados_keepweb_collectioncache_permission_hits//"].Value, check.Equals, int64(1))
391 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifests//"].Value, check.Equals, float64(1))
392 // FooCollection's cached manifest size is 45 ("1f4b0....+45") plus one 51-byte blob signature
393 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifest_bytes//"].Value, check.Equals, float64(45+51))
395 // If the Host header indicates a collection, /metrics.json
396 // refers to a file in the collection -- the metrics handler
397 // must not intercept that route.
398 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
399 req.Host = strings.Replace(arvadostest.FooCollectionPDH, "+", "-", -1) + ".example.com"
400 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
401 resp, err = http.DefaultClient.Do(req)
402 c.Assert(err, check.IsNil)
403 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
406 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
407 arvadostest.StartAPI()
408 arvadostest.StartKeep(2, true)
410 arv, err := arvadosclient.MakeArvadosClient()
411 c.Assert(err, check.Equals, nil)
412 arv.ApiToken = arvadostest.ActiveToken
413 kc, err := keepclient.MakeKeepClient(arv)
414 c.Assert(err, check.Equals, nil)
415 kc.PutB([]byte("Hello world\n"))
416 kc.PutB([]byte("foo"))
417 kc.PutB([]byte("foobar"))
418 kc.PutB([]byte("waz"))
421 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
422 arvadostest.StopKeep(2)
423 arvadostest.StopAPI()
426 func (s *IntegrationSuite) SetUpTest(c *check.C) {
427 arvadostest.ResetEnv()
428 cfg := DefaultConfig()
429 cfg.Client = arvados.Client{
430 APIHost: testAPIHost,
433 cfg.Listen = "127.0.0.1:0"
434 cfg.ManagementToken = arvadostest.ManagementToken
435 s.testServer = &server{Config: cfg}
436 err := s.testServer.Start()
437 c.Assert(err, check.Equals, nil)
440 func (s *IntegrationSuite) TearDownTest(c *check.C) {
442 if s.testServer != nil {
443 err = s.testServer.Close()
445 c.Check(err, check.Equals, nil)
448 // Gocheck boilerplate
449 func Test(t *testing.T) {