From 96e26335e2aa2eb71b4f664a6b4a4e190a6012a2 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Fri, 27 Mar 2015 23:19:58 -0700 Subject: [PATCH] Adding tests and a fix for get object where bucket is not found --- pkg/api/api_object_handlers.go | 9 +- pkg/api/api_test.go | 343 +++++++++++++++++++++++++++++++-- pkg/drivers/mocks/Driver.go | 115 +++++++++++ 3 files changed, 449 insertions(+), 18 deletions(-) create mode 100644 pkg/drivers/mocks/Driver.go diff --git a/pkg/api/api_object_handlers.go b/pkg/api/api_object_handlers.go index 1599e6aba..05156ee66 100644 --- a/pkg/api/api_object_handlers.go +++ b/pkg/api/api_object_handlers.go @@ -83,6 +83,13 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques w.WriteHeader(error.HTTPStatusCode) w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) } + case drivers.BucketNotFound: + { + error := errorCodeError(NoSuchBucket) + errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + w.WriteHeader(error.HTTPStatusCode) + w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + } case drivers.ObjectNameInvalid: { error := errorCodeError(NoSuchKey) @@ -97,7 +104,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques w.WriteHeader(error.HTTPStatusCode) w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) } - case drivers.ImplementationError: + default: { // Embed errors log on serve side log.Errorln(err) diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go index 9721796e1..c0318d7ff 100644 --- a/pkg/api/api_test.go +++ b/pkg/api/api_test.go @@ -31,18 +31,40 @@ import ( "github.com/minio-io/minio/pkg/api" "github.com/minio-io/minio/pkg/drivers" "github.com/minio-io/minio/pkg/drivers/memory" + "github.com/minio-io/minio/pkg/drivers/mocks" + "github.com/stretchr/testify/mock" . "gopkg.in/check.v1" ) func Test(t *testing.T) { TestingT(t) } -type MySuite struct{} +type MySuite struct { + Driver func() drivers.Driver +} + +var _ = Suite(&MySuite{ + Driver: func() drivers.Driver { + return startDriver() + }, +}) -var _ = Suite(&MySuite{}) +var _ = Suite(&MySuite{ + Driver: func() drivers.Driver { + _, _, driver := memory.Start() + return driver + }, +}) func (s *MySuite) TestNonExistantObject(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + switch typedDriver := driver.(type) { + case *mocks.Driver: + { + typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(drivers.ObjectMetadata{}, drivers.BucketNotFound{Bucket: "bucket"}).Once() + defer typedDriver.AssertExpectations(c) + } + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() @@ -54,7 +76,26 @@ func (s *MySuite) TestNonExistantObject(c *C) { } func (s *MySuite) TestEmptyObject(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + switch typedDriver := driver.(type) { + case *mocks.Driver: + { + metadata := drivers.ObjectMetadata{ + Bucket: "bucket", + Key: "key", + ContentType: "application/octet-stream", + Created: time.Now(), + Md5: "d41d8cd98f00b204e9800998ecf8427e", + Size: 0, + } + typedDriver.On("CreateBucket", "bucket").Return(nil).Once() + typedDriver.On("CreateObject", "bucket", "object", "", "", mock.Anything).Return(nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(metadata, nil).Once() + typedDriver.On("GetObject", mock.Anything, "bucket", "object").Return(int64(0), nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(metadata, nil).Once() + defer typedDriver.AssertExpectations(c) + } + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() @@ -74,12 +115,29 @@ func (s *MySuite) TestEmptyObject(c *C) { metadata, err := driver.GetObjectMetadata("bucket", "object", "") c.Assert(err, IsNil) verifyHeaders(c, response.Header, metadata.Created, 0, "application/octet-stream", metadata.Md5) - - // TODO Test Headers } func (s *MySuite) TestObject(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + switch typedDriver := driver.(type) { + case *mocks.Driver: + { + metadata := drivers.ObjectMetadata{ + Bucket: "bucket", + Key: "key", + ContentType: "application/octet-stream", + Created: time.Now(), + Md5: "5eb63bbbe01eeed093cb22bb8f5acdc3", + Size: 11, + } + typedDriver.On("CreateBucket", "bucket").Return(nil).Once() + typedDriver.On("CreateObject", "bucket", "object", "", "", mock.Anything).Return(nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(metadata, nil).Twice() + typedDriver.SetGetObjectWriter("bucket", "object", []byte("hello world")) + typedDriver.On("GetObject", mock.Anything, "bucket", "object").Return(int64(0), nil).Once() + defer typedDriver.AssertExpectations(c) + } + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() @@ -94,7 +152,8 @@ func (s *MySuite) TestObject(c *C) { responseBody, err := ioutil.ReadAll(response.Body) c.Assert(err, IsNil) - c.Assert(true, Equals, bytes.Equal(responseBody, []byte("hello world"))) + println(string(responseBody)) + c.Assert(responseBody, DeepEquals, []byte("hello world")) metadata, err := driver.GetObjectMetadata("bucket", "object", "") c.Assert(err, IsNil) @@ -102,7 +161,45 @@ func (s *MySuite) TestObject(c *C) { } func (s *MySuite) TestMultipleObjects(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + var typedDriver *mocks.Driver + switch driver := driver.(type) { + case *mocks.Driver: + { + typedDriver = driver + + defer typedDriver.AssertExpectations(c) + } + default: + { + typedDriver = startDriver() + } + } + metadata1 := drivers.ObjectMetadata{ + Bucket: "bucket", + Key: "object1", + ContentType: "application/octet-stream", + Created: time.Now(), + // TODO correct md5 + Md5: "5eb63bbbe01eeed093cb22bb8f5acdc3", + Size: 9, + } + metadata2 := drivers.ObjectMetadata{ + Bucket: "bucket", + Key: "object2", + ContentType: "application/octet-stream", + Created: time.Now(), + Md5: "5eb63bbbe01eeed093cb22bb8f5acdc3", // TODO correct md5 + Size: 9, + } + metadata3 := drivers.ObjectMetadata{ + Bucket: "bucket", + Key: "object3", + ContentType: "application/octet-stream", + Created: time.Now(), + Md5: "5eb63bbbe01eeed093cb22bb8f5acdc3", // TODO correct md5 + Size: 11, + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() @@ -111,12 +208,17 @@ func (s *MySuite) TestMultipleObjects(c *C) { buffer2 := bytes.NewBufferString("hello two") buffer3 := bytes.NewBufferString("hello three") + typedDriver.On("CreateBucket", "bucket").Return(nil).Once() driver.CreateBucket("bucket") + typedDriver.On("CreateObject", "bucket", "object1", "", "", mock.Anything).Return(nil).Once() driver.CreateObject("bucket", "object1", "", "", buffer1) + typedDriver.On("CreateObject", "bucket", "object2", "", "", mock.Anything).Return(nil).Once() driver.CreateObject("bucket", "object2", "", "", buffer2) + typedDriver.On("CreateObject", "bucket", "object3", "", "", mock.Anything).Return(nil).Once() driver.CreateObject("bucket", "object3", "", "", buffer3) // test non-existant object + typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(drivers.ObjectMetadata{}, drivers.ObjectNotFound{}).Once() response, err := http.Get(testServer.URL + "/bucket/object") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusNotFound) @@ -125,10 +227,14 @@ func (s *MySuite) TestMultipleObjects(c *C) { //// test object 1 // get object + typedDriver.On("GetObjectMetadata", "bucket", "object1", "").Return(metadata1, nil).Once() + typedDriver.SetGetObjectWriter("bucket", "object1", []byte("hello one")) + typedDriver.On("GetObject", mock.Anything, "bucket", "object1").Return(int64(0), nil).Once() response, err = http.Get(testServer.URL + "/bucket/object1") c.Assert(err, IsNil) // get metadata + typedDriver.On("GetObjectMetadata", "bucket", "object1", "").Return(metadata1, nil).Once() metadata, err := driver.GetObjectMetadata("bucket", "object1", "") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) @@ -144,10 +250,14 @@ func (s *MySuite) TestMultipleObjects(c *C) { // test object 2 // get object + typedDriver.On("GetObjectMetadata", "bucket", "object2", "").Return(metadata2, nil).Once() + typedDriver.SetGetObjectWriter("bucket", "object2", []byte("hello two")) + typedDriver.On("GetObject", mock.Anything, "bucket", "object2").Return(int64(0), nil).Once() response, err = http.Get(testServer.URL + "/bucket/object2") c.Assert(err, IsNil) // get metadata + typedDriver.On("GetObjectMetadata", "bucket", "object2", "").Return(metadata2, nil).Once() metadata, err = driver.GetObjectMetadata("bucket", "object2", "") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) @@ -163,10 +273,14 @@ func (s *MySuite) TestMultipleObjects(c *C) { // test object 3 // get object + typedDriver.On("GetObjectMetadata", "bucket", "object3", "").Return(metadata3, nil).Once() + typedDriver.SetGetObjectWriter("bucket", "object3", []byte("hello three")) + typedDriver.On("GetObject", mock.Anything, "bucket", "object3").Return(int64(0), nil).Once() response, err = http.Get(testServer.URL + "/bucket/object3") c.Assert(err, IsNil) // get metadata + typedDriver.On("GetObjectMetadata", "bucket", "object3", "").Return(metadata3, nil).Once() metadata, err = driver.GetObjectMetadata("bucket", "object3", "") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) @@ -182,7 +296,20 @@ func (s *MySuite) TestMultipleObjects(c *C) { } func (s *MySuite) TestNotImplemented(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + var typedDriver *mocks.Driver + switch driver := driver.(type) { + case *mocks.Driver: + { + typedDriver = driver + defer typedDriver.AssertExpectations(c) + } + default: + { + // we never assert expectations + typedDriver = startDriver() + } + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() @@ -193,38 +320,86 @@ func (s *MySuite) TestNotImplemented(c *C) { } func (s *MySuite) TestHeader(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + var typedDriver *mocks.Driver + switch driver := driver.(type) { + case *mocks.Driver: + { + typedDriver = driver + defer driver.AssertExpectations(c) + } + default: + { + // we never assert expectations + typedDriver = startDriver() + } + } + + typedDriver.AssertExpectations(c) httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() + typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(drivers.ObjectMetadata{}, drivers.ObjectNotFound{}).Once() response, err := http.Get(testServer.URL + "/bucket/object") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusNotFound) buffer := bytes.NewBufferString("hello world") + typedDriver.On("CreateBucket", "bucket").Return(nil).Once() driver.CreateBucket("bucket") + typedDriver.On("CreateObject", "bucket", "object", "", "", mock.Anything).Return(nil).Once() driver.CreateObject("bucket", "object", "", "", buffer) + objectMetadata := drivers.ObjectMetadata{ + Bucket: "bucket", + Key: "object", + ContentType: "application/octet-stream", + Created: time.Now(), + // TODO correct md5 + Md5: "5eb63bbbe01eeed093cb22bb8f5acdc3", + Size: 11, + } + + typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(objectMetadata, nil).Once() + typedDriver.SetGetObjectWriter("", "", []byte("hello world")) + typedDriver.On("GetObject", mock.Anything, "bucket", "object").Return(int64(0), nil).Once() response, err = http.Get(testServer.URL + "/bucket/object") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) + typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(objectMetadata, nil).Once() metadata, err := driver.GetObjectMetadata("bucket", "object", "") c.Assert(err, IsNil) verifyHeaders(c, response.Header, metadata.Created, len("hello world"), "application/octet-stream", metadata.Md5) } func (s *MySuite) TestPutBucket(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + var typedDriver *mocks.Driver + switch driver := driver.(type) { + case *mocks.Driver: + { + typedDriver = driver + defer driver.AssertExpectations(c) + } + default: + { + // we never assert expectations + typedDriver = startDriver() + } + } + httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() + typedDriver.On("ListBuckets").Return(make([]drivers.BucketMetadata, 0), nil).Once() buckets, err := driver.ListBuckets() c.Assert(len(buckets), Equals, 0) c.Assert(err, IsNil) + typedDriver.On("CreateBucket", "bucket").Return(nil).Once() request, err := http.NewRequest("PUT", testServer.URL+"/bucket", bytes.NewBufferString("")) c.Assert(err, IsNil) @@ -234,6 +409,7 @@ func (s *MySuite) TestPutBucket(c *C) { c.Assert(response.StatusCode, Equals, http.StatusOK) // check bucket exists + typedDriver.On("ListBuckets").Return([]drivers.BucketMetadata{{Name: "bucket"}}, nil).Once() buckets, err = driver.ListBuckets() c.Assert(len(buckets), Equals, 1) c.Assert(err, IsNil) @@ -241,7 +417,20 @@ func (s *MySuite) TestPutBucket(c *C) { } func (s *MySuite) TestPutObject(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + var typedDriver *mocks.Driver + switch driver := driver.(type) { + case *mocks.Driver: + { + typedDriver = driver + defer driver.AssertExpectations(c) + } + default: + { + // we never assert expectations + typedDriver = startDriver() + } + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() @@ -250,6 +439,8 @@ func (s *MySuite) TestPutObject(c *C) { resources.Maxkeys = 1000 resources.Prefix = "" + + typedDriver.On("ListObjects", "bucket", mock.Anything).Return([]drivers.ObjectMetadata{}, drivers.BucketResourcesMetadata{}, drivers.BucketNotFound{}).Once() objects, resources, err := driver.ListObjects("bucket", resources) c.Assert(len(objects), Equals, 0) c.Assert(resources.IsTruncated, Equals, false) @@ -258,6 +449,7 @@ func (s *MySuite) TestPutObject(c *C) { date1 := time.Now() // Put Bucket before - Put Object into a bucket + typedDriver.On("CreateBucket", "bucket").Return(nil).Once() request, err := http.NewRequest("PUT", testServer.URL+"/bucket", bytes.NewBufferString("")) c.Assert(err, IsNil) @@ -266,18 +458,30 @@ func (s *MySuite) TestPutObject(c *C) { c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) + typedDriver.On("CreateObject", "bucket", "two", "", "", mock.Anything).Return(nil).Once() request, err = http.NewRequest("PUT", testServer.URL+"/bucket/two", bytes.NewBufferString("hello world")) + println(err) c.Assert(err, IsNil) response, err = client.Do(request) c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) + twoMetadata := drivers.ObjectMetadata{ + Bucket: "bucket", + Key: "two", + ContentType: "application/octet-stream", + Created: time.Now(), + Md5: "5eb63bbbe01eeed093cb22bb8f5acdc3", + Size: 11, + } + date2 := time.Now() resources.Maxkeys = 1000 resources.Prefix = "" + typedDriver.On("ListObjects", "bucket", mock.Anything).Return([]drivers.ObjectMetadata{{}}, drivers.BucketResourcesMetadata{}, nil).Once() objects, resources, err = driver.ListObjects("bucket", resources) c.Assert(len(objects), Equals, 1) c.Assert(resources.IsTruncated, Equals, false) @@ -285,6 +489,9 @@ func (s *MySuite) TestPutObject(c *C) { var writer bytes.Buffer + typedDriver.On("GetObjectMetadata", "bucket", "two", "").Return(twoMetadata, nil).Once() + typedDriver.SetGetObjectWriter("bucket", "two", []byte("hello world")) + typedDriver.On("GetObject", mock.Anything, "bucket", "two").Return(int64(11), nil).Once() driver.GetObject(&writer, "bucket", "two") c.Assert(bytes.Equal(writer.Bytes(), []byte("hello world")), Equals, true) @@ -298,11 +505,25 @@ func (s *MySuite) TestPutObject(c *C) { } func (s *MySuite) TestListBuckets(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + var typedDriver *mocks.Driver + switch driver := driver.(type) { + case *mocks.Driver: + { + typedDriver = driver + defer driver.AssertExpectations(c) + } + default: + { + // we never assert expectations + typedDriver = startDriver() + } + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() + typedDriver.On("ListBuckets").Return([]drivers.BucketMetadata{}, nil).Once() response, err := http.Get(testServer.URL + "/") defer response.Body.Close() c.Assert(err, IsNil) @@ -312,8 +533,13 @@ func (s *MySuite) TestListBuckets(c *C) { c.Assert(err, IsNil) c.Assert(len(listResponse.Buckets.Bucket), Equals, 0) + typedDriver.On("CreateBucket", "foo").Return(nil).Once() driver.CreateBucket("foo") + bucketMetadata := []drivers.BucketMetadata{ + {Name: "foo", Created: time.Now()}, + } + typedDriver.On("ListBuckets").Return(bucketMetadata, nil).Once() response, err = http.Get(testServer.URL + "/") defer response.Body.Close() c.Assert(err, IsNil) @@ -324,8 +550,15 @@ func (s *MySuite) TestListBuckets(c *C) { c.Assert(len(listResponse.Buckets.Bucket), Equals, 1) c.Assert(listResponse.Buckets.Bucket[0].Name, Equals, "foo") + typedDriver.On("CreateBucket", "bar").Return(nil).Once() driver.CreateBucket("bar") + bucketMetadata = []drivers.BucketMetadata{ + {Name: "bar", Created: time.Now()}, + bucketMetadata[0], + } + + typedDriver.On("ListBuckets").Return(bucketMetadata, nil).Once() response, err = http.Get(testServer.URL + "/") defer response.Body.Close() c.Assert(err, IsNil) @@ -377,14 +610,29 @@ func verifyHeaders(c *C, header http.Header, date time.Time, size int, contentTy } func (s *MySuite) TestXMLNameNotInBucketListJson(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + var typedDriver *mocks.Driver + switch driver := driver.(type) { + case *mocks.Driver: + { + typedDriver = driver + defer driver.AssertExpectations(c) + } + default: + { + // we never assert expectations + typedDriver = startDriver() + } + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() + typedDriver.On("CreateBucket", "foo").Return(nil).Once() err := driver.CreateBucket("foo") c.Assert(err, IsNil) + typedDriver.On("ListBuckets").Return([]drivers.BucketMetadata{{Name: "foo", Created: time.Now()}}, nil) request, err := http.NewRequest("GET", testServer.URL+"/", bytes.NewBufferString("")) c.Assert(err, IsNil) @@ -402,14 +650,29 @@ func (s *MySuite) TestXMLNameNotInBucketListJson(c *C) { } func (s *MySuite) TestXMLNameNotInObjectListJson(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + var typedDriver *mocks.Driver + switch driver := driver.(type) { + case *mocks.Driver: + { + typedDriver = driver + defer driver.AssertExpectations(c) + } + default: + { + // we never assert expectations + typedDriver = startDriver() + } + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() + typedDriver.On("CreateBucket", "foo").Return(nil).Once() err := driver.CreateBucket("foo") c.Assert(err, IsNil) + typedDriver.On("ListObjects", "foo", mock.Anything).Return([]drivers.ObjectMetadata{}, drivers.BucketResourcesMetadata{}, nil).Once() request, err := http.NewRequest("GET", testServer.URL+"/foo", bytes.NewBufferString("")) c.Assert(err, IsNil) @@ -427,15 +690,30 @@ func (s *MySuite) TestXMLNameNotInObjectListJson(c *C) { } func (s *MySuite) TestContentTypePersists(c *C) { - _, _, driver := memory.Start() + driver := s.Driver() + var typedDriver *mocks.Driver + switch driver := driver.(type) { + case *mocks.Driver: + { + typedDriver = driver + defer driver.AssertExpectations(c) + } + default: + { + // we never assert expectations + typedDriver = startDriver() + } + } httpHandler := api.HTTPHandler("", driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() + typedDriver.On("CreateBucket", "bucket").Return(nil).Once() err := driver.CreateBucket("bucket") c.Assert(err, IsNil) client := http.Client{} + typedDriver.On("CreateObject", "bucket", "one", "", "", mock.Anything).Return(nil).Once() request, err := http.NewRequest("PUT", testServer.URL+"/bucket/one", bytes.NewBufferString("hello world")) delete(request.Header, "Content-Type") c.Assert(err, IsNil) @@ -444,6 +722,15 @@ func (s *MySuite) TestContentTypePersists(c *C) { c.Assert(response.StatusCode, Equals, http.StatusOK) // test head + oneMetadata := drivers.ObjectMetadata{ + Bucket: "bucket", + Key: "one", + ContentType: "application/octet-stream", + Created: time.Now(), + Md5: "d41d8cd98f00b204e9800998ecf8427e", + Size: 0, + } + typedDriver.On("GetObjectMetadata", "bucket", "one", "").Return(oneMetadata, nil).Once() request, err = http.NewRequest("HEAD", testServer.URL+"/bucket/one", bytes.NewBufferString("")) c.Assert(err, IsNil) response, err = client.Do(request) @@ -451,9 +738,13 @@ func (s *MySuite) TestContentTypePersists(c *C) { c.Assert(response.Header.Get("Content-Type"), Equals, "application/octet-stream") // test get object + typedDriver.SetGetObjectWriter("bucket", "once", []byte("")) + typedDriver.On("GetObjectMetadata", "bucket", "one", "").Return(oneMetadata, nil).Once() + typedDriver.On("GetObject", mock.Anything, "bucket", "one").Return(int64(0), nil).Once() response, err = http.Get(testServer.URL + "/bucket/one") c.Assert(response.Header.Get("Content-Type"), Equals, "application/octet-stream") + typedDriver.On("CreateObject", "bucket", "two", "", "", mock.Anything).Return(nil).Once() request, err = http.NewRequest("PUT", testServer.URL+"/bucket/two", bytes.NewBufferString("hello world")) delete(request.Header, "Content-Type") request.Header.Add("Content-Type", "application/json") @@ -462,6 +753,16 @@ func (s *MySuite) TestContentTypePersists(c *C) { c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) + twoMetadata := drivers.ObjectMetadata{ + Bucket: "bucket", + Key: "one", + ContentType: "application/octet-stream", + Created: time.Now(), + // Fix MD5 + Md5: "d41d8cd98f00b204e9800998ecf8427e", + Size: 0, + } + typedDriver.On("GetObjectMetadata", "bucket", "two", "").Return(twoMetadata, nil).Once() request, err = http.NewRequest("HEAD", testServer.URL+"/bucket/two", bytes.NewBufferString("")) c.Assert(err, IsNil) response, err = client.Do(request) @@ -469,6 +770,14 @@ func (s *MySuite) TestContentTypePersists(c *C) { c.Assert(response.Header.Get("Content-Type"), Equals, "application/octet-stream") // test get object + typedDriver.On("GetObjectMetadata", "bucket", "two", "").Return(twoMetadata, nil).Once() + typedDriver.On("GetObject", mock.Anything, "bucket", "two").Return(int64(0), nil).Once() response, err = http.Get(testServer.URL + "/bucket/two") c.Assert(response.Header.Get("Content-Type"), Equals, "application/octet-stream") } + +func startDriver() *mocks.Driver { + return &mocks.Driver{ + ObjectWriterData: make(map[string][]byte), + } +} diff --git a/pkg/drivers/mocks/Driver.go b/pkg/drivers/mocks/Driver.go new file mode 100644 index 000000000..7971d16df --- /dev/null +++ b/pkg/drivers/mocks/Driver.go @@ -0,0 +1,115 @@ +package mocks + +import "github.com/minio-io/minio/pkg/drivers" +import "github.com/stretchr/testify/mock" + +import ( + "bytes" + "io" +) + +// Driver is a mock +type Driver struct { + mock.Mock + + ObjectWriterData map[string][]byte +} + +// ListBuckets is a mock +func (m *Driver) ListBuckets() ([]drivers.BucketMetadata, error) { + ret := m.Called() + + r0 := ret.Get(0).([]drivers.BucketMetadata) + r1 := ret.Error(1) + + return r0, r1 +} + +// CreateBucket is a mock +func (m *Driver) CreateBucket(bucket string) error { + ret := m.Called(bucket) + + r0 := ret.Error(0) + + return r0 +} + +// CreateBucketPolicy is a mock +func (m *Driver) CreateBucketPolicy(bucket string, p drivers.BucketPolicy) error { + ret := m.Called(bucket, p) + + r0 := ret.Error(0) + + return r0 +} + +// GetBucketPolicy is a mock +func (m *Driver) GetBucketPolicy(bucket string) (drivers.BucketPolicy, error) { + ret := m.Called(bucket) + + r0 := ret.Get(0).(drivers.BucketPolicy) + r1 := ret.Error(1) + + return r0, r1 +} + +// SetGetObjectWriter is a mock +func (m *Driver) SetGetObjectWriter(bucket, object string, data []byte) { + m.ObjectWriterData[bucket+":"+object] = data + println(string(m.ObjectWriterData["bucket:object"])) +} + +// GetObject is a mock +func (m *Driver) GetObject(w io.Writer, bucket string, object string) (int64, error) { + ret := m.Called(w, bucket, object) + r0 := ret.Get(0).(int64) + r1 := ret.Error(1) + if r1 == nil { + if obj, ok := m.ObjectWriterData[bucket+":"+object]; ok { + n, _ := io.Copy(w, bytes.NewBuffer(obj)) + r0 = n + } + } + + return r0, r1 +} + +// GetPartialObject is a mock +func (m *Driver) GetPartialObject(w io.Writer, bucket string, object string, start int64, length int64) (int64, error) { + ret := m.Called(w, bucket, object, start, length) + + r0 := ret.Get(0).(int64) + r1 := ret.Error(1) + + return r0, r1 +} + +// GetObjectMetadata is a mock +func (m *Driver) GetObjectMetadata(bucket string, object string, prefix string) (drivers.ObjectMetadata, error) { + ret := m.Called(bucket, object, prefix) + + r0 := ret.Get(0).(drivers.ObjectMetadata) + r1 := ret.Error(1) + + return r0, r1 +} + +// ListObjects is a mock +func (m *Driver) ListObjects(bucket string, resources drivers.BucketResourcesMetadata) ([]drivers.ObjectMetadata, drivers.BucketResourcesMetadata, error) { + ret := m.Called(bucket, resources) + + r0 := ret.Get(0).([]drivers.ObjectMetadata) + r1 := ret.Get(1).(drivers.BucketResourcesMetadata) + r2 := ret.Error(2) + + return r0, r1, r2 +} + +// CreateObject is a mock +func (m *Driver) CreateObject(bucket string, key string, contentType string, md5sum string, data io.Reader) error { + ret := m.Called(bucket, key, contentType, md5sum, data) + + r0 := ret.Error(0) + + return r0 +}