mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-20 18:37:01 +00:00
Add / update a few tests
This commit is contained in:
parent
c959d79ae4
commit
f7a9c86717
2 changed files with 103 additions and 4 deletions
89
pkg/resource/files_test.go
Normal file
89
pkg/resource/files_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
package resource_test
|
package resource_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/yannh/kubeconform/pkg/resource"
|
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
|
|
||||||
|
"github.com/yannh/kubeconform/pkg/resource"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSignatureFromBytes(t *testing.T) {
|
func TestSignatureFromBytes(t *testing.T) {
|
||||||
|
|
@ -54,13 +55,20 @@ spec:
|
||||||
func TestSignatureFromMap(t *testing.T) {
|
func TestSignatureFromMap(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
b string
|
b string
|
||||||
|
s resource.Signature
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"apiVersion: v1\nkind: ReplicationController\nmetadata:\n name: \"bob\"\nspec:\n replicas: 2\n",
|
"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{
|
res := resource.Resource{
|
||||||
Path: "foo",
|
Path: "foo",
|
||||||
Bytes: []byte(testCase.b),
|
Bytes: []byte(testCase.b),
|
||||||
|
|
@ -73,6 +81,8 @@ func TestSignatureFromMap(t *testing.T) {
|
||||||
|
|
||||||
res.SignatureFromMap(r)
|
res.SignatureFromMap(r)
|
||||||
sig, _ := res.Signature()
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue