From 636832796e6a0f0f61dd5c3e89c69aa012b67d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Thu, 28 Sep 2023 11:23:05 +0300 Subject: [PATCH 1/7] Add yaml outputs of resources to during wait conditions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- internal/templates/00-assert.yaml.tmpl | 4 ++-- internal/templates/01-assert.yaml.tmpl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/templates/00-assert.yaml.tmpl b/internal/templates/00-assert.yaml.tmpl index f21d0fb..58193a0 100644 --- a/internal/templates/00-assert.yaml.tmpl +++ b/internal/templates/00-assert.yaml.tmpl @@ -12,9 +12,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 }} {{- else }} -- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s +- script: ${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o yaml && ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s {{- end }} {{- end }} {{- if $resource.PostAssertScriptPath }} diff --git a/internal/templates/01-assert.yaml.tmpl b/internal/templates/01-assert.yaml.tmpl index 0c95074..b352912 100644 --- a/internal/templates/01-assert.yaml.tmpl +++ b/internal/templates/01-assert.yaml.tmpl @@ -7,9 +7,9 @@ commands: {{continue}} {{- end -}} {{- if $resource.Namespace }} -- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=delete --timeout 10s --namespace {{ $resource.Namespace }} +- script: ${KUBECTL} get --namespace {{ $resource.Namespace }} {{ $resource.KindGroup }}/{{ $resource.Name }} -o yaml && ${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 +- script: ${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o yaml && ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=delete --timeout 10s {{- end }} {{- end }} - command: ${KUBECTL} wait managed --all --for=delete --timeout 10s From b00f8c8a9db776a0a42a2d768a9ba2760d5d7c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Thu, 28 Sep 2023 14:44:01 +0300 Subject: [PATCH 2/7] Fix unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- internal/templates/renderer_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/templates/renderer_test.go b/internal/templates/renderer_test.go index 15ed767..c857203 100644 --- a/internal/templates/renderer_test.go +++ b/internal/templates/renderer_test.go @@ -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 @@ -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 `, }, @@ -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 @@ -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 `, From 3b210de1b88585a1d1e804d5d18c4a2080d5b83c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Tue, 3 Oct 2023 12:07:38 +0300 Subject: [PATCH 3/7] Add update and import steps to uptest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- internal/config/config.go | 17 +++++++++++++ internal/templates/00-assert.yaml.tmpl | 3 ++- internal/templates/01-assert.yaml.tmpl | 11 +++------ internal/templates/01-update.yaml.tmpl | 11 +++++++++ internal/templates/02-assert.yaml.tmpl | 18 ++++++++++++++ internal/templates/02-import.yaml.tmpl | 13 ++++++++++ internal/templates/03-assert.yaml.tmpl | 19 +++++++++++++++ ...1-delete.yaml.tmpl => 03-delete.yaml.tmpl} | 0 internal/templates/embed.go | 24 +++++++++++++++++-- internal/templates/renderer.go | 8 +++++-- internal/tester.go | 22 +++++++++++++++++ 11 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 internal/templates/01-update.yaml.tmpl create mode 100644 internal/templates/02-assert.yaml.tmpl create mode 100644 internal/templates/02-import.yaml.tmpl create mode 100644 internal/templates/03-assert.yaml.tmpl rename internal/templates/{01-delete.yaml.tmpl => 03-delete.yaml.tmpl} (100%) diff --git a/internal/config/config.go b/internal/config/config.go index 57756bf..73e12da 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 @@ -80,4 +92,9 @@ type Resource struct { PostAssertScriptPath string PreDeleteScriptPath string PostDeleteScriptPath string + + UpdateField string + UpdateValue string + UpdateAssertField string + UpdateAssertValue string } diff --git a/internal/templates/00-assert.yaml.tmpl b/internal/templates/00-assert.yaml.tmpl index 58193a0..08ec7b4 100644 --- a/internal/templates/00-assert.yaml.tmpl +++ b/internal/templates/00-assert.yaml.tmpl @@ -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 {{- range $resource := .Resources }} {{- if eq $resource.KindGroup "secret." -}} {{continue}} @@ -14,7 +15,7 @@ commands: {{- 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 }} {{- else }} -- script: ${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o yaml && ${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 }} diff --git a/internal/templates/01-assert.yaml.tmpl b/internal/templates/01-assert.yaml.tmpl index b352912..bb474ff 100644 --- a/internal/templates/01-assert.yaml.tmpl +++ b/internal/templates/01-assert.yaml.tmpl @@ -2,17 +2,12 @@ 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 && ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=delete --timeout 10s --namespace {{ $resource.Namespace }} -{{- else }} -- script: ${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o yaml && ${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 }} diff --git a/internal/templates/01-update.yaml.tmpl b/internal/templates/01-update.yaml.tmpl new file mode 100644 index 0000000..1575e6f --- /dev/null +++ b/internal/templates/01-update.yaml.tmpl @@ -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 }} +- script: '${KUBECTL} patch {{ $resource.KindGroup }}/{{ $resource.Name }} --type=json -p "[{"op": "add", "path": "/spec/forProvider/{{ $resource.UpdateField }}", "value": {{ $resource.UpdateValue }}}]"' +{{- end }} +{{- end }} diff --git a/internal/templates/02-assert.yaml.tmpl b/internal/templates/02-assert.yaml.tmpl new file mode 100644 index 0000000..7b5d9b3 --- /dev/null +++ b/internal/templates/02-assert.yaml.tmpl @@ -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 }} diff --git a/internal/templates/02-import.yaml.tmpl b/internal/templates/02-import.yaml.tmpl new file mode 100644 index 0000000..d1d54af --- /dev/null +++ b/internal/templates/02-import.yaml.tmpl @@ -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 diff --git a/internal/templates/03-assert.yaml.tmpl b/internal/templates/03-assert.yaml.tmpl new file mode 100644 index 0000000..a72d26c --- /dev/null +++ b/internal/templates/03-assert.yaml.tmpl @@ -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 }} +{{- 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 }} diff --git a/internal/templates/01-delete.yaml.tmpl b/internal/templates/03-delete.yaml.tmpl similarity index 100% rename from internal/templates/01-delete.yaml.tmpl rename to internal/templates/03-delete.yaml.tmpl diff --git a/internal/templates/embed.go b/internal/templates/embed.go index 6996d40..ccc07a3 100644 --- a/internal/templates/embed.go +++ b/internal/templates/embed.go @@ -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 +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 diff --git a/internal/templates/renderer.go b/internal/templates/renderer.go index b71870d..37371bd 100644 --- a/internal/templates/renderer.go +++ b/internal/templates/renderer.go @@ -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 diff --git a/internal/tester.go b/internal/tester.go index 2833da6..104a3f2 100644 --- a/internal/tester.go +++ b/internal/tester.go @@ -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 @@ -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) } From cee19d814d7c4da46625fcce505342a690b154b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Wed, 4 Oct 2023 18:16:37 +0300 Subject: [PATCH 4/7] Fix unit tests and make optional the update step MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- .github/workflows/pr-comment-trigger.yml | 5 ++ internal/config/config.go | 20 ++---- internal/templates/00-assert.yaml.tmpl | 2 +- internal/templates/01-assert.yaml.tmpl | 5 +- internal/templates/01-update.yaml.tmpl | 5 +- internal/templates/02-assert.yaml.tmpl | 2 +- internal/templates/02-import.yaml.tmpl | 2 +- internal/templates/03-assert.yaml.tmpl | 2 +- internal/templates/renderer_test.go | 78 ++++++++++++++++++++---- internal/tester.go | 48 +++++++++------ 10 files changed, 117 insertions(+), 52 deletions(-) diff --git a/.github/workflows/pr-comment-trigger.yml b/.github/workflows/pr-comment-trigger.yml index b150438..7bf4466 100644 --- a/.github/workflows/pr-comment-trigger.yml +++ b/.github/workflows/pr-comment-trigger.yml @@ -17,6 +17,10 @@ on: default: 'provider' required: false type: string + update-test-parameter: + default: '' + required: false + type: string secrets: UPTEST_CLOUD_CREDENTIALS: description: 'Uptest cloud credentials to be passed to the uptest target as environment variable' @@ -158,6 +162,7 @@ jobs: UPTEST_EXAMPLE_LIST: ${{ needs.get-example-list.outputs.example_list }} UPTEST_TEST_DIR: ./_output/controlplane-dump UPTEST_DATASOURCE_PATH: .work/uptest-datasource.yaml + UPTEST_UPDATE_PARAMETER: ${{ inputs.update-test-parameter }} run: | mkdir -p .work && echo "${{ secrets.UPTEST_DATASOURCE }}" > .work/uptest-datasource.yaml make e2e diff --git a/internal/config/config.go b/internal/config/config.go index 73e12da..2101f1d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -35,18 +35,9 @@ 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" + // AnnotationKeyUpdateParameter defines the update parameter that will be + // used during the update step + AnnotationKeyUpdateParameter = "uptest.upbound.io/update-parameter" ) // AutomatedTest represents an automated test of resource example @@ -93,8 +84,7 @@ type Resource struct { PreDeleteScriptPath string PostDeleteScriptPath string - UpdateField string - UpdateValue string - UpdateAssertField string + UpdateParameter string + UpdateAssertKey string UpdateAssertValue string } diff --git a/internal/templates/00-assert.yaml.tmpl b/internal/templates/00-assert.yaml.tmpl index 08ec7b4..bede98e 100644 --- a/internal/templates/00-assert.yaml.tmpl +++ b/internal/templates/00-assert.yaml.tmpl @@ -15,7 +15,7 @@ commands: {{- 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 }} {{- else }} -- script: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s +- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s {{- end }} {{- end }} {{- if $resource.PostAssertScriptPath }} diff --git a/internal/templates/01-assert.yaml.tmpl b/internal/templates/01-assert.yaml.tmpl index bb474ff..a84d520 100644 --- a/internal/templates/01-assert.yaml.tmpl +++ b/internal/templates/01-assert.yaml.tmpl @@ -4,10 +4,13 @@ timeout: {{ .TestCase.Timeout }} commands: - command: ${KUBECTL} get managed -o yaml {{- range $resource := .Resources }} +{{- if eq $resource.UpdateParameter "" -}} + {{continue}} +{{- end -}} {{- if eq $resource.KindGroup "secret." -}} {{continue}} {{- end -}} {{- if not $resource.Namespace }} -- script: ${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.status.atProvider.{{ $resource.UpdateAssertField }}}' | grep -q "^{{ $resource.UpdateAssertValue }}$" +- script: ${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.status.atProvider{{ $resource.UpdateAssertKey }}}' | grep -q "^{{ $resource.UpdateAssertValue }}$" {{- end }} {{- end }} diff --git a/internal/templates/01-update.yaml.tmpl b/internal/templates/01-update.yaml.tmpl index 1575e6f..8c7cd3f 100644 --- a/internal/templates/01-update.yaml.tmpl +++ b/internal/templates/01-update.yaml.tmpl @@ -2,10 +2,13 @@ apiVersion: kuttl.dev/v1beta1 kind: TestStep commands: {{- range $resource := .Resources }} +{{- if eq $resource.UpdateParameter "" -}} + {{continue}} +{{- end -}} {{- if eq $resource.KindGroup "secret." -}} {{continue}} {{- end -}} {{- if not $resource.Namespace }} -- script: '${KUBECTL} patch {{ $resource.KindGroup }}/{{ $resource.Name }} --type=json -p "[{"op": "add", "path": "/spec/forProvider/{{ $resource.UpdateField }}", "value": {{ $resource.UpdateValue }}}]"' +- command: ${KUBECTL} patch {{ $resource.KindGroup }}/{{ $resource.Name }} --type=merge -p '{"spec":{"forProvider":{{ $resource.UpdateParameter }}}}' {{- end }} {{- end }} diff --git a/internal/templates/02-assert.yaml.tmpl b/internal/templates/02-assert.yaml.tmpl index 7b5d9b3..3224ea4 100644 --- a/internal/templates/02-assert.yaml.tmpl +++ b/internal/templates/02-assert.yaml.tmpl @@ -9,7 +9,7 @@ commands: {{- end -}} {{- range $condition := $resource.Conditions }} {{- if not $resource.Namespace }} -- script: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s +- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s {{- end }} {{- end }} {{- if not $resource.Namespace }} diff --git a/internal/templates/02-import.yaml.tmpl b/internal/templates/02-import.yaml.tmpl index d1d54af..99829e2 100644 --- a/internal/templates/02-import.yaml.tmpl +++ b/internal/templates/02-import.yaml.tmpl @@ -6,7 +6,7 @@ commands: {{continue}} {{- end -}} {{- if not $resource.Namespace }} -- script: ${KUBECTL} --subresource=status patch {{ $resource.KindGroup }}/{{ $resource.Name }} --type=merge -p '{"status":{"conditions":[]}}' +- command: ${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 }} diff --git a/internal/templates/03-assert.yaml.tmpl b/internal/templates/03-assert.yaml.tmpl index a72d26c..b6839c2 100644 --- a/internal/templates/03-assert.yaml.tmpl +++ b/internal/templates/03-assert.yaml.tmpl @@ -10,7 +10,7 @@ commands: {{- 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 }} {{- else }} -- script: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=delete --timeout 10s +- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=delete --timeout 10s {{- end }} {{- end }} - command: ${KUBECTL} wait managed --all --for=delete --timeout 10s diff --git a/internal/templates/renderer_test.go b/internal/templates/renderer_test.go index c857203..96d0b0a 100644 --- a/internal/templates/renderer_test.go +++ b/internal/templates/renderer_test.go @@ -90,19 +90,46 @@ kind: TestAssert timeout: 10 commands: - command: ${KUBECTL} annotate managed --all upjet.upbound.io/test=true --overwrite -- script: ${KUBECTL} get s3.aws.upbound.io/example-bucket -o yaml && ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s +- command: ${KUBECTL} get managed -o yaml +- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s `, - "01-delete.yaml": `apiVersion: kuttl.dev/v1beta1 + "01-update.yaml": `apiVersion: kuttl.dev/v1beta1 kind: TestStep commands: -- command: ${KUBECTL} delete s3.aws.upbound.io/example-bucket --wait=false --ignore-not-found `, "01-assert.yaml": `apiVersion: kuttl.dev/v1beta1 kind: TestAssert timeout: 10 commands: -- 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} get managed -o yaml +`, + "02-assert.yaml": `apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +timeout: 10 +commands: +- command: ${KUBECTL} get managed -o yaml +- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s +- script: annotation_value_1=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') && annotation_value_2=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.uptest-external-name}') && [ "$annotation_value_1" == "$annotation_value_2" ] +`, + "02-import.yaml": `apiVersion: kuttl.dev/v1beta1 +kind: TestStep +commands: +- 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-external-name=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') --overwrite +- script: ${KUBECTL} -n upbound-system get pods --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system delete pod +`, + "03-assert.yaml": `apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +timeout: 10 +commands: +- command: ${KUBECTL} get managed -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 +kind: TestStep +commands: +- command: ${KUBECTL} delete s3.aws.upbound.io/example-bucket --wait=false --ignore-not-found `, }, }, @@ -152,28 +179,55 @@ kind: TestAssert timeout: 10 commands: - command: ${KUBECTL} annotate managed --all upjet.upbound.io/test=true --overwrite +- command: ${KUBECTL} get managed -o yaml - command: /tmp/bucket/pre-assert.sh -- script: ${KUBECTL} get s3.aws.upbound.io/example-bucket -o yaml && ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s +- 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: /tmp/claim/post-assert.sh `, - "01-delete.yaml": `apiVersion: kuttl.dev/v1beta1 + "01-update.yaml": `apiVersion: kuttl.dev/v1beta1 kind: TestStep commands: -- 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 timeout: 10 commands: -- 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} get managed -o yaml +`, + "02-assert.yaml": `apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +timeout: 10 +commands: +- command: ${KUBECTL} get managed -o yaml +- command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s +- script: annotation_value_1=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') && annotation_value_2=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.uptest-external-name}') && [ "$annotation_value_1" == "$annotation_value_2" ] +`, + "02-import.yaml": `apiVersion: kuttl.dev/v1beta1 +kind: TestStep +commands: +- 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-external-name=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') --overwrite +- script: ${KUBECTL} -n upbound-system get pods --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n upbound-system delete pod +`, + "03-assert.yaml": `apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +timeout: 10 +commands: +- command: ${KUBECTL} get managed -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 - command: ${KUBECTL} wait managed --all --for=delete --timeout 10s - command: /tmp/teardown.sh +`, + "03-delete.yaml": `apiVersion: kuttl.dev/v1beta1 +kind: TestStep +commands: +- 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 `, }, }, diff --git a/internal/tester.go b/internal/tester.go index 104a3f2..2b45733 100644 --- a/internal/tester.go +++ b/internal/tester.go @@ -16,6 +16,7 @@ package internal import ( "bufio" + "encoding/json" "fmt" "io/fs" "os" @@ -79,12 +80,15 @@ 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", + 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 @@ -131,20 +135,13 @@ 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 + if v, ok := annotations[config.AnnotationKeyUpdateParameter]; ok { + example.UpdateParameter = v + var data map[string]interface{} + if err := json.Unmarshal([]byte(v), &data); err != nil { + return nil, nil, errors.Wrap(err, "cannot unmarshal JSON") + } + example.UpdateAssertKey, example.UpdateAssertValue = convertToJSONPath(data, "") } examples = append(examples, example) @@ -172,3 +169,16 @@ func (t *tester) writeKuttlFiles() error { return nil } + +func convertToJSONPath(data map[string]interface{}, currentPath string) (string, string) { + for key, value := range data { + newPath := currentPath + "." + key + switch v := value.(type) { + case map[string]interface{}: + return convertToJSONPath(v, newPath) + default: + return newPath, fmt.Sprintf("%v", v) + } + } + return currentPath, "" +} From 21da2810edec3001e4b58a9bcdbd131435810a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Thu, 5 Oct 2023 13:06:12 +0300 Subject: [PATCH 5/7] Store id field instead of external name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- internal/templates/02-assert.yaml.tmpl | 2 +- internal/templates/02-import.yaml.tmpl | 2 +- internal/templates/renderer_test.go | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/templates/02-assert.yaml.tmpl b/internal/templates/02-assert.yaml.tmpl index 3224ea4..169aad4 100644 --- a/internal/templates/02-assert.yaml.tmpl +++ b/internal/templates/02-assert.yaml.tmpl @@ -13,6 +13,6 @@ commands: {{- 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" ] +- script: new_id=$(${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.status.atProvider.id}') && old_id=$(${KUBECTL} get {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.metadata.annotations.uptest-old-id}') && [ "$new_id" == "$old_id" ] {{- end }} {{- end }} diff --git a/internal/templates/02-import.yaml.tmpl b/internal/templates/02-import.yaml.tmpl index 99829e2..e93f55b 100644 --- a/internal/templates/02-import.yaml.tmpl +++ b/internal/templates/02-import.yaml.tmpl @@ -7,7 +7,7 @@ commands: {{- end -}} {{- if not $resource.Namespace }} - command: ${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 +- 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 diff --git a/internal/templates/renderer_test.go b/internal/templates/renderer_test.go index 96d0b0a..9c67578 100644 --- a/internal/templates/renderer_test.go +++ b/internal/templates/renderer_test.go @@ -109,13 +109,13 @@ timeout: 10 commands: - command: ${KUBECTL} get managed -o yaml - command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s -- script: annotation_value_1=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') && annotation_value_2=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.uptest-external-name}') && [ "$annotation_value_1" == "$annotation_value_2" ] +- 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 kind: TestStep commands: - 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-external-name=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') --overwrite +- 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 `, "03-assert.yaml": `apiVersion: kuttl.dev/v1beta1 @@ -202,13 +202,13 @@ timeout: 10 commands: - command: ${KUBECTL} get managed -o yaml - command: ${KUBECTL} wait s3.aws.upbound.io/example-bucket --for=condition=Test --timeout 10s -- script: annotation_value_1=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') && annotation_value_2=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.uptest-external-name}') && [ "$annotation_value_1" == "$annotation_value_2" ] +- 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 kind: TestStep commands: - 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-external-name=$(${KUBECTL} get s3.aws.upbound.io/example-bucket -o=jsonpath='{.metadata.annotations.crossplane\.io/external-name}') --overwrite +- 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 `, "03-assert.yaml": `apiVersion: kuttl.dev/v1beta1 From 740e2fc7da7fc75b8f2f3dd5a130d13d05cb832b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Thu, 5 Oct 2023 17:53:41 +0300 Subject: [PATCH 6/7] Address reviews MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- .github/workflows/pr-comment-trigger.yml | 6 ++ internal/config/config.go | 5 ++ internal/templates/00-apply.yaml.tmpl | 1 + internal/templates/00-assert.yaml.tmpl | 6 +- internal/templates/01-assert.yaml.tmpl | 5 +- internal/templates/01-update.yaml.tmpl | 3 + internal/templates/02-assert.yaml.tmpl | 3 +- internal/templates/02-import.yaml.tmpl | 6 +- internal/templates/03-assert.yaml.tmpl | 6 +- internal/templates/03-delete.yaml.tmpl | 1 + internal/templates/renderer_test.go | 84 ++++++++++++++++-------- internal/tester.go | 30 +++++---- 12 files changed, 106 insertions(+), 50 deletions(-) diff --git a/.github/workflows/pr-comment-trigger.yml b/.github/workflows/pr-comment-trigger.yml index 7bf4466..52376f3 100644 --- a/.github/workflows/pr-comment-trigger.yml +++ b/.github/workflows/pr-comment-trigger.yml @@ -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 diff --git a/internal/config/config.go b/internal/config/config.go index 2101f1d..c80f061 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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" ) // AutomatedTest represents an automated test of resource example @@ -87,4 +90,6 @@ type Resource struct { UpdateParameter string UpdateAssertKey string UpdateAssertValue string + + Root bool } diff --git a/internal/templates/00-apply.yaml.tmpl b/internal/templates/00-apply.yaml.tmpl index a6cc12c..2734913 100644 --- a/internal/templates/00-apply.yaml.tmpl +++ b/internal/templates/00-apply.yaml.tmpl @@ -1,3 +1,4 @@ +# This file belongs to the resource apply step. {{ if .TestCase.SetupScriptPath -}} apiVersion: kuttl.dev/v1beta1 kind: TestStep diff --git a/internal/templates/00-assert.yaml.tmpl b/internal/templates/00-assert.yaml.tmpl index bede98e..95b184a 100644 --- a/internal/templates/00-assert.yaml.tmpl +++ b/internal/templates/00-assert.yaml.tmpl @@ -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}} @@ -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 }} diff --git a/internal/templates/01-assert.yaml.tmpl b/internal/templates/01-assert.yaml.tmpl index a84d520..a4dc2c5 100644 --- a/internal/templates/01-assert.yaml.tmpl +++ b/internal/templates/01-assert.yaml.tmpl @@ -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}} @@ -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 }} diff --git a/internal/templates/01-update.yaml.tmpl b/internal/templates/01-update.yaml.tmpl index 8c7cd3f..77a6280 100644 --- a/internal/templates/01-update.yaml.tmpl +++ b/internal/templates/01-update.yaml.tmpl @@ -1,3 +1,4 @@ +# This file belongs to the resource update step. apiVersion: kuttl.dev/v1beta1 kind: TestStep commands: @@ -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 }} diff --git a/internal/templates/02-assert.yaml.tmpl b/internal/templates/02-assert.yaml.tmpl index 169aad4..d1c8deb 100644 --- a/internal/templates/02-assert.yaml.tmpl +++ b/internal/templates/02-assert.yaml.tmpl @@ -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}} diff --git a/internal/templates/02-import.yaml.tmpl b/internal/templates/02-import.yaml.tmpl index e93f55b..d392e60 100644 --- a/internal/templates/02-import.yaml.tmpl +++ b/internal/templates/02-import.yaml.tmpl @@ -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}} @@ -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 diff --git a/internal/templates/03-assert.yaml.tmpl b/internal/templates/03-assert.yaml.tmpl index b6839c2..97849fe 100644 --- a/internal/templates/03-assert.yaml.tmpl +++ b/internal/templates/03-assert.yaml.tmpl @@ -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 }} diff --git a/internal/templates/03-delete.yaml.tmpl b/internal/templates/03-delete.yaml.tmpl index 6a572f1..c291505 100644 --- a/internal/templates/03-delete.yaml.tmpl +++ b/internal/templates/03-delete.yaml.tmpl @@ -1,3 +1,4 @@ +# This file belongs to the resource delete step. apiVersion: kuttl.dev/v1beta1 kind: TestStep commands: diff --git a/internal/templates/renderer_test.go b/internal/templates/renderer_test.go index 9c67578..a44206c 100644 --- a/internal/templates/renderer_test.go +++ b/internal/templates/renderer_test.go @@ -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 @@ -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 diff --git a/internal/tester.go b/internal/tester.go index 2b45733..09551ef 100644 --- a/internal/tester.go +++ b/internal/tester.go @@ -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(), @@ -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 { @@ -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 { + 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) } From 61dd80934d0fe6efe123405efffa7baa5b32ada4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Thu, 5 Oct 2023 18:04:26 +0300 Subject: [PATCH 7/7] Fix linter issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- internal/config/config.go | 4 ++-- internal/tester.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index c80f061..05e9ede 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -38,9 +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 + // 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" + AnnotationKeyExampleID = "meta.upbound.io/example-id" ) // AutomatedTest represents an automated test of resource example diff --git a/internal/tester.go b/internal/tester.go index 09551ef..7d89525 100644 --- a/internal/tester.go +++ b/internal/tester.go @@ -140,8 +140,8 @@ func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) { example.UpdateAssertKey, example.UpdateAssertValue = convertToJSONPath(data, "") } - if exampleId, ok := annotations["meta.upbound.io/example-id"]; ok { - if exampleId == strings.ToLower(fmt.Sprintf("%s/%s/%s", strings.Split(groupVersionKind.Group, ".")[0], groupVersionKind.Version, groupVersionKind.Kind)) { + if exampleID, ok := annotations[config.AnnotationKeyExampleID]; ok { + if exampleID == strings.ToLower(fmt.Sprintf("%s/%s/%s", strings.Split(groupVersionKind.Group, ".")[0], groupVersionKind.Version, groupVersionKind.Kind)) { example.Root = true } }