From 461bddf61ccdf05d068a3f47420fd11bacdd85cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Wed, 11 Dec 2024 18:05:29 +0100 Subject: [PATCH] refactor: move test context creation to caller (#2217) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/commands/test/command.go | 7 ++- pkg/engine/bindings/bindings.go | 2 +- pkg/engine/bindings/bindings_test.go | 2 +- pkg/engine/operations/apply/operation.go | 4 +- pkg/engine/operations/command/operation.go | 8 +-- pkg/engine/operations/create/operation.go | 4 +- pkg/engine/operations/delete/operation.go | 4 +- pkg/engine/operations/patch/operation.go | 4 +- pkg/engine/operations/script/operation.go | 8 +-- pkg/engine/operations/update/operation.go | 4 +- pkg/engine/outputs/outputs.go | 2 +- pkg/runner/context.go | 34 +++++------ pkg/runner/context/context.go | 29 +++++----- pkg/runner/context/helpers.go | 28 ++++----- pkg/runner/namespace.go | 2 +- pkg/runner/operation.go | 2 +- pkg/runner/run.go | 66 +++++++--------------- pkg/runner/run_test.go | 9 ++- pkg/runner/step.go | 32 +++++------ pkg/runner/step_test.go | 2 +- pkg/runner/test.go | 10 ++-- 21 files changed, 122 insertions(+), 141 deletions(-) diff --git a/pkg/commands/test/command.go b/pkg/commands/test/command.go index 6acd55b6d..7e3ac2836 100644 --- a/pkg/commands/test/command.go +++ b/pkg/commands/test/command.go @@ -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()) diff --git a/pkg/engine/bindings/bindings.go b/pkg/engine/bindings/bindings.go index 360fff991..eed900383 100644 --- a/pkg/engine/bindings/bindings.go +++ b/pkg/engine/bindings/bindings.go @@ -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)) } diff --git a/pkg/engine/bindings/bindings_test.go b/pkg/engine/bindings/bindings_test.go index 6d3954e2c..719e0f992 100644 --- a/pkg/engine/bindings/bindings_test.go +++ b/pkg/engine/bindings/bindings_test.go @@ -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) diff --git a/pkg/engine/operations/apply/operation.go b/pkg/engine/operations/apply/operation.go index 1dc68ba97..33d164782 100644 --- a/pkg/engine/operations/apply/operation.go +++ b/pkg/engine/operations/apply/operation.go @@ -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 { diff --git a/pkg/engine/operations/command/operation.go b/pkg/engine/operations/command/operation.go index 91c7293d8..dd332b65b 100644 --- a/pkg/engine/operations/command/operation.go +++ b/pkg/engine/operations/command/operation.go @@ -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 { diff --git a/pkg/engine/operations/create/operation.go b/pkg/engine/operations/create/operation.go index 45f194d2d..43817a72e 100644 --- a/pkg/engine/operations/create/operation.go +++ b/pkg/engine/operations/create/operation.go @@ -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 { diff --git a/pkg/engine/operations/delete/operation.go b/pkg/engine/operations/delete/operation.go index 0cac9310a..6e5cdd2b1 100644 --- a/pkg/engine/operations/delete/operation.go +++ b/pkg/engine/operations/delete/operation.go @@ -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 diff --git a/pkg/engine/operations/patch/operation.go b/pkg/engine/operations/patch/operation.go index 21c69645f..0323116f2 100644 --- a/pkg/engine/operations/patch/operation.go +++ b/pkg/engine/operations/patch/operation.go @@ -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 { diff --git a/pkg/engine/operations/script/operation.go b/pkg/engine/operations/script/operation.go index 44d635b0d..5d6307a06 100644 --- a/pkg/engine/operations/script/operation.go +++ b/pkg/engine/operations/script/operation.go @@ -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 { diff --git a/pkg/engine/operations/update/operation.go b/pkg/engine/operations/update/operation.go index 5ba8bfa89..d2aaab22f 100644 --- a/pkg/engine/operations/update/operation.go +++ b/pkg/engine/operations/update/operation.go @@ -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 { diff --git a/pkg/engine/outputs/outputs.go b/pkg/engine/outputs/outputs.go index 75894b7ac..8cae57a15 100644 --- a/pkg/engine/outputs/outputs.go +++ b/pkg/engine/outputs/outputs.go @@ -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{} } diff --git a/pkg/runner/context.go b/pkg/runner/context.go index e6c722877..1fcc5c4ce 100644 --- a/pkg/runner/context.go +++ b/pkg/runner/context.go @@ -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 @@ -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 @@ -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...) } } diff --git a/pkg/runner/context/context.go b/pkg/runner/context/context.go index 3d9ca9962..4638d2058 100644 --- a/pkg/runner/context/context.go +++ b/pkg/runner/context/context.go @@ -1,7 +1,6 @@ package context import ( - "context" "time" "github.com/kyverno/chainsaw/pkg/apis" @@ -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 } @@ -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 } diff --git a/pkg/runner/context/helpers.go b/pkg/runner/context/helpers.go index 230bc6ba7..6f1f8f9af 100644 --- a/pkg/runner/context/helpers.go +++ b/pkg/runner/context/helpers.go @@ -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) } diff --git a/pkg/runner/namespace.go b/pkg/runner/namespace.go index ab98d70c7..005c8e28b 100644 --- a/pkg/runner/namespace.go +++ b/pkg/runner/namespace.go @@ -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 diff --git a/pkg/runner/operation.go b/pkg/runner/operation.go index b7818c97b..5eae8fbb5 100644 --- a/pkg/runner/operation.go +++ b/pkg/runner/operation.go @@ -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 { diff --git a/pkg/runner/run.go b/pkg/runner/run.go index b15ad7fbd..ae80f6869 100644 --- a/pkg/runner/run.go +++ b/pkg/runner/run.go @@ -18,14 +18,7 @@ import ( ) type Runner interface { - Run(context.Context, - // In test context - *rest.Config, - model.Configuration, - map[string]any, - // - ...discovery.Test, - ) (model.SummaryResult, error) + Run(context.Context, model.Configuration, enginecontext.TestContext, ...discovery.Test) (model.SummaryResult, error) } func New(clock clock.PassiveClock, onFailure func()) Runner { @@ -40,24 +33,11 @@ type runner struct { onFailure func() } -func (r *runner) Run( - ctx context.Context, - cfg *rest.Config, - config model.Configuration, - values map[string]any, - tests ...discovery.Test, -) (model.SummaryResult, error) { - return r.run(ctx, cfg, config, nil, values, tests...) +func (r *runner) Run(ctx context.Context, config model.Configuration, tc enginecontext.TestContext, tests ...discovery.Test) (model.SummaryResult, error) { + return r.run(ctx, nil, config, tc, tests...) } -func (r *runner) run( - ctx context.Context, - cfg *rest.Config, - config model.Configuration, - m mainstart, - values map[string]any, - tests ...discovery.Test, -) (model.SummaryResult, error) { +func (r *runner) run(ctx context.Context, m mainstart, config model.Configuration, tc enginecontext.TestContext, tests ...discovery.Test) (model.SummaryResult, error) { // sanity check if len(tests) == 0 { return nil, nil @@ -67,12 +47,6 @@ func (r *runner) run( if err := internal.SetupFlags(config); err != nil { return nil, err } - // setup context - // TODO: should be done externally ? - tc, err := setupTestContext(ctx, values, cfg, config) - if err != nil { - return nil, err - } internalTests := []testing.InternalTest{{ Name: "chainsaw", F: func(t *testing.T) { @@ -110,31 +84,31 @@ func (r *runner) onFail() { } } -func setupTestContext(ctx context.Context, values any, restConfig *rest.Config, config model.Configuration) (enginecontext.TestContext, error) { +func InitContext(config model.Configuration, defaultCluster *rest.Config, values any) (enginecontext.TestContext, error) { tc := enginecontext.EmptyContext() // cleanup options - tc = tc.WithSkipDelete(ctx, config.Cleanup.SkipDelete) + tc = tc.WithSkipDelete(config.Cleanup.SkipDelete) if config.Cleanup.DelayBeforeCleanup != nil { - tc = tc.WithDelayBeforeCleanup(ctx, &config.Cleanup.DelayBeforeCleanup.Duration) + tc = tc.WithDelayBeforeCleanup(&config.Cleanup.DelayBeforeCleanup.Duration) } // templating options - tc = tc.WithTemplating(ctx, config.Templating.Enabled) + tc = tc.WithTemplating(config.Templating.Enabled) if config.Templating.Compiler != nil { tc = tc.WithDefaultCompiler(string(*config.Templating.Compiler)) } // discovery options - tc = tc.WithFullName(ctx, config.Discovery.FullName) + tc = tc.WithFullName(config.Discovery.FullName) // execution options - tc = tc.WithFailFast(ctx, config.Execution.FailFast) + tc = tc.WithFailFast(config.Execution.FailFast) if config.Execution.ForceTerminationGracePeriod != nil { - tc = tc.WithTerminationGrace(ctx, &config.Execution.ForceTerminationGracePeriod.Duration) + tc = tc.WithTerminationGrace(&config.Execution.ForceTerminationGracePeriod.Duration) } // deletion options - tc = tc.WithDeletionPropagation(ctx, config.Deletion.Propagation) + tc = tc.WithDeletionPropagation(config.Deletion.Propagation) // error options - tc = tc.WithCatch(ctx, config.Error.Catch...) + tc = tc.WithCatch(config.Error.Catch...) // timeouts - tc = tc.WithTimeouts(ctx, v1alpha1.Timeouts{ + tc = tc.WithTimeouts(v1alpha1.Timeouts{ Apply: &config.Timeouts.Apply, Assert: &config.Timeouts.Assert, Cleanup: &config.Timeouts.Cleanup, @@ -143,16 +117,16 @@ func setupTestContext(ctx context.Context, values any, restConfig *rest.Config, Exec: &config.Timeouts.Exec, }) // values - tc = enginecontext.WithValues(ctx, tc, values) + tc = enginecontext.WithValues(tc, values) // clusters - tc = enginecontext.WithClusters(ctx, tc, "", config.Clusters) - if restConfig != nil { - cluster, err := clusters.NewClusterFromConfig(restConfig) + tc = enginecontext.WithClusters(tc, "", config.Clusters) + if defaultCluster != nil { + cluster, err := clusters.NewClusterFromConfig(defaultCluster) if err != nil { return tc, err } - tc = tc.WithCluster(ctx, clusters.DefaultClient, cluster) - return enginecontext.WithCurrentCluster(ctx, tc, clusters.DefaultClient) + tc = tc.WithCluster(clusters.DefaultClient, cluster) + return enginecontext.WithCurrentCluster(tc, clusters.DefaultClient) } return tc, nil } diff --git a/pkg/runner/run_test.go b/pkg/runner/run_test.go index c09a2356f..ad0346acd 100644 --- a/pkg/runner/run_test.go +++ b/pkg/runner/run_test.go @@ -157,11 +157,14 @@ func TestRun(t *testing.T) { runner := runner{ clock: fakeClock, } - _, err := runner.run(context.TODO(), tt.restConfig, tt.config, mockMainStart, nil, tt.tests...) + ctx := context.TODO() + tc, err := InitContext(tt.config, tt.restConfig, nil) + assert.NoError(t, err) + _, err = runner.run(ctx, mockMainStart, tt.config, tc, tt.tests...) if tt.wantErr { - assert.Error(t, err, "Run() should return an error") + assert.Error(t, err) } else { - assert.NoError(t, err, "Run() should not return an error") + assert.NoError(t, err) } }) } diff --git a/pkg/runner/step.go b/pkg/runner/step.go index dffddf0a5..1060308e4 100644 --- a/pkg/runner/step.go +++ b/pkg/runner/step.go @@ -58,7 +58,7 @@ func (r *runner) runStep(ctx context.Context, t testing.TTest, basePath string, templating: step.Template, timeouts: step.Timeouts, } - tc, err := setupContextAndBindings(ctx, tc, contextData, step.Bindings...) + tc, err := setupContextAndBindings(tc, contextData, step.Bindings...) if err != nil { t.Fail() logging.Log(ctx, logging.Internal, logging.ErrorStatus, nil, color.BoldRed, logging.ErrSection(err)) @@ -193,7 +193,7 @@ func (r *runner) runStep(ctx context.Context, t testing.TTest, basePath string, } } for k, v := range outputs { - tc = tc.WithBinding(ctx, k, v) + tc = tc.WithBinding(k, v) } } } @@ -387,7 +387,7 @@ func applyOperation(compilers compilers.Compilers, basePath string, id int, name templating: op.Template, timeouts: &v1alpha1.Timeouts{Apply: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData, op.Bindings...); err != nil { + if tc, err := setupContextAndBindings(tc, contextData, op.Bindings...); err != nil { return nil, nil, tc, err } else if err := prepareResource(resource, tc); err != nil { return nil, nil, tc, err @@ -434,7 +434,7 @@ func assertOperation(compilers compilers.Compilers, basePath string, id int, nam templating: op.Template, timeouts: &v1alpha1.Timeouts{Assert: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData, op.Bindings...); err != nil { + if tc, err := setupContextAndBindings(tc, contextData, op.Bindings...); err != nil { return nil, nil, tc, err } else if _, client, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err @@ -471,7 +471,7 @@ func commandOperation(_ compilers.Compilers, basePath string, id int, namespacer clusters: op.Clusters, timeouts: &v1alpha1.Timeouts{Exec: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData, op.Bindings...); err != nil { + if tc, err := setupContextAndBindings(tc, contextData, op.Bindings...); err != nil { return nil, nil, tc, err } else if config, _, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err @@ -512,7 +512,7 @@ func createOperation(compilers compilers.Compilers, basePath string, id int, nam templating: op.Template, timeouts: &v1alpha1.Timeouts{Apply: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData, op.Bindings...); err != nil { + if tc, err := setupContextAndBindings(tc, contextData, op.Bindings...); err != nil { return nil, nil, tc, err } else if err := prepareResource(resource, tc); err != nil { return nil, nil, tc, err @@ -574,7 +574,7 @@ func deleteOperation(compilers compilers.Compilers, basePath string, id int, nam templating: op.Template, timeouts: &v1alpha1.Timeouts{Delete: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData, op.Bindings...); err != nil { + if tc, err := setupContextAndBindings(tc, contextData, op.Bindings...); err != nil { return nil, nil, tc, err } else if _, client, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err @@ -613,7 +613,7 @@ func describeOperation(_ compilers.Compilers, basePath string, id int, namespace clusters: op.Clusters, timeouts: &v1alpha1.Timeouts{Exec: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData); err != nil { + if tc, err := setupContextAndBindings(tc, contextData); err != nil { return nil, nil, tc, err } else if config, client, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err @@ -662,7 +662,7 @@ func errorOperation(compilers compilers.Compilers, basePath string, id int, name templating: op.Template, timeouts: &v1alpha1.Timeouts{Error: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData, op.Bindings...); err != nil { + if tc, err := setupContextAndBindings(tc, contextData, op.Bindings...); err != nil { return nil, nil, tc, err } else if _, client, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err @@ -699,7 +699,7 @@ func getOperation(_ compilers.Compilers, basePath string, id int, namespacer nam clusters: op.Clusters, timeouts: &v1alpha1.Timeouts{Exec: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData); err != nil { + if tc, err := setupContextAndBindings(tc, contextData); err != nil { return nil, nil, tc, err } else if config, client, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err @@ -743,7 +743,7 @@ func logsOperation(_ compilers.Compilers, basePath string, id int, namespacer na clusters: op.Clusters, timeouts: &v1alpha1.Timeouts{Exec: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData); err != nil { + if tc, err := setupContextAndBindings(tc, contextData); err != nil { return nil, nil, tc, err } else if config, _, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err @@ -793,7 +793,7 @@ func patchOperation(compilers compilers.Compilers, basePath string, id int, name templating: op.Template, timeouts: &v1alpha1.Timeouts{Apply: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData, op.Bindings...); err != nil { + if tc, err := setupContextAndBindings(tc, contextData, op.Bindings...); err != nil { return nil, nil, tc, err } else if err := prepareResource(resource, tc); err != nil { return nil, nil, tc, err @@ -834,7 +834,7 @@ func proxyOperation(_ compilers.Compilers, basePath string, id int, namespacer n clusters: op.Clusters, timeouts: &v1alpha1.Timeouts{Exec: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData); err != nil { + if tc, err := setupContextAndBindings(tc, contextData); err != nil { return nil, nil, tc, err } else if config, client, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err @@ -878,7 +878,7 @@ func scriptOperation(_ compilers.Compilers, basePath string, id int, namespacer clusters: op.Clusters, timeouts: &v1alpha1.Timeouts{Exec: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData, op.Bindings...); err != nil { + if tc, err := setupContextAndBindings(tc, contextData, op.Bindings...); err != nil { return nil, nil, tc, err } else if config, _, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err @@ -931,7 +931,7 @@ func updateOperation(compilers compilers.Compilers, basePath string, id int, nam templating: op.Template, timeouts: &v1alpha1.Timeouts{Apply: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData, op.Bindings...); err != nil { + if tc, err := setupContextAndBindings(tc, contextData, op.Bindings...); err != nil { return nil, nil, tc, err } else if err := prepareResource(resource, tc); err != nil { return nil, nil, tc, err @@ -972,7 +972,7 @@ func waitOperation(_ compilers.Compilers, basePath string, id int, namespacer na clusters: op.Clusters, timeouts: &v1alpha1.Timeouts{Exec: op.Timeout}, } - if tc, err := setupContextAndBindings(ctx, tc, contextData); err != nil { + if tc, err := setupContextAndBindings(tc, contextData); err != nil { return nil, nil, tc, err } else if config, client, err := tc.CurrentClusterClient(); err != nil { return nil, nil, tc, err diff --git a/pkg/runner/step_test.go b/pkg/runner/step_test.go index aa3e56a5e..fe267d59f 100644 --- a/pkg/runner/step_test.go +++ b/pkg/runner/step_test.go @@ -786,7 +786,7 @@ func TestStepProcessor_Run(t *testing.T) { nt := &testing.MockT{} ctx := context.Background() ctx = logging.WithLogger(ctx, &fakeLogger.Logger{}) - tcontext := enginecontext.MakeContext(apis.NewBindings(), registry).WithTimeouts(ctx, v1alpha1.Timeouts{ + tcontext := enginecontext.MakeContext(apis.NewBindings(), registry).WithTimeouts(v1alpha1.Timeouts{ Apply: &config.Spec.Timeouts.Apply, Assert: &config.Spec.Timeouts.Assert, Cleanup: &config.Spec.Timeouts.Cleanup, diff --git a/pkg/runner/test.go b/pkg/runner/test.go index c95f1f5b5..285a46419 100644 --- a/pkg/runner/test.go +++ b/pkg/runner/test.go @@ -74,12 +74,12 @@ func (r *runner) runTest( tc.Report.Add(report) }) // setup context - tc = tc.WithBinding(ctx, "test", TestInfo{ + tc = tc.WithBinding("test", TestInfo{ Id: testId, ScenarioId: scenarioId, Metadata: test.Test.ObjectMeta, }) - tc, err := enginecontext.WithBindings(ctx, tc, bindings...) + tc, err := enginecontext.WithBindings(tc, bindings...) if err != nil { t.Fail() logging.Log(ctx, logging.Internal, logging.ErrorStatus, nil, color.BoldRed, logging.ErrSection(err)) @@ -98,7 +98,7 @@ func (r *runner) runTest( terminationGrace: test.Test.Spec.ForceTerminationGracePeriod, timeouts: test.Test.Spec.Timeouts, } - tc, err = setupContext(ctx, tc, contextData) + tc, err = setupContext(tc, contextData) if err != nil { t.Fail() logging.Log(ctx, logging.Internal, logging.ErrorStatus, nil, color.BoldRed, logging.ErrSection(err)) @@ -183,7 +183,7 @@ func (r *runner) runTest( report.Namespace = nspacer.GetNamespace() } // setup bindings - tc, err = setupBindings(ctx, tc, test.Test.Spec.Bindings...) + tc, err = setupBindings(tc, test.Test.Spec.Bindings...) if err != nil { t.Fail() logging.Log(ctx, logging.Internal, logging.ErrorStatus, nil, color.BoldRed, logging.ErrSection(err)) @@ -200,7 +200,7 @@ func (r *runner) runTest( info := StepInfo{ Id: i + 1, } - tc := tc.WithBinding(ctx, "step", info) + tc := tc.WithBinding("step", info) if stop := r.runStep(ctx, t, test.BasePath, nspacer, tc, step, report); stop { return }