Skip to content

Commit

Permalink
Add test for fetching bundles
Browse files Browse the repository at this point in the history
Cover what happens when we fail to fetch bundles from catalog.

Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Jun 17, 2024
1 parent f301f55 commit 76d282b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions internal/controllers/clusterextension_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"

Expand All @@ -28,6 +29,7 @@ import (
"github.com/operator-framework/operator-controller/internal/conditionsets"
"github.com/operator-framework/operator-controller/internal/controllers"
"github.com/operator-framework/operator-controller/pkg/features"
testutil "github.com/operator-framework/operator-controller/test/util"
)

// Describe: ClusterExtension Controller Test
Expand Down Expand Up @@ -1445,3 +1447,48 @@ var testBundleList = []*catalogmetadata.Bundle{
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
},
}

func TestClusterExtensionErrorGettingBundles(t *testing.T) {
ctx := context.Background()
fakeBundleProvider := testutil.NewFakeCatalogClientWithError(errors.New("fake-test-error"))
cl, reconciler := newClientAndReconciler(t, nil)
reconciler.BundleProvider = &fakeBundleProvider
extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))}

t.Log("Creating a test cluster extension object")
clusterExtension := &ocv1alpha1.ClusterExtension{
ObjectMeta: metav1.ObjectMeta{Name: extKey.Name},
Spec: ocv1alpha1.ClusterExtensionSpec{
PackageName: "prometheus",
InstallNamespace: "default",
},
}
err := cl.Create(ctx, clusterExtension)
defer func() {
require.NoError(t, cl.Delete(ctx, &ocv1alpha1.ClusterExtension{
ObjectMeta: metav1.ObjectMeta{Name: extKey.Name},
}))
}()
require.NoError(t, err)

t.Log("Running reconcile")
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
require.Equal(t, ctrl.Result{}, res)
require.ErrorContains(t, err, "error fetching bundles: fake-test-error")

t.Log("Fetching updated cluster extension after reconcile")
require.NoError(t, cl.Get(ctx, extKey, clusterExtension))

t.Log("Checking the status fields")
require.Empty(t, clusterExtension.Status.ResolvedBundle)
require.Empty(t, clusterExtension.Status.InstalledBundle)

t.Log("Checking the expected conditions")
cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1alpha1.TypeResolved)
require.NotNil(t, cond)
require.Equal(t, metav1.ConditionFalse, cond.Status)
require.Equal(t, ocv1alpha1.ReasonResolutionFailed, cond.Reason)
require.Contains(t, cond.Message, "error fetching bundles: fake-test-error")

verifyInvariants(ctx, t, reconciler.Client, clusterExtension)
}

0 comments on commit 76d282b

Please sign in to comment.