Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: test level bindings evaluated too early #1097

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .release-notes/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Release notes for `TODO`.
## 🔧 Fixes 🔧

- Fixed a resource templating issue in non-resource assertions
- Fixed test level bindings evaluated too early, potentially failing to resolve `$namespace` dependency
- Fixed diff not templated in case of `assert` failure
- Fixed resource templating always enabled in `create` operation, regardless of the configured `template` field
- Fixed resource templating always enabled in `patch` operation, regardless of the configured `template` field
Expand Down
9 changes: 8 additions & 1 deletion pkg/runner/bindings/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,19 @@ func RegisterBinding(ctx context.Context, bindings binding.Bindings, input any,
return RegisterNamedBinding(ctx, bindings, name, value), nil
}

func RegisterBindings(ctx context.Context, bindings binding.Bindings, config *rest.Config, client client.Client, variables ...v1alpha1.Binding) (binding.Bindings, error) {
func RegisterClusterBindings(ctx context.Context, bindings binding.Bindings, config *rest.Config, client client.Client) binding.Bindings {
if bindings == nil {
bindings = binding.NewBindings()
}
bindings = bindings.Register("$client", binding.NewBinding(client))
bindings = bindings.Register("$config", binding.NewBinding(config))
return bindings
}

func RegisterBindings(ctx context.Context, bindings binding.Bindings, variables ...v1alpha1.Binding) (binding.Bindings, error) {
if bindings == nil {
bindings = binding.NewBindings()
}
for _, variable := range variables {
next, err := RegisterBinding(ctx, bindings, nil, variable)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/runner/processors/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ func (o operation) execute(ctx context.Context, bindings binding.Bindings) opera
}
}
operation, err := o.operation(ctx, bindings)
bindings = apibindings.RegisterClusterBindings(ctx, bindings, o.config, o.client)
if err != nil {
handleError(err)
} else if bindings, err := apibindings.RegisterBindings(ctx, bindings, o.config, o.client, o.variables...); err != nil {
} else if bindings, err := apibindings.RegisterBindings(ctx, bindings, o.variables...); err != nil {
handleError(err)
} else {
outputs, err := operation.Exec(ctx, apibindings.RegisterNamedBinding(ctx, bindings, "operation", o.info))
Expand Down
3 changes: 2 additions & 1 deletion pkg/runner/processors/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func (p *stepProcessor) Run(ctx context.Context, bindings binding.Bindings) {
t := testing.FromContext(ctx)
logger := logging.FromContext(ctx)
config, cluster := p.clusters.client(p.step.Cluster, p.test.Spec.Cluster)
bindings, err := apibindings.RegisterBindings(ctx, bindings, config, cluster, p.step.Bindings...)
bindings = apibindings.RegisterClusterBindings(ctx, bindings, config, cluster)
bindings, err := apibindings.RegisterBindings(ctx, bindings, p.step.Bindings...)
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
t.FailNow()
Expand Down
11 changes: 6 additions & 5 deletions pkg/runner/processors/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ func (p *testProcessor) Run(ctx context.Context, bindings binding.Bindings, nspa
}
}
config, cluster := p.clusters.client(p.test.Spec.Cluster)
bindings, err := apibindings.RegisterBindings(ctx, bindings, config, cluster, p.test.Spec.Bindings...)
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
t.FailNow()
}
bindings = apibindings.RegisterClusterBindings(ctx, bindings, config, cluster)
setupLogger := logging.NewLogger(t, p.clock, p.test.Name, fmt.Sprintf("%-*s", size, "@setup"))
cleanupLogger := logging.NewLogger(t, p.clock, p.test.Name, fmt.Sprintf("%-*s", size, "@cleanup"))
var namespace *corev1.Namespace
Expand Down Expand Up @@ -171,6 +167,11 @@ func (p *testProcessor) Run(ctx context.Context, bindings binding.Bindings, nspa
}
}
}
bindings, err := apibindings.RegisterBindings(ctx, bindings, p.test.Spec.Bindings...)
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
t.FailNow()
}
delay := p.config.DelayBeforeCleanup
if p.test.Spec.DelayBeforeCleanup != nil {
delay = p.test.Spec.DelayBeforeCleanup
Expand Down
11 changes: 6 additions & 5 deletions pkg/runner/processors/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ func (p *testsProcessor) Run(ctx context.Context, bindings binding.Bindings) {
})
var nspacer namespacer.Namespacer
config, cluster := p.clusters.client()
bindings, err := apibindings.RegisterBindings(ctx, bindings, config, cluster)
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
t.FailNow()
}
bindings = apibindings.RegisterClusterBindings(ctx, bindings, config, cluster)
if cluster != nil {
if p.config.Namespace != "" {
namespace := client.Namespace(p.config.Namespace)
Expand Down Expand Up @@ -118,6 +114,11 @@ func (p *testsProcessor) Run(ctx context.Context, bindings binding.Bindings) {
}
}
}
bindings, err := apibindings.RegisterBindings(ctx, bindings)
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
t.FailNow()
}
for i, test := range p.tests {
name, err := names.Test(p.config, test)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions testdata/e2e/examples/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

*No description*

## Bindings

| # | Name | Value |
|:-:|---|---|
| 1 | `foo` | "(join('-', [$namespace, 'foo']))" |

## Steps

| # | Name | Bindings | Try | Catch | Finally |
Expand Down
9 changes: 6 additions & 3 deletions testdata/e2e/examples/template/chainsaw-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ metadata:
name: template
spec:
template: true
bindings:
- name: foo
value: (join('-', [$namespace, 'foo']))
steps:
- try:
- apply:
resource:
apiVersion: v1
kind: ConfigMap
metadata:
name: ($namespace)
name: ($foo)
- assert:
resource:
apiVersion: v1
kind: ConfigMap
metadata:
name: ($namespace)
name: ($foo)
- delete:
ref:
apiVersion: v1
kind: ConfigMap
name: ($namespace)
name: ($foo)
Loading