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) {
42 exec.Command("python", "run_test_server.py", "start").Run()
43 exec.Command("python", "run_test_server.py", "start_keep").Run()
45 os.Setenv("ARVADOS_API_HOST", "localhost:3001")
46 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
47 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
51 os.Args = []string{"keepproxy", "-listen=:29950"}
53 time.Sleep(100 * time.Millisecond)
56 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
63 exec.Command("python", "run_test_server.py", "stop_keep").Run()
64 exec.Command("python", "run_test_server.py", "stop").Run()
67 func SetupProxyService() {
69 client := &http.Client{Transport: &http.Transport{
70 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
74 if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
77 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
79 reader, writer := io.Pipe()
85 data.Set("keep_service", `{
86 "service_host": "localhost",
87 "service_port": 29950,
88 "service_ssl_flag": false,
89 "service_type": "proxy"
92 writer.Write([]byte(data.Encode()))
96 var resp *http.Response
97 if resp, err = client.Do(req); err != nil {
100 if resp.StatusCode != 200 {
105 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
106 log.Print("TestPutAndGet start")
108 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
109 kc, err := keepclient.MakeKeepClient()
110 c.Check(kc.External, Equals, true)
111 c.Check(kc.Using_proxy, Equals, true)
112 c.Check(len(kc.Service_roots), Equals, 1)
113 c.Check(kc.Service_roots[0], Equals, "http://localhost:29950")
114 c.Check(err, Equals, nil)
115 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
117 log.Print("keepclient created")
119 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
122 _, _, err := kc.Ask(hash)
123 c.Check(err, Equals, keepclient.BlockNotFound)
128 hash2, rep, err := kc.PutB([]byte("foo"))
129 c.Check(hash2, Equals, hash)
130 c.Check(rep, Equals, 2)
131 c.Check(err, Equals, nil)
136 blocklen, _, err := kc.Ask(hash)
137 c.Assert(err, Equals, nil)
138 c.Check(blocklen, Equals, int64(3))
143 reader, blocklen, _, err := kc.Get(hash)
144 c.Assert(err, Equals, nil)
145 all, err := ioutil.ReadAll(reader)
146 c.Check(all, DeepEquals, []byte("foo"))
147 c.Check(blocklen, Equals, int64(3))
151 log.Print("TestPutAndGet done")
154 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
155 log.Print("TestPutAndGet start")
157 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
158 kc, err := keepclient.MakeKeepClient()
159 kc.ApiToken = "123xyz"
160 c.Check(kc.External, Equals, true)
161 c.Check(kc.Using_proxy, Equals, true)
162 c.Check(len(kc.Service_roots), Equals, 1)
163 c.Check(kc.Service_roots[0], Equals, "http://localhost:29950")
164 c.Check(err, Equals, nil)
165 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
167 log.Print("keepclient created")
169 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
172 _, _, err := kc.Ask(hash)
173 c.Check(err, Equals, keepclient.BlockNotFound)
178 hash2, rep, err := kc.PutB([]byte("foo"))
179 c.Check(hash2, Equals, hash)
180 c.Check(rep, Equals, 0)
181 c.Check(err, Equals, keepclient.InsufficientReplicasError)
186 blocklen, _, err := kc.Ask(hash)
187 c.Assert(err, Equals, keepclient.BlockNotFound)
188 c.Check(blocklen, Equals, int64(0))
193 _, blocklen, _, err := kc.Get(hash)
194 c.Assert(err, Equals, keepclient.BlockNotFound)
195 c.Check(blocklen, Equals, int64(0))
199 log.Print("TestPutAndGetForbidden done")