From 105b66ce40d372e15009d030253a1349beaa2d8e Mon Sep 17 00:00:00 2001 From: Alexey Tyuryumov Date: Tue, 14 Feb 2023 15:15:35 -0700 Subject: [PATCH] new: register mock address as environment variables --- README-ru.md | 2 ++ README.md | 2 ++ mocks/mocks.go | 8 ++++++++ runner/runner_testing.go | 11 +++++++++++ 4 files changed, 23 insertions(+) diff --git a/README-ru.md b/README-ru.md index dbbadb2..73c5b53 100644 --- a/README-ru.md +++ b/README-ru.md @@ -908,6 +908,8 @@ runner.RunWithTesting(t, &runner.RunWithTestingParams{ }) ``` +Дополнительно библиотека регистрирует специальные переменные окружения `GONKEY_MOCK_<ИМЯ_MOCK>`, которые содержат адрес и порт соответствующего мок-сервера. Эти переменные окружения вы можете использовать при написании тестов. + ### Описание моков в файле с тестом Каждый тест перед запуском сообщает мок-серверу конфигурацию, которая определяет, что мок-сервер ответит на тот или иной запрос. Эта конфигурация задается в YAML-файле с тестом в секции `mocks`. diff --git a/README.md b/README.md index d2da4e3..dfaeaef 100644 --- a/README.md +++ b/README.md @@ -911,6 +911,8 @@ runner.RunWithTesting(t, &runner.RunWithTestingParams{ }) ``` +Additionally, the library registers special environment variables `GONKEY_MOCK_`, which contain the address and port of the corresponding mock server. You can use these environment variables when writing tests. + ### Mocks definition in the test file Each test communicates a configuration to the mock-server before running. This configuration defines the responses for specific requests in the mock-server. The configuration is defined in a YAML-file with test in the `mocks` section. diff --git a/mocks/mocks.go b/mocks/mocks.go index 383634f..f7fe037 100644 --- a/mocks/mocks.go +++ b/mocks/mocks.go @@ -90,3 +90,11 @@ func (m *Mocks) EndRunningContext() []error { } return errors } + +func (m *Mocks) GetNames() []string { + names := []string{} + for n := range m.mocks { + names = append(names, n) + } + return names +} diff --git a/runner/runner_testing.go b/runner/runner_testing.go index 4d7150c..d689fb5 100644 --- a/runner/runner_testing.go +++ b/runner/runner_testing.go @@ -3,9 +3,11 @@ package runner import ( "database/sql" "errors" + "fmt" "net/http/httptest" "net/url" "os" + "strings" "testing" "github.com/aerospike/aerospike-client-go/v5" @@ -46,12 +48,21 @@ type RunWithTestingParams struct { FixtureLoader fixtures.Loader } +func registerMocksEnvironment(m *mocks.Mocks) { + names := m.GetNames() + for _, n := range names { + varName := fmt.Sprintf("GONKEY_MOCK_%s", strings.ToUpper(n)) + os.Setenv(varName, m.Service(n).ServerAddr()) + } +} + // RunWithTesting is a helper function the wraps the common Run and provides simple way // to configure Gonkey by filling the params structure. func RunWithTesting(t *testing.T, params *RunWithTestingParams) { var mocksLoader *mocks.Loader if params.Mocks != nil { mocksLoader = mocks.NewLoader(params.Mocks) + registerMocksEnvironment(params.Mocks) } if params.EnvFilePath != "" {