Merge branch 'master' into 2756-eventbus-in-workbench
[arvados.git] / services / keep / src / arvados.org / keepproxy / keepproxy_test.go
1 package main
2
3 import (
4         "arvados.org/keepclient"
5         "crypto/md5"
6         "crypto/tls"
7         "fmt"
8         . "gopkg.in/check.v1"
9         "io"
10         "io/ioutil"
11         "log"
12         "net/http"
13         "net/url"
14         "os"
15         "os/exec"
16         "strings"
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         gopath := os.Getenv("GOPATH")
34         return fmt.Sprintf("%s/../../sdk/python", strings.Split(gopath, ":")[0])
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         SetupProxyService()
55
56         os.Args = []string{"keepproxy", "-listen=:29950"}
57         go main()
58         time.Sleep(100 * time.Millisecond)
59 }
60
61 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
62         listener.Close()
63
64         cwd, _ := os.Getwd()
65         defer os.Chdir(cwd)
66
67         os.Chdir(pythonDir())
68         exec.Command("python", "run_test_server.py", "stop_keep").Run()
69         exec.Command("python", "run_test_server.py", "stop").Run()
70 }
71
72 func SetupProxyService() {
73
74         client := &http.Client{Transport: &http.Transport{
75                 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
76
77         var req *http.Request
78         var err error
79         if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
80                 panic(err.Error())
81         }
82         req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
83
84         reader, writer := io.Pipe()
85
86         req.Body = reader
87
88         go func() {
89                 data := url.Values{}
90                 data.Set("keep_service", `{
91   "service_host": "localhost",
92   "service_port": 29950,
93   "service_ssl_flag": false,
94   "service_type": "proxy"
95 }`)
96
97                 writer.Write([]byte(data.Encode()))
98                 writer.Close()
99         }()
100
101         var resp *http.Response
102         if resp, err = client.Do(req); err != nil {
103                 panic(err.Error())
104         }
105         if resp.StatusCode != 200 {
106                 panic(resp.Status)
107         }
108 }
109
110 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
111         log.Print("TestPutAndGet start")
112
113         os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
114         kc, err := keepclient.MakeKeepClient()
115         c.Check(kc.External, Equals, true)
116         c.Check(kc.Using_proxy, Equals, true)
117         c.Check(len(kc.ServiceRoots()), Equals, 1)
118         c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
119         c.Check(err, Equals, nil)
120         os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
121
122         log.Print("keepclient created")
123
124         hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
125
126         {
127                 _, _, err := kc.Ask(hash)
128                 c.Check(err, Equals, keepclient.BlockNotFound)
129                 log.Print("Ask 1")
130         }
131
132         {
133                 hash2, rep, err := kc.PutB([]byte("foo"))
134                 c.Check(hash2, Equals, hash)
135                 c.Check(rep, Equals, 2)
136                 c.Check(err, Equals, nil)
137                 log.Print("PutB")
138         }
139
140         {
141                 blocklen, _, err := kc.Ask(hash)
142                 c.Assert(err, Equals, nil)
143                 c.Check(blocklen, Equals, int64(3))
144                 log.Print("Ask 2")
145         }
146
147         {
148                 reader, blocklen, _, err := kc.Get(hash)
149                 c.Assert(err, Equals, nil)
150                 all, err := ioutil.ReadAll(reader)
151                 c.Check(all, DeepEquals, []byte("foo"))
152                 c.Check(blocklen, Equals, int64(3))
153                 log.Print("Get")
154         }
155
156         log.Print("TestPutAndGet done")
157 }
158
159 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
160         log.Print("TestPutAndGet start")
161
162         os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
163         kc, err := keepclient.MakeKeepClient()
164         kc.ApiToken = "123xyz"
165         c.Check(kc.External, Equals, true)
166         c.Check(kc.Using_proxy, Equals, true)
167         c.Check(len(kc.ServiceRoots()), Equals, 1)
168         c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
169         c.Check(err, Equals, nil)
170         os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
171
172         log.Print("keepclient created")
173
174         hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
175
176         {
177                 _, _, err := kc.Ask(hash)
178                 c.Check(err, Equals, keepclient.BlockNotFound)
179                 log.Print("Ask 1")
180         }
181
182         {
183                 hash2, rep, err := kc.PutB([]byte("foo"))
184                 c.Check(hash2, Equals, hash)
185                 c.Check(rep, Equals, 0)
186                 c.Check(err, Equals, keepclient.InsufficientReplicasError)
187                 log.Print("PutB")
188         }
189
190         {
191                 blocklen, _, err := kc.Ask(hash)
192                 c.Assert(err, Equals, keepclient.BlockNotFound)
193                 c.Check(blocklen, Equals, int64(0))
194                 log.Print("Ask 2")
195         }
196
197         {
198                 _, blocklen, _, err := kc.Get(hash)
199                 c.Assert(err, Equals, keepclient.BlockNotFound)
200                 c.Check(blocklen, Equals, int64(0))
201                 log.Print("Get")
202         }
203
204         log.Print("TestPutAndGetForbidden done")
205 }