Skip to content

Commit

Permalink
refactor: move test context creation to caller (#2217)
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 11, 2024
1 parent d7a7914 commit 461bddf
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 141 deletions.
7 changes: 6 additions & 1 deletion pkg/commands/test/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,13 @@ func Command() *cobra.Command {
fmt.Fscanln(stdIn) //nolint:errcheck
}
}
ctx := context.Background()
tc, err := runner.InitContext(configuration.Spec, restConfig, values)
if err != nil {
return err
}
runner := runner.New(clock, onFailure)
summary, err := runner.Run(context.Background(), restConfig, configuration.Spec, values, testToRun...)
summary, err := runner.Run(ctx, configuration.Spec, tc, testToRun...)
if summary != nil {
fmt.Fprintln(stdOut, "Tests Summary...")
fmt.Fprintln(stdOut, "- Passed tests", summary.Passed())
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/bindings/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func checkBindingName(name string) error {
return nil
}

func RegisterBinding(ctx context.Context, bindings apis.Bindings, name string, value any) apis.Bindings {
func RegisterBinding(bindings apis.Bindings, name string, value any) apis.Bindings {
return bindings.Register("$"+name, apis.NewBinding(value))
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/bindings/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestRegisterBinding(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bindings := RegisterBinding(context.TODO(), tt.bindings, tt.bindingName, tt.value)
bindings := RegisterBinding(tt.bindings, tt.bindingName, tt.value)
assert.NotNil(t, bindings)
got, err := bindings.Get("$" + tt.bindingName)
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operations/apply/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ func (o *operation) createResource(ctx context.Context, tc apis.Bindings, obj un

func (o *operation) handleCheck(ctx context.Context, tc apis.Bindings, obj unstructured.Unstructured, err error) (_outputs outputs.Outputs, _err error) {
if err == nil {
tc = bindings.RegisterBinding(ctx, tc, "error", nil)
tc = bindings.RegisterBinding(tc, "error", nil)
} else {
tc = bindings.RegisterBinding(ctx, tc, "error", err.Error())
tc = bindings.RegisterBinding(tc, "error", err.Error())
}
defer func(tc apis.Bindings) {
if _err == nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/operations/command/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func (o *operation) execute(ctx context.Context, bindings apis.Bindings, cmd *ex
cmd.Stdout = &output.Stdout
cmd.Stderr = &output.Stderr
err := cmd.Run()
bindings = apibindings.RegisterBinding(ctx, bindings, "stdout", output.Out())
bindings = apibindings.RegisterBinding(ctx, bindings, "stderr", output.Err())
bindings = apibindings.RegisterBinding(bindings, "stdout", output.Out())
bindings = apibindings.RegisterBinding(bindings, "stderr", output.Err())
if err == nil {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", nil)
bindings = apibindings.RegisterBinding(bindings, "error", nil)
} else {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", err.Error())
bindings = apibindings.RegisterBinding(bindings, "error", err.Error())
}
defer func(bindings apis.Bindings) {
if _err == nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operations/create/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ func (o *operation) createResource(ctx context.Context, bindings apis.Bindings,

func (o *operation) handleCheck(ctx context.Context, bindings apis.Bindings, obj unstructured.Unstructured, err error) (_outputs outputs.Outputs, _err error) {
if err == nil {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", nil)
bindings = apibindings.RegisterBinding(bindings, "error", nil)
} else {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", err.Error())
bindings = apibindings.RegisterBinding(bindings, "error", err.Error())
}
defer func(bindings apis.Bindings) {
if _err == nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operations/delete/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ func (o *operation) waitForDeletion(ctx context.Context, resource unstructured.U

func (o *operation) handleCheck(ctx context.Context, bindings apis.Bindings, resource unstructured.Unstructured, err error) error {
if err == nil {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", nil)
bindings = apibindings.RegisterBinding(bindings, "error", nil)
} else {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", err.Error())
bindings = apibindings.RegisterBinding(bindings, "error", err.Error())
}
if matched, err := checks.Expect(ctx, o.compilers, resource, bindings, o.expect...); matched {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operations/patch/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func (o *operation) updateResource(ctx context.Context, bindings apis.Bindings,

func (o *operation) handleCheck(ctx context.Context, bindings apis.Bindings, obj unstructured.Unstructured, err error) (_outputs outputs.Outputs, _err error) {
if err == nil {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", nil)
bindings = apibindings.RegisterBinding(bindings, "error", nil)
} else {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", err.Error())
bindings = apibindings.RegisterBinding(bindings, "error", err.Error())
}
defer func(bindings apis.Bindings) {
if _err == nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/operations/script/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ func (o *operation) execute(ctx context.Context, bindings apis.Bindings, cmd *ex
cmd.Stdout = &output.Stdout
cmd.Stderr = &output.Stderr
err := cmd.Run()
bindings = apibindings.RegisterBinding(ctx, bindings, "stdout", output.Out())
bindings = apibindings.RegisterBinding(ctx, bindings, "stderr", output.Err())
bindings = apibindings.RegisterBinding(bindings, "stdout", output.Out())
bindings = apibindings.RegisterBinding(bindings, "stderr", output.Err())
if err == nil {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", nil)
bindings = apibindings.RegisterBinding(bindings, "error", nil)
} else {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", err.Error())
bindings = apibindings.RegisterBinding(bindings, "error", err.Error())
}
defer func(bindings apis.Bindings) {
if _err == nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operations/update/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ func (o *operation) updateResource(ctx context.Context, bindings apis.Bindings,

func (o *operation) handleCheck(ctx context.Context, bindings apis.Bindings, obj unstructured.Unstructured, err error) (_outputs outputs.Outputs, _err error) {
if err == nil {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", nil)
bindings = apibindings.RegisterBinding(bindings, "error", nil)
} else {
bindings = apibindings.RegisterBinding(ctx, bindings, "error", err.Error())
bindings = apibindings.RegisterBinding(bindings, "error", err.Error())
}
defer func(bindings apis.Bindings) {
if _err == nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/outputs/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Process(ctx context.Context, compilers compilers.Compilers, tc apis.Binding
if err != nil {
return nil, err
}
tc = bindings.RegisterBinding(ctx, tc, name, value)
tc = bindings.RegisterBinding(tc, name, value)
if results == nil {
results = Outputs{}
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/runner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,34 @@ type contextData struct {
timeouts *v1alpha1.Timeouts
}

func setupContext(ctx context.Context, tc enginecontext.TestContext, data contextData) (enginecontext.TestContext, error) {
func setupContext(tc enginecontext.TestContext, data contextData) (enginecontext.TestContext, error) {
if len(data.catch) > 0 {
tc = tc.WithCatch(ctx, data.catch...)
tc = tc.WithCatch(data.catch...)
}
if data.dryRun != nil {
tc = tc.WithDryRun(ctx, *data.dryRun)
tc = tc.WithDryRun(*data.dryRun)
}
if data.delayBeforeCleanup != nil {
tc = tc.WithDelayBeforeCleanup(ctx, &data.delayBeforeCleanup.Duration)
tc = tc.WithDelayBeforeCleanup(&data.delayBeforeCleanup.Duration)
}
if data.deletionPropagation != nil {
tc = tc.WithDeletionPropagation(ctx, *data.deletionPropagation)
tc = tc.WithDeletionPropagation(*data.deletionPropagation)
}
if data.skipDelete != nil {
tc = tc.WithSkipDelete(ctx, *data.skipDelete)
tc = tc.WithSkipDelete(*data.skipDelete)
}
if data.templating != nil {
tc = tc.WithTemplating(ctx, *data.templating)
tc = tc.WithTemplating(*data.templating)
}
if data.terminationGrace != nil {
tc = tc.WithTerminationGrace(ctx, &data.terminationGrace.Duration)
tc = tc.WithTerminationGrace(&data.terminationGrace.Duration)
}
if data.timeouts != nil {
tc = tc.WithTimeouts(ctx, *data.timeouts)
tc = tc.WithTimeouts(*data.timeouts)
}
tc = enginecontext.WithClusters(ctx, tc, data.basePath, data.clusters)
tc = enginecontext.WithClusters(tc, data.basePath, data.clusters)
if data.cluster != nil {
if _tc, err := enginecontext.WithCurrentCluster(ctx, tc, *data.cluster); err != nil {
if _tc, err := enginecontext.WithCurrentCluster(tc, *data.cluster); err != nil {
return tc, err
} else {
tc = _tc
Expand Down Expand Up @@ -92,13 +92,13 @@ func setupNamespace(ctx context.Context, tc enginecontext.TestContext, data name
ns = namespace
}
if ns != nil {
tc = enginecontext.WithNamespace(ctx, tc, ns.GetName())
tc = enginecontext.WithNamespace(tc, ns.GetName())
}
return tc, ns, nil
}

func setupBindings(ctx context.Context, tc enginecontext.TestContext, bindings ...v1alpha1.Binding) (enginecontext.TestContext, error) {
if _tc, err := enginecontext.WithBindings(ctx, tc, bindings...); err != nil {
func setupBindings(tc enginecontext.TestContext, bindings ...v1alpha1.Binding) (enginecontext.TestContext, error) {
if _tc, err := enginecontext.WithBindings(tc, bindings...); err != nil {
return tc, err
} else {
tc = _tc
Expand Down Expand Up @@ -127,10 +127,10 @@ func setupCleanup(ctx context.Context, t testing.TTest, onFailure func(), tc eng
return cleaner
}

func setupContextAndBindings(ctx context.Context, tc enginecontext.TestContext, data contextData, bindings ...v1alpha1.Binding) (enginecontext.TestContext, error) {
if tc, err := setupContext(ctx, tc, data); err != nil {
func setupContextAndBindings(tc enginecontext.TestContext, data contextData, bindings ...v1alpha1.Binding) (enginecontext.TestContext, error) {
if tc, err := setupContext(tc, data); err != nil {
return tc, err
} else {
return setupBindings(ctx, tc, bindings...)
return setupBindings(tc, bindings...)
}
}
29 changes: 14 additions & 15 deletions pkg/runner/context/context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package context

import (
"context"
"time"

"github.com/kyverno/chainsaw/pkg/apis"
Expand Down Expand Up @@ -120,12 +119,12 @@ func (tc *TestContext) Timeouts() v1alpha1.DefaultTimeouts {
return tc.timeouts
}

func (tc TestContext) WithBinding(ctx context.Context, name string, value any) TestContext {
tc.bindings = apibindings.RegisterBinding(ctx, tc.bindings, name, value)
func (tc TestContext) WithBinding(name string, value any) TestContext {
tc.bindings = apibindings.RegisterBinding(tc.bindings, name, value)
return tc
}

func (tc TestContext) WithCatch(ctx context.Context, catch ...v1alpha1.CatchFinally) TestContext {
func (tc TestContext) WithCatch(catch ...v1alpha1.CatchFinally) TestContext {
tc.catch = append(tc.catch, catch...)
return tc
}
Expand All @@ -135,57 +134,57 @@ func (tc TestContext) WithDefaultCompiler(name string) TestContext {
return tc
}

func (tc TestContext) WithCluster(ctx context.Context, name string, cluster clusters.Cluster) TestContext {
func (tc TestContext) WithCluster(name string, cluster clusters.Cluster) TestContext {
tc.clusters = tc.clusters.Register(name, cluster)
return tc
}

func (tc TestContext) WithCurrentCluster(ctx context.Context, name string) TestContext {
func (tc TestContext) WithCurrentCluster(name string) TestContext {
tc.cluster = tc.Cluster(name)
return tc
}

func (tc TestContext) WithDelayBeforeCleanup(ctx context.Context, delayBeforeCleanup *time.Duration) TestContext {
func (tc TestContext) WithDelayBeforeCleanup(delayBeforeCleanup *time.Duration) TestContext {
tc.delayBeforeCleanup = delayBeforeCleanup
return tc
}

func (tc TestContext) WithDeletionPropagation(ctx context.Context, deletionPropagation metav1.DeletionPropagation) TestContext {
func (tc TestContext) WithDeletionPropagation(deletionPropagation metav1.DeletionPropagation) TestContext {
tc.deletionPropagation = deletionPropagation
return tc
}

func (tc TestContext) WithDryRun(ctx context.Context, dryRun bool) TestContext {
func (tc TestContext) WithDryRun(dryRun bool) TestContext {
tc.dryRun = dryRun
return tc
}

func (tc TestContext) WithFailFast(ctx context.Context, failFast bool) TestContext {
func (tc TestContext) WithFailFast(failFast bool) TestContext {
tc.failFast = failFast
return tc
}

func (tc TestContext) WithFullName(ctx context.Context, fullName bool) TestContext {
func (tc TestContext) WithFullName(fullName bool) TestContext {
tc.fullName = fullName
return tc
}

func (tc TestContext) WithSkipDelete(ctx context.Context, skipDelete bool) TestContext {
func (tc TestContext) WithSkipDelete(skipDelete bool) TestContext {
tc.skipDelete = skipDelete
return tc
}

func (tc TestContext) WithTemplating(ctx context.Context, templating bool) TestContext {
func (tc TestContext) WithTemplating(templating bool) TestContext {
tc.templating = templating
return tc
}

func (tc TestContext) WithTerminationGrace(ctx context.Context, terminationGrace *time.Duration) TestContext {
func (tc TestContext) WithTerminationGrace(terminationGrace *time.Duration) TestContext {
tc.terminationGrace = terminationGrace
return tc
}

func (tc TestContext) WithTimeouts(ctx context.Context, timeouts v1alpha1.Timeouts) TestContext {
func (tc TestContext) WithTimeouts(timeouts v1alpha1.Timeouts) TestContext {
if new := timeouts.Apply; new != nil {
tc.timeouts.Apply = *new
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/runner/context/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,45 @@ import (
"github.com/kyverno/chainsaw/pkg/expressions"
)

func WithBindings(ctx context.Context, tc TestContext, variables ...v1alpha1.Binding) (TestContext, error) {
func WithBindings(tc TestContext, variables ...v1alpha1.Binding) (TestContext, error) {
for _, variable := range variables {
name, value, err := bindings.ResolveBinding(ctx, tc.Compilers(), tc.Bindings(), nil, variable)
name, value, err := bindings.ResolveBinding(context.TODO(), tc.Compilers(), tc.Bindings(), nil, variable)
if err != nil {
return tc, err
}
tc = tc.WithBinding(ctx, name, value)
tc = tc.WithBinding(name, value)
}
return tc, nil
}

func WithClusters(ctx context.Context, tc TestContext, basePath string, c map[string]v1alpha1.Cluster) TestContext {
func WithClusters(tc TestContext, basePath string, c map[string]v1alpha1.Cluster) TestContext {
for name, cluster := range c {
kubeconfig := filepath.Join(basePath, cluster.Kubeconfig)
cluster := clusters.NewClusterFromKubeconfig(kubeconfig, cluster.Context)
tc = tc.WithCluster(ctx, name, cluster)
tc = tc.WithCluster(name, cluster)
}
return tc
}

func WithCurrentCluster(ctx context.Context, tc TestContext, name string) (TestContext, error) {
name, err := expressions.String(ctx, tc.Compilers(), name, tc.Bindings())
func WithCurrentCluster(tc TestContext, name string) (TestContext, error) {
name, err := expressions.String(context.TODO(), tc.Compilers(), name, tc.Bindings())
if err != nil {
return tc, err
}
tc = tc.WithCurrentCluster(ctx, name)
tc = tc.WithCurrentCluster(name)
config, client, err := tc.CurrentClusterClient()
if err != nil {
return tc, err
}
tc = tc.WithBinding(ctx, "client", client)
tc = tc.WithBinding(ctx, "config", config)
tc = tc.WithBinding("client", client)
tc = tc.WithBinding("config", config)
return tc, nil
}

func WithNamespace(ctx context.Context, tc TestContext, namespace string) TestContext {
return tc.WithBinding(ctx, "namespace", namespace)
func WithNamespace(tc TestContext, namespace string) TestContext {
return tc.WithBinding("namespace", namespace)
}

func WithValues(ctx context.Context, tc TestContext, values any) TestContext {
return tc.WithBinding(ctx, "values", values)
func WithValues(tc TestContext, values any) TestContext {
return tc.WithBinding("values", values)
}
2 changes: 1 addition & 1 deletion pkg/runner/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func buildNamespace(ctx context.Context, compilers compilers.Compilers, name str
return &namespace, nil
}
object := kube.ToUnstructured(&namespace)
tc = bindings.RegisterBinding(ctx, tc, "namespace", object.GetName())
tc = bindings.RegisterBinding(tc, "namespace", object.GetName())
merged, err := templating.TemplateAndMerge(ctx, compilers, object, tc, *template)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (o operation) execute(ctx context.Context, tc enginecontext.TestContext, st
report.Err = err
stepReport.Add(report)
}()
if operation, timeout, tc, err := o.operation(ctx, tc.WithBinding(ctx, "operation", o.info)); err != nil {
if operation, timeout, tc, err := o.operation(ctx, tc.WithBinding("operation", o.info)); err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, nil, color.BoldRed, logging.ErrSection(err))
return nil, err
} else {
Expand Down
Loading

0 comments on commit 461bddf

Please sign in to comment.