How to unit test google cloud storage in golang?

I am writing a Go app that uses Google Cloud Storage .

For example, my β€œreading” code looks like this:

client, err := storage.NewClient(ctx) if err != nil { return nil, err } defer func() { if err := client.Close(); err != nil { panic(err) } }() r, err := client.Bucket(BucketName).Object(id).NewReader(ctx) if err != nil { return nil, err } defer r.Close() return ioutil.ReadAll(r) 

... where ctx is the context from appengine.

When I run this code in a unit test (using aetest ), it actually sends requests to my cloud storage; Instead, I would like to do this hermetically, just as aetest resolves fake data warehouse calls.

(Perhaps a related question , but it deals with python, and the github related issue indicates that it has been resolved in a python-specific way).

How can i do this?

+10
google-app-engine google-cloud-storage unit-testing go
source share
3 answers

One approach also proposed here is to allow your GCS client to replace its bootloader with a stub during unit testing. First, define an interface that matches how you use the Google Cloud Storage library, and then override it with fake data in your unit tests.

Something like that:

 type StorageClient interface { Bucket(string) Bucket // ... and so on, matching the Google library } type Storage struct { client StorageClient } // New creates a new Storage client // This is the function you use in your app func New() Storage { return Storage{ client: realGoogleClient, } } // NewWithClient creates a new Storage client with a custom implementation // This is the function you use in your unit tests func NewWithClient(client StorageClient) { return Storage{ client: client, } } 

It can be many templates to mock all third-party APIs, so maybe you can make it easier by creating some of these mocks with golang / mock or mockery .

+4
source share

Cloud storage on the Python development server is emulated using local files using the Blobstore service, so work using the Blobstore template with a test bench (also using Python). However, there is no such local emulation for Cloud Storage in run mode.

As Sachin suggested, the way to unit test Cloud Storage is to use a layout. This is done as it is done internally and at other stages of execution, such as node .

+2
source share

I would advise you to reduce the number of false publications as much as possible, you may need a sealed approach to make it almost look like the real one. https://testing.googleblog.com/2012/10/hermetic-servers.html

0
source share

All Articles