2019-05-15 07:30:22 -07:00
|
|
|
package client
|
|
|
|
|
2019-06-11 14:35:26 -07:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2019-06-14 14:51:06 -07:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2019-06-11 14:35:26 -07:00
|
|
|
)
|
|
|
|
|
2019-05-15 07:30:22 -07:00
|
|
|
// GetResource
|
|
|
|
// ListResource
|
|
|
|
// CreateResource
|
|
|
|
// getGroupVersionMapper (valid and invalid resources)
|
2019-06-11 14:35:26 -07:00
|
|
|
|
|
|
|
//NewMockClient creates a mock client
|
|
|
|
// - dynamic client
|
|
|
|
// - kubernetes client
|
|
|
|
// - objects to initialize the client
|
|
|
|
|
2019-06-14 16:02:28 -07:00
|
|
|
type fixture struct {
|
2019-06-17 10:31:51 -07:00
|
|
|
t *testing.T
|
|
|
|
objects []runtime.Object
|
|
|
|
client *Client
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
|
2019-06-14 16:02:28 -07:00
|
|
|
func newFixture(t *testing.T) *fixture {
|
2019-06-11 14:35:26 -07:00
|
|
|
// init groupversion
|
2019-06-14 14:51:06 -07:00
|
|
|
regResource := []schema.GroupVersionResource{
|
2020-02-03 13:38:24 -08:00
|
|
|
{Group: "group", Version: "version", Resource: "thekinds"},
|
|
|
|
{Group: "group2", Version: "version", Resource: "thekinds"},
|
|
|
|
{Group: "", Version: "v1", Resource: "namespaces"},
|
|
|
|
{Group: "apps", Version: "v1", Resource: "deployments"},
|
2019-06-14 14:51:06 -07:00
|
|
|
}
|
2019-06-19 15:04:34 -07:00
|
|
|
|
2019-06-11 14:35:26 -07:00
|
|
|
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"),
|
|
|
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-baz"),
|
|
|
|
newUnstructured("group2/version", "TheKind", "ns-foo", "name2-baz"),
|
2019-06-27 13:38:51 -07:00
|
|
|
newUnstructured("apps/v1", "Deployment", "kyverno", "kyverno"),
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
2019-06-14 16:02:28 -07:00
|
|
|
scheme := runtime.NewScheme()
|
|
|
|
// Create mock client
|
2019-06-11 14:35:26 -07:00
|
|
|
client, err := NewMockClient(scheme, objects...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set discovery Client
|
2019-06-14 14:51:06 -07:00
|
|
|
client.SetDiscovery(NewFakeDiscoveryClient(regResource))
|
2019-06-14 16:02:28 -07:00
|
|
|
|
|
|
|
f := fixture{
|
2019-06-17 10:31:51 -07:00
|
|
|
t: t,
|
|
|
|
objects: objects,
|
|
|
|
client: client,
|
2019-06-14 16:02:28 -07:00
|
|
|
}
|
|
|
|
return &f
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCRUDResource(t *testing.T) {
|
|
|
|
f := newFixture(t)
|
2019-06-11 14:35:26 -07:00
|
|
|
// Get Resource
|
2019-07-08 15:34:21 -07:00
|
|
|
_, err := f.client.GetResource("thekind", "ns-foo", "name-foo")
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("GetResource not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
// List Resources
|
2019-07-08 15:34:21 -07:00
|
|
|
_, err = f.client.ListResource("thekind", "ns-foo", nil)
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("ListResource not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
// DeleteResouce
|
2019-11-19 10:13:03 -08:00
|
|
|
err = f.client.DeleteResource("thekind", "ns-foo", "name-bar", false)
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("DeleteResouce not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
// CreateResource
|
2019-07-08 15:34:21 -07:00
|
|
|
_, err = f.client.CreateResource("thekind", "ns-foo", newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"), false)
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("CreateResource not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
// UpdateResource
|
2019-07-08 15:34:21 -07:00
|
|
|
_, err = f.client.UpdateResource("thekind", "ns-foo", newUnstructuredWithSpec("group/version", "TheKind", "ns-foo", "name-foo1", map[string]interface{}{"foo": "bar"}), false)
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("UpdateResource not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
// UpdateStatusResource
|
2019-07-08 15:34:21 -07:00
|
|
|
_, err = f.client.UpdateStatusResource("thekind", "ns-foo", newUnstructuredWithSpec("group/version", "TheKind", "ns-foo", "name-foo1", map[string]interface{}{"foo": "status"}), false)
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("UpdateStatusResource not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
2019-06-14 16:02:28 -07:00
|
|
|
}
|
2019-06-11 14:35:26 -07:00
|
|
|
|
2019-06-14 16:02:28 -07:00
|
|
|
func TestEventInterface(t *testing.T) {
|
|
|
|
f := newFixture(t)
|
|
|
|
iEvent, err := f.client.GetEventsInterface()
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("GetEventsInterface not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
2019-06-14 16:02:28 -07:00
|
|
|
_, err = iEvent.List(meta.ListOptions{})
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("Testing Event interface not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
2019-06-14 16:02:28 -07:00
|
|
|
}
|
|
|
|
func TestCSRInterface(t *testing.T) {
|
|
|
|
f := newFixture(t)
|
|
|
|
iCSR, err := f.client.GetCSRInterface()
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("GetCSRInterface not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
2019-06-14 16:02:28 -07:00
|
|
|
_, err = iCSR.List(meta.ListOptions{})
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
2019-06-14 16:02:28 -07:00
|
|
|
t.Errorf("Testing CSR interface not working: %s", err)
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
2019-06-14 16:02:28 -07:00
|
|
|
}
|
2019-06-11 14:35:26 -07:00
|
|
|
|
2019-06-14 16:02:28 -07:00
|
|
|
func TestKubePolicyDeployment(t *testing.T) {
|
|
|
|
f := newFixture(t)
|
|
|
|
_, err := f.client.GetKubePolicyDeployment()
|
2019-06-11 14:35:26 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|