7 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
8 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
9 "git.curoverse.com/arvados.git/sdk/go/keepclient"
22 // Gocheck boilerplate
23 func Test(t *testing.T) {
27 // Gocheck boilerplate
28 var _ = Suite(&ServerRequiredSuite{})
30 // Tests that require the Keep server running
31 type ServerRequiredSuite struct{}
33 // Wait (up to 1 second) for keepproxy to listen on a port. This
34 // avoids a race condition where we hit a "connection refused" error
35 // because we start testing the proxy too soon.
36 func waitForListener() {
40 for i := 0; listener == nil && i < 1000; i += ms {
41 time.Sleep(ms * time.Millisecond)
44 log.Fatalf("Timed out waiting for listener to start")
48 func closeListener() {
54 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
55 arvadostest.StartAPI()
56 arvadostest.StartKeep()
59 func (s *ServerRequiredSuite) SetUpTest(c *C) {
60 arvadostest.ResetEnv()
63 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
64 arvadostest.StopKeep()
68 func setupProxyService() {
70 client := &http.Client{Transport: &http.Transport{
71 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
75 if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
78 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
80 reader, writer := io.Pipe()
86 data.Set("keep_service", `{
87 "service_host": "localhost",
88 "service_port": 29950,
89 "service_ssl_flag": false,
90 "service_type": "proxy"
93 writer.Write([]byte(data.Encode()))
97 var resp *http.Response
98 if resp, err = client.Do(req); err != nil {
101 if resp.StatusCode != 200 {
106 func runProxy(c *C, args []string, port int, bogusClientToken bool) keepclient.KeepClient {
107 if bogusClientToken {
108 os.Setenv("ARVADOS_API_TOKEN", "bogus-token")
110 arv, err := arvadosclient.MakeArvadosClient()
111 c.Assert(err, Equals, nil)
112 kc := keepclient.KeepClient{
116 Client: &http.Client{},
118 kc.SetServiceRoots(map[string]string{
119 "proxy": fmt.Sprintf("http://localhost:%v", port),
121 c.Check(kc.Using_proxy, Equals, true)
122 c.Check(len(kc.ServiceRoots()), Equals, 1)
123 for _, root := range kc.ServiceRoots() {
124 c.Check(root, Equals, fmt.Sprintf("http://localhost:%v", port))
126 log.Print("keepclient created")
127 if bogusClientToken {
128 arvadostest.ResetEnv()
132 os.Args = append(args, fmt.Sprintf("-listen=:%v", port))
140 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
141 log.Print("TestPutAndGet start")
143 os.Args = []string{"keepproxy", "-listen=:29950"}
146 time.Sleep(100 * time.Millisecond)
150 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
151 arv, err := arvadosclient.MakeArvadosClient()
152 c.Assert(err, Equals, nil)
153 kc, err := keepclient.MakeKeepClient(&arv)
154 c.Assert(err, Equals, nil)
155 c.Check(kc.Arvados.External, Equals, true)
156 c.Check(kc.Using_proxy, Equals, true)
157 c.Check(len(kc.ServiceRoots()), Equals, 1)
158 for _, root := range kc.ServiceRoots() {
159 c.Check(root, Equals, "http://localhost:29950")
161 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
164 defer closeListener()
166 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
170 _, _, err := kc.Ask(hash)
171 c.Check(err, Equals, keepclient.BlockNotFound)
178 hash2, rep, err = kc.PutB([]byte("foo"))
179 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
180 c.Check(rep, Equals, 2)
181 c.Check(err, Equals, nil)
186 blocklen, _, err := kc.Ask(hash2)
187 c.Assert(err, Equals, nil)
188 c.Check(blocklen, Equals, int64(3))
193 reader, blocklen, _, err := kc.Get(hash2)
194 c.Assert(err, Equals, nil)
195 all, err := ioutil.ReadAll(reader)
196 c.Check(all, DeepEquals, []byte("foo"))
197 c.Check(blocklen, Equals, int64(3))
204 hash2, rep, err = kc.PutB([]byte(""))
205 c.Check(hash2, Matches, `^d41d8cd98f00b204e9800998ecf8427e\+0(\+.+)?$`)
206 c.Check(rep, Equals, 2)
207 c.Check(err, Equals, nil)
208 log.Print("PutB zero block")
212 reader, blocklen, _, err := kc.Get("d41d8cd98f00b204e9800998ecf8427e")
213 c.Assert(err, Equals, nil)
214 all, err := ioutil.ReadAll(reader)
215 c.Check(all, DeepEquals, []byte(""))
216 c.Check(blocklen, Equals, int64(0))
217 log.Print("Get zero block")
220 log.Print("TestPutAndGet done")
223 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
224 log.Print("TestPutAskGetForbidden start")
226 kc := runProxy(c, []string{"keepproxy"}, 29951, true)
228 defer closeListener()
230 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
233 _, _, err := kc.Ask(hash)
234 c.Check(err, Equals, keepclient.BlockNotFound)
239 hash2, rep, err := kc.PutB([]byte("bar"))
240 c.Check(hash2, Equals, "")
241 c.Check(rep, Equals, 0)
242 c.Check(err, Equals, keepclient.InsufficientReplicasError)
247 blocklen, _, err := kc.Ask(hash)
248 c.Assert(err, Equals, keepclient.BlockNotFound)
249 c.Check(blocklen, Equals, int64(0))
254 _, blocklen, _, err := kc.Get(hash)
255 c.Assert(err, Equals, keepclient.BlockNotFound)
256 c.Check(blocklen, Equals, int64(0))
260 log.Print("TestPutAskGetForbidden done")
263 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
264 log.Print("TestGetDisabled start")
266 kc := runProxy(c, []string{"keepproxy", "-no-get"}, 29952, false)
268 defer closeListener()
270 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
273 _, _, err := kc.Ask(hash)
274 c.Check(err, Equals, keepclient.BlockNotFound)
279 hash2, rep, err := kc.PutB([]byte("baz"))
280 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
281 c.Check(rep, Equals, 2)
282 c.Check(err, Equals, nil)
287 blocklen, _, err := kc.Ask(hash)
288 c.Assert(err, Equals, keepclient.BlockNotFound)
289 c.Check(blocklen, Equals, int64(0))
294 _, blocklen, _, err := kc.Get(hash)
295 c.Assert(err, Equals, keepclient.BlockNotFound)
296 c.Check(blocklen, Equals, int64(0))
300 log.Print("TestGetDisabled done")
303 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
304 log.Print("TestPutDisabled start")
306 kc := runProxy(c, []string{"keepproxy", "-no-put"}, 29953, false)
308 defer closeListener()
311 hash2, rep, err := kc.PutB([]byte("quux"))
312 c.Check(hash2, Equals, "")
313 c.Check(rep, Equals, 0)
314 c.Check(err, Equals, keepclient.InsufficientReplicasError)
318 log.Print("TestPutDisabled done")
321 func (s *ServerRequiredSuite) TestCorsHeaders(c *C) {
322 runProxy(c, []string{"keepproxy"}, 29954, false)
324 defer closeListener()
327 client := http.Client{}
328 req, err := http.NewRequest("OPTIONS",
329 fmt.Sprintf("http://localhost:29954/%x+3",
330 md5.Sum([]byte("foo"))),
332 req.Header.Add("Access-Control-Request-Method", "PUT")
333 req.Header.Add("Access-Control-Request-Headers", "Authorization, X-Keep-Desired-Replicas")
334 resp, err := client.Do(req)
335 c.Check(err, Equals, nil)
336 c.Check(resp.StatusCode, Equals, 200)
337 body, err := ioutil.ReadAll(resp.Body)
338 c.Check(string(body), Equals, "")
339 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), Equals, "GET, HEAD, POST, PUT, OPTIONS")
340 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
344 resp, err := http.Get(
345 fmt.Sprintf("http://localhost:29954/%x+3",
346 md5.Sum([]byte("foo"))))
347 c.Check(err, Equals, nil)
348 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), Equals, "Authorization, Content-Length, Content-Type, X-Keep-Desired-Replicas")
349 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
353 func (s *ServerRequiredSuite) TestPostWithoutHash(c *C) {
354 runProxy(c, []string{"keepproxy"}, 29955, false)
356 defer closeListener()
359 client := http.Client{}
360 req, err := http.NewRequest("POST",
361 "http://localhost:29955/",
362 strings.NewReader("qux"))
363 req.Header.Add("Authorization", "OAuth2 4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
364 req.Header.Add("Content-Type", "application/octet-stream")
365 resp, err := client.Do(req)
366 c.Check(err, Equals, nil)
367 body, err := ioutil.ReadAll(resp.Body)
368 c.Check(err, Equals, nil)
369 c.Check(string(body), Equals,
370 fmt.Sprintf("%x+%d", md5.Sum([]byte("qux")), 3))