lamoda / gonkey

Gonkey - a testing automation tool
MIT License
346 stars 50 forks source link

Start mockserver with definition at startup #35

Open amuttsch opened 4 years ago

amuttsch commented 4 years ago

Currently, mocks can only be defined on a per test basis. We have a service that connects to an external endpoint (elasticSearch) on startup, before the http server is started. Our service cannot start as m := mocks.NewNop("elastic") returns 204 for all requests on this mock endpoint and it thinks that elasticsearch is not available.

Is it possible to start a mock server with a definition before the actual tests run?

fetinin commented 4 years ago

I'm afraid that that's currently not possible. Mock has SetDefinition() method that accepts mock definition, however definition struct is private. It actually doesn't make sense that public method accepts private structs. I see 2 solutions here:

  1. Simply make definition and newDefinition function public.
  2. Create a new API to allow pre-setup mocks.

What are your thoughts @luza?

luza commented 4 years ago

I think you could initialise a mock in this way (not an elegant, though):

m := mocks.NewNop("elastic")
def := map[string]interface{}{
    "elastic": map[string]interface{}{
        "strategy": // ... and so on in the way you write yaml
    },
}
loader := mocks.NewLoader(m)
err := loader.Load(def)

However I also like the idea to open the mock initialisation API, i.e. not just definition and newDefinition but also interfaces verifier, strategy and bunch of builders newXXX() for standard strategies and verifiers.

amuttsch commented 4 years ago

Thanks for the workaround @luza, I'll have a look at it.

As for a proper solution I'd suggest sth like mocks.NewGlobal("some_mocks.yml") for an easy and similar semantic where the yaml looks sth. like this:

elasticSearch:
      strategy: methodVary
      methods: ...
someOtherMock:
    strategy: ...