From 8ce7d31b03906d8e1a505fd7f7663144e8a61ff0 Mon Sep 17 00:00:00 2001 From: Nitin Goyal Date: Thu, 14 Nov 2024 14:54:57 +0530 Subject: [PATCH] test: fix tests for subscriptions create/update Signed-off-by: Nitin Goyal --- controllers/subscriptions_test.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/controllers/subscriptions_test.go b/controllers/subscriptions_test.go index 9a0975323..54ec512be 100644 --- a/controllers/subscriptions_test.go +++ b/controllers/subscriptions_test.go @@ -22,6 +22,7 @@ import ( "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -60,6 +61,9 @@ func TestEnsureSubscription(t *testing.T) { for _, subscription := range subs { sub := subscription.DeepCopy() sub.Spec.Channel = "fake-channel" + // Set the creation timestamp to a non-zero value to simulate that the subscription already exists + // This is required because the fake client does not set the creation timestamp + sub.CreationTimestamp = metav1.Now() err = fakeReconciler.Client.Create(context.TODO(), sub) assert.NoError(t, err) } @@ -101,10 +105,27 @@ func TestEnsureSubscription(t *testing.T) { } actualSubscription := &operatorv1alpha1.Subscription{} - err = fakeReconciler.Client.Get(context.TODO(), types.NamespacedName{Name: expectedSubscription.Name, Namespace: expectedSubscription.Namespace}, actualSubscription) - assert.NoError(t, err) - - assert.Equal(t, expectedSubscription.Spec, actualSubscription.Spec) + err = fakeReconciler.Client.Get(context.TODO(), + types.NamespacedName{Name: expectedSubscription.Name, Namespace: expectedSubscription.Namespace}, actualSubscription) + + // create case + if !tc.subscriptionAlreadyExist { + if expectedSubscription.Spec.Package == OdfDepsSubscriptionPackage { + assert.NoError(t, err) + // Set odf-dependencies catalog and catalog namespace same as odf-operator in expected subscription + // That is what being set by controller and verify the same + expectedSubscription.Spec.CatalogSource = odfSub.Spec.CatalogSource + expectedSubscription.Spec.CatalogSourceNamespace = odfSub.Spec.CatalogSourceNamespace + assert.Equal(t, expectedSubscription.Spec, actualSubscription.Spec) + } else { + assert.Error(t, err) + assert.True(t, errors.IsNotFound(err)) + } + // update case + } else if tc.subscriptionAlreadyExist { + assert.NoError(t, err) + assert.Equal(t, expectedSubscription.Spec, actualSubscription.Spec) + } } } }