Skip to content

Commit

Permalink
fakestorage: don't expose the Backend field
Browse files Browse the repository at this point in the history
Make it available via a method instead. This way at least the reference
is readonly.
  • Loading branch information
fsouza committed May 10, 2023
1 parent 275d9c6 commit 3881804
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
16 changes: 8 additions & 8 deletions fakestorage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var bucketRegexp = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]$`)
//
// Deprecated: use CreateBucketWithOpts.
func (s *Server) CreateBucket(name string) {
err := s.Backend.CreateBucket(name, false)
err := s.backend.CreateBucket(name, false)
if err != nil {
panic(err)
}
Expand All @@ -43,7 +43,7 @@ type CreateBucketOpts struct {
//
// If the underlying backend returns an error, this method panics.
func (s *Server) CreateBucketWithOpts(opts CreateBucketOpts) {
err := s.Backend.CreateBucket(opts.Name, opts.VersioningEnabled)
err := s.backend.CreateBucket(opts.Name, opts.VersioningEnabled)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func (s *Server) createBucketByPost(r *http.Request) jsonResponse {
return jsonResponse{errorMessage: err.Error(), status: http.StatusBadRequest}
}

_, err := s.Backend.GetBucket(name)
_, err := s.backend.GetBucket(name)
if err == nil {
return jsonResponse{
errorMessage: fmt.Sprintf(
Expand All @@ -84,20 +84,20 @@ func (s *Server) createBucketByPost(r *http.Request) jsonResponse {
}

// Create the named bucket
if err := s.Backend.CreateBucket(name, versioning); err != nil {
if err := s.backend.CreateBucket(name, versioning); err != nil {
return jsonResponse{errorMessage: err.Error()}
}

// Return the created bucket:
bucket, err := s.Backend.GetBucket(name)
bucket, err := s.backend.GetBucket(name)
if err != nil {
return jsonResponse{errorMessage: err.Error()}
}
return jsonResponse{data: newBucketResponse(bucket, s.options.BucketsLocation)}
}

func (s *Server) listBuckets(r *http.Request) jsonResponse {
buckets, err := s.Backend.ListBuckets()
buckets, err := s.backend.ListBuckets()
if err != nil {
return jsonResponse{errorMessage: err.Error()}
}
Expand All @@ -106,7 +106,7 @@ func (s *Server) listBuckets(r *http.Request) jsonResponse {

func (s *Server) getBucket(r *http.Request) jsonResponse {
bucketName := unescapeMuxVars(mux.Vars(r))["bucketName"]
bucket, err := s.Backend.GetBucket(bucketName)
bucket, err := s.backend.GetBucket(bucketName)
if err != nil {
return jsonResponse{status: http.StatusNotFound}
}
Expand All @@ -115,7 +115,7 @@ func (s *Server) getBucket(r *http.Request) jsonResponse {

func (s *Server) deleteBucket(r *http.Request) jsonResponse {
bucketName := unescapeMuxVars(mux.Vars(r))["bucketName"]
err := s.Backend.DeleteBucket(bucketName)
err := s.backend.DeleteBucket(bucketName)
if err == backend.BucketNotFound {
return jsonResponse{status: http.StatusNotFound}
}
Expand Down
22 changes: 11 additions & 11 deletions fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ func (s *Server) CreateObjectStreaming(obj StreamingObject) error {
}

func (s *Server) createObject(obj StreamingObject, conditions backend.Conditions) (StreamingObject, error) {
oldBackendObj, err := s.Backend.GetObject(obj.BucketName, obj.Name)
oldBackendObj, err := s.backend.GetObject(obj.BucketName, obj.Name)
// Calling Close before checking err is okay on objects, and the object
// may need to be closed whether or not there's an error.
defer oldBackendObj.Close() //lint:ignore SA5001 // see above

prevVersionExisted := err == nil

// The caller is responsible for closing the created object.
newBackendObj, err := s.Backend.CreateObject(toBackendObjects([]StreamingObject{obj})[0], conditions)
newBackendObj, err := s.backend.CreateObject(toBackendObjects([]StreamingObject{obj})[0], conditions)
if err != nil {
return StreamingObject{}, err
}
Expand All @@ -304,7 +304,7 @@ func (s *Server) createObject(obj StreamingObject, conditions backend.Conditions
"overwrittenByGeneration": strconv.FormatInt(newBackendObj.Generation, 10),
}

bucket, _ := s.Backend.GetBucket(obj.BucketName)
bucket, _ := s.backend.GetBucket(obj.BucketName)
if bucket.VersioningEnabled {
s.eventManager.Trigger(&oldBackendObj, notification.EventArchive, oldObjEventAttr)
} else {
Expand Down Expand Up @@ -339,7 +339,7 @@ func (s *Server) ListObjects(bucketName, prefix, delimiter string, versions bool
}

func (s *Server) ListObjectsWithOptions(bucketName string, options ListOptions) ([]ObjectAttrs, []string, error) {
backendObjects, err := s.Backend.ListObjects(bucketName, options.Prefix, options.Versions)
backendObjects, err := s.backend.ListObjects(bucketName, options.Prefix, options.Versions)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -514,7 +514,7 @@ func (s *Server) GetObject(bucketName, objectName string) (Object, error) {
// GetObjectStreaming returns the object with the given name in the given
// bucket, or an error if the object doesn't exist.
func (s *Server) GetObjectStreaming(bucketName, objectName string) (StreamingObject, error) {
backendObj, err := s.Backend.GetObject(bucketName, objectName)
backendObj, err := s.backend.GetObject(bucketName, objectName)
if err != nil {
return StreamingObject{}, err
}
Expand All @@ -538,7 +538,7 @@ func (s *Server) GetObjectWithGeneration(bucketName, objectName string, generati
//
// If versioning is enabled, archived versions are considered.
func (s *Server) GetObjectWithGenerationStreaming(bucketName, objectName string, generation int64) (StreamingObject, error) {
backendObj, err := s.Backend.GetObjectWithGeneration(bucketName, objectName, generation)
backendObj, err := s.backend.GetObjectWithGeneration(bucketName, objectName, generation)
if err != nil {
return StreamingObject{}, err
}
Expand Down Expand Up @@ -669,12 +669,12 @@ func (s *Server) deleteObject(r *http.Request) jsonResponse {
// may need to be closed whether or not there's an error.
defer obj.Close() //lint:ignore SA5001 // see above
if err == nil {
err = s.Backend.DeleteObject(vars["bucketName"], vars["objectName"])
err = s.backend.DeleteObject(vars["bucketName"], vars["objectName"])
}
if err != nil {
return jsonResponse{status: http.StatusNotFound}
}
bucket, _ := s.Backend.GetBucket(obj.BucketName)
bucket, _ := s.backend.GetBucket(obj.BucketName)
backendObj := toBackendObjects([]StreamingObject{obj})[0]
if bucket.VersioningEnabled {
s.eventManager.Trigger(&backendObj, notification.EventArchive, nil)
Expand Down Expand Up @@ -1027,7 +1027,7 @@ func (s *Server) patchObject(r *http.Request) jsonResponse {
}
}

backendObj, err := s.Backend.PatchObject(bucketName, objectName, attrsToUpdate)
backendObj, err := s.backend.PatchObject(bucketName, objectName, attrsToUpdate)
if err != nil {
return jsonResponse{
status: http.StatusNotFound,
Expand Down Expand Up @@ -1076,7 +1076,7 @@ func (s *Server) updateObject(r *http.Request) jsonResponse {
attrsToUpdate.ACL = append(attrsToUpdate.ACL, newAcl)
}
}
backendObj, err := s.Backend.UpdateObject(bucketName, objectName, attrsToUpdate)
backendObj, err := s.backend.UpdateObject(bucketName, objectName, attrsToUpdate)
if err != nil {
return jsonResponse{
status: http.StatusNotFound,
Expand Down Expand Up @@ -1119,7 +1119,7 @@ func (s *Server) composeObject(r *http.Request) jsonResponse {
sourceNames = append(sourceNames, n.Name)
}

backendObj, err := s.Backend.ComposeObject(bucketName, sourceNames, destinationObject, composeRequest.Destination.Metadata, composeRequest.Destination.ContentType)
backendObj, err := s.backend.ComposeObject(bucketName, sourceNames, destinationObject, composeRequest.Destination.Metadata, composeRequest.Destination.ContentType)
if err != nil {
return jsonResponse{
status: http.StatusInternalServerError,
Expand Down
12 changes: 8 additions & 4 deletions fakestorage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const defaultPublicHost = "storage.googleapis.com"
//
// It provides a fake implementation of the Google Cloud Storage API.
type Server struct {
Backend backend.Storage
backend backend.Storage
uploads sync.Map
transport http.RoundTripper
ts *httptest.Server
Expand Down Expand Up @@ -211,7 +211,7 @@ func newServer(options Options) (*Server, error) {
}

s := Server{
Backend: backendStorage,
backend: backendStorage,
uploads: sync.Map{},
externalURL: options.ExternalURL,
publicHost: publicHost,
Expand Down Expand Up @@ -313,9 +313,9 @@ func (s *Server) reseedServer(r *http.Request) jsonResponse {

var err error
if s.options.StorageRoot != "" {
s.Backend, err = backend.NewStorageFS(backendObjects, s.options.StorageRoot)
s.backend, err = backend.NewStorageFS(backendObjects, s.options.StorageRoot)
} else {
s.Backend, err = backend.NewStorageMemory(backendObjects)
s.backend, err = backend.NewStorageMemory(backendObjects)
}
if err != nil {
return errToJsonResponse(err)
Expand Down Expand Up @@ -425,6 +425,10 @@ func (s *Server) PublicURL() string {
return fmt.Sprintf("%s://%s", s.scheme(), s.publicHost)
}

func (s *Server) Backend() backend.Storage {
return s.backend
}

func (s *Server) scheme() string {
if s.options.Scheme == "http" {
return "http"
Expand Down
4 changes: 2 additions & 2 deletions fakestorage/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ func TestServerEventNotification(t *testing.T) {
}
eventManager := &fakeEventManager{}
server.eventManager = eventManager
err = server.Backend.CreateBucket(obj.BucketName, test.versioningEnabled)
err = server.backend.CreateBucket(obj.BucketName, test.versioningEnabled)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1051,7 +1051,7 @@ func TestServerBatchRequest(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = server.Backend.CreateBucket("some-bucket", true)
err = server.backend.CreateBucket("some-bucket", true)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion fakestorage/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c generationCondition) ConditionsMet(activeGeneration int64) bool {
func (s *Server) insertObject(r *http.Request) jsonResponse {
bucketName := unescapeMuxVars(mux.Vars(r))["bucketName"]

if _, err := s.Backend.GetBucket(bucketName); err != nil {
if _, err := s.backend.GetBucket(bucketName); err != nil {
return jsonResponse{status: http.StatusNotFound}
}
uploadType := r.URL.Query().Get("uploadType")
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
}

go func() {
err := grpc.NewServerWithBackend(server.Backend, grpcListener)
err := grpc.NewServerWithBackend(server.Backend(), grpcListener)
println("Error while starting grpc server: ", err)
}()

Expand Down

0 comments on commit 3881804

Please sign in to comment.