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"
21 var testAPIHost = os.Getenv("ARVADOS_API_HOST")
23 var _ = check.Suite(&IntegrationSuite{})
25 // IntegrationSuite tests need an API server and a keep-web server
26 type IntegrationSuite struct {
30 func (s *IntegrationSuite) TestNoToken(c *check.C) {
31 for _, token := range []string{
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, "")
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, "")
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, "")
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)
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",
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, "")
84 func (s *IntegrationSuite) Test1GBFile(c *check.C) {
86 c.Skip("skipping 1GB integration test in short mode")
88 s.test100BlockFile(c, 10000000)
91 func (s *IntegrationSuite) Test100BlockFile(c *check.C) {
94 s.test100BlockFile(c, 30000)
97 s.test100BlockFile(c, 3000000)
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(' ')
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)
114 for i := 0; i < 100; i++ {
115 mtext = mtext + " " + loc
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,
126 c.Assert(err, check.Equals, nil)
127 uuid := coll["uuid"].(string)
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)
136 type curlCase struct {
143 func (s *IntegrationSuite) Test200(c *check.C) {
144 s.testServer.Config.AnonymousTokens = []string{arvadostest.AnonymousToken}
145 for _, spec := range []curlCase{
148 auth: arvadostest.ActiveToken,
149 host: arvadostest.FooCollection + "--collections.example.com",
151 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
154 auth: arvadostest.ActiveToken,
155 host: arvadostest.FooCollection + ".collections.example.com",
157 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
160 host: strings.Replace(arvadostest.FooPdh, "+", "-", 1) + ".collections.example.com",
161 path: "/t=" + arvadostest.ActiveToken + "/foo",
162 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
165 path: "/c=" + arvadostest.FooPdh + "/t=" + arvadostest.ActiveToken + "/foo",
166 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
169 path: "/c=" + strings.Replace(arvadostest.FooPdh, "+", "-", 1) + "/t=" + arvadostest.ActiveToken + "/_/foo",
170 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
173 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
174 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
177 auth: "tokensobogus",
178 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
179 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
182 auth: arvadostest.ActiveToken,
183 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
184 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
187 auth: arvadostest.AnonymousToken,
188 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
189 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
192 // Anonymously accessible data
194 path: "/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
195 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
198 host: arvadostest.HelloWorldCollection + ".collections.example.com",
199 path: "/Hello%20world.txt",
200 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
203 host: arvadostest.HelloWorldCollection + ".collections.example.com",
204 path: "/_/Hello%20world.txt",
205 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
208 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
209 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
212 auth: arvadostest.ActiveToken,
213 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
214 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
217 auth: arvadostest.SpectatorToken,
218 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
219 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
222 auth: arvadostest.SpectatorToken,
223 host: arvadostest.HelloWorldCollection + "--collections.example.com",
224 path: "/Hello%20world.txt",
225 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
228 auth: arvadostest.SpectatorToken,
229 path: "/collections/download/" + arvadostest.HelloWorldCollection + "/" + arvadostest.SpectatorToken + "/Hello%20world.txt",
230 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
235 host = "collections.example.com"
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.
245 c.Check(fmt.Sprintf("%x", md5.Sum([]byte(body))), check.Equals, spec.dataMD5)
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)
255 curlArgs = append(curlArgs, "-H", "Authorization: OAuth2 "+token)
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
265 buf := make([]byte, 2<<27)
266 n, err := io.ReadFull(stdout, buf)
267 // Discard (but measure size of) anything past 128 MiB.
269 if err == io.ErrUnexpectedEOF {
273 c.Assert(err, check.Equals, nil)
274 discarded, err = io.Copy(ioutil.Discard, stdout)
275 c.Assert(err, check.Equals, nil)
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)
288 bodyPart = hdrsAndBody[1]
289 bodySize = int64(len(bodyPart)) + discarded
293 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
294 arvadostest.StartAPI()
295 arvadostest.StartKeep(2, true)
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"))
307 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
308 arvadostest.StopKeep(2)
309 arvadostest.StopAPI()
312 func (s *IntegrationSuite) SetUpTest(c *check.C) {
313 arvadostest.ResetEnv()
314 s.testServer = &server{Config: &Config{
315 Client: arvados.Client{
316 APIHost: testAPIHost,
319 Listen: "127.0.0.1:0",
321 err := s.testServer.Start()
322 c.Assert(err, check.Equals, nil)
325 func (s *IntegrationSuite) TearDownTest(c *check.C) {
327 if s.testServer != nil {
328 err = s.testServer.Close()
330 c.Check(err, check.Equals, nil)
333 // Gocheck boilerplate
334 func Test(t *testing.T) {