1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

re-structure unit tests

This commit is contained in:
shivdudhani 2019-06-14 16:02:28 -07:00
parent a8d3dd79ee
commit 45d44cef88
2 changed files with 88 additions and 68 deletions

View file

@ -1,13 +1,11 @@
package client
import (
"fmt"
"testing"
policytypes "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
@ -21,33 +19,19 @@ import (
// - kubernetes client
// - objects to initialize the client
func newUnstructured(apiVersion, kind, namespace, name string) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": map[string]interface{}{
"namespace": namespace,
"name": name,
},
},
}
type fixture struct {
t *testing.T
regresources map[string]string
objects []runtime.Object
client *Client
}
func newUnstructuredWithSpec(apiVersion, kind, namespace, name string, spec map[string]interface{}) *unstructured.Unstructured {
u := newUnstructured(apiVersion, kind, namespace, name)
u.Object["spec"] = spec
return u
}
func TestClient(t *testing.T) {
scheme := runtime.NewScheme()
// init groupversion
func newFixture(t *testing.T) *fixture {
regresource := map[string]string{"group/version": "thekinds",
"group2/version": "thekinds",
"v1": "namespaces",
"apps/v1": "deployments"}
// init resources
objects := []runtime.Object{newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
newUnstructured("group2/version", "TheKind", "ns-foo", "name2-foo"),
newUnstructured("group/version", "TheKind", "ns-foo", "name-bar"),
@ -55,8 +39,8 @@ func TestClient(t *testing.T) {
newUnstructured("group2/version", "TheKind", "ns-foo", "name2-baz"),
newUnstructured("apps/v1", "Deployment", "kyverno", "kyverno-deployment"),
}
// Mock Client
scheme := runtime.NewScheme()
// Create mock client
client, err := NewMockClient(scheme, objects...)
if err != nil {
t.Fatal(err)
@ -64,77 +48,92 @@ func TestClient(t *testing.T) {
// set discovery Client
client.SetDiscovery(NewFakeDiscoveryClient(regresource))
f := fixture{
t: t,
regresources: regresource,
objects: objects,
client: client,
}
return &f
}
func TestCRUDResource(t *testing.T) {
f := newFixture(t)
// Get Resource
res, err := client.GetResource("thekinds", "ns-foo", "name-foo")
_, err := f.client.GetResource("thekinds", "ns-foo", "name-foo")
if err != nil {
t.Fatal(err)
t.Errorf("GetResource not working: %s", err)
}
fmt.Println(res)
// List Resources
list, err := client.ListResource("thekinds", "ns-foo")
_, err = f.client.ListResource("thekinds", "ns-foo")
if err != nil {
t.Fatal(err)
t.Errorf("ListResource not working: %s", err)
}
fmt.Println(len(list.Items))
// DeleteResouce
err = client.DeleteResouce("thekinds", "ns-foo", "name-bar")
err = f.client.DeleteResouce("thekinds", "ns-foo", "name-bar")
if err != nil {
t.Fatal(err)
t.Errorf("DeleteResouce not working: %s", err)
}
// CreateResource
res, err = client.CreateResource("thekinds", "ns-foo", newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"))
_, err = f.client.CreateResource("thekinds", "ns-foo", newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"))
if err != nil {
t.Fatal(err)
t.Errorf("CreateResource not working: %s", err)
}
// UpdateResource
res, err = client.UpdateResource("thekinds", "ns-foo", newUnstructuredWithSpec("group/version", "TheKind", "ns-foo", "name-foo1", map[string]interface{}{"foo": "bar"}))
_, err = f.client.UpdateResource("thekinds", "ns-foo", newUnstructuredWithSpec("group/version", "TheKind", "ns-foo", "name-foo1", map[string]interface{}{"foo": "bar"}))
if err != nil {
t.Fatal(err)
t.Errorf("UpdateResource not working: %s", err)
}
// UpdateStatusResource
res, err = client.UpdateStatusResource("thekinds", "ns-foo", newUnstructuredWithSpec("group/version", "TheKind", "ns-foo", "name-foo1", map[string]interface{}{"foo": "status"}))
_, err = f.client.UpdateStatusResource("thekinds", "ns-foo", newUnstructuredWithSpec("group/version", "TheKind", "ns-foo", "name-foo1", map[string]interface{}{"foo": "status"}))
if err != nil {
t.Fatal(err)
t.Errorf("UpdateStatusResource not working: %s", err)
}
}
iEvent, err := client.GetEventsInterface()
func TestEventInterface(t *testing.T) {
f := newFixture(t)
iEvent, err := f.client.GetEventsInterface()
if err != nil {
t.Fatal(err)
t.Errorf("GetEventsInterface not working: %s", err)
}
eventList, err := iEvent.List(meta.ListOptions{})
_, err = iEvent.List(meta.ListOptions{})
if err != nil {
t.Fatal(err)
t.Errorf("Testing Event interface not working: %s", err)
}
fmt.Println(eventList.Items)
iCSR, err := client.GetCSRInterface()
if err != nil {
t.Fatal(err)
}
csrList, err := iCSR.List(meta.ListOptions{})
if err != nil {
t.Fatal(err)
}
fmt.Println(csrList.Items)
}
func TestCSRInterface(t *testing.T) {
f := newFixture(t)
iCSR, err := f.client.GetCSRInterface()
if err != nil {
t.Errorf("GetCSRInterface not working: %s", err)
}
_, err = iCSR.List(meta.ListOptions{})
if err != nil {
t.Errorf("Testing CSR interface not working: %s", err)
}
}
func TestGenerateResource(t *testing.T) {
f := newFixture(t)
//GenerateResource -> copy From
// 1 create namespace
// 2 generate resource
// create namespace
ns, err := client.CreateResource("namespaces", "", newUnstructured("v1", "Namespace", "", "ns1"))
ns, err := f.client.CreateResource("namespaces", "", newUnstructured("v1", "Namespace", "", "ns1"))
if err != nil {
t.Fatal(err)
}
gen := policytypes.Generation{Kind: "TheKind",
Name: "gen-kind",
Clone: &policytypes.CloneFrom{Namespace: "ns-foo", Name: "name-foo"}}
err = client.GenerateResource(gen, ns.GetName())
err = f.client.GenerateResource(gen, ns.GetName())
if err != nil {
t.Fatal(err)
}
res, err = client.GetResource("thekinds", "ns1", "gen-kind")
_, err = f.client.GetResource("thekinds", "ns1", "gen-kind")
if err != nil {
t.Fatal(err)
}
@ -142,19 +141,20 @@ func TestClient(t *testing.T) {
gen = policytypes.Generation{Kind: "TheKind",
Name: "name2-baz-new",
Data: newUnstructured("group2/version", "TheKind", "ns1", "name2-baz-new")}
err = client.GenerateResource(gen, ns.GetName())
err = f.client.GenerateResource(gen, ns.GetName())
if err != nil {
t.Fatal(err)
}
res, err = client.GetResource("thekinds", "ns1", "name2-baz-new")
_, err = f.client.GetResource("thekinds", "ns1", "name2-baz-new")
if err != nil {
t.Fatal(err)
}
}
func TestKubePolicyDeployment(t *testing.T) {
f := newFixture(t)
_, err := f.client.GetKubePolicyDeployment()
if err != nil {
t.Fatal(err)
}
// Get Kube Policy Deployment
deploy, err := client.GetKubePolicyDeployment()
if err != nil {
t.Fatal(err)
}
fmt.Println(deploy.GetName())
}

View file

@ -4,6 +4,7 @@ import (
"strings"
"time"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic/fake"
@ -65,3 +66,22 @@ func (c *fakeDiscoveryClient) getGVRFromKind(kind string) schema.GroupVersionRes
resource := strings.ToLower(kind) + "s"
return c.getGVR(resource)
}
func newUnstructured(apiVersion, kind, namespace, name string) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": map[string]interface{}{
"namespace": namespace,
"name": name,
},
},
}
}
func newUnstructuredWithSpec(apiVersion, kind, namespace, name string, spec map[string]interface{}) *unstructured.Unstructured {
u := newUnstructured(apiVersion, kind, namespace, name)
u.Object["spec"] = spec
return u
}