Skip to content

Commit

Permalink
Merge pull request #173 from mbbush/matt/selective-disable-import
Browse files Browse the repository at this point in the history
Skip import id-check step on non-root resources
  • Loading branch information
sergenyalcin committed Jan 10, 2024
2 parents 90b2669 + 3b51371 commit bde6d12
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 7 deletions.
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const (
// 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"
// AnnotationKeyDisableImport determines whether the Import
// step of the resource to be tested will be executed or not.
AnnotationKeyDisableImport = "uptest.upbound.io/disable-import"
)

// AutomatedTest represents an automated test of resource example
Expand Down Expand Up @@ -74,6 +77,8 @@ type TestCase struct {
Timeout int
SetupScriptPath string
TeardownScriptPath string
SkipUpdate bool
SkipImport bool

OnlyCleanUptestResources bool
}
Expand All @@ -97,5 +102,7 @@ type Resource struct {
UpdateAssertKey string
UpdateAssertValue string

SkipImport bool

Root bool
}
9 changes: 9 additions & 0 deletions internal/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@
package internal

import (
"fmt"
"os"

"github.com/crossplane/crossplane-runtime/pkg/errors"

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

// RunTest runs the specified automated test
func RunTest(o *config.AutomatedTest) error {
defer func() {
if err := os.RemoveAll(o.Directory); err != nil {
fmt.Println(fmt.Sprint(err, "cannot clean the test directory"))
}
}()

// Read examples and inject data source values to manifests
manifests, err := newPreparer(o.ManifestPaths, withDataSource(o.DataSourcePath), withTestDirectory(o.Directory)).prepareManifests()
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions internal/templates/01-assert.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ timeout: {{ .TestCase.Timeout }}
commands:
- script: echo "Dump MR manifests for the update assertion step:"; ${KUBECTL} get managed -o yaml
{{- range $resource := .Resources }}
{{- if eq $resource.UpdateParameter "" -}}
{{continue}}
{{- end -}}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
{{- end -}}
Expand Down
3 changes: 0 additions & 3 deletions internal/templates/01-update.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
{{- range $resource := .Resources }}
{{- if eq $resource.UpdateParameter "" -}}
{{continue}}
{{- end -}}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
{{- end -}}
Expand Down
2 changes: 1 addition & 1 deletion internal/templates/02-assert.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ commands:
- command: ${KUBECTL} wait {{ $resource.KindGroup }}/{{ $resource.Name }} --for=condition={{ $condition }} --timeout 10s
{{- end }}
{{- end }}
{{- if not $resource.Namespace }}
{{- if not (or $resource.Namespace $resource.SkipImport) }}
- 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 }}
8 changes: 8 additions & 0 deletions internal/templates/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func Render(tc *config.TestCase, resources []config.Resource, skipDelete bool) (

res := make(map[string]string, len(fileTemplates))
for name, tmpl := range fileTemplates {
// Skip templates with names starting with "01-" if skipUpdate is true
if tc.SkipUpdate && strings.HasPrefix(name, "01-") {
continue
}
// Skip templates with names starting with "02-" if skipImport is true
if tc.SkipImport && strings.HasPrefix(name, "02-") {
continue
}
// Skip templates with names starting with "03-" if skipDelete is true
if skipDelete && strings.HasPrefix(name, "03-") {
continue
Expand Down
90 changes: 90 additions & 0 deletions internal/templates/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,96 @@ commands:
- 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
- command: ${KUBECTL} scale deployment crossplane -n ${CROSSPLANE_NAMESPACE} --replicas=1
- script: ${KUBECTL} -n ${CROSSPLANE_NAMESPACE} get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n ${CROSSPLANE_NAMESPACE} scale deploy --replicas=1
`,
},
},
},
"SkipImport": {
args: args{
tc: &config.TestCase{
Timeout: 10,
SetupScriptPath: "/tmp/setup.sh",
TeardownScriptPath: "/tmp/teardown.sh",
},
resources: []config.Resource{
{
YAML: bucketManifest,
Name: "example-bucket",
KindGroup: "s3.aws.upbound.io",
PreAssertScriptPath: "/tmp/bucket/pre-assert.sh",
PostDeleteScriptPath: "/tmp/bucket/post-delete.sh",
SkipImport: true,
Conditions: []string{"Test"},
},
{
YAML: claimManifest,
Name: "test-cluster-claim",
KindGroup: "cluster.gcp.platformref.upbound.io",
Namespace: "upbound-system",
PostAssertScriptPath: "/tmp/claim/post-assert.sh",
PreDeleteScriptPath: "/tmp/claim/pre-delete.sh",
Conditions: []string{"Ready", "Synced"},
},
{
YAML: secretManifest,
Name: "test-secret",
KindGroup: "secret.",
Namespace: "upbound-system",
},
},
},
want: want{
out: map[string]string{
"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": `# 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
- 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
- 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": `# This file belongs to the resource update step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
`,
"01-assert.yaml": `# This assert file belongs to the resource update step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- script: echo "Dump MR manifests for the update assertion step:"; ${KUBECTL} get managed -o yaml
`,
"02-assert.yaml": `# This assert file belongs to the resource import step.
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 10
commands:
- 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
`,
"02-import.yaml": `# This file belongs to the resource import step.
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: ${KUBECTL} scale deployment crossplane -n ${CROSSPLANE_NAMESPACE} --replicas=0
- script: ${KUBECTL} -n ${CROSSPLANE_NAMESPACE} get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n ${CROSSPLANE_NAMESPACE} 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
- command: ${KUBECTL} scale deployment crossplane -n ${CROSSPLANE_NAMESPACE} --replicas=1
- script: ${KUBECTL} -n ${CROSSPLANE_NAMESPACE} get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n ${CROSSPLANE_NAMESPACE} scale deploy --replicas=1
`,
},
},
Expand Down
10 changes: 10 additions & 0 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,21 @@ func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) {
return nil, nil, errors.Wrapf(err, "cannot unmarshal JSON object: %s", updateParameter)
}
example.UpdateAssertKey, example.UpdateAssertValue = convertToJSONPath(data, "")
} else {
tc.SkipUpdate = true
}
disableImport, ok := annotations[config.AnnotationKeyDisableImport]
if ok && disableImport == "true" {
example.SkipImport = true
}

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)) {
if disableImport == "true" {
tc.SkipImport = true
}
example.Root = true

}
}

Expand Down

0 comments on commit bde6d12

Please sign in to comment.