1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 "git.arvados.org/arvados.git/lib/config"
22 "git.arvados.org/arvados.git/sdk/go/arvados"
23 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
24 "git.arvados.org/arvados.git/sdk/go/arvadostest"
25 "git.arvados.org/arvados.git/sdk/go/ctxlog"
26 "git.arvados.org/arvados.git/sdk/go/keepclient"
27 check "gopkg.in/check.v1"
30 var testAPIHost = os.Getenv("ARVADOS_API_HOST")
32 var _ = check.Suite(&IntegrationSuite{})
34 // IntegrationSuite tests need an API server and a keep-web server
35 type IntegrationSuite struct {
39 func (s *IntegrationSuite) TestNoToken(c *check.C) {
40 for _, token := range []string{
44 hdr, body, _ := s.runCurl(c, token, "collections.example.com", "/collections/"+arvadostest.FooCollection+"/foo")
45 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
46 c.Check(body, check.Equals, notFoundMessage+"\n")
49 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/collections/download/"+arvadostest.FooCollection+"/"+token+"/foo")
50 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
51 c.Check(body, check.Equals, notFoundMessage+"\n")
54 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/bad-route")
55 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
56 c.Check(body, check.Equals, notFoundMessage+"\n")
60 // TODO: Move most cases to functional tests -- at least use Go's own
61 // http client instead of forking curl. Just leave enough of an
62 // integration test to assure that the documented way of invoking curl
63 // really works against the server.
64 func (s *IntegrationSuite) Test404(c *check.C) {
65 for _, uri := range []string{
66 // Routing errors (always 404 regardless of what's stored in Keep)
71 // Implicit/generated index is not implemented yet;
72 // until then, return 404.
73 "/collections/" + arvadostest.FooCollection,
74 "/collections/" + arvadostest.FooCollection + "/",
75 "/collections/" + arvadostest.FooBarDirCollection + "/dir1",
76 "/collections/" + arvadostest.FooBarDirCollection + "/dir1/",
77 // Non-existent file in collection
78 "/collections/" + arvadostest.FooCollection + "/theperthcountyconspiracy",
79 "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
80 // Non-existent collection
81 "/collections/" + arvadostest.NonexistentCollection,
82 "/collections/" + arvadostest.NonexistentCollection + "/",
83 "/collections/" + arvadostest.NonexistentCollection + "/theperthcountyconspiracy",
84 "/collections/download/" + arvadostest.NonexistentCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
86 hdr, body, _ := s.runCurl(c, arvadostest.ActiveToken, "collections.example.com", uri)
87 c.Check(hdr, check.Matches, "(?s)HTTP/1.1 404 Not Found\r\n.*")
89 c.Check(body, check.Equals, notFoundMessage+"\n")
94 func (s *IntegrationSuite) Test1GBFile(c *check.C) {
96 c.Skip("skipping 1GB integration test in short mode")
98 s.test100BlockFile(c, 10000000)
101 func (s *IntegrationSuite) Test100BlockFile(c *check.C) {
104 s.test100BlockFile(c, 30000)
107 s.test100BlockFile(c, 3000000)
111 func (s *IntegrationSuite) test100BlockFile(c *check.C, blocksize int) {
112 testdata := make([]byte, blocksize)
113 for i := 0; i < blocksize; i++ {
114 testdata[i] = byte(' ')
116 arv, err := arvadosclient.MakeArvadosClient()
117 c.Assert(err, check.Equals, nil)
118 arv.ApiToken = arvadostest.ActiveToken
119 kc, err := keepclient.MakeKeepClient(arv)
120 c.Assert(err, check.Equals, nil)
121 loc, _, err := kc.PutB(testdata[:])
122 c.Assert(err, check.Equals, nil)
124 for i := 0; i < 100; i++ {
125 mtext = mtext + " " + loc
127 mtext = mtext + fmt.Sprintf(" 0:%d00:testdata.bin\n", blocksize)
128 coll := map[string]interface{}{}
129 err = arv.Create("collections",
130 map[string]interface{}{
131 "collection": map[string]interface{}{
132 "name": fmt.Sprintf("testdata blocksize=%d", blocksize),
133 "manifest_text": mtext,
136 c.Assert(err, check.Equals, nil)
137 uuid := coll["uuid"].(string)
139 hdr, body, size := s.runCurl(c, arv.ApiToken, uuid+".collections.example.com", "/testdata.bin")
140 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
141 c.Check(hdr, check.Matches, `(?si).*Content-length: `+fmt.Sprintf("%d00", blocksize)+`\r\n.*`)
142 c.Check([]byte(body)[:1234], check.DeepEquals, testdata[:1234])
143 c.Check(size, check.Equals, int64(blocksize)*100)
146 type curlCase struct {
153 func (s *IntegrationSuite) Test200(c *check.C) {
154 s.testServer.Config.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
155 for _, spec := range []curlCase{
158 auth: arvadostest.ActiveToken,
159 host: arvadostest.FooCollection + "--collections.example.com",
161 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
164 auth: arvadostest.ActiveToken,
165 host: arvadostest.FooCollection + ".collections.example.com",
167 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
170 host: strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + ".collections.example.com",
171 path: "/t=" + arvadostest.ActiveToken + "/foo",
172 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
175 path: "/c=" + arvadostest.FooCollectionPDH + "/t=" + arvadostest.ActiveToken + "/foo",
176 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
179 path: "/c=" + strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + "/t=" + arvadostest.ActiveToken + "/_/foo",
180 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
183 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
184 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
187 auth: "tokensobogus",
188 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
189 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
192 auth: arvadostest.ActiveToken,
193 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
194 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
197 auth: arvadostest.AnonymousToken,
198 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
199 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
202 // Anonymously accessible data
204 path: "/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
205 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
208 host: arvadostest.HelloWorldCollection + ".collections.example.com",
209 path: "/Hello%20world.txt",
210 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
213 host: arvadostest.HelloWorldCollection + ".collections.example.com",
214 path: "/_/Hello%20world.txt",
215 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
218 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
219 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
222 auth: arvadostest.ActiveToken,
223 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
224 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
227 auth: arvadostest.SpectatorToken,
228 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
229 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
232 auth: arvadostest.SpectatorToken,
233 host: arvadostest.HelloWorldCollection + "--collections.example.com",
234 path: "/Hello%20world.txt",
235 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
238 auth: arvadostest.SpectatorToken,
239 path: "/collections/download/" + arvadostest.HelloWorldCollection + "/" + arvadostest.SpectatorToken + "/Hello%20world.txt",
240 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
245 host = "collections.example.com"
247 hdr, body, _ := s.runCurl(c, spec.auth, host, spec.path)
248 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
249 if strings.HasSuffix(spec.path, ".txt") {
250 c.Check(hdr, check.Matches, `(?s).*\r\nContent-Type: text/plain.*`)
251 // TODO: Check some types that aren't
252 // automatically detected by Go's http server
253 // by sniffing the content.
255 c.Check(fmt.Sprintf("%x", md5.Sum([]byte(body))), check.Equals, spec.dataMD5)
259 // Return header block and body.
260 func (s *IntegrationSuite) runCurl(c *check.C, auth, host, uri string, args ...string) (hdr, bodyPart string, bodySize int64) {
261 curlArgs := []string{"--silent", "--show-error", "--include"}
262 testHost, testPort, _ := net.SplitHostPort(s.testServer.Addr)
263 curlArgs = append(curlArgs, "--resolve", host+":"+testPort+":"+testHost)
264 if strings.Contains(auth, " ") {
265 // caller supplied entire Authorization header value
266 curlArgs = append(curlArgs, "-H", "Authorization: "+auth)
267 } else if auth != "" {
268 // caller supplied Arvados token
269 curlArgs = append(curlArgs, "-H", "Authorization: Bearer "+auth)
271 curlArgs = append(curlArgs, args...)
272 curlArgs = append(curlArgs, "http://"+host+":"+testPort+uri)
273 c.Log(fmt.Sprintf("curlArgs == %#v", curlArgs))
274 cmd := exec.Command("curl", curlArgs...)
275 stdout, err := cmd.StdoutPipe()
276 c.Assert(err, check.IsNil)
277 cmd.Stderr = os.Stderr
279 c.Assert(err, check.IsNil)
280 buf := make([]byte, 2<<27)
281 n, err := io.ReadFull(stdout, buf)
282 // Discard (but measure size of) anything past 128 MiB.
284 if err == io.ErrUnexpectedEOF {
287 c.Assert(err, check.IsNil)
288 discarded, err = io.Copy(ioutil.Discard, stdout)
289 c.Assert(err, check.IsNil)
292 // Without "-f", curl exits 0 as long as it gets a valid HTTP
293 // response from the server, even if the response status
294 // indicates that the request failed. In our test suite, we
295 // always expect a valid HTTP response, and we parse the
296 // headers ourselves. If curl exits non-zero, our testing
297 // environment is broken.
298 c.Assert(err, check.Equals, nil)
299 hdrsAndBody := strings.SplitN(string(buf), "\r\n\r\n", 2)
300 c.Assert(len(hdrsAndBody), check.Equals, 2)
302 bodyPart = hdrsAndBody[1]
303 bodySize = int64(len(bodyPart)) + discarded
307 func (s *IntegrationSuite) TestMetrics(c *check.C) {
308 s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = s.testServer.Addr
309 origin := "http://" + s.testServer.Addr
310 req, _ := http.NewRequest("GET", origin+"/notfound", nil)
311 _, err := http.DefaultClient.Do(req)
312 c.Assert(err, check.IsNil)
313 req, _ = http.NewRequest("GET", origin+"/by_id/", nil)
314 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
315 resp, err := http.DefaultClient.Do(req)
316 c.Assert(err, check.IsNil)
317 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
318 for i := 0; i < 2; i++ {
319 req, _ = http.NewRequest("GET", origin+"/foo", nil)
320 req.Host = arvadostest.FooCollection + ".example.com"
321 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
322 resp, err = http.DefaultClient.Do(req)
323 c.Assert(err, check.IsNil)
324 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
325 buf, _ := ioutil.ReadAll(resp.Body)
326 c.Check(buf, check.DeepEquals, []byte("foo"))
330 s.testServer.Config.Cache.updateGauges()
332 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
333 resp, err = http.DefaultClient.Do(req)
334 c.Assert(err, check.IsNil)
335 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
337 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
338 req.Header.Set("Authorization", "Bearer badtoken")
339 resp, err = http.DefaultClient.Do(req)
340 c.Assert(err, check.IsNil)
341 c.Check(resp.StatusCode, check.Equals, http.StatusForbidden)
343 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
344 req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
345 resp, err = http.DefaultClient.Do(req)
346 c.Assert(err, check.IsNil)
347 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
348 type summary struct {
352 type counter struct {
372 json.NewDecoder(resp.Body).Decode(&ents)
373 summaries := map[string]summary{}
374 gauges := map[string]gauge{}
375 counters := map[string]counter{}
376 for _, e := range ents {
377 for _, m := range e.Metric {
378 labels := map[string]string{}
379 for _, lbl := range m.Label {
380 labels[lbl.Name] = lbl.Value
382 summaries[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Summary
383 counters[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Counter
384 gauges[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Gauge
387 c.Check(summaries["request_duration_seconds/get/200"].SampleSum, check.Not(check.Equals), 0)
388 c.Check(summaries["request_duration_seconds/get/200"].SampleCount, check.Equals, "3")
389 c.Check(summaries["request_duration_seconds/get/404"].SampleCount, check.Equals, "1")
390 c.Check(summaries["time_to_status_seconds/get/404"].SampleCount, check.Equals, "1")
391 c.Check(counters["arvados_keepweb_collectioncache_requests//"].Value, check.Equals, int64(2))
392 c.Check(counters["arvados_keepweb_collectioncache_api_calls//"].Value, check.Equals, int64(1))
393 c.Check(counters["arvados_keepweb_collectioncache_hits//"].Value, check.Equals, int64(1))
394 c.Check(counters["arvados_keepweb_collectioncache_pdh_hits//"].Value, check.Equals, int64(1))
395 c.Check(counters["arvados_keepweb_collectioncache_permission_hits//"].Value, check.Equals, int64(1))
396 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifests//"].Value, check.Equals, float64(1))
397 // FooCollection's cached manifest size is 45 ("1f4b0....+45") plus one 51-byte blob signature
398 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifest_bytes//"].Value, check.Equals, float64(45+51))
400 // If the Host header indicates a collection, /metrics.json
401 // refers to a file in the collection -- the metrics handler
402 // must not intercept that route.
403 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
404 req.Host = strings.Replace(arvadostest.FooCollectionPDH, "+", "-", -1) + ".example.com"
405 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
406 resp, err = http.DefaultClient.Do(req)
407 c.Assert(err, check.IsNil)
408 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
411 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
412 arvadostest.StartAPI()
413 arvadostest.StartKeep(2, true)
415 arv, err := arvadosclient.MakeArvadosClient()
416 c.Assert(err, check.Equals, nil)
417 arv.ApiToken = arvadostest.ActiveToken
418 kc, err := keepclient.MakeKeepClient(arv)
419 c.Assert(err, check.Equals, nil)
420 kc.PutB([]byte("Hello world\n"))
421 kc.PutB([]byte("foo"))
422 kc.PutB([]byte("foobar"))
423 kc.PutB([]byte("waz"))
426 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
427 arvadostest.StopKeep(2)
428 arvadostest.StopAPI()
431 func (s *IntegrationSuite) SetUpTest(c *check.C) {
432 arvadostest.ResetEnv()
433 ldr := config.NewLoader(bytes.NewBufferString("Clusters: {zzzzz: {}}"), ctxlog.TestLogger(c))
435 arvCfg, err := ldr.Load()
436 c.Check(err, check.IsNil)
437 cfg := newConfig(arvCfg)
438 c.Assert(err, check.IsNil)
439 cfg.Client = arvados.Client{
440 APIHost: testAPIHost,
443 listen := "127.0.0.1:0"
444 cfg.cluster.Services.WebDAV.InternalURLs[arvados.URL{Host: listen}] = arvados.ServiceInstance{}
445 cfg.cluster.Services.WebDAVDownload.InternalURLs[arvados.URL{Host: listen}] = arvados.ServiceInstance{}
446 cfg.cluster.ManagementToken = arvadostest.ManagementToken
447 cfg.cluster.SystemRootToken = arvadostest.SystemRootToken
448 cfg.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
449 s.testServer = &server{Config: cfg}
450 err = s.testServer.Start(ctxlog.TestLogger(c))
451 c.Assert(err, check.Equals, nil)
454 func (s *IntegrationSuite) TearDownTest(c *check.C) {
456 if s.testServer != nil {
457 err = s.testServer.Close()
459 c.Check(err, check.Equals, nil)
462 // Gocheck boilerplate
463 func Test(t *testing.T) {