Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: register mock address as environment variables #201

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ runner.RunWithTesting(t, &runner.RunWithTestingParams{
})
```

Дополнительно библиотека регистрирует специальные переменные окружения `GONKEY_MOCK_<ИМЯ_MOCK>`, которые содержат адрес и порт соответствующего мок-сервера. Эти переменные окружения вы можете использовать при написании тестов.

### Описание моков в файле с тестом

Каждый тест перед запуском сообщает мок-серверу конфигурацию, которая определяет, что мок-сервер ответит на тот или иной запрос. Эта конфигурация задается в YAML-файле с тестом в секции `mocks`.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,8 @@ runner.RunWithTesting(t, &runner.RunWithTestingParams{
})
```

Additionally, the library registers special environment variables `GONKEY_MOCK_<MOCK_NAME>`, 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.
Expand Down
8 changes: 8 additions & 0 deletions mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions runner/runner_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 != "" {
Expand Down