12708: Merge branch 'master' into 12708-balance-storage-classes
[arvados.git] / services / keep-web / server_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "crypto/md5"
9         "fmt"
10         "io"
11         "io/ioutil"
12         "net"
13         "os"
14         "os/exec"
15         "strings"
16         "testing"
17
18         "git.curoverse.com/arvados.git/sdk/go/arvados"
19         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
20         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
21         "git.curoverse.com/arvados.git/sdk/go/keepclient"
22         check "gopkg.in/check.v1"
23 )
24
25 var testAPIHost = os.Getenv("ARVADOS_API_HOST")
26
27 var _ = check.Suite(&IntegrationSuite{})
28
29 // IntegrationSuite tests need an API server and a keep-web server
30 type IntegrationSuite struct {
31         testServer *server
32 }
33
34 func (s *IntegrationSuite) TestNoToken(c *check.C) {
35         for _, token := range []string{
36                 "",
37                 "bogustoken",
38         } {
39                 hdr, body, _ := s.runCurl(c, token, "collections.example.com", "/collections/"+arvadostest.FooCollection+"/foo")
40                 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
41                 c.Check(body, check.Equals, "")
42
43                 if token != "" {
44                         hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/collections/download/"+arvadostest.FooCollection+"/"+token+"/foo")
45                         c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
46                         c.Check(body, check.Equals, "")
47                 }
48
49                 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/bad-route")
50                 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
51                 c.Check(body, check.Equals, "")
52         }
53 }
54
55 // TODO: Move most cases to functional tests -- at least use Go's own
56 // http client instead of forking curl. Just leave enough of an
57 // integration test to assure that the documented way of invoking curl
58 // really works against the server.
59 func (s *IntegrationSuite) Test404(c *check.C) {
60         for _, uri := range []string{
61                 // Routing errors (always 404 regardless of what's stored in Keep)
62                 "/foo",
63                 "/download",
64                 "/collections",
65                 "/collections/",
66                 // Implicit/generated index is not implemented yet;
67                 // until then, return 404.
68                 "/collections/" + arvadostest.FooCollection,
69                 "/collections/" + arvadostest.FooCollection + "/",
70                 "/collections/" + arvadostest.FooBarDirCollection + "/dir1",
71                 "/collections/" + arvadostest.FooBarDirCollection + "/dir1/",
72                 // Non-existent file in collection
73                 "/collections/" + arvadostest.FooCollection + "/theperthcountyconspiracy",
74                 "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
75                 // Non-existent collection
76                 "/collections/" + arvadostest.NonexistentCollection,
77                 "/collections/" + arvadostest.NonexistentCollection + "/",
78                 "/collections/" + arvadostest.NonexistentCollection + "/theperthcountyconspiracy",
79                 "/collections/download/" + arvadostest.NonexistentCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
80         } {
81                 hdr, body, _ := s.runCurl(c, arvadostest.ActiveToken, "collections.example.com", uri)
82                 c.Check(hdr, check.Matches, "(?s)HTTP/1.1 404 Not Found\r\n.*")
83                 if len(body) > 0 {
84                         c.Check(body, check.Equals, "404 page not found\n")
85                 }
86         }
87 }
88
89 func (s *IntegrationSuite) Test1GBFile(c *check.C) {
90         if testing.Short() {
91                 c.Skip("skipping 1GB integration test in short mode")
92         }
93         s.test100BlockFile(c, 10000000)
94 }
95
96 func (s *IntegrationSuite) Test100BlockFile(c *check.C) {
97         if testing.Short() {
98                 // 3 MB
99                 s.test100BlockFile(c, 30000)
100         } else {
101                 // 300 MB
102                 s.test100BlockFile(c, 3000000)
103         }
104 }
105
106 func (s *IntegrationSuite) test100BlockFile(c *check.C, blocksize int) {
107         testdata := make([]byte, blocksize)
108         for i := 0; i < blocksize; i++ {
109                 testdata[i] = byte(' ')
110         }
111         arv, err := arvadosclient.MakeArvadosClient()
112         c.Assert(err, check.Equals, nil)
113         arv.ApiToken = arvadostest.ActiveToken
114         kc, err := keepclient.MakeKeepClient(arv)
115         c.Assert(err, check.Equals, nil)
116         loc, _, err := kc.PutB(testdata[:])
117         c.Assert(err, check.Equals, nil)
118         mtext := "."
119         for i := 0; i < 100; i++ {
120                 mtext = mtext + " " + loc
121         }
122         mtext = mtext + fmt.Sprintf(" 0:%d00:testdata.bin\n", blocksize)
123         coll := map[string]interface{}{}
124         err = arv.Create("collections",
125                 map[string]interface{}{
126                         "collection": map[string]interface{}{
127                                 "name":          fmt.Sprintf("testdata blocksize=%d", blocksize),
128                                 "manifest_text": mtext,
129                         },
130                 }, &coll)
131         c.Assert(err, check.Equals, nil)
132         uuid := coll["uuid"].(string)
133
134         hdr, body, size := s.runCurl(c, arv.ApiToken, uuid+".collections.example.com", "/testdata.bin")
135         c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
136         c.Check(hdr, check.Matches, `(?si).*Content-length: `+fmt.Sprintf("%d00", blocksize)+`\r\n.*`)
137         c.Check([]byte(body)[:1234], check.DeepEquals, testdata[:1234])
138         c.Check(size, check.Equals, int64(blocksize)*100)
139 }
140
141 type curlCase struct {
142         auth    string
143         host    string
144         path    string
145         dataMD5 string
146 }
147
148 func (s *IntegrationSuite) Test200(c *check.C) {
149         s.testServer.Config.AnonymousTokens = []string{arvadostest.AnonymousToken}
150         for _, spec := range []curlCase{
151                 // My collection
152                 {
153                         auth:    arvadostest.ActiveToken,
154                         host:    arvadostest.FooCollection + "--collections.example.com",
155                         path:    "/foo",
156                         dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
157                 },
158                 {
159                         auth:    arvadostest.ActiveToken,
160                         host:    arvadostest.FooCollection + ".collections.example.com",
161                         path:    "/foo",
162                         dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
163                 },
164                 {
165                         host:    strings.Replace(arvadostest.FooPdh, "+", "-", 1) + ".collections.example.com",
166                         path:    "/t=" + arvadostest.ActiveToken + "/foo",
167                         dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
168                 },
169                 {
170                         path:    "/c=" + arvadostest.FooPdh + "/t=" + arvadostest.ActiveToken + "/foo",
171                         dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
172                 },
173                 {
174                         path:    "/c=" + strings.Replace(arvadostest.FooPdh, "+", "-", 1) + "/t=" + arvadostest.ActiveToken + "/_/foo",
175                         dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
176                 },
177                 {
178                         path:    "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
179                         dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
180                 },
181                 {
182                         auth:    "tokensobogus",
183                         path:    "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
184                         dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
185                 },
186                 {
187                         auth:    arvadostest.ActiveToken,
188                         path:    "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
189                         dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
190                 },
191                 {
192                         auth:    arvadostest.AnonymousToken,
193                         path:    "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
194                         dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
195                 },
196
197                 // Anonymously accessible data
198                 {
199                         path:    "/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
200                         dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
201                 },
202                 {
203                         host:    arvadostest.HelloWorldCollection + ".collections.example.com",
204                         path:    "/Hello%20world.txt",
205                         dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
206                 },
207                 {
208                         host:    arvadostest.HelloWorldCollection + ".collections.example.com",
209                         path:    "/_/Hello%20world.txt",
210                         dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
211                 },
212                 {
213                         path:    "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
214                         dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
215                 },
216                 {
217                         auth:    arvadostest.ActiveToken,
218                         path:    "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
219                         dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
220                 },
221                 {
222                         auth:    arvadostest.SpectatorToken,
223                         path:    "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
224                         dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
225                 },
226                 {
227                         auth:    arvadostest.SpectatorToken,
228                         host:    arvadostest.HelloWorldCollection + "--collections.example.com",
229                         path:    "/Hello%20world.txt",
230                         dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
231                 },
232                 {
233                         auth:    arvadostest.SpectatorToken,
234                         path:    "/collections/download/" + arvadostest.HelloWorldCollection + "/" + arvadostest.SpectatorToken + "/Hello%20world.txt",
235                         dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
236                 },
237         } {
238                 host := spec.host
239                 if host == "" {
240                         host = "collections.example.com"
241                 }
242                 hdr, body, _ := s.runCurl(c, spec.auth, host, spec.path)
243                 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
244                 if strings.HasSuffix(spec.path, ".txt") {
245                         c.Check(hdr, check.Matches, `(?s).*\r\nContent-Type: text/plain.*`)
246                         // TODO: Check some types that aren't
247                         // automatically detected by Go's http server
248                         // by sniffing the content.
249                 }
250                 c.Check(fmt.Sprintf("%x", md5.Sum([]byte(body))), check.Equals, spec.dataMD5)
251         }
252 }
253
254 // Return header block and body.
255 func (s *IntegrationSuite) runCurl(c *check.C, token, host, uri string, args ...string) (hdr, bodyPart string, bodySize int64) {
256         curlArgs := []string{"--silent", "--show-error", "--include"}
257         testHost, testPort, _ := net.SplitHostPort(s.testServer.Addr)
258         curlArgs = append(curlArgs, "--resolve", host+":"+testPort+":"+testHost)
259         if token != "" {
260                 curlArgs = append(curlArgs, "-H", "Authorization: OAuth2 "+token)
261         }
262         curlArgs = append(curlArgs, args...)
263         curlArgs = append(curlArgs, "http://"+host+":"+testPort+uri)
264         c.Log(fmt.Sprintf("curlArgs == %#v", curlArgs))
265         cmd := exec.Command("curl", curlArgs...)
266         stdout, err := cmd.StdoutPipe()
267         c.Assert(err, check.Equals, nil)
268         cmd.Stderr = cmd.Stdout
269         go cmd.Start()
270         buf := make([]byte, 2<<27)
271         n, err := io.ReadFull(stdout, buf)
272         // Discard (but measure size of) anything past 128 MiB.
273         var discarded int64
274         if err == io.ErrUnexpectedEOF {
275                 buf = buf[:n]
276         } else {
277                 c.Assert(err, check.Equals, nil)
278                 discarded, err = io.Copy(ioutil.Discard, stdout)
279                 c.Assert(err, check.Equals, nil)
280         }
281         err = cmd.Wait()
282         // Without "-f", curl exits 0 as long as it gets a valid HTTP
283         // response from the server, even if the response status
284         // indicates that the request failed. In our test suite, we
285         // always expect a valid HTTP response, and we parse the
286         // headers ourselves. If curl exits non-zero, our testing
287         // environment is broken.
288         c.Assert(err, check.Equals, nil)
289         hdrsAndBody := strings.SplitN(string(buf), "\r\n\r\n", 2)
290         c.Assert(len(hdrsAndBody), check.Equals, 2)
291         hdr = hdrsAndBody[0]
292         bodyPart = hdrsAndBody[1]
293         bodySize = int64(len(bodyPart)) + discarded
294         return
295 }
296
297 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
298         arvadostest.StartAPI()
299         arvadostest.StartKeep(2, true)
300
301         arv, err := arvadosclient.MakeArvadosClient()
302         c.Assert(err, check.Equals, nil)
303         arv.ApiToken = arvadostest.ActiveToken
304         kc, err := keepclient.MakeKeepClient(arv)
305         c.Assert(err, check.Equals, nil)
306         kc.PutB([]byte("Hello world\n"))
307         kc.PutB([]byte("foo"))
308         kc.PutB([]byte("foobar"))
309 }
310
311 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
312         arvadostest.StopKeep(2)
313         arvadostest.StopAPI()
314 }
315
316 func (s *IntegrationSuite) SetUpTest(c *check.C) {
317         arvadostest.ResetEnv()
318         cfg := DefaultConfig()
319         cfg.Client = arvados.Client{
320                 APIHost:  testAPIHost,
321                 Insecure: true,
322         }
323         cfg.Listen = "127.0.0.1:0"
324         s.testServer = &server{Config: cfg}
325         err := s.testServer.Start()
326         c.Assert(err, check.Equals, nil)
327 }
328
329 func (s *IntegrationSuite) TearDownTest(c *check.C) {
330         var err error
331         if s.testServer != nil {
332                 err = s.testServer.Close()
333         }
334         c.Check(err, check.Equals, nil)
335 }
336
337 // Gocheck boilerplate
338 func Test(t *testing.T) {
339         check.TestingT(t)
340 }