1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 "git.curoverse.com/arvados.git/lib/config"
22 "git.curoverse.com/arvados.git/sdk/go/arvados"
23 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
24 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
25 "git.curoverse.com/arvados.git/sdk/go/keepclient"
26 check "gopkg.in/check.v1"
29 var testAPIHost = os.Getenv("ARVADOS_API_HOST")
31 var _ = check.Suite(&IntegrationSuite{})
33 // IntegrationSuite tests need an API server and a keep-web server
34 type IntegrationSuite struct {
38 func (s *IntegrationSuite) TestNoToken(c *check.C) {
39 for _, token := range []string{
43 hdr, body, _ := s.runCurl(c, token, "collections.example.com", "/collections/"+arvadostest.FooCollection+"/foo")
44 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
45 c.Check(body, check.Equals, "")
48 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/collections/download/"+arvadostest.FooCollection+"/"+token+"/foo")
49 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
50 c.Check(body, check.Equals, "")
53 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/bad-route")
54 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
55 c.Check(body, check.Equals, "")
59 // TODO: Move most cases to functional tests -- at least use Go's own
60 // http client instead of forking curl. Just leave enough of an
61 // integration test to assure that the documented way of invoking curl
62 // really works against the server.
63 func (s *IntegrationSuite) Test404(c *check.C) {
64 for _, uri := range []string{
65 // Routing errors (always 404 regardless of what's stored in Keep)
70 // Implicit/generated index is not implemented yet;
71 // until then, return 404.
72 "/collections/" + arvadostest.FooCollection,
73 "/collections/" + arvadostest.FooCollection + "/",
74 "/collections/" + arvadostest.FooBarDirCollection + "/dir1",
75 "/collections/" + arvadostest.FooBarDirCollection + "/dir1/",
76 // Non-existent file in collection
77 "/collections/" + arvadostest.FooCollection + "/theperthcountyconspiracy",
78 "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
79 // Non-existent collection
80 "/collections/" + arvadostest.NonexistentCollection,
81 "/collections/" + arvadostest.NonexistentCollection + "/",
82 "/collections/" + arvadostest.NonexistentCollection + "/theperthcountyconspiracy",
83 "/collections/download/" + arvadostest.NonexistentCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
85 hdr, body, _ := s.runCurl(c, arvadostest.ActiveToken, "collections.example.com", uri)
86 c.Check(hdr, check.Matches, "(?s)HTTP/1.1 404 Not Found\r\n.*")
88 c.Check(body, check.Equals, "404 page not found\n")
93 func (s *IntegrationSuite) Test1GBFile(c *check.C) {
95 c.Skip("skipping 1GB integration test in short mode")
97 s.test100BlockFile(c, 10000000)
100 func (s *IntegrationSuite) Test100BlockFile(c *check.C) {
103 s.test100BlockFile(c, 30000)
106 s.test100BlockFile(c, 3000000)
110 func (s *IntegrationSuite) test100BlockFile(c *check.C, blocksize int) {
111 testdata := make([]byte, blocksize)
112 for i := 0; i < blocksize; i++ {
113 testdata[i] = byte(' ')
115 arv, err := arvadosclient.MakeArvadosClient()
116 c.Assert(err, check.Equals, nil)
117 arv.ApiToken = arvadostest.ActiveToken
118 kc, err := keepclient.MakeKeepClient(arv)
119 c.Assert(err, check.Equals, nil)
120 loc, _, err := kc.PutB(testdata[:])
121 c.Assert(err, check.Equals, nil)
123 for i := 0; i < 100; i++ {
124 mtext = mtext + " " + loc
126 mtext = mtext + fmt.Sprintf(" 0:%d00:testdata.bin\n", blocksize)
127 coll := map[string]interface{}{}
128 err = arv.Create("collections",
129 map[string]interface{}{
130 "collection": map[string]interface{}{
131 "name": fmt.Sprintf("testdata blocksize=%d", blocksize),
132 "manifest_text": mtext,
135 c.Assert(err, check.Equals, nil)
136 uuid := coll["uuid"].(string)
138 hdr, body, size := s.runCurl(c, arv.ApiToken, uuid+".collections.example.com", "/testdata.bin")
139 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
140 c.Check(hdr, check.Matches, `(?si).*Content-length: `+fmt.Sprintf("%d00", blocksize)+`\r\n.*`)
141 c.Check([]byte(body)[:1234], check.DeepEquals, testdata[:1234])
142 c.Check(size, check.Equals, int64(blocksize)*100)
145 type curlCase struct {
152 func (s *IntegrationSuite) Test200(c *check.C) {
153 s.testServer.Config.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
154 for _, spec := range []curlCase{
157 auth: arvadostest.ActiveToken,
158 host: arvadostest.FooCollection + "--collections.example.com",
160 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
163 auth: arvadostest.ActiveToken,
164 host: arvadostest.FooCollection + ".collections.example.com",
166 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
169 host: strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + ".collections.example.com",
170 path: "/t=" + arvadostest.ActiveToken + "/foo",
171 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
174 path: "/c=" + arvadostest.FooCollectionPDH + "/t=" + arvadostest.ActiveToken + "/foo",
175 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
178 path: "/c=" + strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + "/t=" + arvadostest.ActiveToken + "/_/foo",
179 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
182 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
183 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
186 auth: "tokensobogus",
187 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
188 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
191 auth: arvadostest.ActiveToken,
192 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
193 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
196 auth: arvadostest.AnonymousToken,
197 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
198 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
201 // Anonymously accessible data
203 path: "/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
204 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
207 host: arvadostest.HelloWorldCollection + ".collections.example.com",
208 path: "/Hello%20world.txt",
209 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
212 host: arvadostest.HelloWorldCollection + ".collections.example.com",
213 path: "/_/Hello%20world.txt",
214 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
217 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
218 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
221 auth: arvadostest.ActiveToken,
222 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
223 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
226 auth: arvadostest.SpectatorToken,
227 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
228 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
231 auth: arvadostest.SpectatorToken,
232 host: arvadostest.HelloWorldCollection + "--collections.example.com",
233 path: "/Hello%20world.txt",
234 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
237 auth: arvadostest.SpectatorToken,
238 path: "/collections/download/" + arvadostest.HelloWorldCollection + "/" + arvadostest.SpectatorToken + "/Hello%20world.txt",
239 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
244 host = "collections.example.com"
246 hdr, body, _ := s.runCurl(c, spec.auth, host, spec.path)
247 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
248 if strings.HasSuffix(spec.path, ".txt") {
249 c.Check(hdr, check.Matches, `(?s).*\r\nContent-Type: text/plain.*`)
250 // TODO: Check some types that aren't
251 // automatically detected by Go's http server
252 // by sniffing the content.
254 c.Check(fmt.Sprintf("%x", md5.Sum([]byte(body))), check.Equals, spec.dataMD5)
258 // Return header block and body.
259 func (s *IntegrationSuite) runCurl(c *check.C, token, host, uri string, args ...string) (hdr, bodyPart string, bodySize int64) {
260 curlArgs := []string{"--silent", "--show-error", "--include"}
261 testHost, testPort, _ := net.SplitHostPort(s.testServer.Addr)
262 curlArgs = append(curlArgs, "--resolve", host+":"+testPort+":"+testHost)
264 curlArgs = append(curlArgs, "-H", "Authorization: OAuth2 "+token)
266 curlArgs = append(curlArgs, args...)
267 curlArgs = append(curlArgs, "http://"+host+":"+testPort+uri)
268 c.Log(fmt.Sprintf("curlArgs == %#v", curlArgs))
269 cmd := exec.Command("curl", curlArgs...)
270 stdout, err := cmd.StdoutPipe()
271 c.Assert(err, check.IsNil)
272 cmd.Stderr = os.Stderr
274 c.Assert(err, check.IsNil)
275 buf := make([]byte, 2<<27)
276 n, err := io.ReadFull(stdout, buf)
277 // Discard (but measure size of) anything past 128 MiB.
279 if err == io.ErrUnexpectedEOF {
282 c.Assert(err, check.IsNil)
283 discarded, err = io.Copy(ioutil.Discard, stdout)
284 c.Assert(err, check.IsNil)
287 // Without "-f", curl exits 0 as long as it gets a valid HTTP
288 // response from the server, even if the response status
289 // indicates that the request failed. In our test suite, we
290 // always expect a valid HTTP response, and we parse the
291 // headers ourselves. If curl exits non-zero, our testing
292 // environment is broken.
293 c.Assert(err, check.Equals, nil)
294 hdrsAndBody := strings.SplitN(string(buf), "\r\n\r\n", 2)
295 c.Assert(len(hdrsAndBody), check.Equals, 2)
297 bodyPart = hdrsAndBody[1]
298 bodySize = int64(len(bodyPart)) + discarded
302 func (s *IntegrationSuite) TestMetrics(c *check.C) {
303 s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = s.testServer.Addr
304 origin := "http://" + s.testServer.Addr
305 req, _ := http.NewRequest("GET", origin+"/notfound", nil)
306 _, err := http.DefaultClient.Do(req)
307 c.Assert(err, check.IsNil)
308 req, _ = http.NewRequest("GET", origin+"/by_id/", nil)
309 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
310 resp, err := http.DefaultClient.Do(req)
311 c.Assert(err, check.IsNil)
312 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
313 for i := 0; i < 2; i++ {
314 req, _ = http.NewRequest("GET", origin+"/foo", nil)
315 req.Host = arvadostest.FooCollection + ".example.com"
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 buf, _ := ioutil.ReadAll(resp.Body)
321 c.Check(buf, check.DeepEquals, []byte("foo"))
325 s.testServer.Config.Cache.updateGauges()
327 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
328 resp, err = http.DefaultClient.Do(req)
329 c.Assert(err, check.IsNil)
330 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
332 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
333 req.Header.Set("Authorization", "Bearer badtoken")
334 resp, err = http.DefaultClient.Do(req)
335 c.Assert(err, check.IsNil)
336 c.Check(resp.StatusCode, check.Equals, http.StatusForbidden)
338 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
339 req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
340 resp, err = http.DefaultClient.Do(req)
341 c.Assert(err, check.IsNil)
342 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
343 type summary struct {
344 SampleCount string `json:"sample_count"`
345 SampleSum float64 `json:"sample_sum"`
351 type counter struct {
371 json.NewDecoder(resp.Body).Decode(&ents)
372 summaries := map[string]summary{}
373 gauges := map[string]gauge{}
374 counters := map[string]counter{}
375 for _, e := range ents {
376 for _, m := range e.Metric {
377 labels := map[string]string{}
378 for _, lbl := range m.Label {
379 labels[lbl.Name] = lbl.Value
381 summaries[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Summary
382 counters[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Counter
383 gauges[e.Name+"/"+labels["method"]+"/"+labels["code"]] = m.Gauge
386 c.Check(summaries["request_duration_seconds/get/200"].SampleSum, check.Not(check.Equals), 0)
387 c.Check(summaries["request_duration_seconds/get/200"].SampleCount, check.Equals, "3")
388 c.Check(summaries["request_duration_seconds/get/404"].SampleCount, check.Equals, "1")
389 c.Check(summaries["time_to_status_seconds/get/404"].SampleCount, check.Equals, "1")
390 c.Check(counters["arvados_keepweb_collectioncache_requests//"].Value, check.Equals, int64(2))
391 c.Check(counters["arvados_keepweb_collectioncache_api_calls//"].Value, check.Equals, int64(1))
392 c.Check(counters["arvados_keepweb_collectioncache_hits//"].Value, check.Equals, int64(1))
393 c.Check(counters["arvados_keepweb_collectioncache_pdh_hits//"].Value, check.Equals, int64(1))
394 c.Check(counters["arvados_keepweb_collectioncache_permission_hits//"].Value, check.Equals, int64(1))
395 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifests//"].Value, check.Equals, float64(1))
396 // FooCollection's cached manifest size is 45 ("1f4b0....+45") plus one 51-byte blob signature
397 c.Check(gauges["arvados_keepweb_collectioncache_cached_manifest_bytes//"].Value, check.Equals, float64(45+51))
399 // If the Host header indicates a collection, /metrics.json
400 // refers to a file in the collection -- the metrics handler
401 // must not intercept that route.
402 req, _ = http.NewRequest("GET", origin+"/metrics.json", nil)
403 req.Host = strings.Replace(arvadostest.FooCollectionPDH, "+", "-", -1) + ".example.com"
404 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
405 resp, err = http.DefaultClient.Do(req)
406 c.Assert(err, check.IsNil)
407 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
410 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
411 arvadostest.StartAPI()
412 arvadostest.StartKeep(2, true)
414 arv, err := arvadosclient.MakeArvadosClient()
415 c.Assert(err, check.Equals, nil)
416 arv.ApiToken = arvadostest.ActiveToken
417 kc, err := keepclient.MakeKeepClient(arv)
418 c.Assert(err, check.Equals, nil)
419 kc.PutB([]byte("Hello world\n"))
420 kc.PutB([]byte("foo"))
421 kc.PutB([]byte("foobar"))
422 kc.PutB([]byte("waz"))
425 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
426 arvadostest.StopKeep(2)
427 arvadostest.StopAPI()
430 func (s *IntegrationSuite) SetUpTest(c *check.C) {
431 arvadostest.ResetEnv()
432 ldr := config.NewLoader(bytes.NewBufferString("Clusters: {zzzzz: {}}"), nil)
434 arvCfg, err := ldr.Load()
435 c.Check(err, check.IsNil)
436 cfg := newConfig(arvCfg)
437 c.Assert(err, check.IsNil)
438 cfg.Client = arvados.Client{
439 APIHost: testAPIHost,
442 listen := "127.0.0.1:0"
443 cfg.cluster.Services.WebDAV.InternalURLs[arvados.URL{Host: listen}] = arvados.ServiceInstance{}
444 cfg.cluster.Services.WebDAVDownload.InternalURLs[arvados.URL{Host: listen}] = arvados.ServiceInstance{}
445 cfg.cluster.ManagementToken = arvadostest.ManagementToken
446 cfg.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
447 s.testServer = &server{Config: cfg}
448 err = s.testServer.Start()
449 c.Assert(err, check.Equals, nil)
452 func (s *IntegrationSuite) TearDownTest(c *check.C) {
454 if s.testServer != nil {
455 err = s.testServer.Close()
457 c.Check(err, check.Equals, nil)
460 // Gocheck boilerplate
461 func Test(t *testing.T) {