closes #4024
[arvados.git] / services / keepproxy / keepproxy_test.go
index 5944b2c21d0d27d776c6ab54cc3d444160aaa16f..88ac8a6a1dc1ec3c4e6cbb055ed40faaa9c63cc7 100644 (file)
@@ -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)
@@ -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"))