diff --git a/internal/templates/0-setup.yaml.tmpl b/internal/templates/0-setup.yaml.tmpl new file mode 100644 index 0000000..9c9e752 --- /dev/null +++ b/internal/templates/0-setup.yaml.tmpl @@ -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 }} diff --git a/internal/templates/0-setup.yaml.tmpl.license b/internal/templates/0-setup.yaml.tmpl.license new file mode 100644 index 0000000..2072b73 --- /dev/null +++ b/internal/templates/0-setup.yaml.tmpl.license @@ -0,0 +1,3 @@ +SPDX-FileCopyrightText: 2024 The Crossplane Authors + +SPDX-License-Identifier: CC0-1.0 \ No newline at end of file diff --git a/internal/templates/00-apply.yaml.tmpl b/internal/templates/00-apply.yaml.tmpl index e9a7d50..1897853 100644 --- a/internal/templates/00-apply.yaml.tmpl +++ b/internal/templates/00-apply.yaml.tmpl @@ -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: diff --git a/internal/templates/embed.go b/internal/templates/embed.go index cadeb6c..70ee1f2 100644 --- a/internal/templates/embed.go +++ b/internal/templates/embed.go @@ -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 diff --git a/internal/templates/renderer.go b/internal/templates/renderer.go index a3fb3fd..21bbab7 100644 --- a/internal/templates/renderer.go +++ b/internal/templates/renderer.go @@ -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, @@ -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 @@ -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 diff --git a/internal/templates/renderer_test.go b/internal/templates/renderer_test.go index 59e4300..05755be 100644 --- a/internal/templates/renderer_test.go +++ b/internal/templates/renderer_test.go @@ -79,15 +79,13 @@ 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 @@ -95,6 +93,18 @@ spec: 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: @@ -648,15 +658,13 @@ 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 @@ -664,6 +672,18 @@ spec: 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: @@ -979,15 +999,13 @@ 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 @@ -995,6 +1013,18 @@ spec: 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: @@ -1231,15 +1261,13 @@ 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 @@ -1247,6 +1275,18 @@ spec: 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: @@ -1385,15 +1425,13 @@ 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 @@ -1401,6 +1439,18 @@ spec: 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: @@ -1639,15 +1689,13 @@ 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 @@ -1655,6 +1703,18 @@ spec: 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: @@ -1755,15 +1815,13 @@ 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 @@ -1771,6 +1829,18 @@ spec: 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: diff --git a/internal/tester.go b/internal/tester.go index 1967056..7391aca 100644 --- a/internal/tester.go +++ b/internal/tester.go @@ -42,6 +42,7 @@ import ( ) var testFiles = []string{ + "0-setup.yaml", "00-apply.yaml", "01-update.yaml", "02-import.yaml",