Skip to content

Commit 3c21bba

Browse files
committed
add e2e test
Signed-off-by: rashmi_kh <[email protected]>
1 parent b103f5d commit 3c21bba

File tree

2 files changed

+65
-73
lines changed

2 files changed

+65
-73
lines changed

internal/controllers/clusterextension_controller_test.go

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -677,79 +677,6 @@ func TestClusterExtensionInstallationSucceeds(t *testing.T) {
677677
require.NoError(t, cl.DeleteAllOf(ctx, &ocv1.ClusterExtension{}))
678678
}
679679

680-
func TestClusterExtensionInstallationFailsWithNoServiceAccount(t *testing.T) {
681-
cl, reconciler := newClientAndReconciler(t)
682-
reconciler.Unpacker = &MockUnpacker{
683-
result: &source.Result{
684-
State: source.StateUnpacked,
685-
Bundle: fstest.MapFS{},
686-
},
687-
}
688-
689-
ctx := context.Background()
690-
extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))}
691-
692-
t.Log("When the cluster extension specifies a channel with version that exist")
693-
t.Log("By initializing cluster state")
694-
pkgName := "prometheus"
695-
pkgVer := "1.0.0"
696-
pkgChan := "beta"
697-
namespace := fmt.Sprintf("test-ns-%s", rand.String(8))
698-
serviceAccount := fmt.Sprintf("test-does1-not-exist-%s", rand.String(8))
699-
700-
clusterExtension := &ocv1.ClusterExtension{
701-
ObjectMeta: metav1.ObjectMeta{Name: extKey.Name},
702-
Spec: ocv1.ClusterExtensionSpec{
703-
Source: ocv1.SourceConfig{
704-
SourceType: "Catalog",
705-
Catalog: &ocv1.CatalogSource{
706-
PackageName: pkgName,
707-
Version: pkgVer,
708-
Channels: []string{pkgChan},
709-
},
710-
},
711-
Namespace: namespace,
712-
ServiceAccount: ocv1.ServiceAccountReference{
713-
Name: serviceAccount,
714-
},
715-
},
716-
}
717-
err := cl.Create(ctx, clusterExtension)
718-
require.NoError(t, err)
719-
720-
t.Log("By fetching updated cluster extension after reconcile")
721-
require.NoError(t, cl.Get(ctx, extKey, clusterExtension))
722-
723-
t.Log("It sets resolution success status")
724-
t.Log("By running reconcile")
725-
reconciler.Resolver = resolve.Func(func(_ context.Context, _ *ocv1.ClusterExtension, _ *ocv1.BundleMetadata) (*declcfg.Bundle, *bsemver.Version, *declcfg.Deprecation, error) {
726-
v := bsemver.MustParse("1.0.0")
727-
return &declcfg.Bundle{
728-
Name: "prometheus.v1.0.0",
729-
Package: "prometheus",
730-
Image: "quay.io/operatorhubio/[email protected]",
731-
}, &v, nil, nil
732-
})
733-
reconciler.Applier = &MockApplier{
734-
objs: []client.Object{},
735-
}
736-
reconciler.Applier = &MockApplier{
737-
err: errors.New("missing ServiceAccount"),
738-
}
739-
reconciler.Manager = &MockManagedContentCacheManager{
740-
cache: &MockManagedContentCache{},
741-
}
742-
743-
_, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
744-
require.Error(t, err)
745-
746-
t.Log("By fetching updated cluster extension after reconcile")
747-
require.NoError(t, cl.Get(ctx, extKey, clusterExtension))
748-
749-
t.Log("By checking the status fields")
750-
require.Contains(t, err.Error(), "missing ServiceAccount")
751-
}
752-
753680
func TestClusterExtensionDeleteFinalizerFails(t *testing.T) {
754681
cl, reconciler := newClientAndReconciler(t)
755682
reconciler.Unpacker = &MockUnpacker{

test/e2e/cluster_extension_install_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,71 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
351351
}
352352
}
353353

354+
func TestClusterExtensionInstallFailsWithoutServiceAccount(t *testing.T) {
355+
t.Log("When a cluster extension is installed from a catalog")
356+
t.Log("And the ServiceAccount does not exist")
357+
358+
// Step 1: Setup test environment without creating an SA
359+
clusterExtensionName := fmt.Sprintf("clusterextension-%s", rand.String(8))
360+
361+
ns, err := createNamespace(context.Background(), clusterExtensionName)
362+
require.NoError(t, err)
363+
364+
extensionCatalog, err := createTestCatalog(context.Background(), testCatalogName, os.Getenv(testCatalogRefEnvVar))
365+
require.NoError(t, err)
366+
367+
// Important: We do NOT create a ServiceAccount here
368+
defer testCleanup(t, extensionCatalog, nil, nil, ns)
369+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
370+
371+
// Step 2: Define the ClusterExtension with a non-existent SA
372+
clusterExtension := &ocv1.ClusterExtension{
373+
ObjectMeta: metav1.ObjectMeta{
374+
Name: clusterExtensionName,
375+
},
376+
Spec: ocv1.ClusterExtensionSpec{
377+
Source: ocv1.SourceConfig{
378+
SourceType: "Catalog",
379+
Catalog: &ocv1.CatalogSource{
380+
PackageName: "test",
381+
Selector: &metav1.LabelSelector{
382+
MatchLabels: map[string]string{"olm.operatorframework.io/metadata.name": extensionCatalog.Name},
383+
},
384+
},
385+
},
386+
Namespace: ns.Name,
387+
ServiceAccount: ocv1.ServiceAccountReference{
388+
Name: "non-existent-sa", // This SA does NOT exist
389+
},
390+
},
391+
}
392+
393+
t.Log("By creating the ClusterExtension resource without a ServiceAccount")
394+
require.NoError(t, c.Create(context.Background(), clusterExtension))
395+
396+
// Step 3: Validate failure due to missing ServiceAccount
397+
t.Log("By eventually reporting an installation failure due to missing ServiceAccount")
398+
require.EventuallyWithT(t, func(ct *assert.CollectT) {
399+
assert.NoError(ct, c.Get(context.Background(), types.NamespacedName{Name: clusterExtension.Name}, clusterExtension))
400+
401+
// Validate that Progressing reports failure
402+
cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1.TypeProgressing)
403+
if assert.NotNil(ct, cond) {
404+
assert.Equal(ct, metav1.ConditionTrue, cond.Status)
405+
assert.Equal(ct, ocv1.ReasonRetrying, cond.Reason)
406+
assert.Contains(ct, cond.Message, "installation cannot proceed due to missing ServiceAccount")
407+
}
408+
409+
// Validate that Installed condition is false
410+
installCond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1.TypeInstalled)
411+
if assert.NotNil(ct, installCond) {
412+
assert.Equal(ct, metav1.ConditionUnknown, installCond.Status)
413+
assert.Equal(ct, ocv1.ReasonFailed, installCond.Reason)
414+
assert.Contains(ct, installCond.Message, "service account")
415+
}
416+
}, pollDuration, pollInterval)
417+
}
418+
354419
func TestClusterExtensionInstallRegistryDynamic(t *testing.T) {
355420
// NOTE: Like 'TestClusterExtensionInstallRegistry', this test also requires extra configuration in /etc/containers/registries.conf
356421
packageName := "dynamic"

0 commit comments

Comments
 (0)