Add / update a few tests

This commit is contained in:
Yann Hamon 2021-01-02 12:32:12 +01:00
parent c959d79ae4
commit f7a9c86717
2 changed files with 103 additions and 4 deletions

View file

@ -0,0 +1,89 @@
package resource
import (
"os"
"testing"
"time"
)
type MockFileInfo struct {
fileName string
}
func NewMockFileInfo(filename string) *MockFileInfo {
return &MockFileInfo{fileName: filename}
}
func (m *MockFileInfo) Name() string { return m.fileName }
func (m *MockFileInfo) Size() int64 { return 0 } // length in bytes for regular files; system-dependent for others
func (m *MockFileInfo) Mode() os.FileMode { return 0 } // file mode bits
func (m *MockFileInfo) ModTime() time.Time { return time.Time{} } // modification time
func (m *MockFileInfo) IsDir() bool { return false } // abbreviation for Mode().IsDir()
func (m *MockFileInfo) Sys() interface{} { return nil } // underlying data source (can return nil)
func TestIsYamlFile(t *testing.T) {
for i, testCase := range []struct {
filename string
expect bool
}{
{
"file.yaml",
true,
},
{
"/path/to/my/file.yaml",
true,
},
{
"file.yml",
true,
},
{
"/path/to/my/file.yml",
true,
},
{
"file.notyaml",
false,
},
{
"/path/to/my/file.notyaml",
false,
},
{
"/path/to/my/file",
false,
},
} {
if got := isYAMLFile(NewMockFileInfo(testCase.filename)); got != testCase.expect {
t.Errorf("test %d: for filename %s, expected %t, got %t", i+1, testCase.filename, testCase.expect, got)
}
}
}
func TestIsJSONFile(t *testing.T) {
for i, testCase := range []struct {
filename string
expect bool
}{
{
"file.json",
true,
},
{
"/path/to/my/file.json",
true,
},
{
"file.notjson",
false,
},
{
"/path/to/my/file",
false,
},
} {
if got := isJSONFile(NewMockFileInfo(testCase.filename)); got != testCase.expect {
t.Errorf("test %d: for filename %s, expected %t, got %t", i+1, testCase.filename, testCase.expect, got)
}
}
}

View file

@ -1,12 +1,13 @@
package resource_test
import (
"fmt"
"log"
"reflect"
"testing"
"github.com/yannh/kubeconform/pkg/resource"
"sigs.k8s.io/yaml"
"github.com/yannh/kubeconform/pkg/resource"
)
func TestSignatureFromBytes(t *testing.T) {
@ -54,13 +55,20 @@ spec:
func TestSignatureFromMap(t *testing.T) {
testCases := []struct {
b string
s resource.Signature
}{
{
"apiVersion: v1\nkind: ReplicationController\nmetadata:\n name: \"bob\"\nspec:\n replicas: 2\n",
resource.Signature{
Kind: "ReplicationController",
Version: "v1",
Namespace: "",
Name: "bob",
},
},
}
for _, testCase := range testCases {
for i, testCase := range testCases {
res := resource.Resource{
Path: "foo",
Bytes: []byte(testCase.b),
@ -73,6 +81,8 @@ func TestSignatureFromMap(t *testing.T) {
res.SignatureFromMap(r)
sig, _ := res.Signature()
fmt.Printf("%+v", sig)
if !reflect.DeepEqual(*sig, testCase.s) {
t.Errorf("test %d - for resource %s, expected %+v, got %+v", i+1, testCase.b, testCase.s, sig)
}
}
}