3551: Fix source tree layout.
[arvados.git] / services / keepproxy / keepproxy_test.go
1 package main
2
3 import (
4         keepclient "git.curoverse.com/arvados.git/sdk/go/keepclient"
5         arvadosclient "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         if err := exec.Command("python", "run_test_server.py", "start").Run(); err != nil {
44                 panic("'python run_test_server.py start' returned error")
45         }
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")
48         }
49
50         os.Setenv("ARVADOS_API_HOST", "localhost:3001")
51         os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
52         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
53 }
54
55 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
56         cwd, _ := os.Getwd()
57         defer os.Chdir(cwd)
58
59         os.Chdir(pythonDir())
60         exec.Command("python", "run_test_server.py", "stop_keep").Run()
61         exec.Command("python", "run_test_server.py", "stop").Run()
62 }
63
64 func setupProxyService() {
65
66         client := &http.Client{Transport: &http.Transport{
67                 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
68
69         var req *http.Request
70         var err error
71         if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
72                 panic(err.Error())
73         }
74         req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
75
76         reader, writer := io.Pipe()
77
78         req.Body = reader
79
80         go func() {
81                 data := url.Values{}
82                 data.Set("keep_service", `{
83   "service_host": "localhost",
84   "service_port": 29950,
85   "service_ssl_flag": false,
86   "service_type": "proxy"
87 }`)
88
89                 writer.Write([]byte(data.Encode()))
90                 writer.Close()
91         }()
92
93         var resp *http.Response
94         if resp, err = client.Do(req); err != nil {
95                 panic(err.Error())
96         }
97         if resp.StatusCode != 200 {
98                 panic(resp.Status)
99         }
100 }
101
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")
105
106         go main()
107         time.Sleep(100 * time.Millisecond)
108
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")
119         return kc
120 }
121
122 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
123         log.Print("TestPutAndGet start")
124
125         os.Args = []string{"keepproxy", "-listen=:29950"}
126         os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
127         go main()
128         time.Sleep(100 * time.Millisecond)
129
130         setupProxyService()
131
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")
142
143         defer listener.Close()
144
145         hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
146         var hash2 string
147
148         {
149                 _, _, err := kc.Ask(hash)
150                 c.Check(err, Equals, keepclient.BlockNotFound)
151                 log.Print("Ask 1")
152         }
153
154         {
155                 var rep int
156                 var err error
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)
161                 log.Print("PutB")
162         }
163
164         {
165                 blocklen, _, err := kc.Ask(hash2)
166                 c.Assert(err, Equals, nil)
167                 c.Check(blocklen, Equals, int64(3))
168                 log.Print("Ask 2")
169         }
170
171         {
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))
177                 log.Print("Get")
178         }
179
180         log.Print("TestPutAndGet done")
181 }
182
183 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
184         log.Print("TestPutAndGet start")
185
186         kc := runProxy(c, []string{"keepproxy"}, "123abc", 29951)
187         defer listener.Close()
188
189         log.Print("keepclient created")
190
191         hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
192
193         {
194                 _, _, err := kc.Ask(hash)
195                 c.Check(err, Equals, keepclient.BlockNotFound)
196                 log.Print("Ask 1")
197         }
198
199         {
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)
204                 log.Print("PutB")
205         }
206
207         {
208                 blocklen, _, err := kc.Ask(hash)
209                 c.Assert(err, Equals, keepclient.BlockNotFound)
210                 c.Check(blocklen, Equals, int64(0))
211                 log.Print("Ask 2")
212         }
213
214         {
215                 _, blocklen, _, err := kc.Get(hash)
216                 c.Assert(err, Equals, keepclient.BlockNotFound)
217                 c.Check(blocklen, Equals, int64(0))
218                 log.Print("Get")
219         }
220
221         log.Print("TestPutAndGetForbidden done")
222 }
223
224 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
225         log.Print("TestGetDisabled start")
226
227         kc := runProxy(c, []string{"keepproxy", "-no-get"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29952)
228         defer listener.Close()
229
230         hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
231
232         {
233                 _, _, err := kc.Ask(hash)
234                 c.Check(err, Equals, keepclient.BlockNotFound)
235                 log.Print("Ask 1")
236         }
237
238         {
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)
243                 log.Print("PutB")
244         }
245
246         {
247                 blocklen, _, err := kc.Ask(hash)
248                 c.Assert(err, Equals, keepclient.BlockNotFound)
249                 c.Check(blocklen, Equals, int64(0))
250                 log.Print("Ask 2")
251         }
252
253         {
254                 _, blocklen, _, err := kc.Get(hash)
255                 c.Assert(err, Equals, keepclient.BlockNotFound)
256                 c.Check(blocklen, Equals, int64(0))
257                 log.Print("Get")
258         }
259
260         log.Print("TestGetDisabled done")
261 }
262
263 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
264         log.Print("TestPutDisabled start")
265
266         kc := runProxy(c, []string{"keepproxy", "-no-put"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29953)
267         defer listener.Close()
268
269         {
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)
274                 log.Print("PutB")
275         }
276
277         log.Print("TestPutDisabled done")
278 }