X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/0bd1c28bed9a0756c61037947d5a9dccd5066f00..ff7b22c70cd77073d9bdbebac0bf03d43745ed0c:/services/keepproxy/keepproxy_test.go diff --git a/services/keepproxy/keepproxy_test.go b/services/keepproxy/keepproxy_test.go index c4e5ae7258..88ac8a6a1d 100644 --- a/services/keepproxy/keepproxy_test.go +++ b/services/keepproxy/keepproxy_test.go @@ -34,6 +34,25 @@ func pythonDir() string { return fmt.Sprintf("%s/../../sdk/python/tests", cwd) } +// Wait (up to 1 second) for keepproxy to listen on a port. This +// avoids a race condition where we hit a "connection refused" error +// because we start testing the proxy too soon. +func waitForListener() { + const (ms = 5) + for i := 0; listener == nil && i < 1000; i += ms { + time.Sleep(ms * time.Millisecond) + } + if listener == nil { + log.Fatalf("Timed out waiting for listener to start") + } +} + +func closeListener() { + if listener != nil { + listener.Close() + } +} + func (s *ServerRequiredSuite) SetUpSuite(c *C) { cwd, _ := os.Getwd() defer os.Chdir(cwd) @@ -62,7 +81,7 @@ func (s *ServerRequiredSuite) SetUpSuite(c *C) { } } - os.Setenv("ARVADOS_API_HOST", "localhost:3001") + os.Setenv("ARVADOS_API_HOST", "localhost:3000") os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h") os.Setenv("ARVADOS_API_HOST_INSECURE", "true") } @@ -124,11 +143,14 @@ func runProxy(c *C, args []string, token string, port int) keepclient.KeepClient os.Setenv("ARVADOS_KEEP_PROXY", fmt.Sprintf("http://localhost:%v", port)) os.Setenv("ARVADOS_API_TOKEN", token) arv, err := arvadosclient.MakeArvadosClient() + c.Assert(err, Equals, nil) kc, err := keepclient.MakeKeepClient(&arv) + c.Assert(err, Equals, nil) c.Check(kc.Using_proxy, Equals, true) c.Check(len(kc.ServiceRoots()), Equals, 1) - c.Check(kc.ServiceRoots()[0], Equals, fmt.Sprintf("http://localhost:%v", port)) - c.Check(err, Equals, nil) + for _, root := range(kc.ServiceRoots()) { + c.Check(root, Equals, fmt.Sprintf("http://localhost:%v", port)) + } os.Setenv("ARVADOS_KEEP_PROXY", "") log.Print("keepclient created") return kc @@ -146,16 +168,20 @@ func (s *ServerRequiredSuite) TestPutAskGet(c *C) { os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true") arv, err := arvadosclient.MakeArvadosClient() + c.Assert(err, Equals, nil) kc, err := keepclient.MakeKeepClient(&arv) + c.Assert(err, Equals, nil) c.Check(kc.Arvados.External, Equals, true) c.Check(kc.Using_proxy, Equals, true) c.Check(len(kc.ServiceRoots()), Equals, 1) - c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950") - c.Check(err, Equals, nil) + for _, root := range kc.ServiceRoots() { + c.Check(root, Equals, "http://localhost:29950") + } os.Setenv("ARVADOS_EXTERNAL_CLIENT", "") log.Print("keepclient created") - defer listener.Close() + waitForListener() + defer closeListener() hash := fmt.Sprintf("%x", md5.Sum([]byte("foo"))) var hash2 string @@ -199,7 +225,8 @@ func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) { log.Print("TestPutAndGet start") kc := runProxy(c, []string{"keepproxy"}, "123abc", 29951) - defer listener.Close() + waitForListener() + defer closeListener() log.Print("keepclient created") @@ -240,7 +267,8 @@ func (s *ServerRequiredSuite) TestGetDisabled(c *C) { log.Print("TestGetDisabled start") kc := runProxy(c, []string{"keepproxy", "-no-get"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29952) - defer listener.Close() + waitForListener() + defer closeListener() hash := fmt.Sprintf("%x", md5.Sum([]byte("baz"))) @@ -279,7 +307,8 @@ func (s *ServerRequiredSuite) TestPutDisabled(c *C) { log.Print("TestPutDisabled start") kc := runProxy(c, []string{"keepproxy", "-no-put"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29953) - defer listener.Close() + waitForListener() + defer closeListener() { hash2, rep, err := kc.PutB([]byte("quux"))