From 6d7a6e66f7aab6516b26a0397a299514972374f3 Mon Sep 17 00:00:00 2001 From: runzexia Date: Mon, 28 Sep 2020 23:19:57 +0800 Subject: [PATCH 1/5] enable source crd test Signed-off-by: runzexia --- pkg/reconciler/source/crd/crd_test.go | 57 +++++++++++++++------------ pkg/reconciler/testing/factory.go | 6 ++- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/pkg/reconciler/source/crd/crd_test.go b/pkg/reconciler/source/crd/crd_test.go index 36e818750d6..12f94ddb04d 100644 --- a/pkg/reconciler/source/crd/crd_test.go +++ b/pkg/reconciler/source/crd/crd_test.go @@ -18,6 +18,9 @@ package crd import ( "context" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/client-go/rest" + "knative.dev/pkg/injection" "testing" corev1 "k8s.io/api/core/v1" @@ -43,15 +46,17 @@ import ( const ( crdName = "test-crd" -// TODO: See comment below -// crdGroup = "testing.sources.knative.dev" -// crdKind = "TestSource" -// crdPlural = "testsources" -// crdVersionServed = "v1alpha1" + crdGroup = "testing.sources.knative.dev" + crdKind = "TestSource" + crdPlural = "testsources" + crdVersionServed = "v1alpha1" ) func TestAllCases(t *testing.T) { - table := TableTest{{ + ctx := context.Background() + ctx, _ = injection.Fake.SetupInformers(ctx, &rest.Config{}) + table := TableTest{ + { Name: "bad workqueue key", // Make sure Reconcile handles bad keys. Key: "too/many/parts", @@ -73,25 +78,27 @@ func TestAllCases(t *testing.T) { Eventf(corev1.EventTypeWarning, "InternalError", "unable to find GVR or GVK for %s", crdName), }, }, - //}, { - // TODO uncomment the following once we figure out why the eventtype informer is missing from the duck.controller. - //Name: "reconcile succeeded", - //Objects: []runtime.Object{ - // NewCustomResourceDefinition(crdName, - // WithCustomResourceDefinitionLabels(map[string]string{ - // sources.SourceDuckLabelKey: sources.SourceDuckLabelValue, - // }), - // WithCustomResourceDefinitionGroup(crdGroup), - // WithCustomResourceDefinitionNames(apiextensionsv1beta1.CustomResourceDefinitionNames{ - // Kind: crdKind, - // Plural: crdPlural, - // }), - // WithCustomResourceDefinitionVersions([]apiextensionsv1beta1.CustomResourceDefinitionVersion{{ - // Name: crdVersionServed, - // Served: true, - // }})), - //}, - //Key: crdName, + { + + Name: "reconcile succeeded", + Objects: []runtime.Object{ + NewCustomResourceDefinition(crdName, + WithCustomResourceDefinitionLabels(map[string]string{ + sources.SourceDuckLabelKey: sources.SourceDuckLabelValue, + }), + WithCustomResourceDefinitionGroup(crdGroup), + WithCustomResourceDefinitionNames(apiextensionsv1.CustomResourceDefinitionNames{ + Kind: crdKind, + Plural: crdPlural, + }), + WithCustomResourceDefinitionVersions([]apiextensionsv1.CustomResourceDefinitionVersion{{ + Name: crdVersionServed, + Served: true, + }})), + }, + Key: crdName, + Ctx: ctx, + }, } logger := logtesting.TestLogger(t) diff --git a/pkg/reconciler/testing/factory.go b/pkg/reconciler/testing/factory.go index 9a91362d035..d2e9f93b4c8 100644 --- a/pkg/reconciler/testing/factory.go +++ b/pkg/reconciler/testing/factory.go @@ -59,8 +59,10 @@ type Ctor func(context.Context, *Listers, configmap.Watcher) controller.Reconcil func MakeFactory(ctor Ctor, unstructured bool, logger *zap.SugaredLogger) Factory { return func(t *testing.T, r *TableRow) (controller.Reconciler, ActionRecorderList, EventList) { ls := NewListers(r.Objects) - - ctx := context.Background() + ctx := r.Ctx + if ctx == nil { + ctx = context.Background() + } ctx = logging.WithLogger(ctx, logger) ctx, kubeClient := fakekubeclient.With(ctx, ls.GetKubeObjects()...) From 46cd3ccf99ec4020d14a07627ca9401f7e2b0cb7 Mon Sep 17 00:00:00 2001 From: runzexia Date: Mon, 28 Sep 2020 23:29:05 +0800 Subject: [PATCH 2/5] goimports fmt Signed-off-by: runzexia --- pkg/reconciler/source/crd/crd_test.go | 43 ++++++++++++++------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/pkg/reconciler/source/crd/crd_test.go b/pkg/reconciler/source/crd/crd_test.go index 12f94ddb04d..2dc561ca8b5 100644 --- a/pkg/reconciler/source/crd/crd_test.go +++ b/pkg/reconciler/source/crd/crd_test.go @@ -18,10 +18,11 @@ package crd import ( "context" + "testing" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/client-go/rest" "knative.dev/pkg/injection" - "testing" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" @@ -57,27 +58,27 @@ func TestAllCases(t *testing.T) { ctx, _ = injection.Fake.SetupInformers(ctx, &rest.Config{}) table := TableTest{ { - Name: "bad workqueue key", - // Make sure Reconcile handles bad keys. - Key: "too/many/parts", - }, { - Name: "key not found", - // Make sure Reconcile handles good keys that don't exist. - Key: "not-found", - }, { - Name: "reconcile failed, cannot find GVR or GVK", - Objects: []runtime.Object{ - NewCustomResourceDefinition(crdName, - WithCustomResourceDefinitionLabels(map[string]string{ - sources.SourceDuckLabelKey: sources.SourceDuckLabelValue, - })), - }, - Key: crdName, - WantErr: true, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "InternalError", "unable to find GVR or GVK for %s", crdName), + Name: "bad workqueue key", + // Make sure Reconcile handles bad keys. + Key: "too/many/parts", + }, { + Name: "key not found", + // Make sure Reconcile handles good keys that don't exist. + Key: "not-found", + }, { + Name: "reconcile failed, cannot find GVR or GVK", + Objects: []runtime.Object{ + NewCustomResourceDefinition(crdName, + WithCustomResourceDefinitionLabels(map[string]string{ + sources.SourceDuckLabelKey: sources.SourceDuckLabelValue, + })), + }, + Key: crdName, + WantErr: true, + WantEvents: []string{ + Eventf(corev1.EventTypeWarning, "InternalError", "unable to find GVR or GVK for %s", crdName), + }, }, - }, { Name: "reconcile succeeded", From 83fe6fa727511d7cda73ba901aa9707215ae36d1 Mon Sep 17 00:00:00 2001 From: runzexia Date: Tue, 29 Sep 2020 18:03:17 +0800 Subject: [PATCH 3/5] add test cases Signed-off-by: runzexia --- pkg/reconciler/source/crd/crd_test.go | 44 +++++++++++++++++++ .../testing/customresourcedefinition.go | 8 ++++ 2 files changed, 52 insertions(+) diff --git a/pkg/reconciler/source/crd/crd_test.go b/pkg/reconciler/source/crd/crd_test.go index 2dc561ca8b5..05adc7198ac 100644 --- a/pkg/reconciler/source/crd/crd_test.go +++ b/pkg/reconciler/source/crd/crd_test.go @@ -100,6 +100,50 @@ func TestAllCases(t *testing.T) { Key: crdName, Ctx: ctx, }, + { + Name: "reconcile deleted", + Objects: []runtime.Object{ + NewCustomResourceDefinition(crdName, + WithCustomResourceDefinitionLabels(map[string]string{ + sources.SourceDuckLabelKey: sources.SourceDuckLabelValue, + }), + + WithCustomResourceDefinitionGroup(crdGroup), + WithCustomResourceDefinitionDeletionTimestamp(), + WithCustomResourceDefinitionNames(apiextensionsv1.CustomResourceDefinitionNames{ + Kind: crdKind, + Plural: crdPlural, + }), + WithCustomResourceDefinitionVersions([]apiextensionsv1.CustomResourceDefinitionVersion{{ + Name: crdVersionServed, + Served: true, + }})), + }, + Key: crdName, + Ctx: ctx, + }, + { + Name: "reconcile not Served", + Objects: []runtime.Object{ + NewCustomResourceDefinition(crdName, + WithCustomResourceDefinitionLabels(map[string]string{ + sources.SourceDuckLabelKey: sources.SourceDuckLabelValue, + }), + + WithCustomResourceDefinitionGroup(crdGroup), + WithCustomResourceDefinitionDeletionTimestamp(), + WithCustomResourceDefinitionNames(apiextensionsv1.CustomResourceDefinitionNames{ + Kind: crdKind, + Plural: crdPlural, + }), + WithCustomResourceDefinitionVersions([]apiextensionsv1.CustomResourceDefinitionVersion{{ + Name: crdVersionServed, + Served: false, + }})), + }, + Key: crdName, + Ctx: ctx, + }, } logger := logtesting.TestLogger(t) diff --git a/pkg/reconciler/testing/customresourcedefinition.go b/pkg/reconciler/testing/customresourcedefinition.go index 57623aa62f7..3ed27aa3e8c 100644 --- a/pkg/reconciler/testing/customresourcedefinition.go +++ b/pkg/reconciler/testing/customresourcedefinition.go @@ -19,6 +19,7 @@ package testing import ( apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "time" ) // CustomResourceDefinitionOption enables further configuration of a CustomResourceDefinition. @@ -61,3 +62,10 @@ func WithCustomResourceDefinitionNames(names apiextensionsv1.CustomResourceDefin crd.Spec.Names = names } } + +func WithCustomResourceDefinitionDeletionTimestamp() CustomResourceDefinitionOption { + return func(crd *apiextensionsv1.CustomResourceDefinition) { + t := metav1.NewTime(time.Unix(1e9, 0)) + crd.ObjectMeta.SetDeletionTimestamp(&t) + } +} From 060a4246e56c68e15f453f384fbf9ef88485d70f Mon Sep 17 00:00:00 2001 From: runzexia Date: Tue, 29 Sep 2020 18:11:34 +0800 Subject: [PATCH 4/5] refmt Signed-off-by: runzexia --- pkg/reconciler/testing/customresourcedefinition.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/reconciler/testing/customresourcedefinition.go b/pkg/reconciler/testing/customresourcedefinition.go index 3ed27aa3e8c..b5423de39b2 100644 --- a/pkg/reconciler/testing/customresourcedefinition.go +++ b/pkg/reconciler/testing/customresourcedefinition.go @@ -17,9 +17,10 @@ limitations under the License. package testing import ( + "time" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "time" ) // CustomResourceDefinitionOption enables further configuration of a CustomResourceDefinition. From 6544f37fa7fae944054ef93e50eac443227526c5 Mon Sep 17 00:00:00 2001 From: runzexia Date: Tue, 29 Sep 2020 20:42:57 +0800 Subject: [PATCH 5/5] test running controller Signed-off-by: runzexia --- pkg/reconciler/source/crd/crd_test.go | 88 +++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/pkg/reconciler/source/crd/crd_test.go b/pkg/reconciler/source/crd/crd_test.go index 05adc7198ac..890a310757e 100644 --- a/pkg/reconciler/source/crd/crd_test.go +++ b/pkg/reconciler/source/crd/crd_test.go @@ -20,6 +20,8 @@ import ( "context" "testing" + "knative.dev/eventing/pkg/reconciler/source/duck" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/client-go/rest" "knative.dev/pkg/injection" @@ -53,6 +55,18 @@ const ( crdVersionServed = "v1alpha1" ) +var crdGVR = schema.GroupVersionResource{ + Group: crdGroup, + Version: crdVersionServed, + Resource: crdPlural, +} + +var crdGVK = schema.GroupVersionKind{ + Group: crdGroup, + Version: crdVersionServed, + Kind: crdKind, +} + func TestAllCases(t *testing.T) { ctx := context.Background() ctx, _ = injection.Fake.SetupInformers(ctx, &rest.Config{}) @@ -160,3 +174,77 @@ func TestAllCases(t *testing.T) { controller.GetEventRecorder(ctx), r) }, false, logger)) } + +func TestControllerRunning(t *testing.T) { + ctx := context.Background() + ctx, _ = injection.Fake.SetupInformers(ctx, &rest.Config{}) + table := TableTest{ + { + + Name: "reconcile succeeded", + Objects: []runtime.Object{ + NewCustomResourceDefinition(crdName, + WithCustomResourceDefinitionLabels(map[string]string{ + sources.SourceDuckLabelKey: sources.SourceDuckLabelValue, + }), + WithCustomResourceDefinitionGroup(crdGroup), + WithCustomResourceDefinitionNames(apiextensionsv1.CustomResourceDefinitionNames{ + Kind: crdKind, + Plural: crdPlural, + }), + WithCustomResourceDefinitionVersions([]apiextensionsv1.CustomResourceDefinitionVersion{{ + Name: crdVersionServed, + Served: true, + }})), + }, + Key: crdName, + Ctx: ctx, + }, + { + Name: "reconcile deleted", + Objects: []runtime.Object{ + NewCustomResourceDefinition(crdName, + WithCustomResourceDefinitionLabels(map[string]string{ + sources.SourceDuckLabelKey: sources.SourceDuckLabelValue, + }), + + WithCustomResourceDefinitionGroup(crdGroup), + WithCustomResourceDefinitionDeletionTimestamp(), + WithCustomResourceDefinitionNames(apiextensionsv1.CustomResourceDefinitionNames{ + Kind: crdKind, + Plural: crdPlural, + }), + WithCustomResourceDefinitionVersions([]apiextensionsv1.CustomResourceDefinitionVersion{{ + Name: crdVersionServed, + Served: true, + }})), + }, + Key: crdName, + Ctx: ctx, + }, + } + + logger := logtesting.TestLogger(t) + table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler { + ctx = source.WithDuck(ctx) + r := &Reconciler{ + ogctx: ctx, + ogcmw: cmw, + controllers: make(map[schema.GroupVersionResource]runningController), + } + sdc := duck.NewController(crdName, crdGVR, crdGVK) + // Source Duck controller context + sdctx, cancel := context.WithCancel(r.ogctx) + // Source Duck controller instantiation + sd := sdc(sdctx, r.ogcmw) + rc := runningController{ + controller: sd, + cancel: cancel, + } + r.controllers[crdGVR] = rc + + return crdreconciler.NewReconciler(ctx, logger, + fakeclient.Get(ctx), listers.GetCustomResourceDefinitionLister(), + controller.GetEventRecorder(ctx), r) + }, false, logger)) +}