4 "git.curoverse.com/arvados.git/sdk/go/keepclient"
5 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
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 {
34 return fmt.Sprintf("%s/../../sdk/python/tests", cwd)
37 // Wait (up to 1 second) for keepproxy to listen on a port. This
38 // avoids a race condition where we hit a "connection refused" error
39 // because we start testing the proxy too soon.
40 func waitForListener() {
42 for i := 0; listener == nil && i < 1000; i += ms {
43 time.Sleep(ms * time.Millisecond)
46 log.Fatalf("Timed out waiting for listener to start")
50 func closeListener() {
56 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
62 cmd := exec.Command("python", "run_test_server.py", "start")
63 stderr, err := cmd.StderrPipe()
65 log.Fatalf("Setting up stderr pipe: %s", err)
67 go io.Copy(os.Stderr, stderr)
68 if err := cmd.Run(); err != nil {
69 panic(fmt.Sprintf("'python run_test_server.py start' returned error %s", err))
73 cmd := exec.Command("python", "run_test_server.py", "start_keep")
74 stderr, err := cmd.StderrPipe()
76 log.Fatalf("Setting up stderr pipe: %s", err)
78 go io.Copy(os.Stderr, stderr)
79 if err := cmd.Run(); err != nil {
80 panic(fmt.Sprintf("'python run_test_server.py start_keep' returned error %s", err))
84 os.Setenv("ARVADOS_API_HOST", "localhost:3000")
85 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
86 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
89 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
94 exec.Command("python", "run_test_server.py", "stop_keep").Run()
95 exec.Command("python", "run_test_server.py", "stop").Run()
98 func setupProxyService() {
100 client := &http.Client{Transport: &http.Transport{
101 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
103 var req *http.Request
105 if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
108 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
110 reader, writer := io.Pipe()
116 data.Set("keep_service", `{
117 "service_host": "localhost",
118 "service_port": 29950,
119 "service_ssl_flag": false,
120 "service_type": "proxy"
123 writer.Write([]byte(data.Encode()))
127 var resp *http.Response
128 if resp, err = client.Do(req); err != nil {
131 if resp.StatusCode != 200 {
136 func runProxy(c *C, args []string, token string, port int) keepclient.KeepClient {
137 os.Args = append(args, fmt.Sprintf("-listen=:%v", port))
138 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
141 time.Sleep(100 * time.Millisecond)
143 os.Setenv("ARVADOS_KEEP_PROXY", fmt.Sprintf("http://localhost:%v", port))
144 os.Setenv("ARVADOS_API_TOKEN", token)
145 arv, err := arvadosclient.MakeArvadosClient()
146 c.Assert(err, Equals, nil)
147 kc, err := keepclient.MakeKeepClient(&arv)
148 c.Assert(err, Equals, nil)
149 c.Check(kc.Using_proxy, Equals, true)
150 c.Check(len(kc.ServiceRoots()), Equals, 1)
151 for _, root := range(kc.ServiceRoots()) {
152 c.Check(root, Equals, fmt.Sprintf("http://localhost:%v", port))
154 os.Setenv("ARVADOS_KEEP_PROXY", "")
155 log.Print("keepclient created")
159 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
160 log.Print("TestPutAndGet start")
162 os.Args = []string{"keepproxy", "-listen=:29950"}
163 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
165 time.Sleep(100 * time.Millisecond)
169 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
170 arv, err := arvadosclient.MakeArvadosClient()
171 c.Assert(err, Equals, nil)
172 kc, err := keepclient.MakeKeepClient(&arv)
173 c.Assert(err, Equals, nil)
174 c.Check(kc.Arvados.External, Equals, true)
175 c.Check(kc.Using_proxy, Equals, true)
176 c.Check(len(kc.ServiceRoots()), Equals, 1)
177 for _, root := range kc.ServiceRoots() {
178 c.Check(root, Equals, "http://localhost:29950")
180 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
181 log.Print("keepclient created")
184 defer closeListener()
186 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
190 _, _, err := kc.Ask(hash)
191 c.Check(err, Equals, keepclient.BlockNotFound)
198 hash2, rep, err = kc.PutB([]byte("foo"))
199 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
200 c.Check(rep, Equals, 2)
201 c.Check(err, Equals, nil)
206 blocklen, _, err := kc.Ask(hash2)
207 c.Assert(err, Equals, nil)
208 c.Check(blocklen, Equals, int64(3))
213 reader, blocklen, _, err := kc.Get(hash2)
214 c.Assert(err, Equals, nil)
215 all, err := ioutil.ReadAll(reader)
216 c.Check(all, DeepEquals, []byte("foo"))
217 c.Check(blocklen, Equals, int64(3))
221 log.Print("TestPutAndGet done")
224 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
225 log.Print("TestPutAndGet start")
227 kc := runProxy(c, []string{"keepproxy"}, "123abc", 29951)
229 defer closeListener()
231 log.Print("keepclient created")
233 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
236 _, _, err := kc.Ask(hash)
237 c.Check(err, Equals, keepclient.BlockNotFound)
242 hash2, rep, err := kc.PutB([]byte("bar"))
243 c.Check(hash2, Equals, "")
244 c.Check(rep, Equals, 0)
245 c.Check(err, Equals, keepclient.InsufficientReplicasError)
250 blocklen, _, err := kc.Ask(hash)
251 c.Assert(err, Equals, keepclient.BlockNotFound)
252 c.Check(blocklen, Equals, int64(0))
257 _, blocklen, _, err := kc.Get(hash)
258 c.Assert(err, Equals, keepclient.BlockNotFound)
259 c.Check(blocklen, Equals, int64(0))
263 log.Print("TestPutAndGetForbidden done")
266 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
267 log.Print("TestGetDisabled start")
269 kc := runProxy(c, []string{"keepproxy", "-no-get"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29952)
271 defer closeListener()
273 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
276 _, _, err := kc.Ask(hash)
277 c.Check(err, Equals, keepclient.BlockNotFound)
282 hash2, rep, err := kc.PutB([]byte("baz"))
283 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
284 c.Check(rep, Equals, 2)
285 c.Check(err, Equals, nil)
290 blocklen, _, err := kc.Ask(hash)
291 c.Assert(err, Equals, keepclient.BlockNotFound)
292 c.Check(blocklen, Equals, int64(0))
297 _, blocklen, _, err := kc.Get(hash)
298 c.Assert(err, Equals, keepclient.BlockNotFound)
299 c.Check(blocklen, Equals, int64(0))
303 log.Print("TestGetDisabled done")
306 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
307 log.Print("TestPutDisabled start")
309 kc := runProxy(c, []string{"keepproxy", "-no-put"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29953)
311 defer closeListener()
314 hash2, rep, err := kc.PutB([]byte("quux"))
315 c.Check(hash2, Equals, "")
316 c.Check(rep, Equals, 0)
317 c.Check(err, Equals, keepclient.InsufficientReplicasError)
321 log.Print("TestPutDisabled done")