Skip to content
Gasv0.10.0

gas/storage

storage/storagetest

import "github.com/gasmod/gas/storage/storagetest"

Package storagetest holds mocks of storage.Provider, MockProvider, and of storage.Presigner, MockPresigner, for tests of code that stores objects.

A mock records each call, and returns what the function in the field named after the method returns:

m := &storagetest.MockProvider{
GetObjectFn: func(context.Context, *storage.GetObjectInput) (*storage.Object, error) {
return nil, errOffline
},
}
_, err := m.GetObject(ctx, &storage.GetObjectInput{})
// err is errOffline, and m.CallCount("GetObject") is 1

A mock keeps none of the storage package’s rules: it checks no key, stores nothing and reads no body. To test against those rules without a server, use gas/storage/memory.

With a field nil, its method succeeds with a zero output, never a nil one:

  • CreateBucket, DeleteBucket and DeleteObject return nil.
  • HeadBucket returns a Bucket with the name asked for.
  • ListBuckets and ListObjects return an empty page.
  • PutObject returns a zero PutObjectOutput, without reading the body.
  • CopyObject returns a zero CopyObjectOutput.
  • PresignUploadURL and PresignDownloadURL return a PresignURLResponse with only its Method, PUT or GET.

The exception: HeadObject and GetObject return storage.ErrObjectNotFound, as a store that holds no object does, so that code under test never gets a nil object with a nil error.

Each method appends a Call with its name and a copy of its input, before it calls its field. A nil input is recorded as a nil pointer of its type.

A copy shares no pointer or map with the caller’s input, so a caller that reuses an input, as a listing loop does with its ContinuationToken, does not change a recorded call. The Body of a PutObjectInput is the same reader.

The recorded calls are read and cleared with:

  • Calls, which returns a deep copy of the calls in the order they were made, so that changing what it returns does not change them either.
  • CallCount, which counts those of one method.
  • Reset, which forgets them all and keeps the fields.

The methods may be called from several goroutines at once, as the calls are kept under a lock, which Calls, CallCount and Reset take too. Setting a field while a method runs is a data race, and a nil mock panics.

type Call struct {
// Method is the name of the method called.
Method string
// Args are the method's arguments after the context: a copy of its
// input.
Args []any
}

Call is one call of a mock’s method.

type MockPresigner struct {
// PresignUploadURLFn is what PresignUploadURL returns. When it is nil,
// PresignUploadURL returns a zero PresignURLResponse with Method PUT.
PresignUploadURLFn func(
ctx context.Context,
in *storage.PresignUploadURLInput,
) (*storage.PresignURLResponse, error)
// PresignDownloadURLFn is what PresignDownloadURL returns. When it is
// nil, PresignDownloadURL returns a zero PresignURLResponse with
// Method GET.
PresignDownloadURLFn func(
ctx context.Context,
in *storage.PresignDownloadURLInput,
) (*storage.PresignURLResponse, error)
// contains filtered or unexported fields
}

MockPresigner is a storage.Presigner that records its calls, as described in the package documentation. The zero MockPresigner is ready to use.

func (r *MockPresigner) CallCount(method string) int

CallCount returns how many calls were made to method.

func (r *MockPresigner) Calls() []Call

Calls returns a deep copy of the calls, in the order they were made: changing an input it returns changes no recorded call.

func (m *MockPresigner) PresignDownloadURL(
ctx context.Context,
in *storage.PresignDownloadURLInput,
) (*storage.PresignURLResponse, error)

PresignDownloadURL records the call and returns PresignDownloadURLFn’s result.

func (m *MockPresigner) PresignUploadURL(
ctx context.Context,
in *storage.PresignUploadURLInput,
) (*storage.PresignURLResponse, error)

PresignUploadURL records the call and returns PresignUploadURLFn’s result.

func (r *MockPresigner) Reset()

Reset forgets every call. The fields are kept.

type MockProvider struct {
// CreateBucketFn is what CreateBucket returns. When it is nil,
// CreateBucket returns nil.
CreateBucketFn func(ctx context.Context, in *storage.CreateBucketInput) error
// HeadBucketFn is what HeadBucket returns. When it is nil, HeadBucket
// returns a Bucket with the name asked for.
HeadBucketFn func(ctx context.Context, in *storage.HeadBucketInput) (*storage.Bucket, error)
// DeleteBucketFn is what DeleteBucket returns. When it is nil,
// DeleteBucket returns nil.
DeleteBucketFn func(ctx context.Context, in *storage.DeleteBucketInput) error
// ListBucketsFn is what ListBuckets returns. When it is nil,
// ListBuckets returns an empty page.
ListBucketsFn func(
ctx context.Context,
in *storage.ListBucketsInput,
) (*storage.ListBucketsOutput, error)
// HeadObjectFn is what HeadObject returns. When it is nil, HeadObject
// returns storage.ErrObjectNotFound.
HeadObjectFn func(ctx context.Context, in *storage.HeadObjectInput) (*storage.ObjectInfo, error)
// PutObjectFn is what PutObject returns. When it is nil, PutObject
// returns a zero PutObjectOutput, without reading the body.
PutObjectFn func(ctx context.Context, in *storage.PutObjectInput) (*storage.PutObjectOutput, error)
// GetObjectFn is what GetObject returns. When it is nil, GetObject
// returns storage.ErrObjectNotFound.
GetObjectFn func(ctx context.Context, in *storage.GetObjectInput) (*storage.Object, error)
// DeleteObjectFn is what DeleteObject returns. When it is nil,
// DeleteObject returns nil.
DeleteObjectFn func(ctx context.Context, in *storage.DeleteObjectInput) error
// ListObjectsFn is what ListObjects returns. When it is nil,
// ListObjects returns an empty page.
ListObjectsFn func(
ctx context.Context,
in *storage.ListObjectsInput,
) (*storage.ListObjectsOutput, error)
// CopyObjectFn is what CopyObject returns. When it is nil, CopyObject
// returns a zero CopyObjectOutput.
CopyObjectFn func(
ctx context.Context,
in *storage.CopyObjectInput,
) (*storage.CopyObjectOutput, error)
// contains filtered or unexported fields
}

MockProvider is a storage.Provider that records its calls, as described in the package documentation. The zero MockProvider is ready to use.

func (r *MockProvider) CallCount(method string) int

CallCount returns how many calls were made to method.

func (r *MockProvider) Calls() []Call

Calls returns a deep copy of the calls, in the order they were made: changing an input it returns changes no recorded call.

func (m *MockProvider) CopyObject(
ctx context.Context,
in *storage.CopyObjectInput,
) (*storage.CopyObjectOutput, error)

CopyObject records the call and returns CopyObjectFn’s result.

func (m *MockProvider) CreateBucket(ctx context.Context, in *storage.CreateBucketInput) error

CreateBucket records the call and returns CreateBucketFn’s result.

func (m *MockProvider) DeleteBucket(ctx context.Context, in *storage.DeleteBucketInput) error

DeleteBucket records the call and returns DeleteBucketFn’s result.

func (m *MockProvider) DeleteObject(ctx context.Context, in *storage.DeleteObjectInput) error

DeleteObject records the call and returns DeleteObjectFn’s result.

func (m *MockProvider) GetObject(
ctx context.Context,
in *storage.GetObjectInput,
) (*storage.Object, error)

GetObject records the call and returns GetObjectFn’s result.

func (m *MockProvider) HeadBucket(
ctx context.Context,
in *storage.HeadBucketInput,
) (*storage.Bucket, error)

HeadBucket records the call and returns HeadBucketFn’s result.

func (m *MockProvider) HeadObject(
ctx context.Context,
in *storage.HeadObjectInput,
) (*storage.ObjectInfo, error)

HeadObject records the call and returns HeadObjectFn’s result.

func (m *MockProvider) ListBuckets(
ctx context.Context,
in *storage.ListBucketsInput,
) (*storage.ListBucketsOutput, error)

ListBuckets records the call and returns ListBucketsFn’s result.

func (m *MockProvider) ListObjects(
ctx context.Context,
in *storage.ListObjectsInput,
) (*storage.ListObjectsOutput, error)

ListObjects records the call and returns ListObjectsFn’s result.

func (m *MockProvider) PutObject(
ctx context.Context,
in *storage.PutObjectInput,
) (*storage.PutObjectOutput, error)

PutObject records the call and returns PutObjectFn’s result.

func (r *MockProvider) Reset()

Reset forgets every call. The fields are kept.