package keepclient
import (
+ "crypto/md5"
"flag"
"fmt"
. "gopkg.in/check.v1"
"net/http"
"os"
"os/exec"
+ "sort"
"testing"
"time"
)
c.Check(status, Equals, io.ErrShortBuffer)
}
-type StubHandler struct {
+func (s *StandaloneSuite) TestTransferFromBuffer(c *C) {
+ // Buffer for reads from 'r'
+ buffer := make([]byte, 100)
+ for i := 0; i < 100; i += 1 {
+ buffer[i] = byte(i)
+ }
+
+ // Read requests on Transfer() buffer
+ requests := make(chan ReadRequest)
+ defer close(requests)
+
+ go Transfer(buffer, nil, requests, nil)
+
+ br1 := MakeBufferReader(requests)
+
+ in := make([]byte, 64)
+ {
+ n, err := br1.Read(in)
+
+ c.Check(n, Equals, 64)
+ c.Check(err, Equals, nil)
+
+ for i := 0; i < 64; i += 1 {
+ c.Check(in[i], Equals, buffer[i])
+ }
+ }
+ {
+ n, err := br1.Read(in)
+
+ c.Check(n, Equals, 36)
+ c.Check(err, Equals, nil)
+
+ for i := 0; i < 36; i += 1 {
+ c.Check(in[i], Equals, buffer[64+i])
+ }
+ }
+ {
+ n, err := br1.Read(in)
+
+ c.Check(n, Equals, 0)
+ c.Check(err, Equals, io.EOF)
+ }
+}
+
+func (s *StandaloneSuite) TestTransferIoCopy(c *C) {
+ // Buffer for reads from 'r'
+ buffer := make([]byte, 100)
+ for i := 0; i < 100; i += 1 {
+ buffer[i] = byte(i)
+ }
+
+ // Read requests on Transfer() buffer
+ requests := make(chan ReadRequest)
+ defer close(requests)
+
+ go Transfer(buffer, nil, requests, nil)
+
+ br1 := MakeBufferReader(requests)
+
+ reader, writer := io.Pipe()
+
+ go func() {
+ p := make([]byte, 100)
+ n, err := reader.Read(p)
+ c.Check(n, Equals, 100)
+ c.Check(err, Equals, nil)
+ c.Check(p, DeepEquals, buffer)
+ }()
+
+ io.Copy(writer, br1)
+}
+
+type StubPutHandler struct {
c *C
expectPath string
expectApiToken string
expectBody string
- handled chan bool
+ handled chan string
}
-func (this StubHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
- this.c.Check(req.URL.Path, Equals, this.expectPath)
+func (this StubPutHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
+ this.c.Check(req.URL.Path, Equals, "/"+this.expectPath)
this.c.Check(req.Header.Get("Authorization"), Equals, fmt.Sprintf("OAuth2 %s", this.expectApiToken))
body, err := ioutil.ReadAll(req.Body)
this.c.Check(err, Equals, nil)
this.c.Check(body, DeepEquals, []byte(this.expectBody))
resp.WriteHeader(200)
- this.handled <- true
+ this.handled <- fmt.Sprintf("http://%s", req.Host)
}
-func (s *StandaloneSuite) TestUploadToStubKeepServer(c *C) {
- st := StubHandler{
- c,
- "/acbd18db4cc2f85cedef654fccc4a4d8",
- "abc123",
- "foo",
- make(chan bool)}
+func RunBogusKeepServer(st http.Handler, port int) (listener net.Listener, url string) {
server := http.Server{Handler: st}
- listener, _ := net.ListenTCP("tcp", &net.TCPAddr{Port: 2999})
- defer listener.Close()
+ var err error
+ listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: port})
+ if err != nil {
+ panic(fmt.Sprintf("Could not listen on tcp port %v", port))
+ }
- log.Printf("%s", listener.Addr().String())
+ url = fmt.Sprintf("http://localhost:%d", listener.Addr().(*net.TCPAddr).Port)
go server.Serve(listener)
+ return listener, url
+}
+
+func UploadToStubHelper(c *C, st http.Handler, f func(*KeepClient, string,
+ io.ReadCloser, io.WriteCloser, chan UploadStatus)) {
+
+ listener, url := RunBogusKeepServer(st, 2990)
+ defer listener.Close()
+
kc, _ := MakeKeepClient()
kc.ApiToken = "abc123"
reader, writer := io.Pipe()
- upload_status := make(chan UploadError)
+ upload_status := make(chan UploadStatus)
+
+ f(kc, url, reader, writer, upload_status)
+}
+
+func (s *StandaloneSuite) TestUploadToStubKeepServer(c *C) {
+ st := StubPutHandler{
+ c,
+ "acbd18db4cc2f85cedef654fccc4a4d8",
+ "abc123",
+ "foo",
+ make(chan string)}
+
+ UploadToStubHelper(c, st,
+ func(kc *KeepClient, url string, reader io.ReadCloser,
+ writer io.WriteCloser, upload_status chan UploadStatus) {
+
+ go kc.uploadToKeepServer(url, st.expectPath, reader, upload_status, int64(len("foo")))
+
+ writer.Write([]byte("foo"))
+ writer.Close()
+
+ <-st.handled
+ status := <-upload_status
+ c.Check(status, DeepEquals, UploadStatus{nil, fmt.Sprintf("%s/%s", url, st.expectPath), 200})
+ })
+}
+
+func (s *StandaloneSuite) TestUploadToStubKeepServerBufferReader(c *C) {
+ st := StubPutHandler{
+ c,
+ "acbd18db4cc2f85cedef654fccc4a4d8",
+ "abc123",
+ "foo",
+ make(chan string)}
+
+ UploadToStubHelper(c, st,
+ func(kc *KeepClient, url string, reader io.ReadCloser,
+ writer io.WriteCloser, upload_status chan UploadStatus) {
- go kc.uploadToKeepServer("http://localhost:2999", "acbd18db4cc2f85cedef654fccc4a4d8", reader, upload_status)
+ // Buffer for reads from 'r'
+ buffer := make([]byte, 512)
- writer.Write([]byte("foo"))
- writer.Close()
+ // Read requests on Transfer() buffer
+ requests := make(chan ReadRequest)
+ defer close(requests)
- <-st.handled
- status := <-upload_status
- c.Check(status, DeepEquals, UploadError{io.EOF, "http://localhost:2999/acbd18db4cc2f85cedef654fccc4a4d8"})
+ // Reporting reader error states
+ reader_status := make(chan error)
+
+ go Transfer(buffer, reader, requests, reader_status)
+
+ br1 := MakeBufferReader(requests)
+
+ go kc.uploadToKeepServer(url, st.expectPath, br1, upload_status, 3)
+
+ writer.Write([]byte("foo"))
+ writer.Close()
+
+ <-reader_status
+ <-st.handled
+
+ status := <-upload_status
+ c.Check(status, DeepEquals, UploadStatus{nil, fmt.Sprintf("%s/%s", url, st.expectPath), 200})
+
+ //c.Check(true, Equals, false)
+ })
}
type FailHandler struct {
- handled chan bool
+ handled chan string
}
func (this FailHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(400)
- this.handled <- true
+ this.handled <- fmt.Sprintf("http://%s", req.Host)
}
-/*func (s *StandaloneSuite) TestFailedUploadToStubKeepServer(c *C) {
- log.Printf("blup")
+func (s *StandaloneSuite) TestFailedUploadToStubKeepServer(c *C) {
+ st := FailHandler{
+ make(chan string)}
- c.Check(true, Equals, false)
+ hash := "acbd18db4cc2f85cedef654fccc4a4d8"
- log.Printf("blug")
+ UploadToStubHelper(c, st,
+ func(kc *KeepClient, url string, reader io.ReadCloser,
+ writer io.WriteCloser, upload_status chan UploadStatus) {
- st := FailHandler{make(chan bool)}
- server := http.Server{Handler: st}
+ go kc.uploadToKeepServer(url, hash, reader, upload_status, 3)
- listener, _ := net.ListenTCP("tcp", &net.TCPAddr{})
- defer listener.Close()
+ writer.Write([]byte("foo"))
+ writer.Close()
+
+ <-st.handled
+
+ status := <-upload_status
+ c.Check(status.Url, Equals, fmt.Sprintf("%s/%s", url, hash))
+ c.Check(status.StatusCode, Equals, 400)
+ })
+
+}
+
+type KeepServer struct {
+ listener net.Listener
+ url string
+}
+
+func RunSomeFakeKeepServers(st http.Handler, n int, port int) (ks []KeepServer) {
+ ks = make([]KeepServer, n)
+
+ for i := 0; i < n; i += 1 {
+ boguslistener, bogusurl := RunBogusKeepServer(st, port+i)
+ ks[i] = KeepServer{boguslistener, bogusurl}
+ }
+
+ return ks
+}
+
+func (s *StandaloneSuite) TestPutB(c *C) {
+ log.Printf("TestPutB")
+
+ hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+ st := StubPutHandler{
+ c,
+ hash,
+ "abc123",
+ "foo",
+ make(chan string, 2)}
- go server.Serve(listener)
kc, _ := MakeKeepClient()
+
+ kc.Want_replicas = 2
+ kc.ApiToken = "abc123"
+ kc.Service_roots = make([]string, 5)
+
+ ks := RunSomeFakeKeepServers(st, 5, 2990)
+
+ for i := 0; i < len(ks); i += 1 {
+ kc.Service_roots[i] = ks[i].url
+ defer ks[i].listener.Close()
+ }
+
+ sort.Strings(kc.Service_roots)
+
+ kc.PutB([]byte("foo"))
+
+ shuff := kc.ShuffledServiceRoots(fmt.Sprintf("%x", md5.Sum([]byte("foo"))))
+
+ c.Check(<-st.handled, Equals, shuff[0])
+ c.Check(<-st.handled, Equals, shuff[1])
+}
+
+func (s *StandaloneSuite) TestPutHR(c *C) {
+ log.Printf("TestPutHR")
+
+ hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+ st := StubPutHandler{
+ c,
+ hash,
+ "abc123",
+ "foo",
+ make(chan string, 2)}
+
+ kc, _ := MakeKeepClient()
+
+ kc.Want_replicas = 2
kc.ApiToken = "abc123"
+ kc.Service_roots = make([]string, 5)
+
+ ks := RunSomeFakeKeepServers(st, 5, 2990)
+
+ for i := 0; i < len(ks); i += 1 {
+ kc.Service_roots[i] = ks[i].url
+ defer ks[i].listener.Close()
+ }
+
+ sort.Strings(kc.Service_roots)
reader, writer := io.Pipe()
- upload_status := make(chan UploadError)
- go kc.uploadToKeepServer(fmt.Sprintf("http://localhost:%s", listener.Addr().String()), "acbd18db4cc2f85cedef654fccc4a4d8", reader, upload_status)
+ go func() {
+ writer.Write([]byte("foo"))
+ writer.Close()
+ }()
- log.Printf("Writing 1")
+ kc.PutHR(hash, reader, 3)
- writer.Write([]byte("foo"))
+ shuff := kc.ShuffledServiceRoots(hash)
+ log.Print(shuff)
- log.Printf("Writing 2")
+ s1 := <-st.handled
+ s2 := <-st.handled
- writer.Close()
+ c.Check((s1 == shuff[0] && s2 == shuff[1]) ||
+ (s1 == shuff[1] && s2 == shuff[0]),
+ Equals,
+ true)
+}
- log.Printf("Writing 3")
+func (s *StandaloneSuite) TestPutWithFail(c *C) {
+ log.Printf("TestPutWithFail")
- <-st.handled
+ hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+ st := StubPutHandler{
+ c,
+ hash,
+ "abc123",
+ "foo",
+ make(chan string, 2)}
+
+ fh := FailHandler{
+ make(chan string, 1)}
+
+ kc, _ := MakeKeepClient()
+
+ kc.Want_replicas = 2
+ kc.ApiToken = "abc123"
+ kc.Service_roots = make([]string, 5)
+
+ ks1 := RunSomeFakeKeepServers(st, 4, 2990)
+ ks2 := RunSomeFakeKeepServers(fh, 1, 2995)
+
+ for i, k := range ks1 {
+ kc.Service_roots[i] = k.url
+ defer k.listener.Close()
+ }
+ for i, k := range ks2 {
+ kc.Service_roots[len(ks1)+i] = k.url
+ defer k.listener.Close()
+ }
+
+ sort.Strings(kc.Service_roots)
+
+ shuff := kc.ShuffledServiceRoots(fmt.Sprintf("%x", md5.Sum([]byte("foo"))))
+
+ err := kc.PutB([]byte("foo"))
+
+ <-fh.handled
+
+ c.Check(err, Equals, nil)
+ c.Check(<-st.handled, Equals, shuff[1])
+ c.Check(<-st.handled, Equals, shuff[2])
+}
+
+func (s *StandaloneSuite) TestPutWithTooManyFail(c *C) {
+ log.Printf("TestPutWithTooManyFail")
+
+ hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+ st := StubPutHandler{
+ c,
+ hash,
+ "abc123",
+ "foo",
+ make(chan string, 1)}
+
+ fh := FailHandler{
+ make(chan string, 4)}
+
+ kc, _ := MakeKeepClient()
+
+ kc.Want_replicas = 2
+ kc.ApiToken = "abc123"
+ kc.Service_roots = make([]string, 5)
- log.Printf("Handled?!")
+ ks1 := RunSomeFakeKeepServers(st, 1, 2990)
+ ks2 := RunSomeFakeKeepServers(fh, 4, 2991)
- status := <-upload_status
- c.Check(status, DeepEquals, UploadError{io.EOF, "http://localhost:2999/acbd18db4cc2f85cedef654fccc4a4d8"})
-}*/
+ for i, k := range ks1 {
+ kc.Service_roots[i] = k.url
+ defer k.listener.Close()
+ }
+ for i, k := range ks2 {
+ kc.Service_roots[len(ks1)+i] = k.url
+ defer k.listener.Close()
+ }
+
+ sort.Strings(kc.Service_roots)
+
+ shuff := kc.ShuffledServiceRoots(fmt.Sprintf("%x", md5.Sum([]byte("foo"))))
+
+ err := kc.PutB([]byte("foo"))
+
+ c.Check(err, Equals, InsufficientReplicasError)
+ c.Check(<-st.handled, Equals, shuff[1])
+}
+
+type StubGetHandler struct {
+ c *C
+ expectPath string
+ expectApiToken string
+ returnBody []byte
+}
+
+func (this StubGetHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
+ this.c.Check(req.URL.Path, Equals, "/"+this.expectPath)
+ this.c.Check(req.Header.Get("Authorization"), Equals, fmt.Sprintf("OAuth2 %s", this.expectApiToken))
+ resp.Header().Set("Content-Length", fmt.Sprintf("%d", len(this.returnBody)))
+ resp.Write(this.returnBody)
+}
+
+func (s *StandaloneSuite) TestGet(c *C) {
+
+ hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+ st := StubGetHandler{
+ c,
+ hash,
+ "abc123",
+ []byte("foo")}
+
+ listener, url := RunBogusKeepServer(st, 2990)
+ defer listener.Close()
+
+ kc, _ := MakeKeepClient()
+ kc.ApiToken = "abc123"
+ kc.Service_roots = []string{url}
+
+ r, n, url2, err := kc.Get(hash)
+ c.Check(err, Equals, nil)
+ c.Check(n, Equals, int64(3))
+ c.Check(url2, Equals, fmt.Sprintf("%s/%s", url, hash))
+
+ content, err2 := ioutil.ReadAll(r)
+ c.Check(err2, Equals, nil)
+ c.Check(content, DeepEquals, []byte("foo"))
+}
+
+func (s *StandaloneSuite) TestGetFail(c *C) {
+ hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+ st := FailHandler{make(chan string, 1)}
+
+ listener, url := RunBogusKeepServer(st, 2990)
+ defer listener.Close()
+
+ kc, _ := MakeKeepClient()
+ kc.ApiToken = "abc123"
+ kc.Service_roots = []string{url}
+
+ r, n, url2, err := kc.Get(hash)
+ c.Check(err, Equals, BlockNotFound)
+ c.Check(n, Equals, int64(0))
+ c.Check(url2, Equals, "")
+ c.Check(r, Equals, nil)
+}
+
+func (s *StandaloneSuite) TestGetWithFailures(c *C) {
+
+ hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+ fh := FailHandler{
+ make(chan string, 1)}
+
+ st := StubGetHandler{
+ c,
+ hash,
+ "abc123",
+ []byte("foo")}
+
+ kc, _ := MakeKeepClient()
+ kc.ApiToken = "abc123"
+ kc.Service_roots = make([]string, 5)
+
+ ks1 := RunSomeFakeKeepServers(st, 1, 2990)
+ ks2 := RunSomeFakeKeepServers(fh, 4, 2991)
+
+ for i, k := range ks1 {
+ kc.Service_roots[i] = k.url
+ defer k.listener.Close()
+ }
+ for i, k := range ks2 {
+ kc.Service_roots[len(ks1)+i] = k.url
+ defer k.listener.Close()
+ }
+
+ sort.Strings(kc.Service_roots)
+
+ r, n, url2, err := kc.Get(hash)
+ <-fh.handled
+ c.Check(err, Equals, nil)
+ c.Check(n, Equals, int64(3))
+ c.Check(url2, Equals, fmt.Sprintf("%s/%s", ks1[0].url, hash))
+
+ content, err2 := ioutil.ReadAll(r)
+ c.Check(err2, Equals, nil)
+ c.Check(content, DeepEquals, []byte("foo"))
+}