closes #3153
[arvados.git] / services / keepproxy / keepproxy_test.go
1 package main
2
3 import (
4         "git.curoverse.com/arvados.git/sdk/go/keepclient"
5         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
6         "crypto/md5"
7         "crypto/tls"
8         "fmt"
9         . "gopkg.in/check.v1"
10         "io"
11         "io/ioutil"
12         "log"
13         "net/http"
14         "net/url"
15         "os"
16         "os/exec"
17         "testing"
18         "time"
19 )
20
21 // Gocheck boilerplate
22 func Test(t *testing.T) {
23         TestingT(t)
24 }
25
26 // Gocheck boilerplate
27 var _ = Suite(&ServerRequiredSuite{})
28
29 // Tests that require the Keep server running
30 type ServerRequiredSuite struct{}
31
32 func pythonDir() string {
33         cwd, _ := os.Getwd()
34         return fmt.Sprintf("%s/../../sdk/python/tests", cwd)
35 }
36
37 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
38         cwd, _ := os.Getwd()
39         defer os.Chdir(cwd)
40
41         os.Chdir(pythonDir())
42         {
43                 cmd := exec.Command("python", "run_test_server.py", "start")
44                 stderr, err := cmd.StderrPipe()
45                 if err != nil {
46                         log.Fatalf("Setting up stderr pipe: %s", err)
47                 }
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))
51                 }
52         }
53         {
54                 cmd := exec.Command("python", "run_test_server.py", "start_keep")
55                 stderr, err := cmd.StderrPipe()
56                 if err != nil {
57                         log.Fatalf("Setting up stderr pipe: %s", err)
58                 }
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))
62                 }
63         }
64
65         os.Setenv("ARVADOS_API_HOST", "localhost:3001")
66         os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
67         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
68 }
69
70 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
71         cwd, _ := os.Getwd()
72         defer os.Chdir(cwd)
73
74         os.Chdir(pythonDir())
75         exec.Command("python", "run_test_server.py", "stop_keep").Run()
76         exec.Command("python", "run_test_server.py", "stop").Run()
77 }
78
79 func setupProxyService() {
80
81         client := &http.Client{Transport: &http.Transport{
82                 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
83
84         var req *http.Request
85         var err error
86         if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
87                 panic(err.Error())
88         }
89         req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
90
91         reader, writer := io.Pipe()
92
93         req.Body = reader
94
95         go func() {
96                 data := url.Values{}
97                 data.Set("keep_service", `{
98   "service_host": "localhost",
99   "service_port": 29950,
100   "service_ssl_flag": false,
101   "service_type": "proxy"
102 }`)
103
104                 writer.Write([]byte(data.Encode()))
105                 writer.Close()
106         }()
107
108         var resp *http.Response
109         if resp, err = client.Do(req); err != nil {
110                 panic(err.Error())
111         }
112         if resp.StatusCode != 200 {
113                 panic(resp.Status)
114         }
115 }
116
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")
120
121         go main()
122         time.Sleep(100 * time.Millisecond)
123
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")
134         return kc
135 }
136
137 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
138         log.Print("TestPutAndGet start")
139
140         os.Args = []string{"keepproxy", "-listen=:29950"}
141         os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
142         go main()
143         time.Sleep(100 * time.Millisecond)
144
145         setupProxyService()
146
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")
157
158         defer listener.Close()
159
160         hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
161         var hash2 string
162
163         {
164                 _, _, err := kc.Ask(hash)
165                 c.Check(err, Equals, keepclient.BlockNotFound)
166                 log.Print("Ask 1")
167         }
168
169         {
170                 var rep int
171                 var err error
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)
176                 log.Print("PutB")
177         }
178
179         {
180                 blocklen, _, err := kc.Ask(hash2)
181                 c.Assert(err, Equals, nil)
182                 c.Check(blocklen, Equals, int64(3))
183                 log.Print("Ask 2")
184         }
185
186         {
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))
192                 log.Print("Get")
193         }
194
195         log.Print("TestPutAndGet done")
196 }
197
198 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
199         log.Print("TestPutAndGet start")
200
201         kc := runProxy(c, []string{"keepproxy"}, "123abc", 29951)
202         defer listener.Close()
203
204         log.Print("keepclient created")
205
206         hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
207
208         {
209                 _, _, err := kc.Ask(hash)
210                 c.Check(err, Equals, keepclient.BlockNotFound)
211                 log.Print("Ask 1")
212         }
213
214         {
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)
219                 log.Print("PutB")
220         }
221
222         {
223                 blocklen, _, err := kc.Ask(hash)
224                 c.Assert(err, Equals, keepclient.BlockNotFound)
225                 c.Check(blocklen, Equals, int64(0))
226                 log.Print("Ask 2")
227         }
228
229         {
230                 _, blocklen, _, err := kc.Get(hash)
231                 c.Assert(err, Equals, keepclient.BlockNotFound)
232                 c.Check(blocklen, Equals, int64(0))
233                 log.Print("Get")
234         }
235
236         log.Print("TestPutAndGetForbidden done")
237 }
238
239 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
240         log.Print("TestGetDisabled start")
241
242         kc := runProxy(c, []string{"keepproxy", "-no-get"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29952)
243         defer listener.Close()
244
245         hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
246
247         {
248                 _, _, err := kc.Ask(hash)
249                 c.Check(err, Equals, keepclient.BlockNotFound)
250                 log.Print("Ask 1")
251         }
252
253         {
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)
258                 log.Print("PutB")
259         }
260
261         {
262                 blocklen, _, err := kc.Ask(hash)
263                 c.Assert(err, Equals, keepclient.BlockNotFound)
264                 c.Check(blocklen, Equals, int64(0))
265                 log.Print("Ask 2")
266         }
267
268         {
269                 _, blocklen, _, err := kc.Get(hash)
270                 c.Assert(err, Equals, keepclient.BlockNotFound)
271                 c.Check(blocklen, Equals, int64(0))
272                 log.Print("Get")
273         }
274
275         log.Print("TestGetDisabled done")
276 }
277
278 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
279         log.Print("TestPutDisabled start")
280
281         kc := runProxy(c, []string{"keepproxy", "-no-put"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29953)
282         defer listener.Close()
283
284         {
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)
289                 log.Print("PutB")
290         }
291
292         log.Print("TestPutDisabled done")
293 }