Skip to content

Commit

Permalink
refactor: runner test processor (#2235)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Dec 13, 2024
1 parent 9cef443 commit f121618
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 598 deletions.
17 changes: 9 additions & 8 deletions pkg/logging/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package logging
type Status string

const (
BeginStatus Status = "BEGIN"
DoneStatus Status = "DONE"
EndStatus Status = "END"
ErrorStatus Status = "ERROR"
LogStatus Status = "LOG"
OkStatus Status = "OK"
RunStatus Status = "RUN"
WarnStatus Status = "WARN"
BeginStatus Status = "BEGIN"
DoneStatus Status = "DONE"
EndStatus Status = "END"
ErrorStatus Status = "ERROR"
LogStatus Status = "LOG"
OkStatus Status = "OK"
RunStatus Status = "RUN"
SkippedStatus Status = "SKIP"
WarnStatus Status = "WARN"
// DoneStatus Status = "✅"
// ErrorStatus Status = "❌"
// LogStatus Status = "📄"
Expand Down
24 changes: 0 additions & 24 deletions pkg/runner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import (
"github.com/kyverno/chainsaw/pkg/cleanup/cleaner"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/engine/clusters"
"github.com/kyverno/chainsaw/pkg/logging"
"github.com/kyverno/chainsaw/pkg/model"
enginecontext "github.com/kyverno/chainsaw/pkg/runner/context"
"github.com/kyverno/chainsaw/pkg/testing"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"github.com/kyverno/pkg/ext/output/color"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -156,27 +153,6 @@ func setupBindings(tc enginecontext.TestContext, bindings ...v1alpha1.Binding) (
return tc, nil
}

func setupCleanup(ctx context.Context, t testing.TTest, onFailure func(), tc enginecontext.TestContext) cleaner.CleanerCollector {
if tc.SkipDelete() {
return nil
}
cleaner := cleaner.New(tc.Timeouts().Cleanup.Duration, nil, tc.DeletionPropagation())
t.Cleanup(func() {
if !cleaner.Empty() {
logging.Log(ctx, logging.Cleanup, logging.BeginStatus, nil, color.BoldFgCyan)
defer func() {
logging.Log(ctx, logging.Cleanup, logging.EndStatus, nil, color.BoldFgCyan)
}()
for _, err := range cleaner.Run(ctx, nil) {
t.Fail()
logging.Log(ctx, logging.Cleanup, logging.ErrorStatus, nil, color.BoldRed, logging.ErrSection(err))
onFailure()
}
}
})
return cleaner
}

func setupContextAndBindings(tc enginecontext.TestContext, data contextData, bindings ...v1alpha1.Binding) (enginecontext.TestContext, error) {
if tc, err := setupContext(tc, data); err != nil {
return tc, err
Expand Down
10 changes: 9 additions & 1 deletion pkg/runner/names/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/discovery"
)

Expand All @@ -15,7 +16,7 @@ type (
relativePathInterface = func(string, string) (string, error)
)

func Test(full bool, test discovery.Test) (string, error) {
func Test(test discovery.Test, full bool) (string, error) {
if test.Test == nil {
return "", errors.New("test must not be nil")
}
Expand All @@ -25,6 +26,13 @@ func Test(full bool, test discovery.Test) (string, error) {
return helpTest(test, nil, nil, nil)
}

func Step(step v1alpha1.TestStep, i int) string {
if step.Name != "" {
return step.Name
}
return fmt.Sprintf("step-%d", i+1)
}

func helpTest(test discovery.Test, workingDir workignDirInterface, absolutePath absolutePathInterface, relativePath relativePathInterface) (string, error) {
if workingDir == nil {
workingDir = os.Getwd
Expand Down
39 changes: 16 additions & 23 deletions pkg/runner/names/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestTest(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Test(tt.full, tt.test)
got, err := Test(tt.test, tt.full)
if tt.wantErr {
assert.Error(t, err)
} else {
Expand All @@ -102,26 +102,21 @@ func TestHelpTest(t *testing.T) {
absPathFunc absolutePathInterface
relPathFunc relativePathInterface
expectedErrMsg string
}{
{
name: "ErrorGettingWorkingDirectory",
workingDirFunc: func() (string, error) { return "", errors.New("working directory error") },
expectedErrMsg: "failed to get current working dir (working directory error)",
},
{
name: "ErrorGettingAbsolutePath",
absPathFunc: func(string) (string, error) { return "", errors.New("absolute path error") },
expectedErrMsg: "failed to compute absolute path for /some/path (absolute path error)",
},
{
name: "ErrorGettingRelativePath",
workingDirFunc: func() (string, error) { return "/correct/path", nil },
absPathFunc: func(string) (string, error) { return "/correct/path/subdir", nil },
relPathFunc: func(string, string) (string, error) { return "", errors.New("relative path error") },
expectedErrMsg: "failed to compute relative path from /correct/path to /correct/path/subdir (relative path error)",
},
}

}{{
name: "ErrorGettingWorkingDirectory",
workingDirFunc: func() (string, error) { return "", errors.New("working directory error") },
expectedErrMsg: "failed to get current working dir (working directory error)",
}, {
name: "ErrorGettingAbsolutePath",
absPathFunc: func(string) (string, error) { return "", errors.New("absolute path error") },
expectedErrMsg: "failed to compute absolute path for /some/path (absolute path error)",
}, {
name: "ErrorGettingRelativePath",
workingDirFunc: func() (string, error) { return "/correct/path", nil },
absPathFunc: func(string) (string, error) { return "/correct/path/subdir", nil },
relPathFunc: func(string, string) (string, error) { return "", errors.New("relative path error") },
expectedErrMsg: "failed to compute relative path from /correct/path to /correct/path/subdir (relative path error)",
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
test := discovery.Test{
Expand All @@ -132,9 +127,7 @@ func TestHelpTest(t *testing.T) {
},
},
}

_, err := helpTest(test, tc.workingDirFunc, tc.absPathFunc, tc.relPathFunc)

if err == nil {
t.Fatalf("%s: expected an error but got none", tc.name)
}
Expand Down
Loading

0 comments on commit f121618

Please sign in to comment.