2751: Teaching run_test_server how to run the proxy server for testing. Added -pid...
[arvados.git] / services / keep / src / arvados.org / keepproxy / keepproxy_test.go
index 256f7f427717ec7d8cafe93d0247baaf3a633ff7..f03c94f88b9f380ca4dccc31a07b95feed50d28f 100644 (file)
@@ -2,6 +2,7 @@ package main
 
 import (
        "arvados.org/keepclient"
+       "crypto/md5"
        "crypto/tls"
        "fmt"
        . "gopkg.in/check.v1"
@@ -14,6 +15,7 @@ import (
        "os/exec"
        "strings"
        "testing"
+       "time"
 )
 
 // Gocheck boilerplate
@@ -37,17 +39,28 @@ func (s *ServerRequiredSuite) SetUpSuite(c *C) {
        defer os.Chdir(cwd)
 
        os.Chdir(pythonDir())
-       exec.Command("python", "run_test_server.py", "start").Run()
-       exec.Command("python", "run_test_server.py", "start_keep").Run()
+
+       if err := exec.Command("python", "run_test_server.py", "start").Run(); err != nil {
+               panic("'python run_test_server.py start' returned error")
+       }
+       if err := exec.Command("python", "run_test_server.py", "start_keep").Run(); err != nil {
+               panic("'python run_test_server.py start_keep' returned error")
+       }
 
        os.Setenv("ARVADOS_API_HOST", "localhost:3001")
        os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
        os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
 
        SetupProxyService()
+
+       os.Args = []string{"keepproxy", "-listen=:29950"}
+       go main()
+       time.Sleep(100 * time.Millisecond)
 }
 
 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
+       listener.Close()
+
        cwd, _ := os.Getwd()
        defer os.Chdir(cwd)
 
@@ -94,42 +107,99 @@ func SetupProxyService() {
        }
 }
 
-func (s *ServerRequiredSuite) TestPutAndGet(c *C) {
+func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
        log.Print("TestPutAndGet start")
 
        os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
        kc, err := keepclient.MakeKeepClient()
        c.Check(kc.External, Equals, true)
        c.Check(kc.Using_proxy, Equals, true)
-       c.Check(len(kc.Service_roots), Equals, 1)
-       c.Check(kc.Service_roots[0], Equals, "http://localhost:29950")
+       c.Check(len(kc.ServiceRoots()), Equals, 1)
+       c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
        c.Check(err, Equals, nil)
        os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
 
        log.Print("keepclient created")
 
-       os.Args = []string{"keepproxy", "-listen=:29950"}
-       go main()
+       hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
 
-       log.Print("keepproxy main started")
+       {
+               _, _, err := kc.Ask(hash)
+               c.Check(err, Equals, keepclient.BlockNotFound)
+               log.Print("Ask 1")
+       }
 
-       hash, rep, err2 := kc.PutB([]byte("foo"))
+       {
+               hash2, rep, err := kc.PutB([]byte("foo"))
+               c.Check(hash2, Equals, hash)
+               c.Check(rep, Equals, 2)
+               c.Check(err, Equals, nil)
+               log.Print("PutB")
+       }
 
-       log.Print("PutB")
+       {
+               blocklen, _, err := kc.Ask(hash)
+               c.Assert(err, Equals, nil)
+               c.Check(blocklen, Equals, int64(3))
+               log.Print("Ask 2")
+       }
 
-       c.Check(rep, Equals, 2)
-       c.Check(err2, Equals, nil)
+       {
+               reader, blocklen, _, err := kc.Get(hash)
+               c.Assert(err, Equals, nil)
+               all, err := ioutil.ReadAll(reader)
+               c.Check(all, DeepEquals, []byte("foo"))
+               c.Check(blocklen, Equals, int64(3))
+               log.Print("Get")
+       }
 
-       reader, blocklen, _, err3 := kc.Get(hash)
-       all, err := ioutil.ReadAll(reader)
-       c.Check(all, DeepEquals, []byte("foo"))
-       c.Check(blocklen, Equals, int64(3))
-       c.Check(err3, Equals, nil)
+       log.Print("TestPutAndGet done")
+}
 
-       log.Print("Get")
+func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
+       log.Print("TestPutAndGet start")
 
-       // Close internal listener socket.
-       listener.Close()
+       os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
+       kc, err := keepclient.MakeKeepClient()
+       kc.ApiToken = "123xyz"
+       c.Check(kc.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)
+       os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
 
-       log.Print("TestPutAndGet done")
+       log.Print("keepclient created")
+
+       hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+       {
+               _, _, err := kc.Ask(hash)
+               c.Check(err, Equals, keepclient.BlockNotFound)
+               log.Print("Ask 1")
+       }
+
+       {
+               hash2, rep, err := kc.PutB([]byte("foo"))
+               c.Check(hash2, Equals, hash)
+               c.Check(rep, Equals, 0)
+               c.Check(err, Equals, keepclient.InsufficientReplicasError)
+               log.Print("PutB")
+       }
+
+       {
+               blocklen, _, err := kc.Ask(hash)
+               c.Assert(err, Equals, keepclient.BlockNotFound)
+               c.Check(blocklen, Equals, int64(0))
+               log.Print("Ask 2")
+       }
+
+       {
+               _, blocklen, _, err := kc.Get(hash)
+               c.Assert(err, Equals, keepclient.BlockNotFound)
+               c.Check(blocklen, Equals, int64(0))
+               log.Print("Get")
+       }
+
+       log.Print("TestPutAndGetForbidden done")
 }