Skip to content

Commit

Permalink
duck/test: add initial tests for missing functions (#5367)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Panato <ctadeu@gmail.com>
  • Loading branch information
cpanato authored May 7, 2021
1 parent 10c3069 commit f1f4615
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/duck/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ func TestNewPhysicalChannel(t *testing.T) {
},
},
},
{
name: "no ChannelableSpec",
objMeta: metav1.ObjectMeta{
Name: "hello",
Namespace: "world",
},
opts: []PhysicalChannelOption{
WithPhysicalChannelSpec(&physicalChannelSpec),
},
assertFn: assertMyCoolChannelEquality,
want: &MyCoolChannel{
TypeMeta: imcTypeMeta,
ObjectMeta: metav1.ObjectMeta{
Name: "hello",
Namespace: "world",
},
Spec: MyCoolChannelSpec{
MyCoolParameter: "i'm cool",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
114 changes: 114 additions & 0 deletions pkg/duck/listable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,120 @@ func TestResourceTrackerForKReference(t *testing.T) {
}
}

func TestResourceListerForKReference(t *testing.T) {
testCases := map[string]struct {
informerFactoryError error
repeatedTracks int
expectedError error
}{
"informerFactory error": {
informerFactoryError: errTest,
expectedError: errTest,
},
"Only one informer created per GVR": {
repeatedTracks: 1,
},
}

for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
fif := newFakeInformerFactory()
if tc.informerFactoryError != nil {
fif.err = tc.informerFactoryError
}
ctx, _ := fakedynamicclient.With(context.Background(), scheme.Scheme)
ctx = addressable.WithDuck(ctx)
tr := NewListableTracker(ctx, addressable.Get, func(types.NamespacedName) {}, time.Minute)
rt, _ := tr.(*listableTracker)
rt.informerFactory = fif
track := rt.TrackInNamespaceKReference(
context.Background(),
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: "svc",
},
})
for i := 0; i <= tc.repeatedTracks; i++ {
ref := duckv1.KReference{
APIVersion: "v1",
Kind: "Pod",
Name: fmt.Sprintf("ref-%d", i),
}
err := track(ref)
if tc.expectedError != nil {
if err != tc.expectedError {
t.Fatalf("Incorrect error from returned track function. Expected '%v'. Actual '%v'", tc.expectedError, err)
}
return
}

_, err = rt.ListerForKReference(ref)
if err != nil {
t.Fatalf("Expected nil. Actual '%v'", err)
}
}
})
}
}

func TestResourceInformerForKReference(t *testing.T) {
testCases := map[string]struct {
informerFactoryError error
repeatedTracks int
expectedError error
}{
"informerFactory error": {
informerFactoryError: errTest,
expectedError: errTest,
},
"Only one informer created per GVR": {
repeatedTracks: 1,
},
}

for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
fif := newFakeInformerFactory()
if tc.informerFactoryError != nil {
fif.err = tc.informerFactoryError
}
ctx, _ := fakedynamicclient.With(context.Background(), scheme.Scheme)
ctx = addressable.WithDuck(ctx)
tr := NewListableTracker(ctx, addressable.Get, func(types.NamespacedName) {}, time.Minute)
rt, _ := tr.(*listableTracker)
rt.informerFactory = fif
track := rt.TrackInNamespaceKReference(
context.Background(),
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: "svc",
},
})
for i := 0; i <= tc.repeatedTracks; i++ {
ref := duckv1.KReference{
APIVersion: "v1",
Kind: "Pod",
Name: fmt.Sprintf("ref-%d", i),
}
err := track(ref)
if tc.expectedError != nil {
if err != tc.expectedError {
t.Fatalf("Incorrect error from returned track function. Expected '%v'. Actual '%v'", tc.expectedError, err)
}
return
}

_, err = rt.InformerForKReference(ref)
if err != nil {
t.Fatalf("Expected nil. Actual '%v'", err)
}
}
})
}
}

type fakeInformer struct {
cache.SharedIndexInformer
eventHandlerAdded bool
Expand Down

0 comments on commit f1f4615

Please sign in to comment.