Skip to content

Commit

Permalink
Add test for fetching bundles (#951)
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 24, 2024
1 parent 41f1593 commit 6c61a78
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 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,47 @@ 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",
},
}
require.NoError(t, cl.Create(ctx, clusterExtension))
defer func() {
require.NoError(t, cl.Delete(ctx, &ocv1alpha1.ClusterExtension{
ObjectMeta: metav1.ObjectMeta{Name: extKey.Name},
}))
}()

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 6c61a78

Please sign in to comment.