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