Skip to content

Commit

Permalink
Fix multi-resource parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Turken <[email protected]>
  • Loading branch information
turkenh committed Oct 14, 2022
1 parent 65442e1 commit 72a5948
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
10 changes: 9 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -28,7 +36,7 @@ type Resource struct {
Name string
Namespace string
KindGroup string
Manifest string
YAML string

Timeout int
Conditions []string
Expand Down
16 changes: 13 additions & 3 deletions internal/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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),
})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/templates/00-apply.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ commands:
{{ end }}
{{- range $resource := .Resources -}}
---
{{ $resource.Manifest }}
{{ $resource.YAML }}
{{- end }}
10 changes: 6 additions & 4 deletions internal/templates/renderer_test.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -51,7 +53,7 @@ func TestRender(t *testing.T) {
{
Name: "example-bucket",
KindGroup: "s3.aws.upbound.io",
Manifest: bucketManifest,
YAML: bucketManifest,
Conditions: []string{"Test"},
},
},
Expand Down Expand Up @@ -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",
Expand Down
41 changes: 19 additions & 22 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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")
}
Expand Down

0 comments on commit 72a5948

Please sign in to comment.