Skip to content

Commit

Permalink
setup proper schemes in the fake dynamic client used in unit tests (#…
Browse files Browse the repository at this point in the history
…5477)

* dynamic client should have registered types in it's schema

* setup test schemes and include them by default

* use messaging group so we don't have to change many tests

* fix group in test

* revert gvk for bad gvk test

* include our test scheme

* inject a failure when tracking

* fix linting: drop unused variable

* wire in the correct dynamic client
  • Loading branch information
dprotaso authored Jun 8, 2021
1 parent 92d85bf commit 28319c3
Show file tree
Hide file tree
Showing 26 changed files with 118 additions and 201 deletions.
6 changes: 0 additions & 6 deletions pkg/broker/filter/filter_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"go.uber.org/zap/zaptest"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"knative.dev/pkg/apis"

eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
Expand All @@ -64,11 +63,6 @@ var (
validPath = fmt.Sprintf("/triggers/%s/%s/%s", testNS, triggerName, triggerUID)
)

func init() {
// Add types to scheme.
_ = eventingv1.AddToScheme(scheme.Scheme)
}

func TestReceiver(t *testing.T) {
testCases := map[string]struct {
triggers []*eventingv1.Trigger
Expand Down
6 changes: 0 additions & 6 deletions pkg/duck/listable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@ import (
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/apis/duck"
duckv1 "knative.dev/pkg/apis/duck/v1"
duckv1alpha1 "knative.dev/pkg/apis/duck/v1alpha1"
"knative.dev/pkg/client/injection/ducks/duck/v1alpha1/addressable"
fakedynamicclient "knative.dev/pkg/injection/clients/dynamicclient/fake"
)

func init() {
// Add types to scheme
_ = duckv1alpha1.AddToScheme(scheme.Scheme)
}

const ns = "test-ns"

var errTest = errors.New("test error")
Expand Down
11 changes: 0 additions & 11 deletions pkg/duck/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,12 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"

"k8s.io/client-go/kubernetes/scheme"

eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

var (
testNS = "testnamespace"
)

func init() {
// Add types to scheme
_ = eventingv1.AddToScheme(scheme.Scheme)
_ = duckv1.AddToScheme(scheme.Scheme)
}

func TestDomainToURL(t *testing.T) {
d := "default-broker.default.svc.cluster.local"
e := fmt.Sprintf("http://%s/", d)
Expand Down
8 changes: 0 additions & 8 deletions pkg/reconciler/apiserversource/apiserversource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"

sourcesv1 "knative.dev/eventing/pkg/apis/sources/v1"
Expand Down Expand Up @@ -88,13 +87,6 @@ const (
generation = 1
)

func init() {
// Add types to scheme
_ = appsv1.AddToScheme(scheme.Scheme)
_ = corev1.AddToScheme(scheme.Scheme)
_ = duckv1.AddToScheme(scheme.Scheme)
}

func TestReconcile(t *testing.T) {
table := TableTest{{
Name: "not enough permissions",
Expand Down
8 changes: 0 additions & 8 deletions pkg/reconciler/broker/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"

clientgotesting "k8s.io/client-go/testing"
"knative.dev/eventing/pkg/apis/eventing"
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
"knative.dev/eventing/pkg/client/injection/ducks/duck/v1/channelable"
"knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1/broker"
Expand Down Expand Up @@ -84,12 +82,6 @@ var (
}
)

func init() {
// Add types to scheme
_ = eventingv1.AddToScheme(scheme.Scheme)
_ = duckv1.AddToScheme(scheme.Scheme)
}

func TestReconcile(t *testing.T) {
table := TableTest{
{
Expand Down
7 changes: 0 additions & 7 deletions pkg/reconciler/broker/trigger/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1"
v1 "knative.dev/eventing/pkg/apis/duck/v1"
Expand Down Expand Up @@ -140,12 +139,6 @@ var (
}
)

func init() {
// Add types to scheme
_ = eventingv1.AddToScheme(scheme.Scheme)
_ = duckv1.AddToScheme(scheme.Scheme)
}

func TestReconcile(t *testing.T) {
table := TableTest{
{
Expand Down
30 changes: 18 additions & 12 deletions pkg/reconciler/channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package channel

import (
"context"
"errors"
"fmt"
"testing"

Expand All @@ -23,11 +24,9 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"
"k8s.io/utils/pointer"
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1"
messagingv1 "knative.dev/eventing/pkg/apis/messaging/v1"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
"knative.dev/eventing/pkg/client/injection/ducks/duck/v1/channelable"
channelreconciler "knative.dev/eventing/pkg/client/injection/reconciler/messaging/v1/channel"
Expand Down Expand Up @@ -58,12 +57,6 @@ var (
}
)

func init() {
// Add types to scheme
_ = messagingv1.AddToScheme(scheme.Scheme)
_ = eventingduckv1.AddToScheme(scheme.Scheme)
}

func TestReconcile(t *testing.T) {

table := TableTest{{
Expand Down Expand Up @@ -95,7 +88,7 @@ func TestReconcile(t *testing.T) {
WithInitChannelConditions),
},
WantEvents: []string{
Eventf(corev1.EventTypeWarning, "InternalError", "unable to track changes to the backing Channel: %v:%v", "invalid Reference", "\nAPIVersion: name part must be non-empty\nAPIVersion: name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')\nKind: a valid C identifier must start with alphabetic character or '_', followed by a string of alphanumeric characters or '_' (e.g. 'my_name', or 'MY_NAME', or 'MyName', regex used for validation is '[A-Za-z_][A-Za-z0-9_]*')"),
Eventf(corev1.EventTypeWarning, "InternalError", "unable to track changes to the backing Channel: failed to track"),
},
WantErr: true,
}, {
Expand Down Expand Up @@ -304,7 +297,7 @@ func TestReconcile(t *testing.T) {
r := &Reconciler{
dynamicClientSet: fakedynamicclient.Get(ctx),
channelLister: listers.GetMessagingChannelLister(),
channelableTracker: duck.NewListableTracker(ctx, channelable.Get, func(types.NamespacedName) {}, 0),
channelableTracker: &fakeListableTracker{duck.NewListableTracker(ctx, channelable.Get, func(types.NamespacedName) {}, 0)},
}
return channelreconciler.NewReconciler(ctx, logger,
fakeeventingclient.Get(ctx), listers.GetMessagingChannelLister(),
Expand All @@ -315,6 +308,19 @@ func TestReconcile(t *testing.T) {
))
}

type fakeListableTracker struct {
duck.ListableTracker
}

func (f *fakeListableTracker) TrackInNamespaceKReference(ctx context.Context, obj metav1.Object) duck.TrackKReference {
return func(ref duckv1.KReference) error {
if ref.Kind == channelCRDBadGvk().Kind {
return errors.New("failed to track")
}
return f.ListableTracker.TrackInNamespaceKReference(ctx, obj)(ref)
}
}

func channelCRD() metav1.TypeMeta {
return metav1.TypeMeta{
APIVersion: "messaging.knative.dev/v1",
Expand All @@ -324,8 +330,8 @@ func channelCRD() metav1.TypeMeta {

func channelCRDBadGvk() metav1.TypeMeta {
return metav1.TypeMeta{
APIVersion: "",
Kind: "",
APIVersion: "broken.testing.dev",
Kind: "FailsTrack",
}
}

Expand Down
8 changes: 0 additions & 8 deletions pkg/reconciler/containersource/containersource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
"knative.dev/pkg/apis"
Expand Down Expand Up @@ -76,13 +75,6 @@ var (
}
)

func init() {
// Add types to scheme
_ = appsv1.AddToScheme(scheme.Scheme)
_ = corev1.AddToScheme(scheme.Scheme)
_ = duckv1.AddToScheme(scheme.Scheme)
}

func TestAllCases(t *testing.T) {
table := TableTest{
{
Expand Down
7 changes: 0 additions & 7 deletions pkg/reconciler/eventtype/eventtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"
v1 "knative.dev/eventing/pkg/apis/eventing/v1"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
"knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1beta1/eventtype"
. "knative.dev/eventing/pkg/reconciler/testing/v1"
Expand All @@ -53,11 +51,6 @@ var (
}
)

func init() {
// Add types to scheme
_ = v1.AddToScheme(scheme.Scheme)
}

func TestReconcile(t *testing.T) {
table := TableTest{{
Name: "bad workqueue key",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1"
v1 "knative.dev/eventing/pkg/apis/messaging/v1"
"knative.dev/eventing/pkg/reconciler/inmemorychannel/controller/resources"
. "knative.dev/eventing/pkg/reconciler/testing/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/kmeta"
Expand All @@ -67,12 +65,6 @@ const (
imcGeneration = 7
)

func init() {
// Add types to scheme
_ = v1.AddToScheme(scheme.Scheme)
_ = duckv1.AddToScheme(scheme.Scheme)
}

func TestAllCases(t *testing.T) {
imcKey := testNS + "/" + imcName
subscriber1UID := types.UID("2f9b5e8e-deb6-11e8-9f32-f2801f1b9fd1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"
"k8s.io/utils/pointer"
"knative.dev/pkg/apis"
Expand Down Expand Up @@ -101,11 +100,6 @@ var (
subscribers = []eventingduckv1.SubscriberSpec{subscriber1, subscriber2}
)

func init() {
// Add types to scheme
_ = v1.AddToScheme(scheme.Scheme)
}

func TestAllCases(t *testing.T) {
backoffPolicy := eventingduckv1.BackoffPolicyLinear

Expand Down
7 changes: 0 additions & 7 deletions pkg/reconciler/parallel/parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1"
"knative.dev/eventing/pkg/client/injection/ducks/duck/v1/channelable"
Expand Down Expand Up @@ -66,12 +65,6 @@ var (
}
)

func init() {
// Add types to scheme
_ = v1.AddToScheme(scheme.Scheme)
_ = duckv1.AddToScheme(scheme.Scheme)
}

func TestAllBranches(t *testing.T) {
pKey := testNS + "/" + parallelName
imc := &messagingv1.ChannelTemplateSpec{
Expand Down
8 changes: 0 additions & 8 deletions pkg/reconciler/pingsource/pingsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"

"knative.dev/pkg/apis"
Expand Down Expand Up @@ -92,13 +91,6 @@ const (
generation = 1
)

func init() {
// Add types to scheme
_ = appsv1.AddToScheme(scheme.Scheme)
_ = corev1.AddToScheme(scheme.Scheme)
_ = duckv1.AddToScheme(scheme.Scheme)
}

func TestAllCases(t *testing.T) {
table := TableTest{
{
Expand Down
7 changes: 0 additions & 7 deletions pkg/reconciler/sequence/sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clientgotesting "k8s.io/client-go/testing"
v1 "knative.dev/eventing/pkg/apis/flows/v1"
messagingv1 "knative.dev/eventing/pkg/apis/messaging/v1"
Expand Down Expand Up @@ -64,12 +63,6 @@ var (
}
)

func init() {
// Add types to scheme
_ = v1.AddToScheme(scheme.Scheme)
_ = duckv1.AddToScheme(scheme.Scheme)
}

func createReplyChannel(channelName string) *duckv1.Destination {
return &duckv1.Destination{
Ref: &duckv1.KReference{
Expand Down
3 changes: 2 additions & 1 deletion pkg/reconciler/source/crd/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
. "knative.dev/pkg/reconciler/testing"

// Fake injection informers

_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta1/eventtype/fake"
fakeclient "knative.dev/pkg/client/injection/apiextensions/client/fake"
_ "knative.dev/pkg/client/injection/apiextensions/informers/apiextensions/v1/customresourcedefinition/fake"
Expand All @@ -52,7 +53,7 @@ const (
crdGroup = "testing.sources.knative.dev"
crdKind = "TestSource"
crdPlural = "testsources"
crdVersionServed = "v1alpha1"
crdVersionServed = "v1"
)

var crdGVR = schema.GroupVersionResource{
Expand Down
Loading

0 comments on commit 28319c3

Please sign in to comment.