Skip to content

Commit

Permalink
refactor: add base path in test context (#2244)
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 17, 2024
1 parent 38c6953 commit 3abe1fc
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 261 deletions.
10 changes: 10 additions & 0 deletions pkg/runner/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type TestContext struct {
*model.Summary
*model.Report
basePath string
bindings apis.Bindings
catch []v1alpha1.CatchFinally
cluster clusters.Cluster
Expand Down Expand Up @@ -57,6 +58,10 @@ func (tc *TestContext) Bindings() apis.Bindings {
return tc.bindings
}

func (tc *TestContext) BasePath() string {
return tc.basePath
}

func (tc *TestContext) Catch() []v1alpha1.CatchFinally {
return tc.catch
}
Expand Down Expand Up @@ -121,6 +126,11 @@ func (tc *TestContext) Timeouts() v1alpha1.DefaultTimeouts {
return tc.timeouts
}

func (tc TestContext) WithBasePath(basePath string) TestContext {
tc.basePath = basePath
return tc
}

func (tc TestContext) WithBinding(name string, value any) TestContext {
tc.bindings = apibindings.RegisterBinding(tc.bindings, name, value)
return tc
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/context/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func WithBindings(tc TestContext, variables ...v1alpha1.Binding) (TestContext, e
return tc, nil
}

func WithClusters(tc TestContext, basePath string, c map[string]v1alpha1.Cluster) TestContext {
func WithClusters(tc TestContext, c map[string]v1alpha1.Cluster) TestContext {
for name, cluster := range c {
kubeconfig := filepath.Join(basePath, cluster.Kubeconfig)
kubeconfig := filepath.Join(tc.BasePath(), cluster.Kubeconfig)
cluster := clusters.NewClusterFromKubeconfig(kubeconfig, cluster.Context)
tc = tc.WithCluster(name, cluster)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/context/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func InitContext(config model.Configuration, defaultCluster *rest.Config, values
// values
tc = withValues(tc, values)
// clusters
tc = WithClusters(tc, "", config.Clusters)
tc = WithClusters(tc, config.Clusters)
if defaultCluster != nil {
return WithCurrentCluster(tc.WithCluster(clusters.DefaultClient, clusters.NewClusterFromConfig(defaultCluster)), clusters.DefaultClient)
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/runner/context/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type ContextData struct {
BasePath string
BasePath *string
Catch []v1alpha1.CatchFinally
Cluster *string
Clusters v1alpha1.Clusters
Expand All @@ -20,6 +20,9 @@ type ContextData struct {
}

func SetupContext(tc TestContext, data ContextData) (TestContext, error) {
if data.BasePath != nil {
tc = tc.WithBasePath(*data.BasePath)
}
if len(data.Catch) > 0 {
tc = tc.WithCatch(data.Catch...)
}
Expand All @@ -44,7 +47,7 @@ func SetupContext(tc TestContext, data ContextData) (TestContext, error) {
if data.Timeouts != nil {
tc = tc.WithTimeouts(*data.Timeouts)
}
tc = WithClusters(tc, data.BasePath, data.Clusters)
tc = WithClusters(tc, data.Clusters)
if data.Cluster != nil {
if _tc, err := WithCurrentCluster(tc, *data.Cluster); err != nil {
return tc, err
Expand Down
9 changes: 2 additions & 7 deletions pkg/runner/operations/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package operations
import (
"context"

"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/cleanup/cleaner"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
opapply "github.com/kyverno/chainsaw/pkg/engine/operations/apply"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
enginecontext "github.com/kyverno/chainsaw/pkg/runner/context"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type applyAction struct {
basePath string
namespacer namespacer.Namespacer
op v1alpha1.Apply
resource unstructured.Unstructured
Expand All @@ -24,7 +21,6 @@ type applyAction struct {

func (o applyAction) Execute(ctx context.Context, tc enginecontext.TestContext) (outputs.Outputs, error) {
contextData := enginecontext.ContextData{
BasePath: o.basePath,
Cluster: o.op.Cluster,
Clusters: o.op.Clusters,
DryRun: o.op.DryRun,
Expand Down Expand Up @@ -54,16 +50,15 @@ func (o applyAction) Execute(ctx context.Context, tc enginecontext.TestContext)
}
}

func applyOperation(compilers compilers.Compilers, basePath string, namespacer namespacer.Namespacer, cleaner cleaner.CleanerCollector, bindings apis.Bindings, op v1alpha1.Apply) ([]Operation, error) {
resources, err := fileRefOrResource(context.TODO(), op.ActionResourceRef, basePath, compilers, bindings)
func applyOperation(ctx context.Context, tc enginecontext.TestContext, namespacer namespacer.Namespacer, cleaner cleaner.CleanerCollector, op v1alpha1.Apply) ([]Operation, error) {
resources, err := fileRefOrResource(ctx, op.ActionResourceRef, tc.BasePath(), tc.Compilers(), tc.Bindings())
if err != nil {
return nil, err
}
var ops []Operation
for i := range resources {
resource := resources[i]
ops = append(ops, applyAction{
basePath: basePath,
namespacer: namespacer,
op: op,
resource: resource,
Expand Down
9 changes: 2 additions & 7 deletions pkg/runner/operations/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ package operations
import (
"context"

"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
opassert "github.com/kyverno/chainsaw/pkg/engine/operations/assert"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
enginecontext "github.com/kyverno/chainsaw/pkg/runner/context"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type assertAction struct {
basePath string
namespacer namespacer.Namespacer
op v1alpha1.Assert
resource unstructured.Unstructured
}

func (o assertAction) Execute(ctx context.Context, tc enginecontext.TestContext) (outputs.Outputs, error) {
contextData := enginecontext.ContextData{
BasePath: o.basePath,
Cluster: o.op.Cluster,
Clusters: o.op.Clusters,
Templating: o.op.Template,
Expand All @@ -46,16 +42,15 @@ func (o assertAction) Execute(ctx context.Context, tc enginecontext.TestContext)
}
}

func assertOperation(compilers compilers.Compilers, basePath string, namespacer namespacer.Namespacer, bindings apis.Bindings, op v1alpha1.Assert) ([]Operation, error) {
resources, err := fileRefOrCheck(context.TODO(), op.ActionCheckRef, basePath, compilers, bindings)
func assertOperation(ctx context.Context, tc enginecontext.TestContext, namespacer namespacer.Namespacer, op v1alpha1.Assert) ([]Operation, error) {
resources, err := fileRefOrCheck(ctx, op.ActionCheckRef, tc.BasePath(), tc.Compilers(), tc.Bindings())
if err != nil {
return nil, err
}
var ops []Operation
for i := range resources {
resource := resources[i]
ops = append(ops, assertAction{
basePath: basePath,
namespacer: namespacer,
op: op,
resource: resource,
Expand Down
7 changes: 2 additions & 5 deletions pkg/runner/operations/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

type commandAction struct {
basePath string
namespacer namespacer.Namespacer
op v1alpha1.Command
}
Expand All @@ -22,7 +21,6 @@ func (o commandAction) Execute(ctx context.Context, tc enginecontext.TestContext
ns = o.namespacer.GetNamespace()
}
contextData := enginecontext.ContextData{
BasePath: o.basePath,
Cluster: o.op.Cluster,
Clusters: o.op.Clusters,
Timeouts: &v1alpha1.Timeouts{Exec: o.op.Timeout},
Expand All @@ -35,7 +33,7 @@ func (o commandAction) Execute(ctx context.Context, tc enginecontext.TestContext
op := opcommand.New(
tc.Compilers(),
o.op,
o.basePath,
tc.BasePath(),
ns,
config,
)
Expand All @@ -45,9 +43,8 @@ func (o commandAction) Execute(ctx context.Context, tc enginecontext.TestContext
}
}

func commandOperation(basePath string, namespacer namespacer.Namespacer, op v1alpha1.Command) Operation {
func commandOperation(namespacer namespacer.Namespacer, op v1alpha1.Command) Operation {
return commandAction{
basePath: basePath,
namespacer: namespacer,
op: op,
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/runner/operations/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package operations
import (
"context"

"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/cleanup/cleaner"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
opcreate "github.com/kyverno/chainsaw/pkg/engine/operations/create"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
enginecontext "github.com/kyverno/chainsaw/pkg/runner/context"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type createAction struct {
basePath string
namespacer namespacer.Namespacer
op v1alpha1.Create
resource unstructured.Unstructured
Expand All @@ -24,7 +21,6 @@ type createAction struct {

func (o createAction) Execute(ctx context.Context, tc enginecontext.TestContext) (outputs.Outputs, error) {
contextData := enginecontext.ContextData{
BasePath: o.basePath,
Cluster: o.op.Cluster,
Clusters: o.op.Clusters,
DryRun: o.op.DryRun,
Expand Down Expand Up @@ -54,16 +50,15 @@ func (o createAction) Execute(ctx context.Context, tc enginecontext.TestContext)
}
}

func createOperation(compilers compilers.Compilers, basePath string, namespacer namespacer.Namespacer, cleaner cleaner.CleanerCollector, bindings apis.Bindings, op v1alpha1.Create) ([]Operation, error) {
resources, err := fileRefOrResource(context.TODO(), op.ActionResourceRef, basePath, compilers, bindings)
func createOperation(ctx context.Context, tc enginecontext.TestContext, namespacer namespacer.Namespacer, cleaner cleaner.CleanerCollector, op v1alpha1.Create) ([]Operation, error) {
resources, err := fileRefOrResource(ctx, op.ActionResourceRef, tc.BasePath(), tc.Compilers(), tc.Bindings())
if err != nil {
return nil, err
}
var ops []Operation
for i := range resources {
resource := resources[i]
ops = append(ops, createAction{
basePath: basePath,
namespacer: namespacer,
op: op,
resource: resource,
Expand Down
9 changes: 2 additions & 7 deletions pkg/runner/operations/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ package operations
import (
"context"

"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
opdelete "github.com/kyverno/chainsaw/pkg/engine/operations/delete"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
enginecontext "github.com/kyverno/chainsaw/pkg/runner/context"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type deleteAction struct {
basePath string
namespacer namespacer.Namespacer
op v1alpha1.Delete
resource unstructured.Unstructured
}

func (o deleteAction) Execute(ctx context.Context, tc enginecontext.TestContext) (outputs.Outputs, error) {
contextData := enginecontext.ContextData{
BasePath: o.basePath,
Cluster: o.op.Cluster,
Clusters: o.op.Clusters,
DeletionPropagation: o.op.DeletionPropagationPolicy,
Expand All @@ -49,7 +45,7 @@ func (o deleteAction) Execute(ctx context.Context, tc enginecontext.TestContext)
}
}

func deleteOperation(compilers compilers.Compilers, basePath string, namespacer namespacer.Namespacer, bindings apis.Bindings, op v1alpha1.Delete) ([]Operation, error) {
func deleteOperation(ctx context.Context, tc enginecontext.TestContext, namespacer namespacer.Namespacer, op v1alpha1.Delete) ([]Operation, error) {
ref := v1alpha1.ActionResourceRef{
FileRef: v1alpha1.FileRef{
File: op.File,
Expand All @@ -64,15 +60,14 @@ func deleteOperation(compilers compilers.Compilers, basePath string, namespacer
resource.SetLabels(op.Ref.Labels)
ref.Resource = &resource
}
resources, err := fileRefOrResource(context.TODO(), ref, basePath, compilers, bindings)
resources, err := fileRefOrResource(ctx, ref, tc.BasePath(), tc.Compilers(), tc.Bindings())
if err != nil {
return nil, err
}
var ops []Operation
for i := range resources {
resource := resources[i]
ops = append(ops, deleteAction{
basePath: basePath,
namespacer: namespacer,
op: op,
resource: resource,
Expand Down
7 changes: 2 additions & 5 deletions pkg/runner/operations/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

type describeAction struct {
basePath string
namespacer namespacer.Namespacer
op v1alpha1.Describe
}
Expand All @@ -23,7 +22,6 @@ func (o describeAction) Execute(ctx context.Context, tc enginecontext.TestContex
ns = o.namespacer.GetNamespace()
}
contextData := enginecontext.ContextData{
BasePath: o.basePath,
Cluster: o.op.Cluster,
Clusters: o.op.Clusters,
Timeouts: &v1alpha1.Timeouts{Exec: o.op.Timeout},
Expand All @@ -45,7 +43,7 @@ func (o describeAction) Execute(ctx context.Context, tc enginecontext.TestContex
Entrypoint: entrypoint,
Args: args,
},
o.basePath,
tc.BasePath(),
ns,
config,
)
Expand All @@ -55,9 +53,8 @@ func (o describeAction) Execute(ctx context.Context, tc enginecontext.TestContex
}
}

func describeOperation(basePath string, namespacer namespacer.Namespacer, op v1alpha1.Describe) Operation {
func describeOperation(namespacer namespacer.Namespacer, op v1alpha1.Describe) Operation {
return describeAction{
basePath: basePath,
namespacer: namespacer,
op: op,
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/runner/operations/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ package operations
import (
"context"

"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
operror "github.com/kyverno/chainsaw/pkg/engine/operations/error"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
enginecontext "github.com/kyverno/chainsaw/pkg/runner/context"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type errorAction struct {
basePath string
namespacer namespacer.Namespacer
op v1alpha1.Error
resource unstructured.Unstructured
}

func (o errorAction) Execute(ctx context.Context, tc enginecontext.TestContext) (outputs.Outputs, error) {
contextData := enginecontext.ContextData{
BasePath: o.basePath,
Cluster: o.op.Cluster,
Clusters: o.op.Clusters,
Templating: o.op.Template,
Expand All @@ -46,16 +42,15 @@ func (o errorAction) Execute(ctx context.Context, tc enginecontext.TestContext)
}
}

func errorOperation(compilers compilers.Compilers, basePath string, namespacer namespacer.Namespacer, bindings apis.Bindings, op v1alpha1.Error) ([]Operation, error) {
resources, err := fileRefOrCheck(context.TODO(), op.ActionCheckRef, basePath, compilers, bindings)
func errorOperation(ctx context.Context, tc enginecontext.TestContext, namespacer namespacer.Namespacer, op v1alpha1.Error) ([]Operation, error) {
resources, err := fileRefOrCheck(ctx, op.ActionCheckRef, tc.BasePath(), tc.Compilers(), tc.Bindings())
if err != nil {
return nil, err
}
var ops []Operation
for i := range resources {
resource := resources[i]
ops = append(ops, errorAction{
basePath: basePath,
namespacer: namespacer,
op: op,
resource: resource,
Expand Down
Loading

0 comments on commit 3abe1fc

Please sign in to comment.