1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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"
25 var testAPIHost = os.Getenv("ARVADOS_API_HOST")
27 var _ = check.Suite(&IntegrationSuite{})
29 // IntegrationSuite tests need an API server and a keep-web server
30 type IntegrationSuite struct {
34 func (s *IntegrationSuite) TestNoToken(c *check.C) {
35 for _, token := range []string{
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, "")
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, "")
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, "")
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)
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",
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.*")
84 c.Check(body, check.Equals, "404 page not found\n")
89 func (s *IntegrationSuite) Test1GBFile(c *check.C) {
91 c.Skip("skipping 1GB integration test in short mode")
93 s.test100BlockFile(c, 10000000)
96 func (s *IntegrationSuite) Test100BlockFile(c *check.C) {
99 s.test100BlockFile(c, 30000)
102 s.test100BlockFile(c, 3000000)
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(' ')
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)
119 for i := 0; i < 100; i++ {
120 mtext = mtext + " " + loc
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,
131 c.Assert(err, check.Equals, nil)
132 uuid := coll["uuid"].(string)
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)
141 type curlCase struct {
148 func (s *IntegrationSuite) Test200(c *check.C) {
149 s.testServer.Config.AnonymousTokens = []string{arvadostest.AnonymousToken}
150 for _, spec := range []curlCase{
153 auth: arvadostest.ActiveToken,
154 host: arvadostest.FooCollection + "--collections.example.com",
156 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
159 auth: arvadostest.ActiveToken,
160 host: arvadostest.FooCollection + ".collections.example.com",
162 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
165 host: strings.Replace(arvadostest.FooPdh, "+", "-", 1) + ".collections.example.com",
166 path: "/t=" + arvadostest.ActiveToken + "/foo",
167 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
170 path: "/c=" + arvadostest.FooPdh + "/t=" + arvadostest.ActiveToken + "/foo",
171 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
174 path: "/c=" + strings.Replace(arvadostest.FooPdh, "+", "-", 1) + "/t=" + arvadostest.ActiveToken + "/_/foo",
175 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
178 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
179 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
182 auth: "tokensobogus",
183 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
184 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
187 auth: arvadostest.ActiveToken,
188 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
189 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
192 auth: arvadostest.AnonymousToken,
193 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
194 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
197 // Anonymously accessible data
199 path: "/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
200 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
203 host: arvadostest.HelloWorldCollection + ".collections.example.com",
204 path: "/Hello%20world.txt",
205 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
208 host: arvadostest.HelloWorldCollection + ".collections.example.com",
209 path: "/_/Hello%20world.txt",
210 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
213 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
214 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
217 auth: arvadostest.ActiveToken,
218 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
219 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
222 auth: arvadostest.SpectatorToken,
223 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
224 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
227 auth: arvadostest.SpectatorToken,
228 host: arvadostest.HelloWorldCollection + "--collections.example.com",
229 path: "/Hello%20world.txt",
230 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
233 auth: arvadostest.SpectatorToken,
234 path: "/collections/download/" + arvadostest.HelloWorldCollection + "/" + arvadostest.SpectatorToken + "/Hello%20world.txt",
235 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
240 host = "collections.example.com"
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.
250 c.Check(fmt.Sprintf("%x", md5.Sum([]byte(body))), check.Equals, spec.dataMD5)
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)
260 curlArgs = append(curlArgs, "-H", "Authorization: OAuth2 "+token)
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
270 buf := make([]byte, 2<<27)
271 n, err := io.ReadFull(stdout, buf)
272 // Discard (but measure size of) anything past 128 MiB.
274 if err == io.ErrUnexpectedEOF {
277 c.Assert(err, check.Equals, nil)
278 discarded, err = io.Copy(ioutil.Discard, stdout)
279 c.Assert(err, check.Equals, nil)
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)
292 bodyPart = hdrsAndBody[1]
293 bodySize = int64(len(bodyPart)) + discarded
297 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
298 arvadostest.StartAPI()
299 arvadostest.StartKeep(2, true)
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"))
311 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
312 arvadostest.StopKeep(2)
313 arvadostest.StopAPI()
316 func (s *IntegrationSuite) SetUpTest(c *check.C) {
317 arvadostest.ResetEnv()
318 cfg := DefaultConfig()
319 cfg.Client = arvados.Client{
320 APIHost: testAPIHost,
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)
329 func (s *IntegrationSuite) TearDownTest(c *check.C) {
331 if s.testServer != nil {
332 err = s.testServer.Close()
334 c.Check(err, check.Equals, nil)
337 // Gocheck boilerplate
338 func Test(t *testing.T) {