Skip to content

Commit

Permalink
Fix custom-time error (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
RachitSharma2001 committed Jan 9, 2023
1 parent c6b5350 commit 2e73049
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 35 deletions.
68 changes: 33 additions & 35 deletions fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ObjectAttrs struct {
Created time.Time
Updated time.Time
Deleted time.Time
CustomTime time.Time
Generation int64
Metadata map[string]string
}
Expand All @@ -50,24 +51,27 @@ func (o *ObjectAttrs) id() string {
return o.BucketName + "/" + o.Name
}

type jsonObject struct {
BucketName string `json:"bucket"`
Name string `json:"name"`
Size int64 `json:"size,string"`
ContentType string `json:"contentType"`
ContentEncoding string `json:"contentEncoding"`
Crc32c string `json:"crc32c,omitempty"`
Md5Hash string `json:"md5Hash,omitempty"`
Etag string `json:"etag,omitempty"`
ACL []aclRule `json:"acl,omitempty"`
Created time.Time `json:"created,omitempty"`
Updated time.Time `json:"updated,omitempty"`
Deleted time.Time `json:"deleted,omitempty"`
CustomTime time.Time `json:"customTime,omitempty"`
Generation int64 `json:"generation,omitempty,string"`
Metadata map[string]string `json:"metadata,omitempty"`
}

// MarshalJSON for ObjectAttrs to use ACLRule instead of storage.ACLRule
func (o ObjectAttrs) MarshalJSON() ([]byte, error) {
temp := struct {
BucketName string `json:"bucket"`
Name string `json:"name"`
Size int64 `json:"size,string"`
ContentType string `json:"contentType"`
ContentEncoding string `json:"contentEncoding"`
Crc32c string `json:"crc32c,omitempty"`
Md5Hash string `json:"md5Hash,omitempty"`
Etag string `json:"etag,omitempty"`
ACL []aclRule `json:"acl,omitempty"`
Created time.Time `json:"created,omitempty"`
Updated time.Time `json:"updated,omitempty"`
Deleted time.Time `json:"deleted,omitempty"`
Generation int64 `json:"generation,omitempty,string"`
Metadata map[string]string `json:"metadata,omitempty"`
}{
temp := jsonObject{
BucketName: o.BucketName,
Name: o.Name,
ContentType: o.ContentType,
Expand All @@ -79,6 +83,7 @@ func (o ObjectAttrs) MarshalJSON() ([]byte, error) {
Created: o.Created,
Updated: o.Updated,
Deleted: o.Deleted,
CustomTime: o.CustomTime,
Generation: o.Generation,
Metadata: o.Metadata,
}
Expand All @@ -91,22 +96,7 @@ func (o ObjectAttrs) MarshalJSON() ([]byte, error) {

// UnmarshalJSON for ObjectAttrs to use ACLRule instead of storage.ACLRule
func (o *ObjectAttrs) UnmarshalJSON(data []byte) error {
temp := struct {
BucketName string `json:"bucket"`
Name string `json:"name"`
Size int64 `json:"size,string"`
ContentType string `json:"contentType"`
ContentEncoding string `json:"contentEncoding"`
Crc32c string `json:"crc32c,omitempty"`
Md5Hash string `json:"md5Hash,omitempty"`
Etag string `json:"etag,omitempty"`
ACL []aclRule `json:"acl,omitempty"`
Created time.Time `json:"created,omitempty"`
Updated time.Time `json:"updated,omitempty"`
Deleted time.Time `json:"deleted,omitempty"`
Generation int64 `json:"generation,omitempty,string"`
Metadata map[string]string `json:"metadata,omitempty"`
}{}
var temp jsonObject
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
Expand All @@ -123,6 +113,7 @@ func (o *ObjectAttrs) UnmarshalJSON(data []byte) error {
o.Deleted = temp.Deleted
o.Generation = temp.Generation
o.Metadata = temp.Metadata
o.CustomTime = temp.CustomTime
o.ACL = make([]storage.ACLRule, len(temp.ACL))
for i, ACL := range temp.ACL {
o.ACL[i] = storage.ACLRule(ACL)
Expand Down Expand Up @@ -416,6 +407,7 @@ func toBackendObjects(objects []StreamingObject) []backend.StreamingObject {
Created: getCurrentIfZero(o.Created).Format(timestampFormat),
Deleted: o.Deleted.Format(timestampFormat),
Updated: getCurrentIfZero(o.Updated).Format(timestampFormat),
CustomTime: o.CustomTime.Format(timestampFormat),
Generation: o.Generation,
Metadata: o.Metadata,
},
Expand All @@ -439,6 +431,7 @@ func bufferedObjectsToBackendObjects(objects []Object) []backend.StreamingObject
Created: getCurrentIfZero(o.Created).Format(timestampFormat),
Deleted: o.Deleted.Format(timestampFormat),
Updated: getCurrentIfZero(o.Updated).Format(timestampFormat),
CustomTime: o.CustomTime.Format(timestampFormat),
Generation: o.Generation,
Metadata: o.Metadata,
},
Expand All @@ -465,6 +458,7 @@ func fromBackendObjects(objects []backend.StreamingObject) []StreamingObject {
Created: convertTimeWithoutError(o.Created),
Deleted: convertTimeWithoutError(o.Deleted),
Updated: convertTimeWithoutError(o.Updated),
CustomTime: convertTimeWithoutError(o.CustomTime),
Generation: o.Generation,
Metadata: o.Metadata,
},
Expand All @@ -490,6 +484,7 @@ func fromBackendObjectsAttrs(objectAttrs []backend.ObjectAttrs) []ObjectAttrs {
Created: convertTimeWithoutError(o.Created),
Deleted: convertTimeWithoutError(o.Deleted),
Updated: convertTimeWithoutError(o.Updated),
CustomTime: convertTimeWithoutError(o.CustomTime),
Generation: o.Generation,
Metadata: o.Metadata,
})
Expand Down Expand Up @@ -945,8 +940,9 @@ func (s *Server) patchObject(r *http.Request) jsonResponse {
}

var payload struct {
Metadata map[string]string `json:"metadata"`
Acl []acls
Metadata map[string]string `json:"metadata"`
CustomTime string
Acl []acls
}
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
Expand All @@ -959,6 +955,7 @@ func (s *Server) patchObject(r *http.Request) jsonResponse {
var attrsToUpdate backend.ObjectAttrs

attrsToUpdate.Metadata = payload.Metadata
attrsToUpdate.CustomTime = payload.CustomTime

if len(payload.Acl) > 0 {
attrsToUpdate.ACL = []storage.ACLRule{}
Expand All @@ -978,7 +975,8 @@ func (s *Server) patchObject(r *http.Request) jsonResponse {
defer backendObj.Close()

s.eventManager.Trigger(&backendObj, notification.EventMetadata, nil)
return jsonResponse{data: fromBackendObjects([]backend.StreamingObject{backendObj})[0]}
resp := jsonResponse{data: fromBackendObjects([]backend.StreamingObject{backendObj})[0]}
return resp
}

func (s *Server) updateObject(r *http.Request) jsonResponse {
Expand Down
41 changes: 41 additions & 0 deletions fakestorage/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,47 @@ func checkObjectMetadata(actual, expected map[string]string, t *testing.T) {
}
}

func TestServerClientObjectPatchCustomTime(t *testing.T) {
const (
bucketName = "some-bucket"
objectName = "items/data.txt"
content = "some nice content"
contentType = "text/plain; charset=utf-8"
)
startTime := time.Now().Truncate(time.Second)
objs := []Object{
{
ObjectAttrs: ObjectAttrs{
BucketName: bucketName,
Name: objectName,
ContentType: contentType,
CustomTime: startTime.Add(-5 * time.Hour),
},
Content: []byte(content),
},
}

runServersTest(t, runServersOptions{objs: objs}, func(t *testing.T, server *Server) {
client := server.Client()
objHandle := client.Bucket(bucketName).Object(objectName)

ctx := context.TODO()
_, err := objHandle.Update(ctx, storage.ObjectAttrsToUpdate{CustomTime: startTime})
if err != nil {
t.Fatal(err)
}

attrs, err := objHandle.Attrs(ctx)
if err != nil {
t.Fatal(err)
}

if !attrs.CustomTime.Equal(startTime) {
t.Errorf("unexpected custom time\nwant %q\ngot %q", startTime.String(), attrs.CustomTime.String())
}
})
}

func TestParseRangeRequest(t *testing.T) {
ctx := context.TODO()

Expand Down
2 changes: 2 additions & 0 deletions fakestorage/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type objectResponse struct {
TimeDeleted string `json:"timeDeleted,omitempty"`
Updated string `json:"updated,omitempty"`
Generation int64 `json:"generation,string"`
CustomTime string `json:"customTime,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}

Expand All @@ -131,6 +132,7 @@ func newObjectResponse(obj ObjectAttrs) objectResponse {
TimeCreated: formatTime(obj.Created),
TimeDeleted: formatTime(obj.Deleted),
Updated: formatTime(obj.Updated),
CustomTime: formatTime(obj.CustomTime),
Generation: obj.Generation,
}
}
Expand Down
8 changes: 8 additions & 0 deletions fakestorage/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net/url"
"strconv"
"strings"
"time"

"cloud.google.com/go/storage"
"github.com/fsouza/fake-gcs-server/internal/backend"
Expand All @@ -35,6 +36,7 @@ const (
type multipartMetadata struct {
ContentType string `json:"contentType"`
ContentEncoding string `json:"contentEncoding"`
CustomTime time.Time `json:"customTime,omitempty"`
Name string `json:"name"`
Metadata map[string]string `json:"metadata"`
}
Expand Down Expand Up @@ -193,6 +195,7 @@ func (s *Server) simpleUpload(bucketName string, r *http.Request) jsonResponse {
name := r.URL.Query().Get("name")
predefinedACL := r.URL.Query().Get("predefinedAcl")
contentEncoding := r.URL.Query().Get("contentEncoding")
customTime := r.URL.Query().Get("customTime")
if name == "" {
return jsonResponse{
status: http.StatusBadRequest,
Expand All @@ -205,6 +208,7 @@ func (s *Server) simpleUpload(bucketName string, r *http.Request) jsonResponse {
Name: name,
ContentType: r.Header.Get(contentTypeHeader),
ContentEncoding: contentEncoding,
CustomTime: convertTimeWithoutError(customTime),
ACL: getObjectACL(predefinedACL),
},
Content: notImplementedSeeker{r.Body},
Expand All @@ -230,6 +234,7 @@ func (s *Server) signedUpload(bucketName string, r *http.Request) jsonResponse {
name := unescapeMuxVars(mux.Vars(r))["objectName"]
predefinedACL := r.URL.Query().Get("predefinedAcl")
contentEncoding := r.URL.Query().Get("contentEncoding")
customTime := r.URL.Query().Get("customTime")

// Load data from HTTP Headers
if contentEncoding == "" {
Expand All @@ -250,6 +255,7 @@ func (s *Server) signedUpload(bucketName string, r *http.Request) jsonResponse {
Name: name,
ContentType: r.Header.Get(contentTypeHeader),
ContentEncoding: contentEncoding,
CustomTime: convertTimeWithoutError(customTime),
ACL: getObjectACL(predefinedACL),
Metadata: metaData,
},
Expand Down Expand Up @@ -337,6 +343,7 @@ func (s *Server) multipartUpload(bucketName string, r *http.Request) jsonRespons
Name: objName,
ContentType: contentType,
ContentEncoding: metadata.ContentEncoding,
CustomTime: metadata.CustomTime,
ACL: getObjectACL(predefinedACL),
Metadata: metadata.Metadata,
},
Expand Down Expand Up @@ -378,6 +385,7 @@ func (s *Server) resumableUpload(bucketName string, r *http.Request) jsonRespons
Name: objName,
ContentType: metadata.ContentType,
ContentEncoding: contentEncoding,
CustomTime: metadata.CustomTime,
ACL: getObjectACL(predefinedACL),
Metadata: metadata.Metadata,
},
Expand Down
6 changes: 6 additions & 0 deletions fakestorage/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"reflect"
"strings"
"testing"
"time"

"cloud.google.com/go/storage"
"github.com/fsouza/fake-gcs-server/internal/checksum"
Expand Down Expand Up @@ -70,10 +71,12 @@ func TestServerClientObjectWriter(t *testing.T) {
server.CreateBucketWithOpts(CreateBucketOpts{Name: test.bucketName})
client := server.Client()

customTime := time.Now().Truncate(time.Second).Add(-time.Hour)
objHandle := client.Bucket(test.bucketName).Object(test.objectName)
w := objHandle.NewWriter(context.Background())
w.ChunkSize = test.chunkSize
w.ContentType = contentType
w.CustomTime = customTime
w.Metadata = map[string]string{
"foo": "bar",
}
Expand Down Expand Up @@ -115,6 +118,9 @@ func TestServerClientObjectWriter(t *testing.T) {
if !reflect.DeepEqual(obj.Metadata, w.Metadata) {
t.Errorf("wrong meta data\nwant %+v\ngot %+v", w.Metadata, obj.Metadata)
}
if !customTime.Equal(obj.CustomTime) {
t.Errorf("wrong custom time\nwant %q\ngot %q", customTime.String(), obj.CustomTime.String())
}

reader, err := client.Bucket(test.bucketName).Object(test.objectName).NewReader(context.Background())
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/backend/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ObjectAttrs struct {
Created string
Deleted string
Updated string
CustomTime string
Generation int64
}

Expand Down

0 comments on commit 2e73049

Please sign in to comment.