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 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
43 cmd := exec.Command("python", "run_test_server.py", "start")
44 stderr, err := cmd.StderrPipe()
46 log.Fatalf("Setting up stderr pipe: %s", err)
48 go io.Copy(os.Stderr, stderr)
49 if err := cmd.Run(); err != nil {
50 panic(fmt.Sprintf("'python run_test_server.py start' returned error %s", err))
54 cmd := exec.Command("python", "run_test_server.py", "start_keep")
55 stderr, err := cmd.StderrPipe()
57 log.Fatalf("Setting up stderr pipe: %s", err)
59 go io.Copy(os.Stderr, stderr)
60 if err := cmd.Run(); err != nil {
61 panic(fmt.Sprintf("'python run_test_server.py start_keep' returned error %s", err))
65 os.Setenv("ARVADOS_API_HOST", "localhost:3001")
66 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
67 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
70 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
75 exec.Command("python", "run_test_server.py", "stop_keep").Run()
76 exec.Command("python", "run_test_server.py", "stop").Run()
79 func setupProxyService() {
81 client := &http.Client{Transport: &http.Transport{
82 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
86 if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
89 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
91 reader, writer := io.Pipe()
97 data.Set("keep_service", `{
98 "service_host": "localhost",
99 "service_port": 29950,
100 "service_ssl_flag": false,
101 "service_type": "proxy"
104 writer.Write([]byte(data.Encode()))
108 var resp *http.Response
109 if resp, err = client.Do(req); err != nil {
112 if resp.StatusCode != 200 {
117 func runProxy(c *C, args []string, token string, port int) keepclient.KeepClient {
118 os.Args = append(args, fmt.Sprintf("-listen=:%v", port))
119 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
122 time.Sleep(100 * time.Millisecond)
124 os.Setenv("ARVADOS_KEEP_PROXY", fmt.Sprintf("http://localhost:%v", port))
125 os.Setenv("ARVADOS_API_TOKEN", token)
126 arv, err := arvadosclient.MakeArvadosClient()
127 kc, err := keepclient.MakeKeepClient(&arv)
128 c.Check(kc.Using_proxy, Equals, true)
129 c.Check(len(kc.ServiceRoots()), Equals, 1)
130 c.Check(kc.ServiceRoots()[0], Equals, fmt.Sprintf("http://localhost:%v", port))
131 c.Check(err, Equals, nil)
132 os.Setenv("ARVADOS_KEEP_PROXY", "")
133 log.Print("keepclient created")
137 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
138 log.Print("TestPutAndGet start")
140 os.Args = []string{"keepproxy", "-listen=:29950"}
141 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
143 time.Sleep(100 * time.Millisecond)
147 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
148 arv, err := arvadosclient.MakeArvadosClient()
149 kc, err := keepclient.MakeKeepClient(&arv)
150 c.Check(kc.Arvados.External, Equals, true)
151 c.Check(kc.Using_proxy, Equals, true)
152 c.Check(len(kc.ServiceRoots()), Equals, 1)
153 c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
154 c.Check(err, Equals, nil)
155 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
156 log.Print("keepclient created")
158 defer listener.Close()
160 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
164 _, _, err := kc.Ask(hash)
165 c.Check(err, Equals, keepclient.BlockNotFound)
172 hash2, rep, err = kc.PutB([]byte("foo"))
173 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
174 c.Check(rep, Equals, 2)
175 c.Check(err, Equals, nil)
180 blocklen, _, err := kc.Ask(hash2)
181 c.Assert(err, Equals, nil)
182 c.Check(blocklen, Equals, int64(3))
187 reader, blocklen, _, err := kc.Get(hash2)
188 c.Assert(err, Equals, nil)
189 all, err := ioutil.ReadAll(reader)
190 c.Check(all, DeepEquals, []byte("foo"))
191 c.Check(blocklen, Equals, int64(3))
195 log.Print("TestPutAndGet done")
198 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
199 log.Print("TestPutAndGet start")
201 kc := runProxy(c, []string{"keepproxy"}, "123abc", 29951)
202 defer listener.Close()
204 log.Print("keepclient created")
206 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
209 _, _, err := kc.Ask(hash)
210 c.Check(err, Equals, keepclient.BlockNotFound)
215 hash2, rep, err := kc.PutB([]byte("bar"))
216 c.Check(hash2, Equals, "")
217 c.Check(rep, Equals, 0)
218 c.Check(err, Equals, keepclient.InsufficientReplicasError)
223 blocklen, _, err := kc.Ask(hash)
224 c.Assert(err, Equals, keepclient.BlockNotFound)
225 c.Check(blocklen, Equals, int64(0))
230 _, blocklen, _, err := kc.Get(hash)
231 c.Assert(err, Equals, keepclient.BlockNotFound)
232 c.Check(blocklen, Equals, int64(0))
236 log.Print("TestPutAndGetForbidden done")
239 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
240 log.Print("TestGetDisabled start")
242 kc := runProxy(c, []string{"keepproxy", "-no-get"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29952)
243 defer listener.Close()
245 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
248 _, _, err := kc.Ask(hash)
249 c.Check(err, Equals, keepclient.BlockNotFound)
254 hash2, rep, err := kc.PutB([]byte("baz"))
255 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
256 c.Check(rep, Equals, 2)
257 c.Check(err, Equals, nil)
262 blocklen, _, err := kc.Ask(hash)
263 c.Assert(err, Equals, keepclient.BlockNotFound)
264 c.Check(blocklen, Equals, int64(0))
269 _, blocklen, _, err := kc.Get(hash)
270 c.Assert(err, Equals, keepclient.BlockNotFound)
271 c.Check(blocklen, Equals, int64(0))
275 log.Print("TestGetDisabled done")
278 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
279 log.Print("TestPutDisabled start")
281 kc := runProxy(c, []string{"keepproxy", "-no-put"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29953)
282 defer listener.Close()
285 hash2, rep, err := kc.PutB([]byte("quux"))
286 c.Check(hash2, Equals, "")
287 c.Check(rep, Equals, 0)
288 c.Check(err, Equals, keepclient.InsufficientReplicasError)
292 log.Print("TestPutDisabled done")