13 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
14 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15 "git.curoverse.com/arvados.git/sdk/go/keepclient"
16 check "gopkg.in/check.v1"
19 var _ = check.Suite(&IntegrationSuite{})
21 // IntegrationSuite tests need an API server and a keep-web server
22 type IntegrationSuite struct {
26 func (s *IntegrationSuite) TestNoToken(c *check.C) {
27 for _, token := range []string{
31 hdr, body, _ := s.runCurl(c, token, "collections.example.com", "/collections/"+arvadostest.FooCollection+"/foo")
32 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
33 c.Check(body, check.Equals, "")
36 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/collections/download/"+arvadostest.FooCollection+"/"+token+"/foo")
37 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
38 c.Check(body, check.Equals, "")
41 hdr, body, _ = s.runCurl(c, token, "collections.example.com", "/bad-route")
42 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 404 Not Found\r\n.*`)
43 c.Check(body, check.Equals, "")
47 // TODO: Move most cases to functional tests -- at least use Go's own
48 // http client instead of forking curl. Just leave enough of an
49 // integration test to assure that the documented way of invoking curl
50 // really works against the server.
51 func (s *IntegrationSuite) Test404(c *check.C) {
52 for _, uri := range []string{
53 // Routing errors (always 404 regardless of what's stored in Keep)
59 // Implicit/generated index is not implemented yet;
60 // until then, return 404.
61 "/collections/" + arvadostest.FooCollection,
62 "/collections/" + arvadostest.FooCollection + "/",
63 "/collections/" + arvadostest.FooBarDirCollection + "/dir1",
64 "/collections/" + arvadostest.FooBarDirCollection + "/dir1/",
65 // Non-existent file in collection
66 "/collections/" + arvadostest.FooCollection + "/theperthcountyconspiracy",
67 "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
68 // Non-existent collection
69 "/collections/" + arvadostest.NonexistentCollection,
70 "/collections/" + arvadostest.NonexistentCollection + "/",
71 "/collections/" + arvadostest.NonexistentCollection + "/theperthcountyconspiracy",
72 "/collections/download/" + arvadostest.NonexistentCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
74 hdr, body, _ := s.runCurl(c, arvadostest.ActiveToken, "collections.example.com", uri)
75 c.Check(hdr, check.Matches, "(?s)HTTP/1.1 404 Not Found\r\n.*")
76 c.Check(body, check.Equals, "")
80 func (s *IntegrationSuite) Test1GBFile(c *check.C) {
82 c.Skip("skipping 1GB integration test in short mode")
84 s.test100BlockFile(c, 10000000)
87 func (s *IntegrationSuite) Test100BlockFile(c *check.C) {
90 s.test100BlockFile(c, 30000)
93 s.test100BlockFile(c, 3000000)
97 func (s *IntegrationSuite) test100BlockFile(c *check.C, blocksize int) {
98 testdata := make([]byte, blocksize)
99 for i := 0; i < blocksize; i++ {
100 testdata[i] = byte(' ')
102 arv, err := arvadosclient.MakeArvadosClient()
103 c.Assert(err, check.Equals, nil)
104 arv.ApiToken = arvadostest.ActiveToken
105 kc, err := keepclient.MakeKeepClient(&arv)
106 c.Assert(err, check.Equals, nil)
107 loc, _, err := kc.PutB(testdata[:])
108 c.Assert(err, check.Equals, nil)
110 for i := 0; i < 100; i++ {
111 mtext = mtext + " " + loc
113 mtext = mtext + fmt.Sprintf(" 0:%d00:testdata.bin\n", blocksize)
114 coll := map[string]interface{}{}
115 err = arv.Create("collections",
116 map[string]interface{}{
117 "collection": map[string]interface{}{
118 "name": fmt.Sprintf("testdata blocksize=%d", blocksize),
119 "manifest_text": mtext,
122 c.Assert(err, check.Equals, nil)
123 uuid := coll["uuid"].(string)
125 hdr, body, size := s.runCurl(c, arv.ApiToken, uuid+".collections.example.com", "/testdata.bin")
126 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
127 c.Check(hdr, check.Matches, `(?si).*Content-length: `+fmt.Sprintf("%d00", blocksize)+`\r\n.*`)
128 c.Check([]byte(body)[:1234], check.DeepEquals, testdata[:1234])
129 c.Check(size, check.Equals, int64(blocksize)*100)
132 type curlCase struct {
139 func (s *IntegrationSuite) Test200(c *check.C) {
140 anonymousTokens = []string{arvadostest.AnonymousToken}
141 for _, spec := range []curlCase{
144 auth: arvadostest.ActiveToken,
145 host: arvadostest.FooCollection + "--collections.example.com",
147 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
150 auth: arvadostest.ActiveToken,
151 host: arvadostest.FooCollection + ".collections.example.com",
153 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
156 host: strings.Replace(arvadostest.FooPdh, "+", "-", 1) + ".collections.example.com",
157 path: "/t=" + arvadostest.ActiveToken + "/foo",
158 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
161 path: "/c=" + arvadostest.FooPdh + "/t=" + arvadostest.ActiveToken + "/foo",
162 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
165 path: "/c=" + strings.Replace(arvadostest.FooPdh, "+", "-", 1) + "/t=" + arvadostest.ActiveToken + "/_/foo",
166 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
169 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
170 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
173 auth: "tokensobogus",
174 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
175 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
178 auth: arvadostest.ActiveToken,
179 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
180 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
183 auth: arvadostest.AnonymousToken,
184 path: "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/foo",
185 dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
188 // Anonymously accessible data
190 path: "/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
191 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
194 host: arvadostest.HelloWorldCollection + ".collections.example.com",
195 path: "/Hello%20world.txt",
196 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
199 host: arvadostest.HelloWorldCollection + ".collections.example.com",
200 path: "/_/Hello%20world.txt",
201 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
204 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
205 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
208 auth: arvadostest.ActiveToken,
209 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
210 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
213 auth: arvadostest.SpectatorToken,
214 path: "/collections/" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
215 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
218 auth: arvadostest.SpectatorToken,
219 host: arvadostest.HelloWorldCollection + "--collections.example.com",
220 path: "/Hello%20world.txt",
221 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
224 auth: arvadostest.SpectatorToken,
225 path: "/collections/download/" + arvadostest.HelloWorldCollection + "/" + arvadostest.SpectatorToken + "/Hello%20world.txt",
226 dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",
231 host = "collections.example.com"
233 hdr, body, _ := s.runCurl(c, spec.auth, host, spec.path)
234 c.Check(hdr, check.Matches, `(?s)HTTP/1.1 200 OK\r\n.*`)
235 if strings.HasSuffix(spec.path, ".txt") {
236 c.Check(hdr, check.Matches, `(?s).*\r\nContent-Type: text/plain.*`)
237 // TODO: Check some types that aren't
238 // automatically detected by Go's http server
239 // by sniffing the content.
241 c.Check(fmt.Sprintf("%x", md5.Sum([]byte(body))), check.Equals, spec.dataMD5)
245 // Return header block and body.
246 func (s *IntegrationSuite) runCurl(c *check.C, token, host, uri string, args ...string) (hdr, bodyPart string, bodySize int64) {
247 curlArgs := []string{"--silent", "--show-error", "--include"}
248 testHost, testPort, _ := net.SplitHostPort(s.testServer.Addr)
249 curlArgs = append(curlArgs, "--resolve", host+":"+testPort+":"+testHost)
251 curlArgs = append(curlArgs, "-H", "Authorization: OAuth2 "+token)
253 curlArgs = append(curlArgs, args...)
254 curlArgs = append(curlArgs, "http://"+host+":"+testPort+uri)
255 c.Log(fmt.Sprintf("curlArgs == %#v", curlArgs))
256 cmd := exec.Command("curl", curlArgs...)
257 stdout, err := cmd.StdoutPipe()
258 c.Assert(err, check.Equals, nil)
259 cmd.Stderr = cmd.Stdout
261 buf := make([]byte, 2<<27)
262 n, err := io.ReadFull(stdout, buf)
263 // Discard (but measure size of) anything past 128 MiB.
265 if err == io.ErrUnexpectedEOF {
269 c.Assert(err, check.Equals, nil)
270 discarded, err = io.Copy(ioutil.Discard, stdout)
271 c.Assert(err, check.Equals, nil)
274 // Without "-f", curl exits 0 as long as it gets a valid HTTP
275 // response from the server, even if the response status
276 // indicates that the request failed. In our test suite, we
277 // always expect a valid HTTP response, and we parse the
278 // headers ourselves. If curl exits non-zero, our testing
279 // environment is broken.
280 c.Assert(err, check.Equals, nil)
281 hdrsAndBody := strings.SplitN(string(buf), "\r\n\r\n", 2)
282 c.Assert(len(hdrsAndBody), check.Equals, 2)
284 bodyPart = hdrsAndBody[1]
285 bodySize = int64(len(bodyPart)) + discarded
289 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
290 arvadostest.StartAPI()
291 arvadostest.StartKeep(2, true)
293 arv, err := arvadosclient.MakeArvadosClient()
294 c.Assert(err, check.Equals, nil)
295 arv.ApiToken = arvadostest.ActiveToken
296 kc, err := keepclient.MakeKeepClient(&arv)
297 c.Assert(err, check.Equals, nil)
298 kc.PutB([]byte("Hello world\n"))
299 kc.PutB([]byte("foo"))
300 kc.PutB([]byte("foobar"))
303 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
304 arvadostest.StopKeep(2)
305 arvadostest.StopAPI()
308 func (s *IntegrationSuite) SetUpTest(c *check.C) {
309 arvadostest.ResetEnv()
310 s.testServer = &server{}
312 address = "127.0.0.1:0"
313 err = s.testServer.Start()
314 c.Assert(err, check.Equals, nil)
317 func (s *IntegrationSuite) TearDownTest(c *check.C) {
319 if s.testServer != nil {
320 err = s.testServer.Close()
322 c.Check(err, check.Equals, nil)
325 // Gocheck boilerplate
326 func Test(t *testing.T) {