Skip to content

Commit

Permalink
Add support for pre/post delete hooks
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Turken <[email protected]>
  • Loading branch information
turkenh committed Oct 26, 2022
1 parent 8c4e0b0 commit 0537b3d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ uptest e2e examples/user.yaml,examples/bucket.yaml --setup-script="test/hooks/se
### Hooks
There are 4 types of hooks that can be used to customize the test flow:
There are 6 types of hooks that can be used to customize the test flow:
1. `setup-script`: This hook will be executed before running the tests case. It is useful to set up the control plane
before running the tests. For example, you can use it to create a provider config and your cloud credentials. This
Expand All @@ -54,6 +54,10 @@ There are 4 types of hooks that can be used to customize the test flow:
manifest file.
4. `post-assert-hook`: This hook will be executed after running the assertions. This can be configured via
`uptest.upbound.io/post-assert-hook` annotation on the manifest as a relative path to the manifest file.
5. `pre-delete-hook`: This hook will be executed just before deleting the resource. This can be configured via
`uptest.upbound.io/pre-delete-hook` annotation on the manifest as a relative path to the manifest file.
6. `post-delete-hook`: This hook will be executed right after the resource is deleted. This can be configured via
`uptest.upbound.io/post-delete-hook` annotation on the manifest as a relative path to the manifest file.
> All hooks need to be executables, please make sure to set the executable bit on your scripts, e.g. with `chmod +x`.
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const (
AnnotationKeyConditions = "uptest.upbound.io/conditions"
AnnotationKeyPreAssertHook = "uptest.upbound.io/pre-assert-hook"
AnnotationKeyPostAssertHook = "uptest.upbound.io/post-assert-hook"
AnnotationKeyPreDeleteHook = "uptest.upbound.io/pre-delete-hook"
AnnotationKeyPostDeleteHook = "uptest.upbound.io/post-delete-hook"
)

type AutomatedTest struct {
Expand Down Expand Up @@ -44,4 +46,6 @@ type Resource struct {
Conditions []string
PreAssertScriptPath string
PostAssertScriptPath string
PreDeleteScriptPath string
PostDeleteScriptPath string
}
6 changes: 6 additions & 0 deletions internal/templates/01-delete.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
{{- range $resource := .Resources }}
{{- if $resource.PreDeleteScriptPath }}
- command: {{ $resource.PreDeleteScriptPath }}
{{- end }}
{{- if $resource.Namespace }}
- command: ${KUBECTL} delete {{ $resource.KindGroup }}/{{ $resource.Name }} --wait=false --namespace {{ $resource.Namespace }} --ignore-not-found
{{- else }}
- command: ${KUBECTL} delete {{ $resource.KindGroup }}/{{ $resource.Name }} --wait=false --ignore-not-found
{{- end }}
{{- if $resource.PostDeleteScriptPath }}
- command: {{ $resource.PostDeleteScriptPath }}
{{- end }}
{{- end }}
22 changes: 13 additions & 9 deletions internal/templates/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"

"github.com/upbound/uptest/internal/config"
)

Expand Down Expand Up @@ -71,7 +71,7 @@ commands:
"01-delete.yaml": `apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: ${KUBECTL} delete s3.aws.upbound.io/example-bucket --wait=false
- command: ${KUBECTL} delete s3.aws.upbound.io/example-bucket --wait=false --ignore-not-found
`,
"01-assert.yaml": `apiVersion: kuttl.dev/v1beta1
kind: TestAssert
Expand All @@ -92,18 +92,20 @@ commands:
},
resources: []config.Resource{
{
YAML: bucketManifest,
Name: "example-bucket",
KindGroup: "s3.aws.upbound.io",
PreAssertScriptPath: "/tmp/bucket/pre-assert.sh",
Conditions: []string{"Test"},
YAML: bucketManifest,
Name: "example-bucket",
KindGroup: "s3.aws.upbound.io",
PreAssertScriptPath: "/tmp/bucket/pre-assert.sh",
PostDeleteScriptPath: "/tmp/bucket/post-delete.sh",
Conditions: []string{"Test"},
},
{
YAML: claimManifest,
Name: "test-cluster-claim",
KindGroup: "cluster.gcp.platformref.upbound.io",
Namespace: "upbound-system",
PostAssertScriptPath: "/tmp/claim/post-assert.sh",
PreDeleteScriptPath: "/tmp/claim/pre-delete.sh",
Conditions: []string{"Ready", "Synced"},
},
},
Expand All @@ -129,8 +131,10 @@ commands:
"01-delete.yaml": `apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: ${KUBECTL} delete s3.aws.upbound.io/example-bucket --wait=false
- command: ${KUBECTL} delete cluster.gcp.platformref.upbound.io/test-cluster-claim --wait=false --namespace upbound-system
- command: ${KUBECTL} delete s3.aws.upbound.io/example-bucket --wait=false --ignore-not-found
- command: /tmp/bucket/post-delete.sh
- command: /tmp/claim/pre-delete.sh
- command: ${KUBECTL} delete cluster.gcp.platformref.upbound.io/test-cluster-claim --wait=false --namespace upbound-system --ignore-not-found
`,
"01-assert.yaml": `apiVersion: kuttl.dev/v1beta1
kind: TestAssert
Expand Down
14 changes: 14 additions & 0 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ func (t *Tester) prepareConfig() (*config.TestCase, []config.Resource, error) {
}
}

if v, ok := annotations[config.AnnotationKeyPreDeleteHook]; ok {
example.PreDeleteScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(m.FilePath), filepath.Clean(v)))
if err != nil {
return nil, nil, errors.Wrap(err, "cannot find absolute path for pre delete hook")
}
}

if v, ok := annotations[config.AnnotationKeyPostDeleteHook]; ok {
example.PostDeleteScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(m.FilePath), filepath.Clean(v)))
if err != nil {
return nil, nil, errors.Wrap(err, "cannot find absolute path for post delete hook")
}
}

examples = append(examples, example)
}

Expand Down

0 comments on commit 0537b3d

Please sign in to comment.