4 "arvados.org/keepclient"
22 // Gocheck boilerplate
23 func Test(t *testing.T) {
27 // Gocheck boilerplate
28 var _ = Suite(&ServerRequiredSuite{})
30 // Tests that require the Keep server running
31 type ServerRequiredSuite struct{}
33 func pythonDir() string {
34 gopath := os.Getenv("GOPATH")
35 return fmt.Sprintf("%s/../../sdk/python/tests", strings.Split(gopath, ":")[0])
38 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
44 if err := exec.Command("python", "run_test_server.py", "start").Run(); err != nil {
45 panic("'python run_test_server.py start' returned error")
47 if err := exec.Command("python", "run_test_server.py", "start_keep").Run(); err != nil {
48 panic("'python run_test_server.py start_keep' returned error")
51 os.Setenv("ARVADOS_API_HOST", "localhost:3001")
52 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
53 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
56 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
61 exec.Command("python", "run_test_server.py", "stop_keep").Run()
62 exec.Command("python", "run_test_server.py", "stop").Run()
65 func setupProxyService() {
67 client := &http.Client{Transport: &http.Transport{
68 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
72 if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
75 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
77 reader, writer := io.Pipe()
83 data.Set("keep_service", `{
84 "service_host": "localhost",
85 "service_port": 29950,
86 "service_ssl_flag": false,
87 "service_type": "proxy"
90 writer.Write([]byte(data.Encode()))
94 var resp *http.Response
95 if resp, err = client.Do(req); err != nil {
98 if resp.StatusCode != 200 {
103 func runProxy(c *C, args []string, token string, port int) keepclient.KeepClient {
104 os.Args = append(args, fmt.Sprintf("-listen=:%v", port))
105 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
108 time.Sleep(100 * time.Millisecond)
110 os.Setenv("ARVADOS_KEEP_PROXY", fmt.Sprintf("http://localhost:%v", port))
111 os.Setenv("ARVADOS_API_TOKEN", token)
112 arv, err := sdk.MakeArvadosClient()
113 kc, err := keepclient.MakeKeepClient(&arv)
114 c.Check(kc.Using_proxy, Equals, true)
115 c.Check(len(kc.ServiceRoots()), Equals, 1)
116 c.Check(kc.ServiceRoots()[0], Equals, fmt.Sprintf("http://localhost:%v", port))
117 c.Check(err, Equals, nil)
118 os.Setenv("ARVADOS_KEEP_PROXY", "")
119 log.Print("keepclient created")
123 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
124 log.Print("TestPutAndGet start")
126 os.Args = []string{"keepproxy", "-listen=:29950"}
127 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
129 time.Sleep(100 * time.Millisecond)
133 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
134 arv, err := sdk.MakeArvadosClient()
135 kc, err := keepclient.MakeKeepClient(&arv)
136 c.Check(kc.Arvados.External, Equals, true)
137 c.Check(kc.Using_proxy, Equals, true)
138 c.Check(len(kc.ServiceRoots()), Equals, 1)
139 c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
140 c.Check(err, Equals, nil)
141 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
142 log.Print("keepclient created")
144 defer listener.Close()
146 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
150 _, _, err := kc.Ask(hash)
151 c.Check(err, Equals, keepclient.BlockNotFound)
158 hash2, rep, err = kc.PutB([]byte("foo"))
159 c.Check(hash2, Equals, fmt.Sprintf("%s+3", hash))
160 c.Check(rep, Equals, 2)
161 c.Check(err, Equals, nil)
166 blocklen, _, err := kc.Ask(hash2)
167 c.Assert(err, Equals, nil)
168 c.Check(blocklen, Equals, int64(3))
173 reader, blocklen, _, err := kc.Get(hash2)
174 c.Assert(err, Equals, nil)
175 all, err := ioutil.ReadAll(reader)
176 c.Check(all, DeepEquals, []byte("foo"))
177 c.Check(blocklen, Equals, int64(3))
181 log.Print("TestPutAndGet done")
184 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
185 log.Print("TestPutAndGet start")
187 kc := runProxy(c, []string{"keepproxy"}, "123abc", 29951)
188 defer listener.Close()
190 log.Print("keepclient created")
192 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
195 _, _, err := kc.Ask(hash)
196 c.Check(err, Equals, keepclient.BlockNotFound)
201 hash2, rep, err := kc.PutB([]byte("bar"))
202 c.Check(hash2, Equals, "")
203 c.Check(rep, Equals, 0)
204 c.Check(err, Equals, keepclient.InsufficientReplicasError)
209 blocklen, _, err := kc.Ask(hash)
210 c.Assert(err, Equals, keepclient.BlockNotFound)
211 c.Check(blocklen, Equals, int64(0))
216 _, blocklen, _, err := kc.Get(hash)
217 c.Assert(err, Equals, keepclient.BlockNotFound)
218 c.Check(blocklen, Equals, int64(0))
222 log.Print("TestPutAndGetForbidden done")
225 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
226 log.Print("TestGetDisabled start")
228 kc := runProxy(c, []string{"keepproxy", "-no-get"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29952)
229 defer listener.Close()
231 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
234 _, _, err := kc.Ask(hash)
235 c.Check(err, Equals, keepclient.BlockNotFound)
240 hash2, rep, err := kc.PutB([]byte("baz"))
241 c.Check(hash2, Equals, fmt.Sprintf("%s+3", hash))
242 c.Check(rep, Equals, 2)
243 c.Check(err, Equals, nil)
248 blocklen, _, err := kc.Ask(hash)
249 c.Assert(err, Equals, keepclient.BlockNotFound)
250 c.Check(blocklen, Equals, int64(0))
255 _, blocklen, _, err := kc.Get(hash)
256 c.Assert(err, Equals, keepclient.BlockNotFound)
257 c.Check(blocklen, Equals, int64(0))
261 log.Print("TestGetDisabled done")
264 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
265 log.Print("TestPutDisabled start")
267 kc := runProxy(c, []string{"keepproxy", "-no-put"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29953)
268 defer listener.Close()
271 hash2, rep, err := kc.PutB([]byte("quux"))
272 c.Check(hash2, Equals, "")
273 c.Check(rep, Equals, 0)
274 c.Check(err, Equals, keepclient.InsufficientReplicasError)
278 log.Print("TestPutDisabled done")