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")
55 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
60 exec.Command("python", "run_test_server.py", "stop_keep").Run()
61 exec.Command("python", "run_test_server.py", "stop").Run()
64 func setupProxyService() {
66 client := &http.Client{Transport: &http.Transport{
67 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
71 if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
74 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
76 reader, writer := io.Pipe()
82 data.Set("keep_service", `{
83 "service_host": "localhost",
84 "service_port": 29950,
85 "service_ssl_flag": false,
86 "service_type": "proxy"
89 writer.Write([]byte(data.Encode()))
93 var resp *http.Response
94 if resp, err = client.Do(req); err != nil {
97 if resp.StatusCode != 200 {
102 func runProxy(c *C, args []string, token string, port int) keepclient.KeepClient {
103 os.Args = append(args, fmt.Sprintf("-listen=:%v", port))
104 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
107 time.Sleep(100 * time.Millisecond)
109 os.Setenv("ARVADOS_KEEP_PROXY", fmt.Sprintf("http://localhost:%v", port))
110 os.Setenv("ARVADOS_API_TOKEN", token)
111 kc, err := keepclient.MakeKeepClient()
112 c.Check(kc.Using_proxy, Equals, true)
113 c.Check(len(kc.ServiceRoots()), Equals, 1)
114 c.Check(kc.ServiceRoots()[0], Equals, fmt.Sprintf("http://localhost:%v", port))
115 c.Check(err, Equals, nil)
116 os.Setenv("ARVADOS_KEEP_PROXY", "")
117 log.Print("keepclient created")
121 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
122 log.Print("TestPutAndGet start")
124 os.Args = []string{"keepproxy", "-listen=:29950"}
125 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
127 time.Sleep(100 * time.Millisecond)
131 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
132 kc, err := keepclient.MakeKeepClient()
133 c.Check(kc.External, Equals, true)
134 c.Check(kc.Using_proxy, Equals, true)
135 c.Check(len(kc.ServiceRoots()), Equals, 1)
136 c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
137 c.Check(err, Equals, nil)
138 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
139 log.Print("keepclient created")
141 defer listener.Close()
143 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
147 _, _, err := kc.Ask(hash)
148 c.Check(err, Equals, keepclient.BlockNotFound)
155 hash2, rep, err = kc.PutB([]byte("foo"))
156 c.Check(hash2, Equals, fmt.Sprintf("%s+3", hash))
157 c.Check(rep, Equals, 2)
158 c.Check(err, Equals, nil)
163 blocklen, _, err := kc.Ask(hash2)
164 c.Assert(err, Equals, nil)
165 c.Check(blocklen, Equals, int64(3))
170 reader, blocklen, _, err := kc.Get(hash2)
171 c.Assert(err, Equals, nil)
172 all, err := ioutil.ReadAll(reader)
173 c.Check(all, DeepEquals, []byte("foo"))
174 c.Check(blocklen, Equals, int64(3))
178 log.Print("TestPutAndGet done")
181 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
182 log.Print("TestPutAndGet start")
184 kc := runProxy(c, []string{"keepproxy"}, "123abc", 29951)
185 defer listener.Close()
187 log.Print("keepclient created")
189 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
192 _, _, err := kc.Ask(hash)
193 c.Check(err, Equals, keepclient.BlockNotFound)
198 hash2, rep, err := kc.PutB([]byte("bar"))
199 c.Check(hash2, Equals, "")
200 c.Check(rep, Equals, 0)
201 c.Check(err, Equals, keepclient.InsufficientReplicasError)
206 blocklen, _, err := kc.Ask(hash)
207 c.Assert(err, Equals, keepclient.BlockNotFound)
208 c.Check(blocklen, Equals, int64(0))
213 _, blocklen, _, err := kc.Get(hash)
214 c.Assert(err, Equals, keepclient.BlockNotFound)
215 c.Check(blocklen, Equals, int64(0))
219 log.Print("TestPutAndGetForbidden done")
222 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
223 log.Print("TestGetDisabled start")
225 kc := runProxy(c, []string{"keepproxy", "-no-get"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29952)
226 defer listener.Close()
228 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
231 _, _, err := kc.Ask(hash)
232 c.Check(err, Equals, keepclient.BlockNotFound)
237 hash2, rep, err := kc.PutB([]byte("baz"))
238 c.Check(hash2, Equals, fmt.Sprintf("%s+3", hash))
239 c.Check(rep, Equals, 2)
240 c.Check(err, Equals, nil)
245 blocklen, _, err := kc.Ask(hash)
246 c.Assert(err, Equals, keepclient.BlockNotFound)
247 c.Check(blocklen, Equals, int64(0))
252 _, blocklen, _, err := kc.Get(hash)
253 c.Assert(err, Equals, keepclient.BlockNotFound)
254 c.Check(blocklen, Equals, int64(0))
258 log.Print("TestGetDisabled done")
261 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
262 log.Print("TestPutDisabled start")
264 kc := runProxy(c, []string{"keepproxy", "-no-put"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29953)
265 defer listener.Close()
268 hash2, rep, err := kc.PutB([]byte("quux"))
269 c.Check(hash2, Equals, "")
270 c.Check(rep, Equals, 0)
271 c.Check(err, Equals, keepclient.InsufficientReplicasError)
275 log.Print("TestPutDisabled done")