Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Add tester sanitize helper
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Shepherd <darren@acorn.io>
  • Loading branch information
ibuildthecloud committed Aug 27, 2023
1 parent 70c3149 commit 50fb849
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions pkg/router/tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/acorn-io/baaah/pkg/yaml"
"github.com/hexops/autogold/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -199,19 +200,8 @@ func (b *Harness) Invoke(t *testing.T, input kclient.Object, handler router.Hand
assert.Equal(t, b.ExpectedDelay, resp.Delay)

if b.ExpectedGoldenPath != "" {
var yamls []string
for _, o := range resp.Collected {
gvk, err := apiutil.GVKForObject(o, b.Scheme)
if err != nil {
return nil, err
}
o.GetObjectKind().SetGroupVersionKind(gvk)
left, _ := yaml2.Marshal(o)

left = stripLastTransition(left)
yamls = append(yamls, string(left))
}
autogold.ExpectFile(t, strings.Join(yamls, "\n---\n"), autogold.Dir(b.ExpectedGoldenPath), autogold.Name("expected"))
yamls := b.SanitizedYAML(t, resp.Collected)
autogold.ExpectFile(t, yamls, autogold.Dir(b.ExpectedGoldenPath), autogold.Name("expected"))
}

if len(b.ExpectedOutput) == 0 {
Expand Down Expand Up @@ -246,6 +236,20 @@ func (b *Harness) Invoke(t *testing.T, input kclient.Object, handler router.Hand
return &resp, nil
}

func (b *Harness) SanitizedYAML(t *testing.T, objs []kclient.Object) string {

Check failure on line 239 in pkg/router/tester/tester.go

View workflow job for this annotation

GitHub Actions / test

test helper function should start from t.Helper() (thelper)
var yamls []string
for _, o := range objs {
gvk, err := apiutil.GVKForObject(o, b.Scheme)
require.NoError(t, err)
o.GetObjectKind().SetGroupVersionKind(gvk)
left, _ := yaml2.Marshal(o)

left = stripLastTransition(left)
yamls = append(yamls, string(left))
}
return strings.Join(yamls, "\n---\n")
}

func stripLastTransition(buf []byte) []byte {
result := &bytes.Buffer{}
s := bufio.NewScanner(bytes.NewReader(buf))
Expand Down

0 comments on commit 50fb849

Please sign in to comment.