Skip to content

Commit

Permalink
chore: add cli unit tests (kyverno#8326)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Sep 10, 2023
1 parent 74fed89 commit 173bb90
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 13 deletions.
118 changes: 118 additions & 0 deletions cmd/cli/kubectl-kyverno/test/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/cli/kubectl-kyverno/test/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ type TestCase struct {
}

func (tc TestCase) Dir() string {
return filepath.Dir(tc.Path)
return filepath.Clean(filepath.Dir(tc.Path))
}
32 changes: 20 additions & 12 deletions cmd/cli/kubectl-kyverno/test/test_case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 173bb90

Please sign in to comment.