18 // Gocheck boilerplate
19 func Test(t *testing.T) { TestingT(t) }
21 // Gocheck boilerplate
22 var _ = Suite(&ServerRequiredSuite{})
23 var _ = Suite(&StandaloneSuite{})
25 var no_server = flag.Bool("no-server", false, "Skip 'ServerRequireSuite'")
27 // Tests that require the Keep server running
28 type ServerRequiredSuite struct{}
31 type StandaloneSuite struct{}
33 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
35 c.Skip("Skipping tests that require server")
37 os.Chdir(os.ExpandEnv("$GOPATH../python"))
38 exec.Command("python", "run_test_server.py", "start").Run()
39 exec.Command("python", "run_test_server.py", "start_keep").Run()
43 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
44 os.Chdir(os.ExpandEnv("$GOPATH../python"))
45 exec.Command("python", "run_test_server.py", "stop_keep").Run()
46 exec.Command("python", "run_test_server.py", "stop").Run()
49 func (s *ServerRequiredSuite) TestInit(c *C) {
50 os.Setenv("ARVADOS_API_HOST", "localhost:3001")
51 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
52 os.Setenv("ARVADOS_API_HOST_INSECURE", "")
54 kc, err := MakeKeepClient()
55 c.Assert(kc.ApiServer, Equals, "localhost:3001")
56 c.Assert(kc.ApiToken, Equals, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
57 c.Assert(kc.ApiInsecure, Equals, false)
59 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
61 kc, err = MakeKeepClient()
62 c.Assert(kc.ApiServer, Equals, "localhost:3001")
63 c.Assert(kc.ApiToken, Equals, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
64 c.Assert(kc.ApiInsecure, Equals, true)
66 c.Assert(err, Equals, nil)
67 c.Assert(len(kc.Service_roots), Equals, 2)
68 c.Assert(kc.Service_roots[0], Equals, "http://localhost:25107")
69 c.Assert(kc.Service_roots[1], Equals, "http://localhost:25108")
72 func (s *StandaloneSuite) TestShuffleServiceRoots(c *C) {
73 kc := KeepClient{Service_roots: []string{"http://localhost:25107", "http://localhost:25108", "http://localhost:25109", "http://localhost:25110", "http://localhost:25111", "http://localhost:25112", "http://localhost:25113", "http://localhost:25114", "http://localhost:25115", "http://localhost:25116", "http://localhost:25117", "http://localhost:25118", "http://localhost:25119", "http://localhost:25120", "http://localhost:25121", "http://localhost:25122", "http://localhost:25123"}}
75 // "foo" acbd18db4cc2f85cedef654fccc4a4d8
76 foo_shuffle := []string{"http://localhost:25116", "http://localhost:25120", "http://localhost:25119", "http://localhost:25122", "http://localhost:25108", "http://localhost:25114", "http://localhost:25112", "http://localhost:25107", "http://localhost:25118", "http://localhost:25111", "http://localhost:25113", "http://localhost:25121", "http://localhost:25110", "http://localhost:25117", "http://localhost:25109", "http://localhost:25115", "http://localhost:25123"}
77 c.Check(kc.ShuffledServiceRoots("acbd18db4cc2f85cedef654fccc4a4d8"), DeepEquals, foo_shuffle)
79 // "bar" 37b51d194a7513e45b56f6524f2d51f2
80 bar_shuffle := []string{"http://localhost:25108", "http://localhost:25112", "http://localhost:25119", "http://localhost:25107", "http://localhost:25110", "http://localhost:25116", "http://localhost:25122", "http://localhost:25120", "http://localhost:25121", "http://localhost:25117", "http://localhost:25111", "http://localhost:25123", "http://localhost:25118", "http://localhost:25113", "http://localhost:25114", "http://localhost:25115", "http://localhost:25109"}
81 c.Check(kc.ShuffledServiceRoots("37b51d194a7513e45b56f6524f2d51f2"), DeepEquals, bar_shuffle)
84 func ReadIntoBufferHelper(c *C, bufsize int) {
85 buffer := make([]byte, bufsize)
87 reader, writer := io.Pipe()
88 slices := make(chan ReaderSlice)
90 go ReadIntoBuffer(buffer, reader, slices)
93 out := make([]byte, 128)
94 for i := 0; i < 128; i += 1 {
99 c.Check(len(s1.slice), Equals, 128)
100 c.Check(s1.reader_error, Equals, nil)
101 for i := 0; i < 128; i += 1 {
102 c.Check(s1.slice[i], Equals, byte(i))
104 for i := 0; i < len(buffer); i += 1 {
106 c.Check(buffer[i], Equals, byte(i))
108 c.Check(buffer[i], Equals, byte(0))
113 out := make([]byte, 96)
114 for i := 0; i < 96; i += 1 {
119 c.Check(len(s1.slice), Equals, 96)
120 c.Check(s1.reader_error, Equals, nil)
121 for i := 0; i < 96; i += 1 {
122 c.Check(s1.slice[i], Equals, byte(i/2))
124 for i := 0; i < len(buffer); i += 1 {
126 c.Check(buffer[i], Equals, byte(i))
127 } else if i < (128 + 96) {
128 c.Check(buffer[i], Equals, byte((i-128)/2))
130 c.Check(buffer[i], Equals, byte(0))
137 c.Check(len(s1.slice), Equals, 0)
138 c.Check(s1.reader_error, Equals, io.EOF)
142 func (s *StandaloneSuite) TestReadIntoBuffer(c *C) {
143 ReadIntoBufferHelper(c, 512)
144 ReadIntoBufferHelper(c, 225)
145 ReadIntoBufferHelper(c, 224)
148 func (s *StandaloneSuite) TestReadIntoShortBuffer(c *C) {
149 buffer := make([]byte, 223)
150 reader, writer := io.Pipe()
151 slices := make(chan ReaderSlice)
153 go ReadIntoBuffer(buffer, reader, slices)
156 out := make([]byte, 128)
157 for i := 0; i < 128; i += 1 {
162 c.Check(len(s1.slice), Equals, 128)
163 c.Check(s1.reader_error, Equals, nil)
164 for i := 0; i < 128; i += 1 {
165 c.Check(s1.slice[i], Equals, byte(i))
167 for i := 0; i < len(buffer); i += 1 {
169 c.Check(buffer[i], Equals, byte(i))
171 c.Check(buffer[i], Equals, byte(0))
176 out := make([]byte, 96)
177 for i := 0; i < 96; i += 1 {
181 // Write will deadlock because it can't write all the data, so
182 // spin it off to a goroutine
186 c.Check(len(s1.slice), Equals, 95)
187 c.Check(s1.reader_error, Equals, nil)
188 for i := 0; i < 95; i += 1 {
189 c.Check(s1.slice[i], Equals, byte(i/2))
191 for i := 0; i < len(buffer); i += 1 {
193 c.Check(buffer[i], Equals, byte(i))
194 } else if i < (128 + 95) {
195 c.Check(buffer[i], Equals, byte((i-128)/2))
197 c.Check(buffer[i], Equals, byte(0))
204 c.Check(len(s1.slice), Equals, 0)
205 c.Check(s1.reader_error, Equals, io.ErrShortBuffer)
210 func (s *StandaloneSuite) TestTransfer(c *C) {
211 reader, writer := io.Pipe()
213 // Buffer for reads from 'r'
214 buffer := make([]byte, 512)
216 // Read requests on Transfer() buffer
217 requests := make(chan ReadRequest)
218 defer close(requests)
220 // Reporting reader error states
221 reader_status := make(chan error)
223 go Transfer(buffer, reader, requests, reader_status)
225 br1 := MakeBufferReader(requests)
226 out := make([]byte, 128)
229 // Write some data, and read into a buffer shorter than
231 for i := 0; i < 128; i += 1 {
235 writer.Write(out[:100])
237 in := make([]byte, 64)
238 n, err := br1.Read(in)
240 c.Check(n, Equals, 64)
241 c.Check(err, Equals, nil)
243 for i := 0; i < 64; i += 1 {
244 c.Check(in[i], Equals, out[i])
249 // Write some more data, and read into buffer longer than
251 in := make([]byte, 64)
252 n, err := br1.Read(in)
253 c.Check(n, Equals, 36)
254 c.Check(err, Equals, nil)
256 for i := 0; i < 36; i += 1 {
257 c.Check(in[i], Equals, out[64+i])
263 // Test read before write
269 in := make([]byte, 64)
272 n, err := br1.Read(in)
276 time.Sleep(100 * time.Millisecond)
277 writer.Write(out[100:])
281 c.Check(got.n, Equals, 28)
282 c.Check(got.err, Equals, nil)
284 for i := 0; i < 28; i += 1 {
285 c.Check(in[i], Equals, out[100+i])
289 br2 := MakeBufferReader(requests)
291 // Test 'catch up' reader
292 in := make([]byte, 256)
293 n, err := br2.Read(in)
295 c.Check(n, Equals, 128)
296 c.Check(err, Equals, nil)
298 for i := 0; i < 128; i += 1 {
299 c.Check(in[i], Equals, out[i])
304 // Test closing the reader
306 status := <-reader_status
307 c.Check(status, Equals, io.EOF)
309 in := make([]byte, 256)
310 n1, err1 := br1.Read(in)
311 n2, err2 := br2.Read(in)
312 c.Check(n1, Equals, 0)
313 c.Check(err1, Equals, io.EOF)
314 c.Check(n2, Equals, 0)
315 c.Check(err2, Equals, io.EOF)
319 // Test 'catch up' reader after closing
320 br3 := MakeBufferReader(requests)
321 in := make([]byte, 256)
322 n, err := br3.Read(in)
324 c.Check(n, Equals, 128)
325 c.Check(err, Equals, nil)
327 for i := 0; i < 128; i += 1 {
328 c.Check(in[i], Equals, out[i])
331 n, err = br3.Read(in)
333 c.Check(n, Equals, 0)
334 c.Check(err, Equals, io.EOF)
338 func (s *StandaloneSuite) TestTransferShortBuffer(c *C) {
339 reader, writer := io.Pipe()
341 // Buffer for reads from 'r'
342 buffer := make([]byte, 100)
344 // Read requests on Transfer() buffer
345 requests := make(chan ReadRequest)
346 defer close(requests)
348 // Reporting reader error states
349 reader_status := make(chan error)
351 go Transfer(buffer, reader, requests, reader_status)
353 out := make([]byte, 101)
356 status := <-reader_status
357 c.Check(status, Equals, io.ErrShortBuffer)
360 type StubHandler struct {
363 expectApiToken string
368 func (this StubHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
369 this.c.Check(req.URL.Path, Equals, this.expectPath)
370 this.c.Check(req.Header.Get("Authorization"), Equals, fmt.Sprintf("OAuth2 %s", this.expectApiToken))
371 body, err := ioutil.ReadAll(req.Body)
372 this.c.Check(err, Equals, nil)
373 this.c.Check(body, DeepEquals, []byte(this.expectBody))
374 resp.WriteHeader(200)
378 func (s *StandaloneSuite) TestUploadToStubKeepServer(c *C) {
381 "/acbd18db4cc2f85cedef654fccc4a4d8",
385 server := http.Server{Handler: st}
387 listener, _ := net.ListenTCP("tcp", &net.TCPAddr{Port: 2999})
388 defer listener.Close()
390 log.Printf("%s", listener.Addr().String())
392 go server.Serve(listener)
393 kc, _ := MakeKeepClient()
394 kc.ApiToken = "abc123"
396 reader, writer := io.Pipe()
397 upload_status := make(chan UploadError)
399 go kc.uploadToKeepServer("http://localhost:2999", "acbd18db4cc2f85cedef654fccc4a4d8", reader, upload_status)
401 writer.Write([]byte("foo"))
405 status := <-upload_status
406 c.Check(status, DeepEquals, UploadError{io.EOF, "http://localhost:2999/acbd18db4cc2f85cedef654fccc4a4d8"})
409 type FailHandler struct {
413 func (this FailHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
414 resp.WriteHeader(400)
418 /*func (s *StandaloneSuite) TestFailedUploadToStubKeepServer(c *C) {
421 c.Check(true, Equals, false)
425 st := FailHandler{make(chan bool)}
426 server := http.Server{Handler: st}
428 listener, _ := net.ListenTCP("tcp", &net.TCPAddr{})
429 defer listener.Close()
431 go server.Serve(listener)
432 kc, _ := MakeKeepClient()
433 kc.ApiToken = "abc123"
435 reader, writer := io.Pipe()
436 upload_status := make(chan UploadError)
438 go kc.uploadToKeepServer(fmt.Sprintf("http://localhost:%s", listener.Addr().String()), "acbd18db4cc2f85cedef654fccc4a4d8", reader, upload_status)
440 log.Printf("Writing 1")
442 writer.Write([]byte("foo"))
444 log.Printf("Writing 2")
448 log.Printf("Writing 3")
452 log.Printf("Handled?!")
454 status := <-upload_status
455 c.Check(status, DeepEquals, UploadError{io.EOF, "http://localhost:2999/acbd18db4cc2f85cedef654fccc4a4d8"})