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.FooCollectionPDH, "+", "-", 1) + ".collections.example.com",
168 path: "/t=" + arvadostest.ActiveToken + "/foo",
169 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
172 path: "/c=" + arvadostest.FooCollectionPDH + "/t=" + arvadostest.ActiveToken + "/foo",
173 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
176 path: "/c=" + strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 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.IsNil)
270 cmd.Stderr = os.Stderr
272 c.Assert(err, check.IsNil)
273 buf := make([]byte, 2<<27)
274 n, err := io.ReadFull(stdout, buf)
275 // Discard (but measure size of) anything past 128 MiB.
277 if err == io.ErrUnexpectedEOF {
280 c.Assert(err, check.IsNil)
281 discarded, err = io.Copy(ioutil.Discard, stdout)
282 c.Assert(err, check.IsNil)
285 // Without "-f", curl exits 0 as long as it gets a valid HTTP
286 // response from the server, even if the response status
287 // indicates that the request failed. In our test suite, we
288 // always expect a valid HTTP response, and we parse the
289 // headers ourselves. If curl exits non-zero, our testing
290 // environment is broken.
291 c.Assert(err, check.Equals, nil)
292 hdrsAndBody := strings.SplitN(string(buf), "\r\n\r\n", 2)
293 c.Assert(len(hdrsAndBody), check.Equals, 2)
295 bodyPart = hdrsAndBody[1]
296 bodySize = int64(len(bodyPart)) + discarded
300 func (s *IntegrationSuite) TestMetrics(c *check.C) {
301 origin := "http://" + s.testServer.Addr
302 req, _ := http.NewRequest("GET", origin+"/notfound", nil)
303 _, err := http.DefaultClient.Do(req)
304 c.Assert(err, check.IsNil)
305 req, _ = http.NewRequest("GET", origin+"/by_id/", nil)
306 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
307 resp, err := http.DefaultClient.Do(req)
308 c.Assert(err, check.IsNil)
309 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
310 for i := 0; i < 2; i++ {
311 req, _ = http.NewRequest("GET", origin+"/foo", nil)
312 req.Host = arvadostest.FooCollection + ".example.com"
313 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
314 resp, err = http.DefaultClient.Do(req)
315 c.Assert(err, check.IsNil)
316 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
317 buf, _ := ioutil.ReadAll(resp.Body)
318 c.Check(buf, check.DeepEquals, []byte("foo"))
322 s.testServer.Config.Cache.updateGauges()
324 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
325 resp, err = http.DefaultClient.Do(req)
326 c.Assert(err, check.IsNil)
327 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
329 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
330 req.Header.Set("Authorization", "Bearer badtoken")
331 resp, err = http.DefaultClient.Do(req)
332 c.Assert(err, check.IsNil)
333 c.Check(resp.StatusCode, check.Equals, http.StatusForbidden)
335 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
336 req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
337 resp, err = http.DefaultClient.Do(req)
338 c.Assert(err, check.IsNil)
339 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
340 type summary struct {
341 SampleCount string `json:"sample_count"`
342 SampleSum float64 `json:"sample_sum"`
348 type counter struct {
368 json.NewDecoder(resp.Body).Decode(&ents)
369 summaries := map[string]summary{}
370 gauges := map[string]gauge{}
371 counters := map[string]counter{}
372 for _, e := range ents {
373 for _, m := range e.Metric {
374 labels := map[string]string{}
375 for _, lbl := range m.Label {
376 labels[lbl.Name] = lbl.Value
378 summaries[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Summary
379 counters[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Counter
380 gauges[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Gauge
383 c.Check(summaries["request_duration_seconds/get/200"].SampleSum, check.Not(check.Equals), 0)
384 c.Check(summaries["request_duration_seconds/get/200"].SampleCount, check.Equals, "3")
385 c.Check(summaries["request_duration_seconds/get/404"].SampleCount, check.Equals, "1")
386 c.Check(summaries["time_to_status_seconds/get/404"].SampleCount, check.Equals, "1")
387 c.Check(counters["arvados_keepweb_collectioncache_requests//"].Value, check.Equals, int64(2))
388 c.Check(counters["arvados_keepweb_collectioncache_api_calls//"].Value, check.Equals, int64(1))
389 c.Check(counters["arvados_keepweb_collectioncache_hits//"].Value, check.Equals, int64(1))
390 c.Check(counters["arvados_keepweb_collectioncache_pdh_hits//"].Value, check.Equals, int64(1))
391 c.Check(counters["arvados_keepweb_collectioncache_permission_hits//"].Value, check.Equals, int64(1))
392 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifests//"].Value, check.Equals, float64(1))
393 // FooCollection's cached manifest size is 45 ("1f4b0....+45") plus one 51-byte blob signature
394 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifest_bytes//"].Value, check.Equals, float64(45+51))
396 // If the Host header indicates a collection, /metrics.json
397 // refers to a file in the collection -- the metrics handler
398 // must not intercept that route.
399 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
400 req.Host = strings.Replace(arvadostest.FooCollectionPDH, "+", "-", -1) + ".example.com"
401 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
402 resp, err = http.DefaultClient.Do(req)
403 c.Assert(err, check.IsNil)
404 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
407 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
408 arvadostest.StartAPI()
409 arvadostest.StartKeep(2, true)
411 arv, err := arvadosclient.MakeArvadosClient()
412 c.Assert(err, check.Equals, nil)
413 arv.ApiToken = arvadostest.ActiveToken
414 kc, err := keepclient.MakeKeepClient(arv)
415 c.Assert(err, check.Equals, nil)
416 kc.PutB([]byte("Hello world\n"))
417 kc.PutB([]byte("foo"))
418 kc.PutB([]byte("foobar"))
419 kc.PutB([]byte("waz"))
422 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
423 arvadostest.StopKeep(2)
424 arvadostest.StopAPI()
427 func (s *IntegrationSuite) SetUpTest(c *check.C) {
428 arvadostest.ResetEnv()
429 cfg := DefaultConfig()
430 cfg.Client = arvados.Client{
431 APIHost: testAPIHost,
434 cfg.Listen = "127.0.0.1:0"
435 cfg.ManagementToken = arvadostest.ManagementToken
436 s.testServer = &server{Config: cfg}
437 err := s.testServer.Start()
438 c.Assert(err, check.Equals, nil)
441 func (s *IntegrationSuite) TearDownTest(c *check.C) {
443 if s.testServer != nil {
444 err = s.testServer.Close()
446 c.Check(err, check.Equals, nil)
449 // Gocheck boilerplate
450 func Test(t *testing.T) {