From 5878dd5e5007118d1049d6a421483eb274b96a37 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Thu, 6 Nov 2014 22:45:27 -0800 Subject: [PATCH] Cleaning up more code, system generated by config --- .gitignore | 1 + Makefile | 9 ++++++- gateway.go | 36 ++++++++++++++++---------- gateway_test.go | 67 ++++++++++++++++++++++++++++++++++--------------- minio/main.go | 2 +- 5 files changed, 80 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index ead84456e..2ac51d598 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ **/*.swp +/cover/ diff --git a/Makefile b/Makefile index 21f37f526..a418f66f6 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ all: test install test: - godep go test -race github.com/minios/minios + mkdir -p cover + godep go test -race -coverprofile=cover/cover.out github.com/minios/minios godep go test -race github.com/minios/minios/minio install: @@ -17,3 +18,9 @@ restore: env: godep go env + +run: all + minio gateway + +cover: test + go tool cover -html=cover/cover.out diff --git a/gateway.go b/gateway.go index 7b9594c68..b85816633 100644 --- a/gateway.go +++ b/gateway.go @@ -8,34 +8,39 @@ import ( "net/http" ) +// Stores system configuration, populated from CLI or test runner +type GatewayConfig struct { + StorageDriver StorageDriver +} + +// Message for requesting a bucket type BucketRequest struct { name string context Context callback chan Bucket } +// Context interface for security and session information type Context interface{} -type BucketService interface { - Serve(chan BucketRequest) Bucket -} - +// Bucket definition type Bucket interface { GetName(Context) string Get(Context, string) ([]byte, error) Put(Context, string, []byte) error } -type fakeContext struct{} +// Storage driver function, should read from a channel and respond through callback channels +type StorageDriver func(bucket string, input chan ObjectRequest) -func GatewayHandler(w http.ResponseWriter, req *http.Request) { - fmt.Fprintf(w, "Gateway") -} +// TODO remove when building real context +type fakeContext struct{} type GatewayGetHandler struct { requestBucketChan chan BucketRequest } +// GET requests server func (handler GatewayGetHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) bucketName := vars["bucket"] @@ -51,6 +56,8 @@ func (handler GatewayGetHandler) ServeHTTP(w http.ResponseWriter, req *http.Requ object, err := bucket.Get(context, string(path)) if err != nil { http.Error(w, err.Error(), 404) + } else if object == nil { + http.Error(w, errors.New("Object not found").Error(), 404) } else { fmt.Fprintf(w, string(object)) } @@ -76,21 +83,21 @@ func (handler GatewayPutHandler) ServeHTTP(w http.ResponseWriter, req *http.Requ bucket.Put(context, path, object) } -func RegisterGatewayHandlers(router *mux.Router) { +func RegisterGatewayHandlers(router *mux.Router, config GatewayConfig) { requestBucketChan := make(chan BucketRequest) - go SynchronizedBucketService(requestBucketChan) + go SynchronizedBucketService(requestBucketChan, config) getHandler := GatewayGetHandler{requestBucketChan: requestBucketChan} putHandler := GatewayPutHandler{requestBucketChan: requestBucketChan} router.Handle("/{bucket}/{path:.*}", getHandler).Methods("GET") router.Handle("/{bucket}/{path:.*}", putHandler).Methods("PUT") } -func SynchronizedBucketService(input chan BucketRequest) { +func SynchronizedBucketService(input chan BucketRequest, config GatewayConfig) { buckets := make(map[string]*SynchronizedBucket) for request := range input { if buckets[request.name] == nil { bucketChannel := make(chan ObjectRequest) - go inMemoryBucketServer(bucketChannel) + go config.StorageDriver(request.name, bucketChannel) buckets[request.name] = &SynchronizedBucket{ name: request.name, channel: bucketChannel, @@ -159,13 +166,16 @@ func (bucket *SynchronizedBucket) closeChannel() { close(bucket.channel) } -func inMemoryBucketServer(input chan ObjectRequest) { +func InMemoryStorageDriver(bucket string, input chan ObjectRequest) { objects := make(map[string][]byte) for request := range input { + fmt.Println("objects:", objects) switch request.requestType { case "GET": + fmt.Println("GET: " + request.path) request.callback <- objects[request.path] case "PUT": + fmt.Println("PUT: " + request.path) objects[request.path] = request.object request.callback <- nil default: diff --git a/gateway_test.go b/gateway_test.go index eb53e6634..e8e527ccc 100644 --- a/gateway_test.go +++ b/gateway_test.go @@ -1,11 +1,12 @@ package minio import ( + "github.com/gorilla/mux" . "gopkg.in/check.v1" "io/ioutil" - "log" "net/http" "net/http/httptest" + "strings" "testing" ) @@ -16,21 +17,41 @@ var _ = Suite(&MySuite{}) func Test(t *testing.T) { TestingT(t) } func (s *MySuite) TestPrintsGateway(c *C) { - server := httptest.NewServer(http.HandlerFunc(GatewayHandler)) + // set up router with in memory storage driver + router := mux.NewRouter() + config := GatewayConfig{StorageDriver: InMemoryStorageDriver} + RegisterGatewayHandlers(router, config) + server := httptest.NewServer(router) defer server.Close() - res, err := http.Get(server.URL) - if err != nil { - log.Fatal(err) - } - body, err := ioutil.ReadAll(res.Body) - res.Body.Close() - if err != nil { - log.Fatal(err) - } - bodyString := string(body) - if bodyString == "" { - log.Fatal("Expected '', Received '" + bodyString + "'") - } + + // GET request, empty + getReq1, _ := http.NewRequest("GET", server.URL+"/one/two/three", nil) + client := &http.Client{} + resp, err := client.Do(getReq1) + c.Assert(resp.StatusCode, Equals, 404) + c.Assert(err, IsNil) + + // assert object not found response + body, _ := ioutil.ReadAll(resp.Body) + c.Assert(string(body), Equals, "Object not found\n") + c.Assert(err, IsNil) + + // add new object + putReq, _ := http.NewRequest("PUT", server.URL+"/one/two/three", strings.NewReader("hello")) + resp, err = client.Do(putReq) + c.Assert(resp.StatusCode, Equals, 200) + c.Assert(err, IsNil) + + // verify object exists + getReq2, _ := http.NewRequest("GET", server.URL+"/one/two/three", strings.NewReader("hello")) + resp, err = client.Do(getReq2) + c.Assert(resp.StatusCode, Equals, 200) + c.Assert(err, IsNil) + + // verify object's contents + body2, _ := ioutil.ReadAll(resp.Body) + c.Assert(string(body2), Equals, "hello") + c.Assert(err, IsNil) } type TestContext struct{} @@ -38,9 +59,10 @@ type TestContext struct{} func (s *MySuite) TestBucketCreation(c *C) { requestBucketChan := make(chan BucketRequest) defer close(requestBucketChan) - go SynchronizedBucketService(requestBucketChan) + go SynchronizedBucketService(requestBucketChan, GatewayConfig{StorageDriver: InMemoryStorageDriver}) context := TestContext{} + // get new bucket A var bucketA1 Bucket callback := make(chan Bucket) requestBucketChan <- BucketRequest{ @@ -51,6 +73,7 @@ func (s *MySuite) TestBucketCreation(c *C) { bucketA1 = <-callback c.Assert(bucketA1.GetName(context), Equals, "bucketA") + // get bucket A again var bucketA2 Bucket callback = make(chan Bucket) requestBucketChan <- BucketRequest{ @@ -60,9 +83,9 @@ func (s *MySuite) TestBucketCreation(c *C) { } bucketA2 = <-callback c.Assert(bucketA2.GetName(context), Equals, "bucketA") - c.Assert(bucketA1, DeepEquals, bucketA2) + // get new bucket B var bucketB Bucket callback = make(chan Bucket) requestBucketChan <- BucketRequest{ @@ -74,29 +97,33 @@ func (s *MySuite) TestBucketCreation(c *C) { c.Assert(bucketB.GetName(context), Equals, "bucketB") } -func (s *MySuite) TestBucketOperations(c *C) { +func (s *MySuite) TestInMemoryBucketOperations(c *C) { + // Test in memory bucket operations requestBucketChan := make(chan BucketRequest) defer close(requestBucketChan) - go SynchronizedBucketService(requestBucketChan) + go SynchronizedBucketService(requestBucketChan, GatewayConfig{StorageDriver: InMemoryStorageDriver}) context := TestContext{} + // get bucket callback := make(chan Bucket) requestBucketChan <- BucketRequest{ name: "bucket", context: context, callback: callback, } - bucket := <-callback c.Assert(bucket.GetName(context), Equals, "bucket") + // get missing value nilResult, err := bucket.Get(context, "foo") c.Assert(nilResult, IsNil) c.Assert(err, IsNil) + // add new value err = bucket.Put(context, "foo", []byte("bar")) c.Assert(err, IsNil) + // retrieve value barResult, err := bucket.Get(context, "foo") c.Assert(err, IsNil) c.Assert(string(barResult), Equals, "bar") diff --git a/minio/main.go b/minio/main.go index cfb4e60b4..3e1c9609a 100644 --- a/minio/main.go +++ b/minio/main.go @@ -26,7 +26,7 @@ func main() { Name: "gateway", Usage: "Start a gateway node", Action: func(c *cli.Context) { - minio.RegisterGatewayHandlers(router) + minio.RegisterGatewayHandlers(router, minio.GatewayConfig{StorageDriver: minio.InMemoryStorageDriver}) runServer = true }, },