Skip to content

Commit

Permalink
Add unit test for syncSourceState
Browse files Browse the repository at this point in the history
Signed-off-by: Vu Dinh <vdinh@redhat.com>
  • Loading branch information
dinhxuanvu committed Nov 20, 2019
1 parent 1502b75 commit 693a312
Showing 1 changed file with 145 additions and 2 deletions.
147 changes: 145 additions & 2 deletions pkg/controller/operators/catalog/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import (
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/scoped"

"google.golang.org/grpc/connectivity"
)

type mockTransitioner struct {
Expand Down Expand Up @@ -512,7 +514,7 @@ func TestSyncCatalogSources(t *testing.T) {
Namespace: "cool-namespace",
UID: types.UID("configmap-uid"),
ResourceVersion: "resource-version",
LastUpdateTime: now,
LastUpdateTime: now,
},
RegistryServiceStatus: nil,
},
Expand Down Expand Up @@ -758,6 +760,147 @@ func TestCompetingCRDOwnersExist(t *testing.T) {
}
}

func TestSyncSourceState(t *testing.T) {
clockFake := utilclock.NewFakeClock(time.Date(2018, time.January, 26, 20, 40, 0, 0, time.UTC))

tests := []struct {
testName string
watchedns string
namespace string
sourcestate grpc.SourceState
clientObjs []runtime.Object
expectedns []string
}{
{
testName: "Ready/GlobalCatalogSyncMultipleNamespaces",
watchedns: "cool-namespace-1,test-namespace-1",
namespace: "cool-namespace-1",
sourcestate: grpc.SourceState{
Key: resolver.CatalogKey{
Name: "test-catalog",
Namespace: "cool-namespace-1",
},
State: connectivity.Ready,
},
clientObjs: []runtime.Object{
&v1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-namespace-1",
Name: "sub",
UID: types.UID("uid"),
},
Spec: &v1alpha1.SubscriptionSpec{
Package: "test-pkg ",
Channel: "test-channel",
CatalogSource: "test-catalog",
CatalogSourceNamespace: "cool-namespace-1",
},
},
},
expectedns: []string{"cool-namespace-1", "test-namespace-1"},
},
{
testName: "Ready/LocalCatalogSyncMultipleNamespaces",
watchedns: "cool-namespace-2,test-namespace-2",
namespace: "cool-namespace-2",
sourcestate: grpc.SourceState{
Key: resolver.CatalogKey{
Name: "test-catalog",
Namespace: "test-namespace-2",
},
State: connectivity.Ready,
},
clientObjs: []runtime.Object{
&v1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-namespace-2",
Name: "sub",
UID: types.UID("uid"),
},
},
},
expectedns: []string{"test-namespace-2"},
},
{
testName: "Ready/GlobalCatalogSyncSingleNamespace",
watchedns: "cool-namespace-3,test-namespace-3",
namespace: "cool-namespace-3",
sourcestate: grpc.SourceState{
Key: resolver.CatalogKey{
Name: "test-catalog",
Namespace: "cool-namespace-3",
},
State: connectivity.Ready,
},
clientObjs: []runtime.Object{
&v1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Namespace: "cool-namespace-3",
Name: "sub",
UID: types.UID("uid"),
},
Spec: &v1alpha1.SubscriptionSpec{
Package: "test-pkg ",
Channel: "test-channel",
CatalogSource: "test-catalog",
CatalogSourceNamespace: "cool-namespace-3",
},
},
},
expectedns: []string{"cool-namespace-3"},
},
{
testName: "NotReady/GlobalCatalogSyncNamespace",
watchedns: "cool-namespace-4,test-namespace-4",
namespace: "cool-namespace-4",
sourcestate: grpc.SourceState{
Key: resolver.CatalogKey{
Name: "test-catalog",
Namespace: "cool-namespace-4",
},
State: connectivity.Connecting,
},
clientObjs: []runtime.Object{
&v1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-namespace-4",
Name: "sub",
UID: types.UID("uid"),
},
Spec: &v1alpha1.SubscriptionSpec{
Package: "test-pkg ",
Channel: "test-channel",
CatalogSource: "test-catalog",
CatalogSourceNamespace: "cool-namespace-4",
},
},
},
expectedns: []string{},
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
// Create test operator
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

op, err := NewFakeOperator(ctx, tt.namespace, []string{tt.watchedns}, withClock(clockFake), withClientObjs(tt.clientObjs...))
require.NoError(t, err)

// Run sync
op.syncSourceState(tt.sourcestate)

// Validate sync namespace(s)
if tt.expectedns != nil {
require.Equal(t, len(tt.expectedns), op.nsResolveQueue.Len())
// for _, item := range tt.expectedns {
// require.
// }
}
})
}
}

func fakeConfigMapData() map[string]string {
data := make(map[string]string)
yaml, err := yaml.Marshal([]v1beta1.CustomResourceDefinition{crd("fake-crd")})
Expand Down Expand Up @@ -917,7 +1060,7 @@ func NewFakeOperator(ctx context.Context, namespace string, watchedNamespaces []
reconciler: config.reconciler,
clientAttenuator: scoped.NewClientAttenuator(logger, &rest.Config{}, opClientFake, clientFake),
serviceAccountQuerier: scoped.NewUserDefinedServiceAccountQuerier(logger, clientFake),
catsrcQueueSet: queueinformer.NewEmptyResourceQueueSet(),
catsrcQueueSet: queueinformer.NewEmptyResourceQueueSet(),
}
op.sources = grpc.NewSourceStore(config.logger, 1*time.Second, 5*time.Second, op.syncSourceState)
if op.reconciler == nil {
Expand Down

0 comments on commit 693a312

Please sign in to comment.