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

feat: run testcase with assertion only #468

Merged
merged 1 commit into from
Dec 16, 2021
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ tests.log
*.jpg
./.venomrc
test_results.xml
*.prof
6 changes: 3 additions & 3 deletions executors/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func New() venom.Executor {

// Executor represents a Test Exec
type Executor struct {
Script string `json:"script,omitempty" yaml:"script,omitempty"`
Script *string `json:"script,omitempty" yaml:"script,omitempty"`
}

// Result represents a step result
Expand Down Expand Up @@ -59,11 +59,11 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
return nil, err
}

if e.Script == "" {
if e.Script != nil && *e.Script == "" {
return nil, fmt.Errorf("Invalid command")
}

scriptContent := e.Script
scriptContent := *e.Script

// Default shell is sh
shell := "/bin/sh"
Expand Down
28 changes: 15 additions & 13 deletions process_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,23 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase) {
break
}

_, known := knowExecutors[e.Name()]
if !known {
knowExecutors[e.Name()] = struct{}{}
ctx, err = e.Setup(ctx, tc.Vars)
if err != nil {
tc.AppendError(err)
Error(ctx, "unable to setup executor: %v", err)
break
}
defer func(ctx context.Context) {
if err := e.TearDown(ctx); err != nil {
if e != nil {
_, known := knowExecutors[e.Name()]
if !known {
knowExecutors[e.Name()] = struct{}{}
ctx, err = e.Setup(ctx, tc.Vars)
if err != nil {
tc.AppendError(err)
Error(ctx, "unable to teardown executor: %v", err)
Error(ctx, "unable to setup executor: %v", err)
break
}
}(ctx)
defer func(ctx context.Context) {
if err := e.TearDown(ctx); err != nil {
tc.AppendError(err)
Error(ctx, "unable to teardown executor: %v", err)
}
}(ctx)
}
}

v.RunTestStep(ctx, e, tc, stepNumber, step)
Expand Down
11 changes: 8 additions & 3 deletions process_teststep.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,15 @@ func (v *Venom) RunTestStep(ctx context.Context, e ExecutorRunner, tc *TestCase,
tc.computedInfo = append(tc.computedInfo, info)
}

if h, ok := e.(executorWithDefaultAssertions); ok {
assertRes = applyAssertions(result, *tc, stepNumber, step, h.GetDefaultAssertions())
if result == nil {
Debug(ctx, "empty testcase, applying assertions on variables: %v", AllVarsFromCtx(ctx))
assertRes = applyAssertions(AllVarsFromCtx(ctx), *tc, stepNumber, step, nil)
} else {
assertRes = applyAssertions(result, *tc, stepNumber, step, nil)
if h, ok := e.(executorWithDefaultAssertions); ok {
assertRes = applyAssertions(result, *tc, stepNumber, step, h.GetDefaultAssertions())
} else {
assertRes = applyAssertions(result, *tc, stepNumber, step, nil)
}
}

tc.computedVars.AddAll(H(mapResult))
Expand Down
10 changes: 10 additions & 0 deletions tests/assertions_only.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Assertions only tests suite

vars:
foo: "bar"

testcases:
- name: ShouldEqual on global variable
steps:
- assertions:
- foo ShouldEqual bar
19 changes: 19 additions & 0 deletions types_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func (e executor) GetExecutor() Executor {
}

func (e executor) GetDefaultAssertions() *StepAssertions {
if e.Executor == nil {
return nil
}
x, ok := e.Executor.(executorWithDefaultAssertions)
if ok {
return x.GetDefaultAssertions()
Expand All @@ -82,6 +85,9 @@ func (e executor) GetDefaultAssertions() *StepAssertions {
}

func (e executor) ZeroValueResult() interface{} {
if e.Executor == nil {
return nil
}
x, ok := e.Executor.(executorWithZeroValueResult)
if ok {
return x.ZeroValueResult()
Expand All @@ -90,6 +96,9 @@ func (e executor) ZeroValueResult() interface{} {
}

func (e executor) Setup(ctx context.Context, vars H) (context.Context, error) {
if e.Executor == nil {
return ctx, nil
}
x, ok := e.Executor.(ExecutorWithSetup)
if ok {
return x.Setup(ctx, vars)
Expand All @@ -98,13 +107,23 @@ func (e executor) Setup(ctx context.Context, vars H) (context.Context, error) {
}

func (e executor) TearDown(ctx context.Context) error {
if e.Executor == nil {
return nil
}
x, ok := e.Executor.(ExecutorWithSetup)
if ok {
return x.TearDown(ctx)
}
return nil
}

func (e executor) Run(ctx context.Context, step TestStep) (interface{}, error) {
if e.Executor == nil {
return nil, nil
}
return e.Executor.Run(ctx, step)
}

func newExecutorRunner(e Executor, name, stype string, retry, delay, timeout int, info []string) ExecutorRunner {
return &executor{
Executor: e,
Expand Down
7 changes: 6 additions & 1 deletion venom.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func (v *Venom) RegisterExecutorUser(name string, e Executor) {
// no type -> exec is default
func (v *Venom) GetExecutorRunner(ctx context.Context, ts TestStep, h H) (context.Context, ExecutorRunner, error) {
name, _ := ts.StringValue("type")
if name == "" {
script, _ := ts.StringValue("script")
if name == "" && script != "" {
name = "exec"
}
retry, err := ts.IntValue("retry")
Expand Down Expand Up @@ -129,6 +130,10 @@ func (v *Venom) GetExecutorRunner(ctx context.Context, ts TestStep, h H) (contex
}
ctx = context.WithValue(ctx, ContextKey("vars"), allKeys)

if name == "" {
return ctx, newExecutorRunner(nil, name, "builtin", retry, delay, timeout, info), nil
}

if ex, ok := v.executorsBuiltin[name]; ok {
return ctx, newExecutorRunner(ex, name, "builtin", retry, delay, timeout, info), nil
}
Expand Down