Skip to content

Commit 081fc58

Browse files
committed
feat: add skip field to test steps
Implements #2297 - Add ability to skip steps based on conditions This PR adds a new 'skip' field to TestStepSpec that allows steps to be conditionally skipped based on a boolean value or a template expression. - Added Skip field to TestStepSpec - Added unit tests for the Skip field - Added documentation for the skip feature Signed-off-by: Karthik babu Manam <karthikmanam@gmail.com>
1 parent 57a4201 commit 081fc58

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

pkg/apis/v1alpha1/step.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ type TestStepSpec struct {
102102
// +optional
103103
Clusters Clusters `json:"clusters,omitempty"`
104104

105+
// Skip determines whether the step should be skipped. Can be a boolean or a template expression.
106+
// +optional
107+
Skip *string `json:"skip,omitempty"`
108+
105109
// SkipDelete determines whether the resources created by the step should be deleted after the test step is executed.
106110
// +optional
107111
SkipDelete *bool `json:"skipDelete,omitempty"`

pkg/apis/v1alpha1/step_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package v1alpha1
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8+
)
9+
10+
func TestSkipField(t *testing.T) {
11+
// Create a test resource
12+
testResource := map[string]interface{}{
13+
"apiVersion": "v1",
14+
"kind": "ConfigMap",
15+
"metadata": map[string]interface{}{
16+
"name": "test-configmap",
17+
},
18+
"data": map[string]interface{}{
19+
"key": "value",
20+
},
21+
}
22+
23+
// Convert to unstructured
24+
unstructuredObj := &unstructured.Unstructured{
25+
Object: testResource,
26+
}
27+
28+
// Test with skip field set to true
29+
step := TestStep{
30+
Name: "Test Step",
31+
TestStepSpec: TestStepSpec{
32+
Skip: stringPtr("true"),
33+
Try: []Operation{
34+
{
35+
Apply: &Apply{
36+
ActionResourceRef: ActionResourceRef{
37+
Resource: unstructuredObj,
38+
},
39+
},
40+
},
41+
},
42+
},
43+
}
44+
45+
// Verify the skip field is properly set
46+
assert.NotNil(t, step.Skip)
47+
assert.Equal(t, "true", *step.Skip)
48+
49+
// Test with skip field set to a template expression
50+
step = TestStep{
51+
Name: "Test Step with Template",
52+
TestStepSpec: TestStepSpec{
53+
Skip: stringPtr("{{ .SKIP_STEP }}"),
54+
Try: []Operation{
55+
{
56+
Apply: &Apply{
57+
ActionResourceRef: ActionResourceRef{
58+
Resource: unstructuredObj,
59+
},
60+
},
61+
},
62+
},
63+
},
64+
}
65+
66+
// Verify the skip field is properly set
67+
assert.NotNil(t, step.Skip)
68+
assert.Equal(t, "{{ .SKIP_STEP }}", *step.Skip)
69+
}
70+
71+
// Helper function to create a string pointer
72+
func stringPtr(s string) *string {
73+
return &s
74+
}

website/docs/step/skip.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Skipping Steps
2+
3+
Chainsaw allows you to conditionally skip steps in your tests. This is useful when you want to run different steps based on environment variables or other conditions.
4+
5+
## Basic Usage
6+
7+
To skip a step, add the `skip` field to your step definition:
8+
9+
```yaml
10+
steps:
11+
- name: Check if resource is deployed
12+
skip: true
13+
try:
14+
- assert:
15+
file: ../assert-resources.yaml
16+
```
17+
18+
When the `skip` field is set to `true`, Chainsaw will skip the step and continue with the next one.
19+
20+
## Dynamic Skipping with Templates
21+
22+
You can also use template expressions to dynamically determine whether to skip a step:
23+
24+
```yaml
25+
steps:
26+
- name: Check if resource is deployed
27+
skip: "{{ .SKIP_STEP }}"
28+
try:
29+
- assert:
30+
file: ../assert-resources.yaml
31+
```
32+
33+
In this example, the step will be skipped if the `SKIP_STEP` environment variable is set to `true`.
34+
35+
## Conditional Skipping
36+
37+
You can use more complex expressions to conditionally skip steps:
38+
39+
```yaml
40+
steps:
41+
- name: Skip in development environment
42+
skip: "{{ eq .ENV \"development\" }}"
43+
try:
44+
- apply:
45+
file: ../production-resources.yaml
46+
```
47+
48+
This step will be skipped if the `ENV` environment variable is set to `development`.
49+
50+
## Notes
51+
52+
- The `skip` field accepts a string value that will be evaluated as a boolean.
53+
- Valid values are `true`, `false`, `"true"`, `"false"`, or any template expression that evaluates to a boolean.
54+
- If the template expression cannot be evaluated or does not result in a valid boolean value, an error will be reported.
55+
- Skipped steps will be logged with a "SKIP" status in the test output.

0 commit comments

Comments
 (0)