From 4ad7e28c82c4f736c1066bb2dda270fc053478c1 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Fri, 15 Jul 2022 09:25:31 +0000 Subject: dashapi: add query mocking functionality At the moment it's only possible to do by emulating http requests, which looks like an overkill for simple query mocking in unit tests. --- dashboard/dashapi/dashapi.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'dashboard/dashapi') diff --git a/dashboard/dashapi/dashapi.go b/dashboard/dashapi/dashapi.go index bcd964852..028448b33 100644 --- a/dashboard/dashapi/dashapi.go +++ b/dashboard/dashapi/dashapi.go @@ -23,12 +23,17 @@ import ( ) type Dashboard struct { - Client string - Addr string - Key string - ctor RequestCtor - doer RequestDoer - logger RequestLogger + Client string + Addr string + Key string + ctor RequestCtor + doer RequestDoer + logger RequestLogger + // Yes, we have the ability to set custom constructor, doer and logger, but + // there are also cases when we just want to mock the whole request processing. + // Implementing that on top of http.Request/http.Response would complicate the + // code too much. + mocker RequestMocker errorHandler func(error) } @@ -36,10 +41,17 @@ func New(client, addr, key string) (*Dashboard, error) { return NewCustom(client, addr, key, http.NewRequest, http.DefaultClient.Do, nil, nil) } +func NewMock(mocker RequestMocker) *Dashboard { + return &Dashboard{ + mocker: mocker, + } +} + type ( RequestCtor func(method, url string, body io.Reader) (*http.Request, error) RequestDoer func(req *http.Request) (*http.Response, error) RequestLogger func(msg string, args ...interface{}) + RequestMocker func(method string, req, resp interface{}) error ) // key == "" indicates that the ambient GCE service account authority @@ -592,6 +604,9 @@ func (dash *Dashboard) Query(method string, req, reply interface{}) error { if dash.logger != nil { dash.logger("API(%v): %#v", method, req) } + if dash.mocker != nil { + return dash.mocker(method, req, reply) + } err := dash.queryImpl(method, req, reply) if err != nil { if dash.logger != nil { -- cgit mrf-deployment