1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/sdk/go/arvados"
16 "git.arvados.org/arvados.git/sdk/go/arvadostest"
17 "github.com/prometheus/common/expfmt"
21 func (s *IntegrationSuite) checkCacheMetrics(c *check.C, regs ...string) {
22 s.handler.Cache.updateGauges()
23 reg := s.handler.Cache.registry
24 mfs, err := reg.Gather()
25 c.Check(err, check.IsNil)
26 buf := &bytes.Buffer{}
27 enc := expfmt.NewEncoder(buf, expfmt.FmtText)
28 for _, mf := range mfs {
29 c.Check(enc.Encode(mf), check.IsNil)
32 // Remove comments to make the "value vs. regexp" failure
33 // output easier to read.
34 mm = regexp.MustCompile(`(?m)^#.*\n`).ReplaceAllString(mm, "")
35 for _, reg := range regs {
36 c.Check(mm, check.Matches, `(?ms).*keepweb_sessions_`+reg+`\n.*`)
40 func (s *IntegrationSuite) TestCache(c *check.C) {
41 // Hit the same collection 5 times using the same token. Only
42 // the first req should cause an API call; the next 4 should
44 u := mustParseURL("http://" + arvadostest.FooCollection + ".keep-web.example/foo")
49 RequestURI: u.RequestURI(),
51 "Authorization": {"Bearer " + arvadostest.ActiveToken},
54 for i := 0; i < 5; i++ {
55 resp := httptest.NewRecorder()
56 s.handler.ServeHTTP(resp, req)
57 c.Check(resp.Code, check.Equals, http.StatusOK)
59 s.checkCacheMetrics(c,
64 // Hit a shared collection 3 times using PDH, using a
66 u2 := mustParseURL("http://" + strings.Replace(arvadostest.BarFileCollectionPDH, "+", "-", 1) + ".keep-web.example/bar")
67 req2 := &http.Request{
71 RequestURI: u2.RequestURI(),
73 "Authorization": {"Bearer " + arvadostest.SpectatorToken},
76 for i := 0; i < 3; i++ {
77 resp2 := httptest.NewRecorder()
78 s.handler.ServeHTTP(resp2, req2)
79 c.Check(resp2.Code, check.Equals, http.StatusOK)
81 s.checkCacheMetrics(c,
86 // Alternating between two collections/tokens N times should
87 // use the existing sessions.
88 for i := 0; i < 7; i++ {
89 resp := httptest.NewRecorder()
90 s.handler.ServeHTTP(resp, req)
91 c.Check(resp.Code, check.Equals, http.StatusOK)
93 resp2 := httptest.NewRecorder()
94 s.handler.ServeHTTP(resp2, req2)
95 c.Check(resp2.Code, check.Equals, http.StatusOK)
97 s.checkCacheMetrics(c,
103 func (s *IntegrationSuite) TestForceReloadPDH(c *check.C) {
104 filename := strings.Replace(time.Now().Format(time.RFC3339Nano), ":", ".", -1)
105 manifest := ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:" + filename + "\n"
106 pdh := arvados.PortableDataHash(manifest)
107 client := arvados.NewClientFromEnv()
108 client.AuthToken = arvadostest.ActiveToken
110 _, resp := s.do("GET", "http://"+strings.Replace(pdh, "+", "-", 1)+".keep-web.example/"+filename, arvadostest.ActiveToken, nil)
111 c.Check(resp.Code, check.Equals, http.StatusNotFound)
113 var coll arvados.Collection
114 err := client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
115 "collection": map[string]string{
116 "manifest_text": manifest,
119 c.Assert(err, check.IsNil)
120 defer client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+coll.UUID, nil, nil)
121 c.Assert(coll.PortableDataHash, check.Equals, pdh)
123 _, resp = s.do("GET", "http://"+strings.Replace(pdh, "+", "-", 1)+".keep-web.example/"+filename, "", http.Header{
124 "Authorization": {"Bearer " + arvadostest.ActiveToken},
125 "Cache-Control": {"must-revalidate"},
127 c.Check(resp.Code, check.Equals, http.StatusOK)
129 _, resp = s.do("GET", "http://"+strings.Replace(pdh, "+", "-", 1)+".keep-web.example/missingfile", "", http.Header{
130 "Authorization": {"Bearer " + arvadostest.ActiveToken},
131 "Cache-Control": {"must-revalidate"},
133 c.Check(resp.Code, check.Equals, http.StatusNotFound)
136 func (s *IntegrationSuite) TestForceReloadUUID(c *check.C) {
137 client := arvados.NewClientFromEnv()
138 client.AuthToken = arvadostest.ActiveToken
139 var coll arvados.Collection
140 err := client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
141 "collection": map[string]string{
142 "manifest_text": ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:oldfile\n",
145 c.Assert(err, check.IsNil)
146 defer client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+coll.UUID, nil, nil)
148 _, resp := s.do("GET", "http://"+coll.UUID+".keep-web.example/newfile", arvadostest.ActiveToken, nil)
149 c.Check(resp.Code, check.Equals, http.StatusNotFound)
150 _, resp = s.do("GET", "http://"+coll.UUID+".keep-web.example/oldfile", arvadostest.ActiveToken, nil)
151 c.Check(resp.Code, check.Equals, http.StatusOK)
152 _, resp = s.do("GET", "http://"+coll.UUID+".keep-web.example/newfile", arvadostest.ActiveToken, nil)
153 c.Check(resp.Code, check.Equals, http.StatusNotFound)
154 err = client.RequestAndDecode(&coll, "PATCH", "arvados/v1/collections/"+coll.UUID, nil, map[string]interface{}{
155 "collection": map[string]string{
156 "manifest_text": ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:oldfile 0:0:newfile\n",
159 c.Assert(err, check.IsNil)
160 _, resp = s.do("GET", "http://"+coll.UUID+".keep-web.example/newfile", arvadostest.ActiveToken, nil)
161 c.Check(resp.Code, check.Equals, http.StatusNotFound)
162 _, resp = s.do("GET", "http://"+coll.UUID+".keep-web.example/newfile", "", http.Header{
163 "Authorization": {"Bearer " + arvadostest.ActiveToken},
164 "Cache-Control": {"must-revalidate"},
166 c.Check(resp.Code, check.Equals, http.StatusOK)