1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

chore: improve test coverage of source cli utils package (#8232)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-09-04 10:31:59 +02:00 committed by GitHub
parent 05d320fb6c
commit 6b7c204f05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 151 additions and 12 deletions

View file

@ -4,8 +4,17 @@ import (
"os"
)
var defaultStater = (*os.File).Stat
func IsStdin() bool {
fileInfo, err := os.Stdin.Stat()
return isStdin(defaultStater)
}
func isStdin(stater func(*os.File) (os.FileInfo, error)) bool {
if stater == nil {
stater = defaultStater
}
fileInfo, err := stater(os.Stdin)
if err != nil {
return false
}

View file

@ -1,6 +1,10 @@
package source
import "testing"
import (
"errors"
"os"
"testing"
)
func TestIsStdin(t *testing.T) {
tests := []struct {
@ -18,3 +22,32 @@ func TestIsStdin(t *testing.T) {
})
}
}
func Test_isStdin(t *testing.T) {
tests := []struct {
name string
stater func(*os.File) (os.FileInfo, error)
want bool
}{{
name: "nil stater",
stater: nil,
want: false,
}, {
name: "default stater",
stater: defaultStater,
want: false,
}, {
name: "error stater",
stater: func(_ *os.File) (os.FileInfo, error) {
return nil, errors.New("test")
},
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isStdin(tt.stater); got != tt.want {
t.Errorf("isStdin() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -7,6 +7,16 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
type (
marshaler = func(*unstructured.Unstructured) ([]byte, error)
patcher = func(originalJSON, modifiedJSON []byte) ([]byte, error)
)
var (
defaultMarshaler = (*unstructured.Unstructured).MarshalJSON
defaultPatcher = jsonpatch.CreateMergePatch
)
func TidyObject(obj interface{}) interface{} {
switch typedPatternElement := obj.(type) {
case map[string]interface{}:
@ -66,15 +76,25 @@ func Compare(a, e unstructured.Unstructured, tidy bool) (bool, error) {
a = Tidy(a)
e = Tidy(e)
}
actual, err := a.MarshalJSON()
return compare(a, e, defaultMarshaler, defaultPatcher)
}
func compare(a, e unstructured.Unstructured, marshaler marshaler, patcher patcher) (bool, error) {
if marshaler == nil {
marshaler = defaultMarshaler
}
actual, err := marshaler(&a)
if err != nil {
return false, err
}
expected, err := e.MarshalJSON()
expected, err := marshaler(&e)
if err != nil {
return false, err
}
patch, err := jsonpatch.CreateMergePatch(actual, expected)
if patcher == nil {
patcher = defaultPatcher
}
patch, err := patcher(actual, expected)
if err != nil {
return false, err
}

View file

@ -1,6 +1,7 @@
package unstructured
import (
"errors"
"reflect"
"testing"
@ -98,19 +99,19 @@ func TestCompare(t *testing.T) {
tests := []struct {
name string
a unstructured.Unstructured
b unstructured.Unstructured
e unstructured.Unstructured
tidy bool
want bool
wantErr bool
}{{
a: unstructured.Unstructured{},
b: unstructured.Unstructured{},
e: unstructured.Unstructured{},
tidy: true,
want: true,
wantErr: false,
}, {
a: unstructured.Unstructured{},
b: unstructured.Unstructured{},
e: unstructured.Unstructured{},
tidy: false,
want: true,
wantErr: false,
@ -122,7 +123,7 @@ func TestCompare(t *testing.T) {
},
},
},
b: unstructured.Unstructured{
e: unstructured.Unstructured{
Object: map[string]interface{}{
"map": map[string]interface{}{
"foo": "bar",
@ -141,7 +142,7 @@ func TestCompare(t *testing.T) {
},
},
},
b: unstructured.Unstructured{
e: unstructured.Unstructured{
Object: map[string]interface{}{
"map": map[string]interface{}{
"foo": "bar",
@ -160,7 +161,7 @@ func TestCompare(t *testing.T) {
},
},
},
b: unstructured.Unstructured{
e: unstructured.Unstructured{
Object: map[string]interface{}{
"map": map[string]interface{}{
"foo": "bar",
@ -173,7 +174,7 @@ func TestCompare(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Compare(tt.a, tt.b, tt.tidy)
got, err := Compare(tt.a, tt.e, tt.tidy)
if (err != nil) != tt.wantErr {
t.Errorf("Compare() error = %v, wantErr %v", err, tt.wantErr)
return
@ -261,3 +262,79 @@ func TestFixupGenerateLabels(t *testing.T) {
})
}
}
func Test_compare(t *testing.T) {
errorMarshaller := func(count int) marshaler {
var current = 0
return func(obj *unstructured.Unstructured) ([]byte, error) {
if current == count {
return nil, errors.New("test")
}
current++
return defaultMarshaler(obj)
}
}
tests := []struct {
name string
a unstructured.Unstructured
e unstructured.Unstructured
marshaler marshaler
patcher patcher
want bool
wantErr bool
}{{
name: "nil marshaler",
a: unstructured.Unstructured{},
e: unstructured.Unstructured{},
marshaler: nil,
patcher: defaultPatcher,
want: true,
wantErr: false,
}, {
name: "nil patcher",
a: unstructured.Unstructured{},
e: unstructured.Unstructured{},
marshaler: defaultMarshaler,
patcher: nil,
want: true,
wantErr: false,
}, {
name: "error patcher",
a: unstructured.Unstructured{},
e: unstructured.Unstructured{},
marshaler: defaultMarshaler,
patcher: func(originalJSON, modifiedJSON []byte) ([]byte, error) {
return nil, errors.New("test")
},
want: false,
wantErr: true,
}, {
name: "error marshaller",
a: unstructured.Unstructured{},
e: unstructured.Unstructured{},
marshaler: errorMarshaller(0),
patcher: defaultPatcher,
want: false,
wantErr: true,
}, {
name: "error marshaller",
a: unstructured.Unstructured{},
e: unstructured.Unstructured{},
marshaler: errorMarshaller(1),
patcher: defaultPatcher,
want: false,
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := compare(tt.a, tt.e, tt.marshaler, tt.patcher)
if (err != nil) != tt.wantErr {
t.Errorf("compare() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("compare() = %v, want %v", got, tt.want)
}
})
}
}