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 1A 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.
Defaults
Section titled “Defaults”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.
API reference
Section titled “API reference”type Call
Section titled “type Call”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
Section titled “type MockPresigner”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 (*MockPresigner) CallCount
Section titled “func (*MockPresigner) CallCount”func (r *MockPresigner) CallCount(method string) intCallCount returns how many calls were made to method.
func (*MockPresigner) Calls
Section titled “func (*MockPresigner) Calls”func (r *MockPresigner) Calls() []CallCalls returns a deep copy of the calls, in the order they were made: changing an input it returns changes no recorded call.
func (*MockPresigner) PresignDownloadURL
Section titled “func (*MockPresigner) PresignDownloadURL”func (m *MockPresigner) PresignDownloadURL( ctx context.Context, in *storage.PresignDownloadURLInput,) (*storage.PresignURLResponse, error)PresignDownloadURL records the call and returns PresignDownloadURLFn’s result.
func (*MockPresigner) PresignUploadURL
Section titled “func (*MockPresigner) PresignUploadURL”func (m *MockPresigner) PresignUploadURL( ctx context.Context, in *storage.PresignUploadURLInput,) (*storage.PresignURLResponse, error)PresignUploadURL records the call and returns PresignUploadURLFn’s result.
func (*MockPresigner) Reset
Section titled “func (*MockPresigner) Reset”func (r *MockPresigner) Reset()Reset forgets every call. The fields are kept.
type MockProvider
Section titled “type MockProvider”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 (*MockProvider) CallCount
Section titled “func (*MockProvider) CallCount”func (r *MockProvider) CallCount(method string) intCallCount returns how many calls were made to method.
func (*MockProvider) Calls
Section titled “func (*MockProvider) Calls”func (r *MockProvider) Calls() []CallCalls returns a deep copy of the calls, in the order they were made: changing an input it returns changes no recorded call.
func (*MockProvider) CopyObject
Section titled “func (*MockProvider) CopyObject”func (m *MockProvider) CopyObject( ctx context.Context, in *storage.CopyObjectInput,) (*storage.CopyObjectOutput, error)CopyObject records the call and returns CopyObjectFn’s result.
func (*MockProvider) CreateBucket
Section titled “func (*MockProvider) CreateBucket”func (m *MockProvider) CreateBucket(ctx context.Context, in *storage.CreateBucketInput) errorCreateBucket records the call and returns CreateBucketFn’s result.
func (*MockProvider) DeleteBucket
Section titled “func (*MockProvider) DeleteBucket”func (m *MockProvider) DeleteBucket(ctx context.Context, in *storage.DeleteBucketInput) errorDeleteBucket records the call and returns DeleteBucketFn’s result.
func (*MockProvider) DeleteObject
Section titled “func (*MockProvider) DeleteObject”func (m *MockProvider) DeleteObject(ctx context.Context, in *storage.DeleteObjectInput) errorDeleteObject records the call and returns DeleteObjectFn’s result.
func (*MockProvider) GetObject
Section titled “func (*MockProvider) GetObject”func (m *MockProvider) GetObject( ctx context.Context, in *storage.GetObjectInput,) (*storage.Object, error)GetObject records the call and returns GetObjectFn’s result.
func (*MockProvider) HeadBucket
Section titled “func (*MockProvider) HeadBucket”func (m *MockProvider) HeadBucket( ctx context.Context, in *storage.HeadBucketInput,) (*storage.Bucket, error)HeadBucket records the call and returns HeadBucketFn’s result.
func (*MockProvider) HeadObject
Section titled “func (*MockProvider) HeadObject”func (m *MockProvider) HeadObject( ctx context.Context, in *storage.HeadObjectInput,) (*storage.ObjectInfo, error)HeadObject records the call and returns HeadObjectFn’s result.
func (*MockProvider) ListBuckets
Section titled “func (*MockProvider) ListBuckets”func (m *MockProvider) ListBuckets( ctx context.Context, in *storage.ListBucketsInput,) (*storage.ListBucketsOutput, error)ListBuckets records the call and returns ListBucketsFn’s result.
func (*MockProvider) ListObjects
Section titled “func (*MockProvider) ListObjects”func (m *MockProvider) ListObjects( ctx context.Context, in *storage.ListObjectsInput,) (*storage.ListObjectsOutput, error)ListObjects records the call and returns ListObjectsFn’s result.
func (*MockProvider) PutObject
Section titled “func (*MockProvider) PutObject”func (m *MockProvider) PutObject( ctx context.Context, in *storage.PutObjectInput,) (*storage.PutObjectOutput, error)PutObject records the call and returns PutObjectFn’s result.
func (*MockProvider) Reset
Section titled “func (*MockProvider) Reset”func (r *MockProvider) Reset()Reset forgets every call. The fields are kept.