1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-08 18:15:48 +00:00

chore: add cli unit tests (#8326)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-09-11 00:03:24 +02:00 committed by GitHub
parent 74fed89a17
commit 173bb907b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 139 additions and 13 deletions

View file

@ -2,9 +2,12 @@ package test
import (
"errors"
"os"
"reflect"
"testing"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
testapi "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/test"
)
@ -153,3 +156,118 @@ func TestLoadTests(t *testing.T) {
})
}
}
func TestLoadTest(t *testing.T) {
mustReadFile := func(path string) []byte {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return data
}
tests := []struct {
name string
fs billy.Filesystem
path string
want TestCase
wantErr bool
}{{
name: "empty",
path: "",
wantErr: true,
}, {
name: "ok",
path: "../_testdata/tests/test-1/kyverno-test.yaml",
want: TestCase{
Path: "../_testdata/tests/test-1/kyverno-test.yaml",
Test: &testapi.Test{
Name: "test-registry",
Policies: []string{"image-example.yaml"},
Resources: []string{"resources.yaml"},
Results: []testapi.TestResults{{
Kind: "Pod",
Policy: "images",
Resources: []string{"test-pod-with-non-root-user-image"},
Result: policyreportv1alpha2.StatusPass,
Rule: "only-allow-trusted-images",
}, {
Kind: "Pod",
Policy: "images",
Resources: []string{"test-pod-with-trusted-registry"},
Result: policyreportv1alpha2.StatusPass,
Rule: "only-allow-trusted-images",
}},
},
},
}, {
name: "ok (billy)",
path: "kyverno-test.yaml",
want: TestCase{
Path: "kyverno-test.yaml",
Test: &testapi.Test{
Name: "test-registry",
Policies: []string{"image-example.yaml"},
Resources: []string{"resources.yaml"},
Results: []testapi.TestResults{{
Kind: "Pod",
Policy: "images",
Resources: []string{"test-pod-with-non-root-user-image"},
Result: policyreportv1alpha2.StatusPass,
Rule: "only-allow-trusted-images",
}, {
Kind: "Pod",
Policy: "images",
Resources: []string{"test-pod-with-trusted-registry"},
Result: policyreportv1alpha2.StatusPass,
Rule: "only-allow-trusted-images",
}},
},
},
fs: func() billy.Filesystem {
f := memfs.New()
file, err := f.Create("kyverno-test.yaml")
if err != nil {
t.Fatal(err)
}
defer file.Close()
if _, err := file.Write(mustReadFile("../_testdata/tests/test-1/kyverno-test.yaml")); err != nil {
t.Fatal(err)
}
return f
}(),
}, {
name: "bad file (billy)",
path: "kyverno-test-bad.yaml",
fs: func() billy.Filesystem {
f := memfs.New()
file, err := f.Create("kyverno-test.yaml")
if err != nil {
t.Fatal(err)
}
defer file.Close()
if _, err := file.Write(mustReadFile("../_testdata/tests/test-1/kyverno-test.yaml")); err != nil {
t.Fatal(err)
}
return f
}(),
want: TestCase{
Path: "kyverno-test-bad.yaml",
},
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := LoadTest(tt.fs, tt.path)
if (got.Err != nil) != tt.wantErr {
t.Errorf("LoadTest() error = %v, wantErr %v", got.Err, tt.wantErr)
return
}
got.Err = nil
tt.want.Fs = tt.fs
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("LoadTest() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -15,5 +15,5 @@ type TestCase struct {
}
func (tc TestCase) Dir() string {
return filepath.Dir(tc.Path)
return filepath.Clean(filepath.Dir(tc.Path))
}

View file

@ -9,25 +9,33 @@ import (
func TestTestCase_Dir(t *testing.T) {
type fields struct {
}
tests := []struct {
name string
Path string
Fs billy.Filesystem
Test *testapi.Test
Err error
}
tests := []struct {
name string
fields fields
want string
}{
// TODO: Add test cases.
}
want string
}{{
name: "empty",
want: ".",
}, {
name: "relative",
Path: "foo/bar/baz.yaml",
want: "foo/bar",
}, {
name: "absolute",
Path: "/foo/bar/baz.yaml",
want: "/foo/bar",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := TestCase{
Path: tt.fields.Path,
Fs: tt.fields.Fs,
Test: tt.fields.Test,
Err: tt.fields.Err,
Path: tt.Path,
Fs: tt.Fs,
Test: tt.Test,
Err: tt.Err,
}
if got := tc.Dir(); got != tt.want {
t.Errorf("TestCase.Dir() = %v, want %v", got, tt.want)