2019-05-15 07:30:22 -07:00
|
|
|
package client
|
|
|
|
|
2019-06-11 14:35:26 -07:00
|
|
|
import (
|
2020-11-09 11:26:12 -08:00
|
|
|
"context"
|
2019-06-11 14:35:26 -07:00
|
|
|
"testing"
|
|
|
|
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2019-06-11 14:35:26 -07:00
|
|
|
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
|
|
|
|
2021-02-07 20:26:56 -08:00
|
|
|
gvrToListKind := map[schema.GroupVersionResource]string{
|
|
|
|
{Group: "group", Version: "version", Resource: "thekinds"}: "TheKindList",
|
|
|
|
{Group: "group2", Version: "version", Resource: "thekinds"}: "TheKindList",
|
|
|
|
{Group: "", Version: "v1", Resource: "namespaces"}: "NamespaceList",
|
|
|
|
{Group: "apps", Version: "v1", Resource: "deployments"}: "DeploymentList",
|
|
|
|
}
|
|
|
|
|
|
|
|
objects := []runtime.Object{
|
|
|
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
2019-06-11 14:35:26 -07:00
|
|
|
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"),
|
2020-11-26 16:07:06 -08:00
|
|
|
newUnstructured("apps/v1", "Deployment", config.KyvernoNamespace, config.KyvernoDeploymentName),
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
2021-02-07 20:26:56 -08:00
|
|
|
|
2019-06-14 16:02:28 -07:00
|
|
|
scheme := runtime.NewScheme()
|
|
|
|
// Create mock client
|
2021-02-07 20:26:56 -08:00
|
|
|
client, err := NewMockClient(scheme, gvrToListKind, objects...)
|
2019-06-11 14:35:26 -07:00
|
|
|
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
|
2020-08-07 09:47:33 +05:30
|
|
|
_, 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
|
2020-08-07 09:47:33 +05:30
|
|
|
_, 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
|
2020-08-07 09:47:33 +05:30
|
|
|
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
|
2020-08-07 09:47:33 +05:30
|
|
|
_, 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
|
2020-08-07 09:47:33 +05:30
|
|
|
_, 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
|
2020-08-07 09:47:33 +05:30
|
|
|
_, 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
|
|
|
}
|
2020-11-09 11:26:12 -08:00
|
|
|
_, err = iEvent.List(context.TODO(), 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
|
|
|
}
|
2020-11-09 11:26:12 -08:00
|
|
|
_, err = iCSR.List(context.TODO(), 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
|
|
|
}
|