4 keepclient "git.curoverse.com/arvados.git/sdk/go/keepclient"
5 arvadosclient "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 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 arv, err := arvadosclient.MakeArvadosClient()
112 kc, err := keepclient.MakeKeepClient(&arv)
113 c.Check(kc.Using_proxy, Equals, true)
114 c.Check(len(kc.ServiceRoots()), Equals, 1)
115 c.Check(kc.ServiceRoots()[0], Equals, fmt.Sprintf("http://localhost:%v", port))
116 c.Check(err, Equals, nil)
117 os.Setenv("ARVADOS_KEEP_PROXY", "")
118 log.Print("keepclient created")
122 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
123 log.Print("TestPutAndGet start")
125 os.Args = []string{"keepproxy", "-listen=:29950"}
126 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
128 time.Sleep(100 * time.Millisecond)
132 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
133 arv, err := arvadosclient.MakeArvadosClient()
134 kc, err := keepclient.MakeKeepClient(&arv)
135 c.Check(kc.Arvados.External, Equals, true)
136 c.Check(kc.Using_proxy, Equals, true)
137 c.Check(len(kc.ServiceRoots()), Equals, 1)
138 c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
139 c.Check(err, Equals, nil)
140 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
141 log.Print("keepclient created")
143 defer listener.Close()
145 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
149 _, _, err := kc.Ask(hash)
150 c.Check(err, Equals, keepclient.BlockNotFound)
157 hash2, rep, err = kc.PutB([]byte("foo"))
158 c.Check(hash2, Equals, fmt.Sprintf("%s+3", hash))
159 c.Check(rep, Equals, 2)
160 c.Check(err, Equals, nil)
165 blocklen, _, err := kc.Ask(hash2)
166 c.Assert(err, Equals, nil)
167 c.Check(blocklen, Equals, int64(3))
172 reader, blocklen, _, err := kc.Get(hash2)
173 c.Assert(err, Equals, nil)
174 all, err := ioutil.ReadAll(reader)
175 c.Check(all, DeepEquals, []byte("foo"))
176 c.Check(blocklen, Equals, int64(3))
180 log.Print("TestPutAndGet done")
183 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
184 log.Print("TestPutAndGet start")
186 kc := runProxy(c, []string{"keepproxy"}, "123abc", 29951)
187 defer listener.Close()
189 log.Print("keepclient created")
191 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
194 _, _, err := kc.Ask(hash)
195 c.Check(err, Equals, keepclient.BlockNotFound)
200 hash2, rep, err := kc.PutB([]byte("bar"))
201 c.Check(hash2, Equals, "")
202 c.Check(rep, Equals, 0)
203 c.Check(err, Equals, keepclient.InsufficientReplicasError)
208 blocklen, _, err := kc.Ask(hash)
209 c.Assert(err, Equals, keepclient.BlockNotFound)
210 c.Check(blocklen, Equals, int64(0))
215 _, blocklen, _, err := kc.Get(hash)
216 c.Assert(err, Equals, keepclient.BlockNotFound)
217 c.Check(blocklen, Equals, int64(0))
221 log.Print("TestPutAndGetForbidden done")
224 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
225 log.Print("TestGetDisabled start")
227 kc := runProxy(c, []string{"keepproxy", "-no-get"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29952)
228 defer listener.Close()
230 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
233 _, _, err := kc.Ask(hash)
234 c.Check(err, Equals, keepclient.BlockNotFound)
239 hash2, rep, err := kc.PutB([]byte("baz"))
240 c.Check(hash2, Equals, fmt.Sprintf("%s+3", hash))
241 c.Check(rep, Equals, 2)
242 c.Check(err, Equals, nil)
247 blocklen, _, err := kc.Ask(hash)
248 c.Assert(err, Equals, keepclient.BlockNotFound)
249 c.Check(blocklen, Equals, int64(0))
254 _, blocklen, _, err := kc.Get(hash)
255 c.Assert(err, Equals, keepclient.BlockNotFound)
256 c.Check(blocklen, Equals, int64(0))
260 log.Print("TestGetDisabled done")
263 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
264 log.Print("TestPutDisabled start")
266 kc := runProxy(c, []string{"keepproxy", "-no-put"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29953)
267 defer listener.Close()
270 hash2, rep, err := kc.PutB([]byte("quux"))
271 c.Check(hash2, Equals, "")
272 c.Check(rep, Equals, 0)
273 c.Check(err, Equals, keepclient.InsufficientReplicasError)
277 log.Print("TestPutDisabled done")