Skip to content

Commit

Permalink
Merge pull request #560 from uber/lu.ms
Browse files Browse the repository at this point in the history
Better runtime config for mock service
  • Loading branch information
ChuntaoLu committed Feb 12, 2019
2 parents 7abaf91 + 08a66a7 commit b341b6c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 34 deletions.
18 changes: 17 additions & 1 deletion codegen/post_gen_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package codegen

import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -240,7 +241,7 @@ func ServiceMockGenHook(h *PackageHelper, t *Template) PostGenHook {
}
files.Store(filepath.Join(genDir, "mock_init.go"), mockInit)

mockService, err := t.ExecTemplate("service_mock.tmpl", instance, h)
mockService, err := generateServiceMock(instance, h, t)
if err != nil {
ec <- errors.Wrapf(
err,
Expand Down Expand Up @@ -303,6 +304,21 @@ func generateMockInitializer(instance *ModuleInstance, h *PackageHelper, t *Temp
return t.ExecTemplate("module_mock_initializer.tmpl", data, h)
}

// generateServiceMock generates mock service
func generateServiceMock(instance *ModuleInstance, h *PackageHelper, t *Template) ([]byte, error) {
configPath := path.Join(strings.Replace(instance.Directory, "services", "config", 1), "test.yaml")
if _, err := os.Stat(filepath.Join(h.ConfigRoot(), configPath)); err != nil {
if os.IsNotExist(err) {
configPath = "config/test.yaml"
}
}
data := map[string]interface{}{
"Instance": instance,
"TestConfigPath": filepath.Join(h.packageRoot, configPath),
}
return t.ExecTemplate("service_mock.tmpl", data, h)
}

// FindClientsWithFixture finds the given module's dependent clients that have fixture config
func FindClientsWithFixture(instance *ModuleInstance) (map[string]string, error) {
clientsWithFixture := map[string]string{}
Expand Down
24 changes: 16 additions & 8 deletions codegen/template_bundle/template_files.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions codegen/templates/service_mock.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{- $instance := . -}}
{{- $instance := .Instance -}}
{{- $defaultTestConfigPath := .TestConfigPath -}}
{{- $leafClass := firstIsClientOrEmpty $instance.DependencyOrder -}}
{{- $mockType := printf "Mock%sNodes" (title $leafClass) -}}
{{- $mock := printf "Mock%ss" (title $leafClass) -}}
Expand All @@ -10,8 +11,8 @@ import (
"errors"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -54,11 +55,18 @@ type mockService struct {
}

// MustCreateTestService creates a new MockService, panics if it fails doing so.
func MustCreateTestService(t *testing.T) MockService {
_, file, _, _ := runtime.Caller(0)
currentDir := zanzibar.GetDirnameFromRuntimeCaller(file)
testConfigPath := filepath.Join(currentDir, "../../../../config/test.yaml")
c := config.NewRuntimeConfigOrDie([]string{testConfigPath}, nil)
// Optional testConfigPaths specifies runtime config files used in tests, it
// should be paths that are relative to "$GOPATH/src".
// If testConfigPaths is absent, a default test config file is used.
// The default test config file is chosen base on existence in order below:
// - "../../config/test.yaml" where current dir is the dir of service-config.yaml for the mocked service
// - "config/test.yaml" where current dir is the project root
func MustCreateTestService(t *testing.T, testConfigPaths ...string) MockService {
if len(testConfigPaths) == 0 {
defaultPath := filepath.Join(os.Getenv("GOPATH"), "src", "{{$defaultTestConfigPath}}")
testConfigPaths = append(testConfigPaths, defaultPath)
}
c := config.NewRuntimeConfigOrDie(testConfigPaths, nil)

server, err := zanzibar.CreateGateway(c, nil)
if err != nil {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b341b6c

Please sign in to comment.