Thursday, December 19, 2019

Creating fakes for IBM Cloud Object storage using counterfeiter

Intro : 


I have encountered a problem lately where I needed to create fake objects for IBM cloud objects storage objects (COS) in order to fake its behavior during unit tests, seemingly a trivial problem but I actually didn't find many resources on the subject.

So here are some useful links for anyone thinking about how to achieve this:

The ingredients: 


Counterfeiter: https://github.com/maxbrunsfeld/counterfeiter
A framework for automatically creating Fakes and Mocks in GoLang (needs to be installed on your machine, follow instructions on the link above)


IBM Cloud Object Storage go SDK: https://github.com/IBM/ibm-cos-sdk-go
A cloud object storage solution made available by IBM

The implementation process for S3 interface:

  • clone the IBM COS repo to your machine 
  • Get to the COS s3 interface file under /service/s3/s3iface
  • Annotate the interface.go file as follows:


package s3iface
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
import (
   "github.com/IBM/ibm-cos-sdk-go/aws"   "github.com/IBM/ibm-cos-sdk-go/aws/request"   "github.com/IBM/ibm-cos-sdk-go/service/s3")

And later on on the same file

//counterfeiter:generate . S3APItype S3API interface {
   AbortMultipartUpload(*s3.AbortMultipartUploadInput) (*s3.AbortMultipartUploadOutput, error)
   AbortMultipartUploadWithContext(aws.Context, *s3.AbortMultipartUploadInput, ...request.Option) (*s3.AbortMultipartUploadOutput, error)
   AbortMultipartUploadRequest(*s3.AbortMultipartUploadInput) (*request.Request, *s3.AbortMultipartUploadOutput)




  • Run counterfeiter from that directory
              $ go generate
            Writing `FakeS3API` to `s3ifacefakes/fake_s3api.go`... Done


            And you're done. Good luck







    No comments:

    Post a Comment