googleapis / google-cloud-go

Google Cloud Client Libraries for Go.
https://cloud.google.com/go/docs/reference
Apache License 2.0
3.76k stars 1.29k forks source link

storage: how to test code that depends on *storage.Client #387

Closed aeneasr closed 6 years ago

aeneasr commented 8 years ago

How can I test my code that depends on *storage.Client? For example, I want to write a test that tests this code:

        ow := bucket.Object(id).NewWriter(context.Background())
        if _, err := io.Copy(ow, f); err != nil {
            h.H.WriteErrorCode(w, r, errors.Wrap(err, "Could not read file from memory / disk"), http.StatusInternalServerError)
            return
        }

gomock is not an option as there are no interfaces available, and it would be way easier to set up a preconfigured mock gcp storage server instead.

jba commented 8 years ago

At some point (in 2017, ,I believe) there will be an emulator for Cloud Storage.

seeruk commented 7 years ago

Just to add to the original post here, an emulator sounds good, but it would be nice for more interfaces be used for the purposes of mocking. I've been able to get around this in my own unit tests, but it doesn't look particularly nice and can be a little tricky to get set up.

Is there any ETA on that emulator?

Edit: I've actually been unsuccessful in getting the interfaces I need right so far, the amount of wrapping and stubbing is making the actual code itself much harder to follow as a result too.

jba commented 7 years ago

I'm going to look into building an in-memory mock/fake. I don't have an ETA for that either :(

jba commented 7 years ago

https://code-review.googlesource.com/12970 is a POC for a storage testing framework. Please comment here or on the CL.

jba commented 6 years ago

We now have two new ways to test the storage client:

These should suffice for nearly all unit testing purposes. Re-open if not.

bfirsh commented 4 years ago

For anyone landing here from Google who's trying to test their storage client, here's an example of using httpreplay: https://github.com/googleapis/google-cloud-go/blob/b560988bf949eec95fc850e4bf7fad9040260b55/storage/integration_test.go