Skip to content

Commit

Permalink
feat: support env variables in definition file (#658)
Browse files Browse the repository at this point in the history
* feat: support env variables in definition file

* remove partial parallel support
  • Loading branch information
mathnogueira committed Jun 3, 2022
1 parent 7cda828 commit 908b555
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cli/file/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package file
import (
"fmt"
"os"
"regexp"
"strings"

"github.com/kubeshop/tracetest/cli/definition"
"gopkg.in/yaml.v2"
Expand All @@ -14,6 +16,11 @@ func LoadDefinition(file string) (definition.Test, error) {
return definition.Test{}, fmt.Errorf("could not read test definition file %s: %w", file, err)
}

fileBytes, err = injectEnvVariables(fileBytes)
if err != nil {
return definition.Test{}, fmt.Errorf("could not inject env variables into definition file: %w", err)
}

test := definition.Test{}
err = yaml.Unmarshal(fileBytes, &test)
if err != nil {
Expand All @@ -36,3 +43,22 @@ func SaveDefinition(file string, definition definition.Test) error {

return nil
}

func injectEnvVariables(fileBytes []byte) ([]byte, error) {
envVarRegex, err := regexp.Compile(`\$\{\w+\}`)
if err != nil {
return []byte{}, fmt.Errorf("could not compile env variable regex: %w", err)
}

fileString := string(fileBytes)
allEnvVariables := envVarRegex.FindAllString(fileString, -1)

for _, envVariableExpression := range allEnvVariables {
envVarName := envVariableExpression[2 : len(envVariableExpression)-1] // removes '${' and '}'
envVarValue := os.Getenv(envVarName)

fileString = strings.Replace(fileString, envVariableExpression, envVarValue, -1)
}

return []byte(fileString), nil
}
46 changes: 46 additions & 0 deletions cli/file/definition_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file_test

import (
"os"
"testing"

"github.com/kubeshop/tracetest/cli/definition"
Expand All @@ -15,6 +16,7 @@ func TestLoadDefinition(t *testing.T) {
File string
ExpectedDefinition definition.Test
ShouldSucceed bool
EnvVariables map[string]string
}{
{
Name: "Should_parse_valid_definition_file",
Expand Down Expand Up @@ -131,10 +133,54 @@ func TestLoadDefinition(t *testing.T) {
},
},
},
{
Name: "Should_parse_env_variables",
File: "../testdata/definitions/valid_http_test_definition_with_env_variables.yml",
ShouldSucceed: true,
EnvVariables: map[string]string{
"POKEMON_APP_API_KEY": "my secret key",
},
ExpectedDefinition: definition.Test{
Name: "POST import pokemon",
Description: "Import a pokemon using its ID",
Trigger: definition.TestTrigger{
Type: "http",
HTTPRequest: definition.HttpRequest{
URL: "http://pokemon-demo.tracetest.io/pokemon/import",
Method: "POST",
Headers: []definition.HTTPHeader{
{Key: "Content-Type", Value: "application/json"},
},
Body: definition.HTTPBody{
Type: "raw",
Raw: `{ "id": 52 }`,
},
Authentication: definition.HTTPAuthentication{
Type: "apiKey",
ApiKey: definition.HTTPAPIKeyAuth{
Key: "X-Key",
Value: "my secret key",
In: "header",
},
},
},
},
},
},
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
for key, value := range testCase.EnvVariables {
os.Setenv(key, value)
}

t.Cleanup(func() {
for key := range testCase.EnvVariables {
os.Unsetenv(key)
}
})

definition, err := file.LoadDefinition(testCase.File)
if testCase.ShouldSucceed {
require.NoError(t, err, "LoadDefinition should not fail")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: POST import pokemon
description: Import a pokemon using its ID
trigger:
type: http
http_request:
url: http://pokemon-demo.tracetest.io/pokemon/import
method: POST
headers:
- key: Content-Type
value: application/json
authentication:
type: apiKey
apiKey:
key: X-Key
value: ${POKEMON_APP_API_KEY}
in: header
body:
type: raw
raw: '{ "id": 52 }'

0 comments on commit 908b555

Please sign in to comment.