Skip to content

Commit

Permalink
chore: add unit tests (#1819)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Jul 31, 2024
1 parent 51207b9 commit d72f227
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
26 changes: 26 additions & 0 deletions pkg/utils/yaml/with-anchors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: &name yaml-anchors
spec:
namespace: &namespace default
steps:
- name: *name
try:
- apply:
resource: &resource
apiVersion: v1
kind: Pod
metadata:
name: *name
namespace: *namespace
spec:
containers:
- name: main
image: alpine
- assert:
resource:
<<: *resource
spec:
restartPolicy: Always
timeout: 1s
30 changes: 30 additions & 0 deletions pkg/utils/yaml/without-anchors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: yaml-anchors
spec:
namespace: default
steps:
- name: yaml-anchors
try:
- apply:
resource:
apiVersion: v1
kind: Pod
metadata:
name: yaml-anchors
namespace: default
spec:
containers:
- image: alpine
name: main
- assert:
resource:
apiVersion: v1
kind: Pod
metadata:
name: yaml-anchors
namespace: default
spec:
restartPolicy: Always
timeout: 1s
11 changes: 9 additions & 2 deletions pkg/utils/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import (
"gopkg.in/yaml.v3"
)

func Remarshal(document []byte) ([]byte, error) {
func remarshal(document []byte, unmarshal func(in []byte, out interface{}) (err error)) ([]byte, error) {
if unmarshal == nil {
unmarshal = yaml.Unmarshal
}
var pre map[string]any
err := yaml.Unmarshal(document, &pre)
err := unmarshal(document, &pre)
if err != nil {
return nil, err
}
return yaml.Marshal(pre)
}

func Remarshal(document []byte) ([]byte, error) {
return remarshal(document, nil)
}
74 changes: 74 additions & 0 deletions pkg/utils/yaml/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package yaml

import (
"errors"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_remarshal(t *testing.T) {
in, err := os.ReadFile("with-anchors.yaml")
assert.NoError(t, err)
out, err := os.ReadFile("without-anchors.yaml")
assert.NoError(t, err)
tests := []struct {
name string
document string
unmarshal func(in []byte, out interface{}) (err error)
want string
wantErr bool
}{{
name: "ok",
document: string(in),
want: string(out),
wantErr: false,
}, {
name: "error",
unmarshal: func(in []byte, out interface{}) (err error) {
return errors.New("dummy")
},
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := remarshal([]byte(tt.document), tt.unmarshal)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, string(got))
})
}
}

func TestRemarshal(t *testing.T) {
in, err := os.ReadFile("with-anchors.yaml")
assert.NoError(t, err)
out, err := os.ReadFile("without-anchors.yaml")
assert.NoError(t, err)
tests := []struct {
name string
document string
want string
wantErr bool
}{{
name: "ok",
document: string(in),
want: string(out),
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Remarshal([]byte(tt.document))
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, string(got))
})
}
}

0 comments on commit d72f227

Please sign in to comment.