From 72a5948d3283ff9dce682aafc53225ec1f0f6011 Mon Sep 17 00:00:00 2001 From: Hasan Turken Date: Fri, 14 Oct 2022 12:30:10 +0300 Subject: [PATCH] Fix multi-resource parsing Signed-off-by: Hasan Turken --- internal/config/config.go | 10 ++++++- internal/prepare.go | 16 +++++++++-- internal/templates/00-apply.yaml.tmpl | 2 +- internal/templates/renderer_test.go | 10 ++++--- internal/tester.go | 41 +++++++++++++-------------- 5 files changed, 48 insertions(+), 31 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index b552fff..0a4106e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,5 +1,7 @@ package config +import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + const ( AnnotationKeyTimeout = "uptest.upbound.io/timeout" AnnotationKeyConditions = "uptest.upbound.io/conditions" @@ -18,6 +20,12 @@ type AutomatedTest struct { DefaultConditions []string } +type Manifest struct { + FilePath string + Object *unstructured.Unstructured + YAML string +} + type TestCase struct { Timeout int SetupScriptPath string @@ -28,7 +36,7 @@ type Resource struct { Name string Namespace string KindGroup string - Manifest string + YAML string Timeout int Conditions []string diff --git a/internal/prepare.go b/internal/prepare.go index 1dc65ac..5916791 100644 --- a/internal/prepare.go +++ b/internal/prepare.go @@ -16,6 +16,8 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" kyaml "k8s.io/apimachinery/pkg/util/yaml" + + "github.com/upbound/uptest/internal/config" ) var ( @@ -53,7 +55,7 @@ type Preparer struct { dataSourcePath string } -func (p *Preparer) PrepareManifests() (map[string]*unstructured.Unstructured, error) { +func (p *Preparer) PrepareManifests() ([]config.Manifest, error) { if err := os.MkdirAll(caseDirectory, os.ModePerm); err != nil { return nil, errors.Wrapf(err, "cannot create directory %s", caseDirectory) } @@ -63,7 +65,7 @@ func (p *Preparer) PrepareManifests() (map[string]*unstructured.Unstructured, er return nil, errors.Wrap(err, "cannot inject variables") } - manifests := make(map[string]*unstructured.Unstructured, len(injectedFiles)) + manifests := make([]config.Manifest, 0, len(injectedFiles)) for path, data := range injectedFiles { decoder := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(data), 1024) for { @@ -79,7 +81,15 @@ func (p *Preparer) PrepareManifests() (map[string]*unstructured.Unstructured, er fmt.Printf("Skipping %s with name %s since it requires the following manual intervention: %s\n", u.GroupVersionKind().String(), u.GetName(), v) continue } - manifests[path] = u + y, err := yaml.Marshal(u) + if err != nil { + return nil, errors.Wrapf(err, "cannot marshal manifest for \"%s/%s\"", u.GetObjectKind(), u.GetName()) + } + manifests = append(manifests, config.Manifest{ + FilePath: path, + Object: u, + YAML: string(y), + }) } } } diff --git a/internal/templates/00-apply.yaml.tmpl b/internal/templates/00-apply.yaml.tmpl index f0ee6c6..a6cc12c 100644 --- a/internal/templates/00-apply.yaml.tmpl +++ b/internal/templates/00-apply.yaml.tmpl @@ -6,5 +6,5 @@ commands: {{ end }} {{- range $resource := .Resources -}} --- -{{ $resource.Manifest }} +{{ $resource.YAML }} {{- end }} \ No newline at end of file diff --git a/internal/templates/renderer_test.go b/internal/templates/renderer_test.go index 612100e..e1b0b68 100644 --- a/internal/templates/renderer_test.go +++ b/internal/templates/renderer_test.go @@ -1,10 +1,12 @@ package templates import ( + "testing" + "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" + "github.com/upbound/uptest/internal/config" - "testing" ) const ( @@ -51,7 +53,7 @@ func TestRender(t *testing.T) { { Name: "example-bucket", KindGroup: "s3.aws.upbound.io", - Manifest: bucketManifest, + YAML: bucketManifest, Conditions: []string{"Test"}, }, }, @@ -90,14 +92,14 @@ commands: }, resources: []config.Resource{ { - Manifest: bucketManifest, + YAML: bucketManifest, Name: "example-bucket", KindGroup: "s3.aws.upbound.io", PreAssertScriptPath: "/tmp/bucket/pre-assert.sh", Conditions: []string{"Test"}, }, { - Manifest: claimManifest, + YAML: claimManifest, Name: "test-cluster-claim", KindGroup: "cluster.gcp.platformref.upbound.io", Namespace: "upbound-system", diff --git a/internal/tester.go b/internal/tester.go index ae517b1..273a3e0 100644 --- a/internal/tester.go +++ b/internal/tester.go @@ -3,31 +3,29 @@ package internal import ( "bufio" "fmt" - "github.com/upbound/uptest/internal/templates" "io/fs" "os" "os/exec" "path/filepath" - "sigs.k8s.io/yaml" "strconv" "strings" "github.com/crossplane/crossplane-runtime/pkg/errors" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "github.com/upbound/uptest/internal/config" + "github.com/upbound/uptest/internal/templates" ) -func NewTester(manifests map[string]*unstructured.Unstructured, opts *config.AutomatedTest) *Tester { +func NewTester(ms []config.Manifest, opts *config.AutomatedTest) *Tester { return &Tester{ options: opts, - manifests: manifests, + manifests: ms, } } type Tester struct { options *config.AutomatedTest - manifests map[string]*unstructured.Unstructured + manifests []config.Manifest } func (t *Tester) ExecuteTests() error { @@ -56,27 +54,26 @@ func (t *Tester) prepareConfig() (*config.TestCase, []config.Resource, error) { } examples := make([]config.Resource, 0, len(t.manifests)) - for fp, m := range t.manifests { - if m.GroupVersionKind().String() == "/v1, Kind=Secret" { + for _, m := range t.manifests { + obj := m.Object + if obj.GroupVersionKind().String() == "/v1, Kind=Secret" { continue } - kg := strings.ToLower(m.GroupVersionKind().Kind + "." + m.GroupVersionKind().Group) - d, err := yaml.Marshal(m) - if err != nil { - return nil, nil, errors.Wrapf(err, "cannot marshal manifest for \"%s/%s\"", kg, m.GetName()) - } + kg := strings.ToLower(obj.GroupVersionKind().Kind + "." + obj.GroupVersionKind().Group) example := config.Resource{ - Name: m.GetName(), - Namespace: m.GetNamespace(), + Name: obj.GetName(), + Namespace: obj.GetNamespace(), KindGroup: kg, - Manifest: string(d), + YAML: m.YAML, Timeout: t.options.DefaultTimeout, Conditions: t.options.DefaultConditions, } - if v, ok := m.GetAnnotations()[config.AnnotationKeyTimeout]; ok { + var err error + annotations := obj.GetAnnotations() + if v, ok := annotations[config.AnnotationKeyTimeout]; ok { example.Timeout, err = strconv.Atoi(v) if err != nil { return nil, nil, errors.Wrap(err, "timeout value is not valid") @@ -86,19 +83,19 @@ func (t *Tester) prepareConfig() (*config.TestCase, []config.Resource, error) { } } - if v, ok := m.GetAnnotations()[config.AnnotationKeyConditions]; ok { + if v, ok := annotations[config.AnnotationKeyConditions]; ok { example.Conditions = strings.Split(v, ",") } - if v, ok := m.GetAnnotations()[config.AnnotationKeyPreAssertHook]; ok { - example.PreAssertScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(fp), filepath.Clean(v))) + if v, ok := annotations[config.AnnotationKeyPreAssertHook]; ok { + example.PreAssertScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(m.FilePath), filepath.Clean(v))) if err != nil { return nil, nil, errors.Wrap(err, "cannot find absolute path for pre assert hook") } } - if v, ok := m.GetAnnotations()[config.AnnotationKeyPostAssertHook]; ok { - example.PostAssertScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(fp), filepath.Clean(v))) + if v, ok := annotations[config.AnnotationKeyPostAssertHook]; ok { + example.PostAssertScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(m.FilePath), filepath.Clean(v))) if err != nil { return nil, nil, errors.Wrap(err, "cannot find absolute path for post assert hook") }