Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add yaml outputs of resources to streaming logs and add import&update steps to uptest #136

Merged
merged 7 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ const (
// AnnotationKeyPostDeleteHook defines the path to a post-delete
// hook script to be executed after the tested resource is deleted.
AnnotationKeyPostDeleteHook = "uptest.upbound.io/post-delete-hook"
// AnnotationKeyUpdateField defines the field that will be updated during
// update step
AnnotationKeyUpdateField = "uptest.upbound.io/update-field"
// AnnotationKeyUpdateValue defines the field's value that will be updated
// during update step
AnnotationKeyUpdateValue = "uptest.upbound.io/update-value"
// AnnotationKeyUpdateAssertField defines the field that will be asserted
//during the update step
AnnotationKeyUpdateAssertField = "uptest.upbound.io/update-assert-field"
// AnnotationKeyUpdateAssertValue defines the field's value that will be
// asserted during the update step
AnnotationKeyUpdateAssertValue = "uptest.upbound.io/update-assert-value"
)

// AutomatedTest represents an automated test of resource example
Expand Down Expand Up @@ -80,4 +92,9 @@ type Resource struct {
PostAssertScriptPath string
PreDeleteScriptPath string
PostDeleteScriptPath string

UpdateField string
UpdateValue string
UpdateAssertField string
UpdateAssertValue string
}
5 changes: 3 additions & 2 deletions internal/templates/00-assert.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ kind: TestAssert
timeout: {{ .TestCase.Timeout }}
commands:
- command: ${KUBECTL} annotate managed --all upjet.upbound.io/test=true --overwrite
- command: ${KUBECTL} get managed -o yaml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- command: ${KUBECTL} get managed -o yaml
- script: ${KUBECTL} get managed -o yaml || ${KUBECTL} get claim --all-namespaces -o yaml

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We had better include a header in the debug log messages like Dumping all MRs: , Dumping all claims: .

{{- range $resource := .Resources }}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
Expand All @@ -12,9 +13,9 @@ commands:
{{- end }}
{{- range $condition := $resource.Conditions }}
{{- if $resource.Namespace }}
- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s --namespace {{ $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 }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may just dump all the claims above with the MRs:

Suggested change
- 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 }}
- script: ${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
- script: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s
{{- end }}
{{- end }}
{{- if $resource.PostAssertScriptPath }}
Expand Down
11 changes: 3 additions & 8 deletions internal/templates/01-assert.yaml.tmpl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put a YAML comment at the top of this file telling that this assert step belongs to the resource update test.

Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: {{ .TestCase.Timeout }}
commands:
- command: ${KUBECTL} get managed -o yaml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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.KindGroup "secret." -}}
{{continue}}
{{- end -}}
{{- if $resource.Namespace }}
- command: ${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
{{- if not $resource.Namespace }}
- script: ${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.status.atProvider.{{ $resource.UpdateAssertField }}}' | grep -q "^{{ $resource.UpdateAssertValue }}$"
{{- end }}
{{- end }}
- command: ${KUBECTL} wait managed --all --for=delete --timeout 10s
{{- if .TestCase.TeardownScriptPath }}
- command: {{ .TestCase.TeardownScriptPath }}
{{- end }}
11 changes: 11 additions & 0 deletions internal/templates/01-update.yaml.tmpl
sergenyalcin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
{{- range $resource := .Resources }}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
{{- end -}}
{{- if not $resource.Namespace }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the next iteration, we can consider adding claim update tests.

- script: '${KUBECTL} patch {{ $resource.KindGroup }}/{{ $resource.Name }} --type=json -p "[{"op": "add", "path": "/spec/forProvider/{{ $resource.UpdateField }}", "value": {{ $resource.UpdateValue }}}]"'
sergenyalcin marked this conversation as resolved.
Show resolved Hide resolved
{{- end }}
{{- end }}
18 changes: 18 additions & 0 deletions internal/templates/02-assert.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: {{ .TestCase.Timeout }}
commands:
- command: ${KUBECTL} get managed -o yaml
{{- range $resource := .Resources }}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
{{- end -}}
{{- range $condition := $resource.Conditions }}
{{- if not $resource.Namespace }}
- script: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s
{{- end }}
{{- end }}
{{- if not $resource.Namespace }}
- script: annotation_value_1=$(${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') && annotation_value_2=$(${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.metadata.annotations.uptest-external-name}') && [ "$annotation_value_1" == "$annotation_value_2" ]
{{- end }}
{{- end }}
13 changes: 13 additions & 0 deletions internal/templates/02-import.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
{{- range $resource := .Resources }}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
{{- end -}}
{{- if not $resource.Namespace }}
- script: ${KUBECTL} --subresource=status patch {{ $resource.KindGroup }}/{{ $resource.Name }} --type=merge -p '{"status":{"conditions":[]}}'
- script: ${KUBECTL} annotate {{ $resource.KindGroup }}/{{ $resource.Name }} uptest-external-name=$(${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') --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
19 changes: 19 additions & 0 deletions internal/templates/03-assert.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: {{ .TestCase.Timeout }}
commands:
- command: ${KUBECTL} get managed -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 }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may consider moving the kubectl get outside the loop.

{{- else }}
- script: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=delete --timeout 10s
{{- end }}
{{- end }}
- command: ${KUBECTL} wait managed --all --for=delete --timeout 10s
{{- if .TestCase.TeardownScriptPath }}
- command: {{ .TestCase.TeardownScriptPath }}
{{- end }}
24 changes: 22 additions & 2 deletions internal/templates/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,32 @@ var inputFileTemplate string
//go:embed 00-assert.yaml.tmpl
var assertFileTemplate string

// updateFileTemplate is the template for the update file.
//
//go:embed 01-update.yaml.tmpl
var updateFileTemplate string

// assertUpdatedFileTemplate is the template for update assert file.
//
//go:embed 01-assert.yaml.tmpl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
//go:embed 01-assert.yaml.tmpl
//go:embed 01-assert-update.yaml.tmpl

var assertUpdatedFileTemplate string

// deleteFileTemplate is the template for the import file.
//
//go:embed 02-import.yaml.tmpl
var importFileTemplate string

// assertDeletedFileTemplate is the template for import assert file.
//
//go:embed 02-assert.yaml.tmpl
var assertImportedFileTemplate string

// deleteFileTemplate is the template for the delete file.
//
//go:embed 01-delete.yaml.tmpl
//go:embed 03-delete.yaml.tmpl
var deleteFileTemplate string

// assertDeletedFileTemplate is the template for delete assert file.
//
//go:embed 01-assert.yaml.tmpl
//go:embed 03-assert.yaml.tmpl
var assertDeletedFileTemplate string
8 changes: 6 additions & 2 deletions internal/templates/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ import (
var fileTemplates = map[string]string{
"00-apply.yaml": inputFileTemplate,
"00-assert.yaml": assertFileTemplate,
"01-delete.yaml": deleteFileTemplate,
"01-assert.yaml": assertDeletedFileTemplate,
"01-update.yaml": updateFileTemplate,
"01-assert.yaml": assertUpdatedFileTemplate,
"02-import.yaml": importFileTemplate,
"02-assert.yaml": assertImportedFileTemplate,
"03-delete.yaml": deleteFileTemplate,
"03-assert.yaml": assertDeletedFileTemplate,
}

// Render renders the specified list of resources as a test case
Expand Down
14 changes: 7 additions & 7 deletions internal/templates/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} annotate managed --all upjet.upbound.io/test=true --overwrite
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s
- script: ${KUBECTL} get s3.aws.upbound.io/example-bucket -o yaml && ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s
`,
"01-delete.yaml": `apiVersion: kuttl.dev/v1beta1
kind: TestStep
Expand All @@ -101,7 +101,7 @@ commands:
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=delete --timeout 10s
- script: ${KUBECTL} get s3.aws.upbound.io/example-bucket -o yaml && ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=delete --timeout 10s
- command: ${KUBECTL} wait managed --all --for=delete --timeout 10s
`,
},
Expand Down Expand Up @@ -153,9 +153,9 @@ timeout: 10
commands:
- command: ${KUBECTL} annotate managed --all upjet.upbound.io/test=true --overwrite
- command: /tmp/bucket/pre-assert.sh
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s
- 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
- script: ${KUBECTL} get s3.aws.upbound.io/example-bucket -o yaml && ${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: /tmp/claim/post-assert.sh
`,
"01-delete.yaml": `apiVersion: kuttl.dev/v1beta1
Expand All @@ -170,8 +170,8 @@ commands:
kind: TestAssert
timeout: 10
commands:
- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=delete --timeout 10s
- command: ${KUBECTL} wait cluster.gcp.platformref.upbound.io/test-cluster-claim --for=delete --timeout 10s --namespace upbound-system
- script: ${KUBECTL} get s3.aws.upbound.io/example-bucket -o yaml && ${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 && ${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
`,
Expand Down
22 changes: 22 additions & 0 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) {
YAML: m.YAML,
Timeout: t.options.DefaultTimeout,
Conditions: t.options.DefaultConditions,

// As default, used the tags field
UpdateField: "tags",
UpdateValue: `{"uptest": "update"}`,
UpdateAssertField: "tags.uptest",
UpdateAssertValue: "update",
}

var err error
Expand Down Expand Up @@ -125,6 +131,22 @@ func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) {
}
}

if v, ok := annotations[config.AnnotationKeyUpdateField]; ok {
example.UpdateField = v
}

if v, ok := annotations[config.AnnotationKeyUpdateValue]; ok {
example.UpdateValue = v
}

if v, ok := annotations[config.AnnotationKeyUpdateAssertField]; ok {
example.UpdateAssertField = v
}

if v, ok := annotations[config.AnnotationKeyUpdateAssertValue]; ok {
example.UpdateAssertValue = v
}

examples = append(examples, example)
}

Expand Down
Loading