Skip to content

Commit

Permalink
feat: ask for missing variables in the CLI (#1884)
Browse files Browse the repository at this point in the history
* feat: ask for missing variables in the CLI

* update how we show default values in UI library

* Apply suggestions from code review

Co-authored-by: Daniel Baptista Dias <danielbdias@users.noreply.github.com>

Co-authored-by: Daniel Baptista Dias <danielbdias@users.noreply.github.com>
  • Loading branch information
mathnogueira and danielbdias committed Jan 24, 2023
1 parent fcf1a9f commit 6b94893
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 217 deletions.
69 changes: 64 additions & 5 deletions cli/actions/run_test_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package actions

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand All @@ -16,6 +18,7 @@ import (
"github.com/kubeshop/tracetest/cli/file"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/ui"
"github.com/kubeshop/tracetest/server/model/yaml"
"go.uber.org/zap"
)
Expand All @@ -36,11 +39,12 @@ type runTestAction struct {
var _ Action[RunTestConfig] = &runTestAction{}

type runDefParams struct {
DefinitionFile string
EnvID string
WaitForResult bool
JunitFile string
Metadata map[string]string
DefinitionFile string
EnvID string
WaitForResult bool
JunitFile string
Metadata map[string]string
EnvironmentVariables map[string]string
}

func NewRunTestAction(config config.Config, logger *zap.Logger, client *openapi.APIClient) runTestAction {
Expand Down Expand Up @@ -238,17 +242,34 @@ func (a runTestAction) runDefinitionFile(ctx context.Context, f file.File, param
}
}

variables := make([]openapi.EnvironmentValue, 0)
for name, value := range params.EnvironmentVariables {
variables = append(variables, openapi.EnvironmentValue{Key: openapi.PtrString(name), Value: openapi.PtrString(value)})
}

body, resp, err := a.client.ApiApi.
ExecuteDefinition(ctx).
TextDefinition(openapi.TextDefinition{
Content: openapi.PtrString(f.Contents()),
RunInformation: &openapi.RunInformation{
Metadata: params.Metadata,
EnvironmentId: &params.EnvID,
Variables: variables,
},
}).
Execute()

if resp.StatusCode == http.StatusUnprocessableEntity {
filledVariables, err := a.askForMissingVariables(resp)
if err != nil {
return err
}

params.EnvironmentVariables = filledVariables

return a.runDefinitionFile(ctx, f, params)
}

if err != nil {
return fmt.Errorf("could not execute definition: %w", err)
}
Expand Down Expand Up @@ -290,6 +311,44 @@ func (a runTestAction) runDefinitionFile(ctx context.Context, f file.File, param
return fmt.Errorf(`unsuported run type "%s"`, body.GetType())
}

func (a runTestAction) askForMissingVariables(resp *http.Response) (map[string]string, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return map[string]string{}, fmt.Errorf("could not read response body: %w", err)
}

var missingVariablesError openapi.MissingVariablesError
err = json.Unmarshal(body, &missingVariablesError)
if err != nil {
return map[string]string{}, fmt.Errorf("could not unmarshal response: %w", err)
}

uniqueMissingVariables := map[string]string{}
for _, missingVariables := range missingVariablesError.MissingVariables {
for _, variable := range missingVariables.Variables {
defaultValue := ""
if variable.DefaultValue != nil {
defaultValue = *variable.DefaultValue
}
uniqueMissingVariables[*variable.Key] = defaultValue
}
}

if len(uniqueMissingVariables) > 0 {
ui.DefaultUI.Warning("Some variables are required by one or more tests")
ui.DefaultUI.Info("Fill the values for each variable:")
}

filledVariables := map[string]string{}

for variableName, variableDefaultValue := range uniqueMissingVariables {
value := ui.DefaultUI.TextInput(variableName, variableDefaultValue)
filledVariables[variableName] = value
}

return filledVariables, nil
}

func (a runTestAction) getTransaction(ctx context.Context, id string) (openapi.Transaction, error) {
test, _, err := a.client.ApiApi.
GetTransaction(ctx, id).
Expand Down

0 comments on commit 6b94893

Please sign in to comment.