Skip to content

Commit

Permalink
Add disable options for Update and Import steps
Browse files Browse the repository at this point in the history
Add defer statment to remove all test directory before completing the cases

Signed-off-by: Sergen Yalçın <[email protected]>
  • Loading branch information
sergenyalcin committed Dec 15, 2023
1 parent 0d3be02 commit 000c280
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
5 changes: 5 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 defines that 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 @@ -72,6 +75,8 @@ type TestCase struct {
Timeout int
SetupScriptPath string
TeardownScriptPath string
SkipUpdate bool
SkipImport bool
}

// Resource represents a Kubernetes object to be tested and asserted
Expand Down
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
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
6 changes: 6 additions & 0 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,16 @@ 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
}

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)) {
disableImport, ok := annotations[config.AnnotationKeyDisableImport]
if ok && disableImport == "true" {
tc.SkipImport = true
}
example.Root = true
}
}
Expand Down

0 comments on commit 000c280

Please sign in to comment.