15 // Gocheck boilerplate
16 func Test(t *testing.T) { TestingT(t) }
18 // Gocheck boilerplate
19 var _ = Suite(&ServerRequiredSuite{})
20 var _ = Suite(&StandaloneSuite{})
22 var no_server = flag.Bool("no-server", false, "Skip 'ServerRequireSuite'")
24 // Tests that require the Keep server running
25 type ServerRequiredSuite struct{}
28 type StandaloneSuite struct{}
30 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
32 c.Skip("Skipping tests that require server")
34 os.Chdir(os.ExpandEnv("$GOPATH../python"))
35 exec.Command("python", "run_test_server.py", "start").Run()
36 exec.Command("python", "run_test_server.py", "start_keep").Run()
40 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
41 os.Chdir(os.ExpandEnv("$GOPATH../python"))
42 exec.Command("python", "run_test_server.py", "stop_keep").Run()
43 exec.Command("python", "run_test_server.py", "stop").Run()
46 func (s *ServerRequiredSuite) TestInit(c *C) {
47 os.Setenv("ARVADOS_API_HOST", "localhost:3001")
48 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
49 os.Setenv("ARVADOS_API_HOST_INSECURE", "")
51 kc, err := MakeKeepClient()
52 c.Assert(kc.ApiServer, Equals, "localhost:3001")
53 c.Assert(kc.ApiToken, Equals, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
54 c.Assert(kc.ApiInsecure, Equals, false)
56 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
58 kc, err = MakeKeepClient()
59 c.Assert(kc.ApiServer, Equals, "localhost:3001")
60 c.Assert(kc.ApiToken, Equals, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
61 c.Assert(kc.ApiInsecure, Equals, true)
63 c.Assert(err, Equals, nil)
64 c.Assert(len(kc.Service_roots), Equals, 2)
65 c.Assert(kc.Service_roots[0], Equals, "http://localhost:25107")
66 c.Assert(kc.Service_roots[1], Equals, "http://localhost:25108")
69 func (s *StandaloneSuite) TestShuffleServiceRoots(c *C) {
70 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"}}
72 // "foo" acbd18db4cc2f85cedef654fccc4a4d8
73 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"}
74 c.Check(kc.ShuffledServiceRoots("acbd18db4cc2f85cedef654fccc4a4d8"), DeepEquals, foo_shuffle)
76 // "bar" 37b51d194a7513e45b56f6524f2d51f2
77 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"}
78 c.Check(kc.ShuffledServiceRoots("37b51d194a7513e45b56f6524f2d51f2"), DeepEquals, bar_shuffle)
81 func ReadIntoBufferHelper(c *C, bufsize int) {
82 buffer := make([]byte, bufsize)
84 reader, writer := io.Pipe()
85 slices := make(chan ReaderSlice)
87 go ReadIntoBuffer(buffer, reader, slices)
90 out := make([]byte, 128)
91 for i := 0; i < 128; i += 1 {
96 c.Check(len(s1.slice), Equals, 128)
97 c.Check(s1.reader_error, Equals, nil)
98 for i := 0; i < 128; i += 1 {
99 c.Check(s1.slice[i], Equals, byte(i))
101 for i := 0; i < len(buffer); i += 1 {
103 c.Check(buffer[i], Equals, byte(i))
105 c.Check(buffer[i], Equals, byte(0))
110 out := make([]byte, 96)
111 for i := 0; i < 96; i += 1 {
116 c.Check(len(s1.slice), Equals, 96)
117 c.Check(s1.reader_error, Equals, nil)
118 for i := 0; i < 96; i += 1 {
119 c.Check(s1.slice[i], Equals, byte(i/2))
121 for i := 0; i < len(buffer); i += 1 {
123 c.Check(buffer[i], Equals, byte(i))
124 } else if i < (128 + 96) {
125 c.Check(buffer[i], Equals, byte((i-128)/2))
127 c.Check(buffer[i], Equals, byte(0))
134 c.Check(len(s1.slice), Equals, 0)
135 c.Check(s1.reader_error, Equals, io.EOF)
139 func (s *StandaloneSuite) TestReadIntoBuffer(c *C) {
140 ReadIntoBufferHelper(c, 512)
141 ReadIntoBufferHelper(c, 225)
142 ReadIntoBufferHelper(c, 224)
145 func (s *StandaloneSuite) TestReadIntoShortBuffer(c *C) {
146 buffer := make([]byte, 223)
147 reader, writer := io.Pipe()
148 slices := make(chan ReaderSlice)
150 go ReadIntoBuffer(buffer, reader, slices)
153 out := make([]byte, 128)
154 for i := 0; i < 128; i += 1 {
159 c.Check(len(s1.slice), Equals, 128)
160 c.Check(s1.reader_error, Equals, nil)
161 for i := 0; i < 128; i += 1 {
162 c.Check(s1.slice[i], Equals, byte(i))
164 for i := 0; i < len(buffer); i += 1 {
166 c.Check(buffer[i], Equals, byte(i))
168 c.Check(buffer[i], Equals, byte(0))
173 out := make([]byte, 96)
174 for i := 0; i < 96; i += 1 {
178 // Write will deadlock because it can't write all the data, so
179 // spin it off to a goroutine
183 c.Check(len(s1.slice), Equals, 95)
184 c.Check(s1.reader_error, Equals, nil)
185 for i := 0; i < 95; i += 1 {
186 c.Check(s1.slice[i], Equals, byte(i/2))
188 for i := 0; i < len(buffer); i += 1 {
190 c.Check(buffer[i], Equals, byte(i))
191 } else if i < (128 + 95) {
192 c.Check(buffer[i], Equals, byte((i-128)/2))
194 c.Check(buffer[i], Equals, byte(0))
201 c.Check(len(s1.slice), Equals, 0)
202 c.Check(s1.reader_error, Equals, io.ErrShortBuffer)
207 func (s *StandaloneSuite) TestTransfer(c *C) {
208 reader, writer := io.Pipe()
210 // Buffer for reads from 'r'
211 buffer := make([]byte, 512)
213 // Read requests on Transfer() buffer
214 requests := make(chan ReadRequest)
215 defer close(requests)
217 // Reporting reader error states
218 reader_status := make(chan error)
220 go Transfer(buffer, reader, requests, reader_status)
222 br1 := MakeBufferReader(requests)
223 out := make([]byte, 128)
226 // Write some data, and read into a buffer shorter than
228 for i := 0; i < 128; i += 1 {
232 writer.Write(out[:100])
234 in := make([]byte, 64)
235 n, err := br1.Read(in)
237 c.Check(n, Equals, 64)
238 c.Check(err, Equals, nil)
240 for i := 0; i < 64; i += 1 {
241 c.Check(in[i], Equals, out[i])
246 // Write some more data, and read into buffer longer than
248 in := make([]byte, 64)
249 n, err := br1.Read(in)
250 c.Check(n, Equals, 36)
251 c.Check(err, Equals, nil)
253 for i := 0; i < 36; i += 1 {
254 c.Check(in[i], Equals, out[64+i])
260 // Test read before write
266 in := make([]byte, 64)
269 n, err := br1.Read(in)
273 time.Sleep(100 * time.Millisecond)
274 writer.Write(out[100:])
278 c.Check(got.n, Equals, 28)
279 c.Check(got.err, Equals, nil)
281 for i := 0; i < 28; i += 1 {
282 c.Check(in[i], Equals, out[100+i])
286 br2 := MakeBufferReader(requests)
288 // Test 'catch up' reader
289 in := make([]byte, 256)
290 n, err := br2.Read(in)
292 c.Check(n, Equals, 128)
293 c.Check(err, Equals, nil)
295 for i := 0; i < 128; i += 1 {
296 c.Check(in[i], Equals, out[i])
301 // Test closing the reader
303 status := <-reader_status
304 c.Check(status, Equals, io.EOF)
306 in := make([]byte, 256)
307 n1, err1 := br1.Read(in)
308 n2, err2 := br2.Read(in)
309 c.Check(n1, Equals, 0)
310 c.Check(err1, Equals, io.EOF)
311 c.Check(n2, Equals, 0)
312 c.Check(err2, Equals, io.EOF)
316 // Test 'catch up' reader after closing
317 br3 := MakeBufferReader(requests)
318 in := make([]byte, 256)
319 n, err := br3.Read(in)
321 c.Check(n, Equals, 128)
322 c.Check(err, Equals, nil)
324 for i := 0; i < 128; i += 1 {
325 c.Check(in[i], Equals, out[i])
328 n, err = br3.Read(in)
330 c.Check(n, Equals, 0)
331 c.Check(err, Equals, io.EOF)
335 func (s *StandaloneSuite) TestTransferShortBuffer(c *C) {
336 reader, writer := io.Pipe()
338 // Buffer for reads from 'r'
339 buffer := make([]byte, 100)
341 // Read requests on Transfer() buffer
342 requests := make(chan ReadRequest)
343 defer close(requests)
345 // Reporting reader error states
346 reader_status := make(chan error)
348 go Transfer(buffer, reader, requests, reader_status)
350 out := make([]byte, 101)
353 status := <-reader_status
354 c.Check(status, Equals, io.ErrShortBuffer)