From deb1b9b00c75b1c87fafe85d22f7d93ebd51b8c5 Mon Sep 17 00:00:00 2001 From: Chuntao Lu Date: Fri, 16 Feb 2018 18:16:16 -0800 Subject: [PATCH] Rework mock service and tests to accommadate mockgen --- codegen/template_bundle/template_files.go | 27 ++++++++++++----- .../templates/module_mock_initializer.tmpl | 14 ++++++--- codegen/templates/service_mock.tmpl | 9 ++++-- .../services/example-gateway/mock_service.go | 10 +++++-- .../example-gateway/module/mock_init.go | 30 ++++++++++--------- .../endpoints/contacts/save_contacts_test.go | 6 ++-- .../endpoints/tchannel/baz/baz_call_test.go | 6 ++-- 7 files changed, 66 insertions(+), 36 deletions(-) diff --git a/codegen/template_bundle/template_files.go b/codegen/template_bundle/template_files.go index 8e6465fb4..597bdf8b2 100644 --- a/codegen/template_bundle/template_files.go +++ b/codegen/template_bundle/template_files.go @@ -1526,16 +1526,21 @@ var _module_mock_initializerTmpl = []byte(`{{$instance := . -}} package module import ( + "testing" + + "github.com/golang/mock/gomock" + zanzibar "github.com/uber/zanzibar/runtime" + {{range $classType, $moduleInstances := $instance.RecursiveDependencies -}} {{range $idx, $moduleInstance := $moduleInstances -}} + {{if eq $classType $leafClass -}} + {{$moduleInstance.PackageInfo.ImportPackageAlias}} "{{$moduleInstance.PackageInfo.ImportPackagePath}}/mock_client" + {{else -}} {{$moduleInstance.PackageInfo.ImportPackageAlias}} "{{$moduleInstance.PackageInfo.ImportPackagePath}}" - {{if not (eq $classType $leafClass) -}} {{$moduleInstance.PackageInfo.ModulePackageAlias}} "{{$moduleInstance.PackageInfo.ModulePackagePath}}" {{end -}} {{end -}} {{end}} - - zanzibar "github.com/uber/zanzibar/runtime" ) {{$moduleInstances := (index $instance.RecursiveDependencies $leafClass) -}} @@ -1550,6 +1555,7 @@ type {{$mockDeps}} struct { // for the {{$instance.InstanceName}} {{$instance.ClassName}} with leaf nodes being mocks func InitializeDependenciesMock( g *zanzibar.Gateway, + ctrl *gomock.Controller, ) (*DependenciesTree, *Dependencies, *{{$mockDeps}}) { tree := &DependenciesTree{} @@ -1566,7 +1572,7 @@ func InitializeDependenciesMock( {{camel $mockDeps}} := &{{$mockDeps}}{ {{- range $idx, $dependency := $moduleInstances}} {{- $pkgInfo := $dependency.PackageInfo }} - {{$pkgInfo.QualifiedInstanceName}}: &{{$pkgInfo.ImportPackageAlias}}.Mock{{title $className}}{}, + {{$pkgInfo.QualifiedInstanceName}}: {{$pkgInfo.ImportPackageAlias}}.NewMock{{title $className}}(ctrl), {{- end }} } {{- $initializedDeps := printf "initialized%sDependencies" (title $className) }} @@ -1606,7 +1612,7 @@ func module_mock_initializerTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "module_mock_initializer.tmpl", size: 2919, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "module_mock_initializer.tmpl", size: 3118, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -1692,6 +1698,7 @@ import ( "net/http" "path/filepath" "runtime" + "testing" "time" @@ -1724,13 +1731,14 @@ type MockService interface { type mockService struct { started bool server *zanzibar.Gateway + ctrl *gomock.Controller {{camel $mockType}} *module.{{$mockType}} httpClient *http.Client tChannelClient zanzibar.TChannelCaller } // MustCreateTestService creates a new MockService, panics if it fails doing so. -func MustCreateTestService() MockService { +func MustCreateTestService(t *testing.T) MockService { _, file, _, _ := runtime.Caller(0) currentDir := zanzibar.GetDirnameFromRuntimeCaller(file) testConfigPath := filepath.Join(currentDir, "../../../config/test.json") @@ -1741,7 +1749,8 @@ func MustCreateTestService() MockService { panic(err) } - _, dependencies, mockNodes := module.InitializeDependenciesMock(server) + ctrl := gomock.NewController(t) + _, dependencies, mockNodes := module.InitializeDependenciesMock(server, ctrl) registerErr := registerDeps(server, dependencies) if registerErr != nil { panic(registerErr) @@ -1772,6 +1781,7 @@ func MustCreateTestService() MockService { return &mockService{ server: server, + ctrl: ctrl, {{camel $mockType}}: mockNodes, httpClient: httpClient, tChannelClient: tchannelClient, @@ -1788,6 +1798,7 @@ func (m *mockService) Start() { // Stop stops the mock server func (m *mockService) Stop() { + m.ctrl.Finish() m.server.Close() m.started = false } @@ -1852,7 +1863,7 @@ func service_mockTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "service_mock.tmpl", size: 3871, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "service_mock.tmpl", size: 4017, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/codegen/templates/module_mock_initializer.tmpl b/codegen/templates/module_mock_initializer.tmpl index af0c87d1c..b4449ab69 100644 --- a/codegen/templates/module_mock_initializer.tmpl +++ b/codegen/templates/module_mock_initializer.tmpl @@ -5,16 +5,21 @@ package module import ( + "testing" + + "github.com/golang/mock/gomock" + zanzibar "github.com/uber/zanzibar/runtime" + {{range $classType, $moduleInstances := $instance.RecursiveDependencies -}} {{range $idx, $moduleInstance := $moduleInstances -}} + {{if eq $classType $leafClass -}} + {{$moduleInstance.PackageInfo.ImportPackageAlias}} "{{$moduleInstance.PackageInfo.ImportPackagePath}}/mock_client" + {{else -}} {{$moduleInstance.PackageInfo.ImportPackageAlias}} "{{$moduleInstance.PackageInfo.ImportPackagePath}}" - {{if not (eq $classType $leafClass) -}} {{$moduleInstance.PackageInfo.ModulePackageAlias}} "{{$moduleInstance.PackageInfo.ModulePackagePath}}" {{end -}} {{end -}} {{end}} - - zanzibar "github.com/uber/zanzibar/runtime" ) {{$moduleInstances := (index $instance.RecursiveDependencies $leafClass) -}} @@ -29,6 +34,7 @@ type {{$mockDeps}} struct { // for the {{$instance.InstanceName}} {{$instance.ClassName}} with leaf nodes being mocks func InitializeDependenciesMock( g *zanzibar.Gateway, + ctrl *gomock.Controller, ) (*DependenciesTree, *Dependencies, *{{$mockDeps}}) { tree := &DependenciesTree{} @@ -45,7 +51,7 @@ func InitializeDependenciesMock( {{camel $mockDeps}} := &{{$mockDeps}}{ {{- range $idx, $dependency := $moduleInstances}} {{- $pkgInfo := $dependency.PackageInfo }} - {{$pkgInfo.QualifiedInstanceName}}: &{{$pkgInfo.ImportPackageAlias}}.Mock{{title $className}}{}, + {{$pkgInfo.QualifiedInstanceName}}: {{$pkgInfo.ImportPackageAlias}}.NewMock{{title $className}}(ctrl), {{- end }} } {{- $initializedDeps := printf "initialized%sDependencies" (title $className) }} diff --git a/codegen/templates/service_mock.tmpl b/codegen/templates/service_mock.tmpl index ac6a95704..3cd980a29 100644 --- a/codegen/templates/service_mock.tmpl +++ b/codegen/templates/service_mock.tmpl @@ -11,6 +11,7 @@ import ( "net/http" "path/filepath" "runtime" + "testing" "time" @@ -43,13 +44,14 @@ type MockService interface { type mockService struct { started bool server *zanzibar.Gateway + ctrl *gomock.Controller {{camel $mockType}} *module.{{$mockType}} httpClient *http.Client tChannelClient zanzibar.TChannelCaller } // MustCreateTestService creates a new MockService, panics if it fails doing so. -func MustCreateTestService() MockService { +func MustCreateTestService(t *testing.T) MockService { _, file, _, _ := runtime.Caller(0) currentDir := zanzibar.GetDirnameFromRuntimeCaller(file) testConfigPath := filepath.Join(currentDir, "../../../config/test.json") @@ -60,7 +62,8 @@ func MustCreateTestService() MockService { panic(err) } - _, dependencies, mockNodes := module.InitializeDependenciesMock(server) + ctrl := gomock.NewController(t) + _, dependencies, mockNodes := module.InitializeDependenciesMock(server, ctrl) registerErr := registerDeps(server, dependencies) if registerErr != nil { panic(registerErr) @@ -91,6 +94,7 @@ func MustCreateTestService() MockService { return &mockService{ server: server, + ctrl: ctrl, {{camel $mockType}}: mockNodes, httpClient: httpClient, tChannelClient: tchannelClient, @@ -107,6 +111,7 @@ func (m *mockService) Start() { // Stop stops the mock server func (m *mockService) Stop() { + m.ctrl.Finish() m.server.Close() m.started = false } diff --git a/examples/example-gateway/build/services/example-gateway/mock_service.go b/examples/example-gateway/build/services/example-gateway/mock_service.go index 38caf9aff..d48283123 100644 --- a/examples/example-gateway/build/services/example-gateway/mock_service.go +++ b/examples/example-gateway/build/services/example-gateway/mock_service.go @@ -32,8 +32,10 @@ import ( "net/http" "path/filepath" "runtime" + "testing" "time" + "github.com/golang/mock/gomock" "github.com/uber/zanzibar/config" "github.com/uber/zanzibar/runtime" @@ -63,13 +65,14 @@ type MockService interface { type mockService struct { started bool server *zanzibar.Gateway + ctrl *gomock.Controller mockClientNodes *module.MockClientNodes httpClient *http.Client tChannelClient zanzibar.TChannelCaller } // MustCreateTestService creates a new MockService, panics if it fails doing so. -func MustCreateTestService() MockService { +func MustCreateTestService(t *testing.T) MockService { _, file, _, _ := runtime.Caller(0) currentDir := zanzibar.GetDirnameFromRuntimeCaller(file) testConfigPath := filepath.Join(currentDir, "../../../config/test.json") @@ -80,7 +83,8 @@ func MustCreateTestService() MockService { panic(err) } - _, dependencies, mockNodes := module.InitializeDependenciesMock(server) + ctrl := gomock.NewController(t) + _, dependencies, mockNodes := module.InitializeDependenciesMock(server, ctrl) registerErr := registerDeps(server, dependencies) if registerErr != nil { panic(registerErr) @@ -111,6 +115,7 @@ func MustCreateTestService() MockService { return &mockService{ server: server, + ctrl: ctrl, mockClientNodes: mockNodes, httpClient: httpClient, tChannelClient: tchannelClient, @@ -127,6 +132,7 @@ func (m *mockService) Start() { // Stop stops the mock server func (m *mockService) Stop() { + m.ctrl.Finish() m.server.Close() m.started = false } diff --git a/examples/example-gateway/build/services/example-gateway/module/mock_init.go b/examples/example-gateway/build/services/example-gateway/module/mock_init.go index 3c765b59b..718e3858f 100644 --- a/examples/example-gateway/build/services/example-gateway/module/mock_init.go +++ b/examples/example-gateway/build/services/example-gateway/module/mock_init.go @@ -26,11 +26,14 @@ package module import ( - barClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/bar" - bazClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/baz" - contactsClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/contacts" - googlenowClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/google-now" - multiClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/multi" + "github.com/golang/mock/gomock" + zanzibar "github.com/uber/zanzibar/runtime" + + barClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/bar/mock_client" + bazClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/baz/mock_client" + contactsClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/contacts/mock_client" + googlenowClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/google-now/mock_client" + multiClientGenerated "github.com/uber/zanzibar/examples/example-gateway/build/clients/multi/mock_client" barEndpointGenerated "github.com/uber/zanzibar/examples/example-gateway/build/endpoints/bar" barEndpointModule "github.com/uber/zanzibar/examples/example-gateway/build/endpoints/bar/module" bazEndpointGenerated "github.com/uber/zanzibar/examples/example-gateway/build/endpoints/baz" @@ -45,9 +48,7 @@ import ( baztchannelEndpointModule "github.com/uber/zanzibar/examples/example-gateway/build/endpoints/tchannel/baz/module" exampleMiddlewareGenerated "github.com/uber/zanzibar/examples/example-gateway/build/middlewares/example" exampleMiddlewareModule "github.com/uber/zanzibar/examples/example-gateway/build/middlewares/example/module" - quuxClientStatic "github.com/uber/zanzibar/examples/example-gateway/clients/quux" - - zanzibar "github.com/uber/zanzibar/runtime" + quuxClientStatic "github.com/uber/zanzibar/examples/example-gateway/clients/quux/mock_client" ) // MockClientNodes contains mock client dependencies @@ -64,6 +65,7 @@ type MockClientNodes struct { // for the example-gateway service with leaf nodes being mocks func InitializeDependenciesMock( g *zanzibar.Gateway, + ctrl *gomock.Controller, ) (*DependenciesTree, *Dependencies, *MockClientNodes) { tree := &DependenciesTree{} @@ -75,12 +77,12 @@ func InitializeDependenciesMock( } mockClientNodes := &MockClientNodes{ - Bar: &barClientGenerated.MockClient{}, - Baz: &bazClientGenerated.MockClient{}, - Contacts: &contactsClientGenerated.MockClient{}, - GoogleNow: &googlenowClientGenerated.MockClient{}, - Multi: &multiClientGenerated.MockClient{}, - Quux: &quuxClientStatic.MockClient{}, + Bar: barClientGenerated.NewMockClient(ctrl), + Baz: bazClientGenerated.NewMockClient(ctrl), + Contacts: contactsClientGenerated.NewMockClient(ctrl), + GoogleNow: googlenowClientGenerated.NewMockClient(ctrl), + Multi: multiClientGenerated.NewMockClient(ctrl), + Quux: quuxClientStatic.NewMockClient(ctrl), } initializedClientDependencies := &ClientDependenciesNodes{} tree.Client = initializedClientDependencies diff --git a/examples/example-gateway/endpoints/contacts/save_contacts_test.go b/examples/example-gateway/endpoints/contacts/save_contacts_test.go index fa9eb4163..2d1b13474 100644 --- a/examples/example-gateway/endpoints/contacts/save_contacts_test.go +++ b/examples/example-gateway/endpoints/contacts/save_contacts_test.go @@ -4,8 +4,8 @@ import ( "bytes" "testing" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" clientContacts "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/clients/contacts/contacts" endpointContacts "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/endpoints/contacts/contacts" @@ -13,7 +13,7 @@ import ( ) func TestSaveContactsCall(t *testing.T) { - ms := examplegatewayServiceGenerated.MustCreateTestService() + ms := examplegatewayServiceGenerated.MustCreateTestService(t) ms.Start() defer ms.Stop() @@ -27,7 +27,7 @@ func TestSaveContactsCall(t *testing.T) { } clientResponse := &clientContacts.SaveContactsResponse{} - ms.MockClientNodes().Contacts.On("SaveContacts", mock.Anything, mock.Anything, clientRequest). + ms.MockClientNodes().Contacts.EXPECT().SaveContacts(gomock.Any(), gomock.Any(), clientRequest). Return(clientResponse, nil, nil) res, err := ms.MakeHTTPRequest( diff --git a/examples/example-gateway/endpoints/tchannel/baz/baz_call_test.go b/examples/example-gateway/endpoints/tchannel/baz/baz_call_test.go index 99cf11569..b2803e49f 100644 --- a/examples/example-gateway/endpoints/tchannel/baz/baz_call_test.go +++ b/examples/example-gateway/endpoints/tchannel/baz/baz_call_test.go @@ -4,15 +4,15 @@ import ( "context" "testing" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/endpoints/tchannel/baz/baz" ms "github.com/uber/zanzibar/examples/example-gateway/build/services/example-gateway" ) func TestBazCall(t *testing.T) { - ms := ms.MustCreateTestService() + ms := ms.MustCreateTestService(t) ms.Start() defer ms.Stop() @@ -34,7 +34,7 @@ func TestBazCall(t *testing.T) { ctx := context.Background() var result baz.SimpleService_Call_Result - ms.MockClientNodes().Baz.On("Call", mock.Anything, reqHeaders, mock.Anything). + ms.MockClientNodes().Baz.EXPECT().Call(gomock.Any(), reqHeaders, gomock.Any()). Return(map[string]string{"some-res-header": "something"}, nil) success, resHeaders, err := ms.MakeTChannelRequest(