Skip to content

Commit

Permalink
Address reviews
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <[email protected]>
  • Loading branch information
sergenyalcin committed Oct 5, 2023
1 parent 21da281 commit 740e2fc
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/pr-comment-trigger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ on:
required: false
type: string
update-test-parameter:
description: 'Input parameter to use during update step. If this field
is empty, then the update step will be skipped.
The update parameter should be a serialized JSON object and if the JSON
object has nested children, then there must be only one leaf.
Example: {"tags":{"uptest":"update"}}
This parameter will add a tag to the test resource.'
default: ''
required: false
type: string
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (
// AnnotationKeyUpdateParameter defines the update parameter that will be
// used during the update step
AnnotationKeyUpdateParameter = "uptest.upbound.io/update-parameter"
// AnnotationKeyExampleId is id of example that populated from example
// manifest. This information will be used for determining the root resource
AnnotationKeyExampleId = "meta.upbound.io/example-id"

Check warning on line 43 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: const AnnotationKeyExampleId should be AnnotationKeyExampleID (revive)
)

// AutomatedTest represents an automated test of resource example
Expand Down Expand Up @@ -87,4 +90,6 @@ type Resource struct {
UpdateParameter string
UpdateAssertKey string
UpdateAssertValue string

Root bool
}
1 change: 1 addition & 0 deletions internal/templates/00-apply.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This file belongs to the resource apply step.
{{ if .TestCase.SetupScriptPath -}}
apiVersion: kuttl.dev/v1beta1
kind: TestStep
Expand Down
6 changes: 4 additions & 2 deletions internal/templates/00-assert.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# This assert file belongs to the resource apply step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: {{ .TestCase.Timeout }}
commands:
- command: ${KUBECTL} annotate managed --all upjet.upbound.io/test=true --overwrite
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the apply assertion step:"; ${KUBECTL} get managed -o yaml
- script: echo "Dump Claim manifests for the apply assertion step:" || ${KUBECTL} get claim --all-namespaces -o yaml
{{- range $resource := .Resources }}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
Expand All @@ -13,7 +15,7 @@ commands:
{{- end }}
{{- range $condition := $resource.Conditions }}
{{- if $resource.Namespace }}
- script: ${KUBECTL} get --namespace {{ $resource.Namespace }} {{ $resource.KindGroup }}/{{ $resource.Name }} -o yaml && ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s --namespace {{ $resource.Namespace }}
- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s --namespace {{ $resource.Namespace }}
{{- else }}
- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s
{{- end }}
Expand Down
5 changes: 4 additions & 1 deletion internal/templates/01-assert.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# This assert file belongs to the resource update step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: {{ .TestCase.Timeout }}
commands:
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the update assertion step:"; ${KUBECTL} get managed -o yaml
{{- range $resource := .Resources }}
{{- if eq $resource.UpdateParameter "" -}}
{{continue}}
Expand All @@ -11,6 +12,8 @@ commands:
{{continue}}
{{- end -}}
{{- if not $resource.Namespace }}
{{- if $resource.Root }}
- script: ${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.status.atProvider{{ $resource.UpdateAssertKey }}}' | grep -q "^{{ $resource.UpdateAssertValue }}$"
{{- end }}
{{- end }}
{{- end }}
3 changes: 3 additions & 0 deletions internal/templates/01-update.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This file belongs to the resource update step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
Expand All @@ -9,6 +10,8 @@ commands:
{{continue}}
{{- end -}}
{{- if not $resource.Namespace }}
{{- if $resource.Root }}
- command: ${KUBECTL} patch {{ $resource.KindGroup }}/{{ $resource.Name }} --type=merge -p '{"spec":{"forProvider":{{ $resource.UpdateParameter }}}}'
{{- end }}
{{- end }}
{{- end }}
3 changes: 2 additions & 1 deletion internal/templates/02-assert.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# This assert file belongs to the resource import step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: {{ .TestCase.Timeout }}
commands:
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the import assertion step:"; ${KUBECTL} get managed -o yaml
{{- range $resource := .Resources }}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
Expand Down
6 changes: 5 additions & 1 deletion internal/templates/02-import.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# This file belongs to the resource import step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: ${KUBECTL} scale deployment crossplane -n upbound-system --replicas=0
- script: ${KUBECTL} -n upbound-system get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system scale deploy --replicas=0
{{- range $resource := .Resources }}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
Expand All @@ -10,4 +13,5 @@ commands:
- script: ${KUBECTL} annotate {{ $resource.KindGroup }}/{{ $resource.Name }} uptest-old-id=$(${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.status.atProvider.id}') --overwrite
{{- end }}
{{- end }}
- script: ${KUBECTL} -n upbound-system get pods --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system delete pod
- command: ${KUBECTL} scale deployment crossplane -n upbound-system --replicas=1
- script: ${KUBECTL} -n upbound-system get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system scale deploy --replicas=1
6 changes: 4 additions & 2 deletions internal/templates/03-assert.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# This assert file belongs to the resource delete step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: {{ .TestCase.Timeout }}
commands:
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the delete assertion step:"; ${KUBECTL} get managed -o yaml
- script: echo "Dump Claim manifests for the delete assertion step:" || ${KUBECTL} get claim --all-namespaces -o yaml
{{- range $resource := .Resources }}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
{{- end -}}
{{- if $resource.Namespace }}
- script: ${KUBECTL} get --namespace {{ $resource.Namespace }} {{ $resource.KindGroup }}/{{ $resource.Name }} -o yaml --ignore-not-found && ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=delete --timeout 10s --namespace {{ $resource.Namespace }}
- script: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=delete --timeout 10s --namespace {{ $resource.Namespace }}
{{- else }}
- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=delete --timeout 10s
{{- end }}
Expand Down
1 change: 1 addition & 0 deletions internal/templates/03-delete.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This file belongs to the resource delete step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
Expand Down
84 changes: 55 additions & 29 deletions internal/templates/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,49 +84,62 @@ func TestRender(t *testing.T) {
},
want: want{
out: map[string]string{
"00-apply.yaml": "---\n" + bucketManifest,
"00-assert.yaml": `apiVersion: kuttl.dev/v1beta1
"00-apply.yaml": "# This file belongs to the resource apply step.\n---\n" + bucketManifest,
"00-assert.yaml": `# This assert file belongs to the resource apply step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} annotate managed --all upjet.upbound.io/test=true --overwrite
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the apply assertion step:"; ${KUBECTL} get managed -o yaml
- script: echo "Dump Claim manifests for the apply assertion step:" || ${KUBECTL} get claim --all-namespaces -o yaml
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s
`,
"01-update.yaml": `apiVersion: kuttl.dev/v1beta1
"01-update.yaml": `# This file belongs to the resource update step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
`,
"01-assert.yaml": `apiVersion: kuttl.dev/v1beta1
"01-assert.yaml": `# This assert file belongs to the resource update step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the update assertion step:"; ${KUBECTL} get managed -o yaml
`,
"02-assert.yaml": `apiVersion: kuttl.dev/v1beta1
"02-assert.yaml": `# This assert file belongs to the resource import step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the import assertion step:"; ${KUBECTL} get managed -o yaml
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s
- script: new_id=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.status.atProvider.id}') && old_id=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.uptest-old-id}') && [ "$new_id" == "$old_id" ]
`,
"02-import.yaml": `apiVersion: kuttl.dev/v1beta1
"02-import.yaml": `# This file belongs to the resource import step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: ${KUBECTL} scale deployment crossplane -n upbound-system --replicas=0
- script: ${KUBECTL} -n upbound-system get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system scale deploy --replicas=0
- command: ${KUBECTL} --subresource=status patch s3.aws.upbound.io/example-bucket --type=merge -p '{"status":{"conditions":[]}}'
- script: ${KUBECTL} annotate s3.aws.upbound.io/example-bucket uptest-old-id=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.status.atProvider.id}') --overwrite
- script: ${KUBECTL} -n upbound-system get pods --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system delete pod
- command: ${KUBECTL} scale deployment crossplane -n upbound-system --replicas=1
- script: ${KUBECTL} -n upbound-system get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system scale deploy --replicas=1
`,
"03-assert.yaml": `apiVersion: kuttl.dev/v1beta1

"03-assert.yaml": `# This assert file belongs to the resource delete step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the delete assertion step:"; ${KUBECTL} get managed -o yaml
- script: echo "Dump Claim manifests for the delete assertion step:" || ${KUBECTL} get claim --all-namespaces -o yaml
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=delete --timeout 10s
- command: ${KUBECTL} wait managed --all --for=delete --timeout 10s
`,
"03-delete.yaml": `apiVersion: kuttl.dev/v1beta1
"03-delete.yaml": `# This file belongs to the resource delete step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: ${KUBECTL} delete s3.aws.upbound.io/example-bucket --wait=false --ignore-not-found
Expand Down Expand Up @@ -169,59 +182,72 @@ commands:
},
want: want{
out: map[string]string{
"00-apply.yaml": `apiVersion: kuttl.dev/v1beta1
"00-apply.yaml": `# This file belongs to the resource apply step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: /tmp/setup.sh
` + "---\n" + bucketManifest + "---\n" + claimManifest + "---\n" + secretManifest,
"00-assert.yaml": `apiVersion: kuttl.dev/v1beta1
"00-assert.yaml": `# This assert file belongs to the resource apply step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} annotate managed --all upjet.upbound.io/test=true --overwrite
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the apply assertion step:"; ${KUBECTL} get managed -o yaml
- script: echo "Dump Claim manifests for the apply assertion step:" || ${KUBECTL} get claim --all-namespaces -o yaml
- command: /tmp/bucket/pre-assert.sh
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s
- script: ${KUBECTL} get --namespace upbound-system cluster.gcp.platformref.upbound.io/test-cluster-claim -o yaml && ${KUBECTL} wait cluster.gcp.platformref.upbound.io/test-cluster-claim --for=condition=Ready --timeout 10s --namespace upbound-system
- script: ${KUBECTL} get --namespace upbound-system cluster.gcp.platformref.upbound.io/test-cluster-claim -o yaml && ${KUBECTL} wait cluster.gcp.platformref.upbound.io/test-cluster-claim --for=condition=Synced --timeout 10s --namespace upbound-system
- command: ${KUBECTL} wait cluster.gcp.platformref.upbound.io/test-cluster-claim --for=condition=Ready --timeout 10s --namespace upbound-system
- command: ${KUBECTL} wait cluster.gcp.platformref.upbound.io/test-cluster-claim --for=condition=Synced --timeout 10s --namespace upbound-system
- command: /tmp/claim/post-assert.sh
`,
"01-update.yaml": `apiVersion: kuttl.dev/v1beta1
"01-update.yaml": `# This file belongs to the resource update step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
`,
"01-assert.yaml": `apiVersion: kuttl.dev/v1beta1
"01-assert.yaml": `# This assert file belongs to the resource update step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the update assertion step:"; ${KUBECTL} get managed -o yaml
`,
"02-assert.yaml": `apiVersion: kuttl.dev/v1beta1
"02-assert.yaml": `# This assert file belongs to the resource import step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the import assertion step:"; ${KUBECTL} get managed -o yaml
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s
- script: new_id=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.status.atProvider.id}') && old_id=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.uptest-old-id}') && [ "$new_id" == "$old_id" ]
`,
"02-import.yaml": `apiVersion: kuttl.dev/v1beta1
"02-import.yaml": `# This file belongs to the resource import step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: ${KUBECTL} scale deployment crossplane -n upbound-system --replicas=0
- script: ${KUBECTL} -n upbound-system get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system scale deploy --replicas=0
- command: ${KUBECTL} --subresource=status patch s3.aws.upbound.io/example-bucket --type=merge -p '{"status":{"conditions":[]}}'
- script: ${KUBECTL} annotate s3.aws.upbound.io/example-bucket uptest-old-id=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.status.atProvider.id}') --overwrite
- script: ${KUBECTL} -n upbound-system get pods --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system delete pod
- command: ${KUBECTL} scale deployment crossplane -n upbound-system --replicas=1
- script: ${KUBECTL} -n upbound-system get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system scale deploy --replicas=1
`,
"03-assert.yaml": `apiVersion: kuttl.dev/v1beta1
"03-assert.yaml": `# This assert file belongs to the resource delete step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} get managed -o yaml
- script: echo "Dump MR manifests for the delete assertion step:"; ${KUBECTL} get managed -o yaml
- script: echo "Dump Claim manifests for the delete assertion step:" || ${KUBECTL} get claim --all-namespaces -o yaml
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=delete --timeout 10s
- script: ${KUBECTL} get --namespace upbound-system cluster.gcp.platformref.upbound.io/test-cluster-claim -o yaml --ignore-not-found && ${KUBECTL} wait cluster.gcp.platformref.upbound.io/test-cluster-claim --for=delete --timeout 10s --namespace upbound-system
- script: ${KUBECTL} wait cluster.gcp.platformref.upbound.io/test-cluster-claim --for=delete --timeout 10s --namespace upbound-system
- command: ${KUBECTL} wait managed --all --for=delete --timeout 10s
- command: /tmp/teardown.sh
`,
"03-delete.yaml": `apiVersion: kuttl.dev/v1beta1
"03-delete.yaml": `# This file belongs to the resource delete step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: ${KUBECTL} delete s3.aws.upbound.io/example-bucket --wait=false --ignore-not-found
Expand Down
30 changes: 16 additions & 14 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) {

for _, m := range t.manifests {
obj := m.Object
kg := strings.ToLower(obj.GroupVersionKind().Kind + "." + obj.GroupVersionKind().Group)
groupVersionKind := obj.GroupVersionKind()
kg := strings.ToLower(groupVersionKind.Kind + "." + groupVersionKind.Group)

example := config.Resource{
Name: obj.GetName(),
Expand All @@ -82,15 +83,6 @@ func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) {
Conditions: t.options.DefaultConditions,
}

if updateParameter := os.Getenv("UPTEST_UPDATE_PARAMETER"); updateParameter != "" {
example.UpdateParameter = updateParameter
var data map[string]interface{}
if err := json.Unmarshal([]byte(updateParameter), &data); err != nil {
return nil, nil, errors.Wrap(err, "cannot unmarshal JSON")
}
example.UpdateAssertKey, example.UpdateAssertValue = convertToJSONPath(data, "")
}

var err error
annotations := obj.GetAnnotations()
if v, ok := annotations[config.AnnotationKeyTimeout]; ok {
Expand Down Expand Up @@ -135,15 +127,25 @@ func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) {
}
}

if v, ok := annotations[config.AnnotationKeyUpdateParameter]; ok {
example.UpdateParameter = v
updateParameter, ok := annotations[config.AnnotationKeyUpdateParameter]
if !ok {
updateParameter = os.Getenv("UPTEST_UPDATE_PARAMETER")
}
if updateParameter != "" {
example.UpdateParameter = updateParameter
var data map[string]interface{}
if err := json.Unmarshal([]byte(v), &data); err != nil {
return nil, nil, errors.Wrap(err, "cannot unmarshal JSON")
if err := json.Unmarshal([]byte(updateParameter), &data); err != nil {
return nil, nil, errors.Wrapf(err, "cannot unmarshal JSON object: %s", updateParameter)
}
example.UpdateAssertKey, example.UpdateAssertValue = convertToJSONPath(data, "")
}

if exampleId, ok := annotations["meta.upbound.io/example-id"]; ok {

Check warning on line 143 in internal/tester.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var exampleId should be exampleID (revive)
if exampleId == strings.ToLower(fmt.Sprintf("%s/%s/%s", strings.Split(groupVersionKind.Group, ".")[0], groupVersionKind.Version, groupVersionKind.Kind)) {
example.Root = true
}
}

examples = append(examples, example)
}

Expand Down

0 comments on commit 740e2fc

Please sign in to comment.