Skip to content
Open
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
16 changes: 16 additions & 0 deletions internal/templates/0-setup.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file belongs to the resource setup step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: setup
spec:
timeouts:
exec: {{ .TestCase.Timeout }}
steps:
{{- if .TestCase.SetupScriptPath }}
- name: Run Setup Script
description: Setup the test environment by running the setup script.
try:
- command:
entrypoint: {{ .TestCase.SetupScriptPath }}
{{- end }}
3 changes: 3 additions & 0 deletions internal/templates/0-setup.yaml.tmpl.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>

SPDX-License-Identifier: CC0-1.0
7 changes: 0 additions & 7 deletions internal/templates/00-apply.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ spec:
assert: {{ .TestCase.Timeout }}
exec: {{ .TestCase.Timeout }}
steps:
{{- if .TestCase.SetupScriptPath }}
- name: Run Setup Script
description: Setup the test environment by running the setup script.
try:
- command:
entrypoint: {{ .TestCase.SetupScriptPath }}
{{- end }}
- name: Apply Resources
description: Apply resources to the cluster.
try:
Expand Down
5 changes: 5 additions & 0 deletions internal/templates/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ package templates

import _ "embed"

// setupFileTemplate is the template for the setup file.
//
//go:embed 0-setup.yaml.tmpl
var setupFileTemplate string

// inputFileTemplate is the template for the input file.
//
//go:embed 00-apply.yaml.tmpl
Expand Down
8 changes: 8 additions & 0 deletions internal/templates/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var fileTemplates = map[string]string{
"0-setup.yaml": setupFileTemplate,
"00-apply.yaml": inputFileTemplate,
"01-update.yaml": updateFileTemplate,
"02-import.yaml": importFileTemplate,
Expand All @@ -24,6 +25,8 @@ var fileTemplates = map[string]string{

// Render renders the specified list of resources as a test case
// with the specified configuration.
//
// nolint:gocyclo
func Render(tc *config.TestCase, resources []config.Resource, skipDelete bool) (map[string]string, error) {
data := struct {
Resources []config.Resource
Expand All @@ -35,6 +38,11 @@ func Render(tc *config.TestCase, resources []config.Resource, skipDelete bool) (

res := make(map[string]string, len(fileTemplates))
for name, tmpl := range fileTemplates {
// Skip templates with names starting with "0-" if no setup script is set
if tc.SetupScriptPath == "" && strings.HasPrefix(name, "0-") {
continue
}

// Skip templates with names starting with "01-" if skipUpdate is true
if tc.SkipUpdate && strings.HasPrefix(name, "01-") {
continue
Expand Down
126 changes: 98 additions & 28 deletions internal/templates/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,32 @@ func TestRender(t *testing.T) {
},
want: want{
out: map[string]string{
"00-apply.yaml": `# This file belongs to the resource apply step.
"0-setup.yaml": `# This file belongs to the resource setup step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
name: setup
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Run Setup Script
description: Setup the test environment by running the setup script.
try:
- command:
entrypoint: /tmp/setup.sh
`,
"00-apply.yaml": `# This file belongs to the resource apply step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Apply Resources
description: Apply resources to the cluster.
try:
Expand Down Expand Up @@ -648,22 +658,32 @@ spec:
},
want: want{
out: map[string]string{
"00-apply.yaml": `# This file belongs to the resource apply step.
"0-setup.yaml": `# This file belongs to the resource setup step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
name: setup
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Run Setup Script
description: Setup the test environment by running the setup script.
try:
- command:
entrypoint: /tmp/setup.sh
`,
"00-apply.yaml": `# This file belongs to the resource apply step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Apply Resources
description: Apply resources to the cluster.
try:
Expand Down Expand Up @@ -979,22 +999,32 @@ func TestRenderWithSkipDelete(t *testing.T) {
},
want: want{
out: map[string]string{
"00-apply.yaml": `# This file belongs to the resource apply step.
"0-setup.yaml": `# This file belongs to the resource setup step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
name: setup
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Run Setup Script
description: Setup the test environment by running the setup script.
try:
- command:
entrypoint: /tmp/setup.sh
`,
"00-apply.yaml": `# This file belongs to the resource apply step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Apply Resources
description: Apply resources to the cluster.
try:
Expand Down Expand Up @@ -1231,22 +1261,32 @@ spec:
},
want: want{
out: map[string]string{
"00-apply.yaml": `# This file belongs to the resource apply step.
"0-setup.yaml": `# This file belongs to the resource setup step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
name: setup
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Run Setup Script
description: Setup the test environment by running the setup script.
try:
- command:
entrypoint: /tmp/setup.sh
`,
"00-apply.yaml": `# This file belongs to the resource apply step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Apply Resources
description: Apply resources to the cluster.
try:
Expand Down Expand Up @@ -1385,22 +1425,32 @@ spec:
},
want: want{
out: map[string]string{
"00-apply.yaml": `# This file belongs to the resource apply step.
"0-setup.yaml": `# This file belongs to the resource setup step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
name: setup
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Run Setup Script
description: Setup the test environment by running the setup script.
try:
- command:
entrypoint: /tmp/setup.sh
`,
"00-apply.yaml": `# This file belongs to the resource apply step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Apply Resources
description: Apply resources to the cluster.
try:
Expand Down Expand Up @@ -1639,22 +1689,32 @@ spec:
},
want: want{
out: map[string]string{
"00-apply.yaml": `# This file belongs to the resource apply step.
"0-setup.yaml": `# This file belongs to the resource setup step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
name: setup
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Run Setup Script
description: Setup the test environment by running the setup script.
try:
- command:
entrypoint: /tmp/setup.sh
`,
"00-apply.yaml": `# This file belongs to the resource apply step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Apply Resources
description: Apply resources to the cluster.
try:
Expand Down Expand Up @@ -1755,22 +1815,32 @@ spec:
},
want: want{
out: map[string]string{
"00-apply.yaml": `# This file belongs to the resource apply step.
"0-setup.yaml": `# This file belongs to the resource setup step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
name: setup
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Run Setup Script
description: Setup the test environment by running the setup script.
try:
- command:
entrypoint: /tmp/setup.sh
`,
"00-apply.yaml": `# This file belongs to the resource apply step.
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: apply
spec:
timeouts:
apply: 10m0s
assert: 10m0s
exec: 10m0s
steps:
- name: Apply Resources
description: Apply resources to the cluster.
try:
Expand Down
1 change: 1 addition & 0 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
)

var testFiles = []string{
"0-setup.yaml",
"00-apply.yaml",
"01-update.yaml",
"02-import.yaml",
Expand Down