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

fix: minor improvments #744

Merged
merged 2 commits into from
Nov 27, 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
8 changes: 7 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package venom

import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -35,11 +36,16 @@ func fieldsFromContext(ctx context.Context, keys ...string) logrus.Fields {
return fields
}

func asJsonString(i interface{}) string {
btes, _ := json.Marshal(i)
return string(btes)
}

// HideSensitive replace the value with __hidden__
func HideSensitive(ctx context.Context, arg interface{}) string {
s := ctx.Value(ContextKey("secrets"))
cleanVars := fmt.Sprint(arg)
if s != nil && &s != nil {
if s != nil {
switch reflect.TypeOf(s).Kind() {
case reflect.Slice:
secrets := reflect.ValueOf(s)
Expand Down
51 changes: 30 additions & 21 deletions process_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe
var previousStepVars = H{}
fromUserExecutor := tsIn != nil

loopRawTestSteps:
for stepNumber, rawStep := range tc.RawTestSteps {
stepVars := tc.Vars.Clone()
stepVars.AddAll(previousStepVars)
Expand Down Expand Up @@ -269,7 +270,9 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe
if err := yaml.Unmarshal([]byte(content), &step); err != nil {
tsResult.appendError(err)
Error(ctx, "unable to parse step #%d: %v", stepNumber, err)
return
Error(ctx, content)
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false)
break loopRawTestSteps
}

data2, err := yaml.JSONToYAML([]byte(content))
Expand All @@ -286,12 +289,12 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe

tc.testSteps = append(tc.testSteps, step)
var e ExecutorRunner
Info(ctx, "variables before execution %v", HideSensitive(ctx, stepVars))
ctx, e, err = v.GetExecutorRunner(ctx, step, stepVars)
if err != nil {
tsResult.appendError(err)
Error(ctx, "unable to get executor: %v", err)
break
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false)
break loopRawTestSteps
}

if e != nil {
Expand All @@ -312,9 +315,10 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe
}(ctx)
}
}

printStepName := v.Verbose >= 1 && !fromUserExecutor
v.setTestStepName(tsResult, e, step, &ranged, &rangedData, rangedIndex, printStepName)
v.setTestStepName(tsResult, e, step, &ranged, &rangedData, rangedIndex)
if v.Verbose >= 1 && !fromUserExecutor {
v.Print(" \t\t• %s", tsResult.Name)
}

// ##### RUN Test Step Here
skip, err := parseSkip(ctx, tc, tsResult, rawStep, stepNumber)
Expand All @@ -341,32 +345,41 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe

var isRequired bool

if tsResult.Status != StatusFail {
Warn(ctx, "Step %q result is %q", tsResult.Name, tsResult.Status)
}

if tsResult.Status == StatusFail {
Error(ctx, "Step %q result is %q", tsResult.Name, tsResult.Status)
Error(ctx, "Errors: ")
for _, e := range tsResult.Errors {
Error(ctx, "%v", e)
isRequired = isRequired || e.AssertionRequired
isRequired = isRequired || e.AssertionRequired || v.StopOnFailure
}

if isRequired {
failure := newFailure(ctx, *tc, stepNumber, rangedIndex, "", fmt.Errorf("At least one required assertion failed, skipping remaining steps"))
failure := newFailure(ctx, *tc, stepNumber, rangedIndex, "", errors.New("At least one required assertion failed, skipping remaining steps"))
tsResult.appendFailure(*failure)
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, true)
return
}
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false)
continue
}
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false)

allVars := tc.Vars.Clone()
allVars.AddAll(tsResult.ComputedVars.Clone())

assign, _, err := processVariableAssignments(ctx, tc.Name, allVars, rawStep)
if err != nil {
tsResult.appendError(err)
Error(ctx, "unable to process variable assignments: %v", err)
break
assign, _, errAssignment := processVariableAssignments(ctx, tc.Name, allVars, rawStep)
if errAssignment != nil {
tsResult.appendError(errAssignment)
Error(ctx, "unable to process variable assignments: %v", errAssignment)
}

v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false)

if errAssignment != nil {
break loopRawTestSteps
}

tc.computedVars.AddAll(assign)
Expand All @@ -376,7 +389,7 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe
}

// Set test step name (defaults to executor name, excepted if it got a "name" attribute. in range, also print key)
func (v *Venom) setTestStepName(ts *TestStepResult, e ExecutorRunner, step TestStep, ranged *Range, rangedData *RangeData, rangedIndex int, print bool) {
func (v *Venom) setTestStepName(ts *TestStepResult, e ExecutorRunner, step TestStep, ranged *Range, rangedData *RangeData, rangedIndex int) {
name := e.Name()
if value, ok := step["name"]; ok {
switch value := value.(type) {
Expand All @@ -388,10 +401,6 @@ func (v *Venom) setTestStepName(ts *TestStepResult, e ExecutorRunner, step TestS
name = fmt.Sprintf("%s (range=%s)", name, rangedData.Key)
}
ts.Name = name

if print {
v.Print(" \t\t• %s", ts.Name)
}
}

// Print a single step result (if verbosity is enabled)
Expand Down Expand Up @@ -581,8 +590,8 @@ func processVariableAssignments(ctx context.Context, tcName string, tcVars H, ra
varValue, has = tcVars[tcName+"."+assignment.From]
if !has {
if assignment.Default == nil {
err := fmt.Errorf("%s reference not found in %s", assignment.From, strings.Join(tcVarsKeys, "\n"))
Info(ctx, "%v", err)
err := fmt.Errorf("%s reference not found", assignment.From)
Error(ctx, "%v", err)
return nil, true, err
}
varValue = assignment.Default
Expand Down
2 changes: 1 addition & 1 deletion process_teststep.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (v *Venom) RunTestStep(ctx context.Context, e ExecutorRunner, tc *TestCase,
continue
}

Debug(ctx, "result of runTestStepExecutor: %+v", HideSensitive(ctx, result))
Debug(ctx, "result of executor: %s", HideSensitive(ctx, result))
mapResult := GetExecutorResult(result)
mapResultString, _ := DumpString(result)

Expand Down
2 changes: 0 additions & 2 deletions venom.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ func (v *Venom) registerUserExecutors(ctx context.Context, name string, vars map
return errors.Wrapf(err, "unable to parse file %q with content %v", f, content)
}

Debug(ctx, "User executor %q revolved with content %v", f, content)

for k, vr := range varsComputed {
ux.Input.Add(k, vr)
}
Expand Down