af1377b249c6fca6e811f258f00aad5a965927a1
[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         exec.Command("python", "run_test_server.py", "start").Run()
43         exec.Command("python", "run_test_server.py", "start_keep").Run()
44
45         os.Setenv("ARVADOS_API_HOST", "localhost:3001")
46         os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
47         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
48
49         SetupProxyService()
50
51         os.Args = []string{"keepproxy", "-listen=:29950"}
52         go main()
53         time.Sleep(100 * time.Millisecond)
54 }
55
56 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
57         listener.Close()
58
59         cwd, _ := os.Getwd()
60         defer os.Chdir(cwd)
61
62         os.Chdir(pythonDir())
63         exec.Command("python", "run_test_server.py", "stop_keep").Run()
64         exec.Command("python", "run_test_server.py", "stop").Run()
65 }
66
67 func SetupProxyService() {
68
69         client := &http.Client{Transport: &http.Transport{
70                 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
71
72         var req *http.Request
73         var err error
74         if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
75                 panic(err.Error())
76         }
77         req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
78
79         reader, writer := io.Pipe()
80
81         req.Body = reader
82
83         go func() {
84                 data := url.Values{}
85                 data.Set("keep_service", `{
86   "service_host": "localhost",
87   "service_port": 29950,
88   "service_ssl_flag": false,
89   "service_type": "proxy"
90 }`)
91
92                 writer.Write([]byte(data.Encode()))
93                 writer.Close()
94         }()
95
96         var resp *http.Response
97         if resp, err = client.Do(req); err != nil {
98                 panic(err.Error())
99         }
100         if resp.StatusCode != 200 {
101                 panic(resp.Status)
102         }
103 }
104
105 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
106         log.Print("TestPutAndGet start")
107
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", "")
116
117         log.Print("keepclient created")
118
119         hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
120
121         {
122                 _, _, err := kc.Ask(hash)
123                 c.Check(err, Equals, keepclient.BlockNotFound)
124                 log.Print("Ask 1")
125         }
126
127         {
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)
132                 log.Print("PutB")
133         }
134
135         {
136                 blocklen, _, err := kc.Ask(hash)
137                 c.Assert(err, Equals, nil)
138                 c.Check(blocklen, Equals, int64(3))
139                 log.Print("Ask 2")
140         }
141
142         {
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))
148                 log.Print("Get")
149         }
150
151         log.Print("TestPutAndGet done")
152 }
153
154 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
155         log.Print("TestPutAndGet start")
156
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", "")
166
167         log.Print("keepclient created")
168
169         hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
170
171         {
172                 _, _, err := kc.Ask(hash)
173                 c.Check(err, Equals, keepclient.BlockNotFound)
174                 log.Print("Ask 1")
175         }
176
177         {
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)
182                 log.Print("PutB")
183         }
184
185         {
186                 blocklen, _, err := kc.Ask(hash)
187                 c.Assert(err, Equals, keepclient.BlockNotFound)
188                 c.Check(blocklen, Equals, int64(0))
189                 log.Print("Ask 2")
190         }
191
192         {
193                 _, blocklen, _, err := kc.Get(hash)
194                 c.Assert(err, Equals, keepclient.BlockNotFound)
195                 c.Check(blocklen, Equals, int64(0))
196                 log.Print("Get")
197         }
198
199         log.Print("TestPutAndGetForbidden done")
200 }