4 "arvados.org/keepclient"
21 // Gocheck boilerplate
22 func Test(t *testing.T) {
26 // Gocheck boilerplate
27 var _ = Suite(&ServerRequiredSuite{})
29 // Tests that require the Keep server running
30 type ServerRequiredSuite struct{}
32 func pythonDir() string {
33 gopath := os.Getenv("GOPATH")
34 return fmt.Sprintf("%s/../../sdk/python", strings.Split(gopath, ":")[0])
37 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
43 if err := exec.Command("python", "run_test_server.py", "start").Run(); err != nil {
44 panic("'python run_test_server.py start' returned error")
46 if err := exec.Command("python", "run_test_server.py", "start_keep").Run(); err != nil {
47 panic("'python run_test_server.py start_keep' returned error")
50 os.Setenv("ARVADOS_API_HOST", "localhost:3001")
51 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
52 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
56 os.Args = []string{"keepproxy", "-listen=:29950"}
58 time.Sleep(100 * time.Millisecond)
61 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
68 exec.Command("python", "run_test_server.py", "stop_keep").Run()
69 exec.Command("python", "run_test_server.py", "stop").Run()
72 func SetupProxyService() {
74 client := &http.Client{Transport: &http.Transport{
75 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
79 if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
82 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
84 reader, writer := io.Pipe()
90 data.Set("keep_service", `{
91 "service_host": "localhost",
92 "service_port": 29950,
93 "service_ssl_flag": false,
94 "service_type": "proxy"
97 writer.Write([]byte(data.Encode()))
101 var resp *http.Response
102 if resp, err = client.Do(req); err != nil {
105 if resp.StatusCode != 200 {
110 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
111 log.Print("TestPutAndGet start")
113 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
114 kc, err := keepclient.MakeKeepClient()
115 c.Check(kc.External, Equals, true)
116 c.Check(kc.Using_proxy, Equals, true)
117 c.Check(len(kc.ServiceRoots()), Equals, 1)
118 c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
119 c.Check(err, Equals, nil)
120 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
122 log.Print("keepclient created")
124 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
127 _, _, err := kc.Ask(hash)
128 c.Check(err, Equals, keepclient.BlockNotFound)
133 hash2, rep, err := kc.PutB([]byte("foo"))
134 c.Check(hash2, Equals, hash)
135 c.Check(rep, Equals, 2)
136 c.Check(err, Equals, nil)
141 blocklen, _, err := kc.Ask(hash)
142 c.Assert(err, Equals, nil)
143 c.Check(blocklen, Equals, int64(3))
148 reader, blocklen, _, err := kc.Get(hash)
149 c.Assert(err, Equals, nil)
150 all, err := ioutil.ReadAll(reader)
151 c.Check(all, DeepEquals, []byte("foo"))
152 c.Check(blocklen, Equals, int64(3))
156 log.Print("TestPutAndGet done")
159 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
160 log.Print("TestPutAndGet start")
162 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
163 kc, err := keepclient.MakeKeepClient()
164 kc.ApiToken = "123xyz"
165 c.Check(kc.External, Equals, true)
166 c.Check(kc.Using_proxy, Equals, true)
167 c.Check(len(kc.ServiceRoots()), Equals, 1)
168 c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
169 c.Check(err, Equals, nil)
170 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
172 log.Print("keepclient created")
174 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
177 _, _, err := kc.Ask(hash)
178 c.Check(err, Equals, keepclient.BlockNotFound)
183 hash2, rep, err := kc.PutB([]byte("foo"))
184 c.Check(hash2, Equals, hash)
185 c.Check(rep, Equals, 0)
186 c.Check(err, Equals, keepclient.InsufficientReplicasError)
191 blocklen, _, err := kc.Ask(hash)
192 c.Assert(err, Equals, keepclient.BlockNotFound)
193 c.Check(blocklen, Equals, int64(0))
198 _, blocklen, _, err := kc.Get(hash)
199 c.Assert(err, Equals, keepclient.BlockNotFound)
200 c.Check(blocklen, Equals, int64(0))
204 log.Print("TestPutAndGetForbidden done")