Skip to content

Commit 5e85adf

Browse files
committed
fix(#433): Optimize failure() script condition
Make sure to run the pre/post script steps that use condition failure() only on those tests that actually have a failed test result instead of evaluating the failure state for the whole test suite.
1 parent f096386 commit 5e85adf

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

pkg/cmd/run.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ func (o *runCmdOptions) runTest(cmd *cobra.Command, source string, results *v1al
198198
handleError := func(err error) {
199199
handleTestError(runConfig.Config.Namespace.Name, source, results, err)
200200
}
201-
defer runSteps(runConfig.Post, runConfig.Config.Namespace.Name, runConfig.BaseDir, results, handleError)
202-
if !runSteps(runConfig.Pre, runConfig.Config.Namespace.Name, runConfig.BaseDir, results, handleError) {
201+
defer runSteps(runConfig.Post, runConfig.Config.Namespace.Name, runConfig.BaseDir, source, results, handleError)
202+
if !runSteps(runConfig.Pre, runConfig.Config.Namespace.Name, runConfig.BaseDir, source, results, handleError) {
203203
return
204204
}
205205

@@ -254,8 +254,8 @@ func (o *runCmdOptions) runTestGroup(cmd *cobra.Command, source string, results
254254
handleError := func(err error) {
255255
handleTestError(runConfig.Config.Namespace.Name, source, results, err)
256256
}
257-
defer runSteps(runConfig.Post, runConfig.Config.Namespace.Name, runConfig.BaseDir, results, handleError)
258-
if !runSteps(runConfig.Pre, runConfig.Config.Namespace.Name, runConfig.BaseDir, results, handleError) {
257+
defer runSteps(runConfig.Post, runConfig.Config.Namespace.Name, runConfig.BaseDir, source, results, handleError)
258+
if !runSteps(runConfig.Pre, runConfig.Config.Namespace.Name, runConfig.BaseDir, source, results, handleError) {
259259
return
260260
}
261261

@@ -352,7 +352,7 @@ func (o *runCmdOptions) createTempNamespace(runConfig *config.RunConfig, cmd *co
352352
instance, err = v1alpha1.FindGlobalInstance(o.Context, c)
353353

354354
if err != nil && k8serrors.IsForbidden(err) {
355-
// not allowed to list all instances on the clusterr
355+
// not allowed to list all instances on the cluster
356356
return namespace, nil
357357
} else if err != nil {
358358
return namespace, err
@@ -548,9 +548,7 @@ func (o *runCmdOptions) createAndRunTest(ctx context.Context, c client.Client, c
548548
}
549549

550550
if runConfig.Config.Dump.Enabled {
551-
if runConfig.Config.Dump.FailedOnly &&
552-
test.Status.Phase != v1alpha1.TestPhaseFailed && test.Status.Phase != v1alpha1.TestPhaseError &&
553-
len(test.Status.Errors) == 0 && !hasSuiteErrors(&test.Status.Results) {
551+
if runConfig.Config.Dump.FailedOnly && !isFailed(&test) {
554552
fmt.Println("Skip dump for successful test")
555553
} else {
556554
var fileName string
@@ -766,13 +764,13 @@ func (o *runCmdOptions) newSettings(ctx context.Context, runConfig *config.RunCo
766764
return nil, nil
767765
}
768766

769-
func runSteps(steps []config.StepConfig, namespace, baseDir string, results *v1alpha1.TestResults, handleError func(err error)) bool {
767+
func runSteps(steps []config.StepConfig, namespace, baseDir string, name string, results *v1alpha1.TestResults, handleError func(err error)) bool {
770768
for idx, step := range steps {
771769
if len(step.Name) == 0 {
772770
step.Name = fmt.Sprintf("step-%d", idx)
773771
}
774772

775-
if skipStep(step, results) {
773+
if skipStep(step, name, results) {
776774
fmt.Printf("Skip %s\n", step.Name)
777775
continue
778776
}
@@ -782,7 +780,7 @@ func runSteps(steps []config.StepConfig, namespace, baseDir string, results *v1a
782780
if desc == "" {
783781
desc = fmt.Sprintf("script %s", step.Script)
784782
}
785-
if err := runScript(step.Script, desc, namespace, baseDir, hasErrors(results), step.Timeout); err != nil {
783+
if err := runScript(step.Script, desc, namespace, baseDir, hasError(name, results), step.Timeout); err != nil {
786784
handleError(fmt.Errorf(fmt.Sprintf("Failed to run %s: %v", desc, err)))
787785
return false
788786
}
@@ -824,7 +822,7 @@ func runSteps(steps []config.StepConfig, namespace, baseDir string, results *v1a
824822
if desc == "" {
825823
desc = fmt.Sprintf("inline command %d", idx)
826824
}
827-
if err := runScript(file.Name(), desc, namespace, baseDir, hasErrors(results), step.Timeout); err != nil {
825+
if err := runScript(file.Name(), desc, namespace, baseDir, hasError(name, results), step.Timeout); err != nil {
828826
handleError(fmt.Errorf(fmt.Sprintf("Failed to run %s: %v", desc, err)))
829827
return false
830828
}
@@ -834,7 +832,7 @@ func runSteps(steps []config.StepConfig, namespace, baseDir string, results *v1a
834832
return true
835833
}
836834

837-
func skipStep(step config.StepConfig, results *v1alpha1.TestResults) bool {
835+
func skipStep(step config.StepConfig, name string, results *v1alpha1.TestResults) bool {
838836
if step.If == "" {
839837
return false
840838
}
@@ -867,7 +865,7 @@ func skipStep(step config.StepConfig, results *v1alpha1.TestResults) bool {
867865
case "os":
868866
skipStep = (keyValue)[1] != r.GOOS
869867
case "failure()":
870-
skipStep = !hasErrors(results)
868+
skipStep = !hasError(name, results)
871869
}
872870

873871
if skipStep {

pkg/cmd/run_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestStepOsCheck(t *testing.T) {
4545
saveErr := func(err error) {
4646
scriptError = err
4747
}
48-
runSteps(steps, "default", "", &v1alpha1.TestResults{}, saveErr)
48+
runSteps(steps, "default", "", "foo", &v1alpha1.TestResults{}, saveErr)
4949

5050
assert.NilError(t, scriptError)
5151
}
@@ -81,7 +81,7 @@ func TestStepEnvCheck(t *testing.T) {
8181
saveErr := func(err error) {
8282
scriptError = err
8383
}
84-
runSteps(steps, "default", "", &v1alpha1.TestResults{}, saveErr)
84+
runSteps(steps, "default", "", "foo", &v1alpha1.TestResults{}, saveErr)
8585

8686
assert.NilError(t, scriptError)
8787
}
@@ -117,7 +117,7 @@ func TestStepCheckCombinations(t *testing.T) {
117117
saveErr := func(err error) {
118118
scriptError = err
119119
}
120-
runSteps(steps, "default", "", &v1alpha1.TestResults{}, saveErr)
120+
runSteps(steps, "default", "", "foo", &v1alpha1.TestResults{}, saveErr)
121121

122122
assert.NilError(t, scriptError)
123123
}
@@ -146,7 +146,8 @@ func TestStepOnFailure(t *testing.T) {
146146
saveErr := func(err error) {
147147
scriptError = err
148148
}
149-
runSteps(steps, "default", "", &v1alpha1.TestResults{}, saveErr)
149+
150+
runSteps(steps, "default", "", "foo", &v1alpha1.TestResults{}, saveErr)
150151

151152
assert.NilError(t, scriptError)
152153
}

pkg/cmd/util.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"github.com/citrusframework/yaks/pkg/apis/yaks/v1alpha1"
3535
"github.com/citrusframework/yaks/pkg/cmd/config"
36+
"github.com/citrusframework/yaks/pkg/util/kubernetes"
3637
p "github.com/gertd/go-pluralize"
3738
"github.com/mitchellh/mapstructure"
3839
"github.com/spf13/cobra"
@@ -230,8 +231,18 @@ func resolvePath(runConfig *config.RunConfig, resource string) string {
230231
}
231232

232233
func hasErrors(results *v1alpha1.TestResults) bool {
233-
for i := range results.Suites {
234-
if hasSuiteErrors(&results.Suites[i]) {
234+
for _, suite := range results.Suites {
235+
if hasSuiteErrors(&suite) {
236+
return true
237+
}
238+
}
239+
240+
return false
241+
}
242+
243+
func hasError(name string, results *v1alpha1.TestResults) bool {
244+
for _, suite := range results.Suites {
245+
if hasTestError(name, &suite) {
235246
return true
236247
}
237248
}
@@ -240,13 +251,24 @@ func hasErrors(results *v1alpha1.TestResults) bool {
240251
}
241252

242253
func hasSuiteErrors(suite *v1alpha1.TestSuite) bool {
243-
if len(suite.Errors) > 0 || suite.Summary.Errors > 0 || suite.Summary.Failed > 0 {
244-
return true
254+
return len(suite.Errors) > 0 || suite.Summary.Errors > 0 || suite.Summary.Failed > 0
255+
}
256+
257+
func hasTestError(name string, suite *v1alpha1.TestSuite) bool {
258+
for _, test := range suite.Tests {
259+
if test.Name == kubernetes.SanitizeName(name) {
260+
return test.ErrorType != "" || test.ErrorMessage != ""
261+
}
245262
}
246263

247264
return false
248265
}
249266

267+
func isFailed(test *v1alpha1.Test) bool {
268+
return test.Status.Phase == v1alpha1.TestPhaseFailed ||
269+
test.Status.Phase == v1alpha1.TestPhaseError && len(test.Status.Errors) > 0
270+
}
271+
250272
func loadData(ctx context.Context, fileName string) (string, error) {
251273
var content []byte
252274
var err error

0 commit comments

Comments
 (0)