Skip to content

Commit

Permalink
Use a separate namespace for each e2e test
Browse files Browse the repository at this point in the history
Fixes operator-framework#1307

Create and use a new namespace for every e2e test. This means that
extension resources are placed in their own namespace. The tests
deletes the namespace, and then waits until completion. This ensures
that _most_ of an extension's resources are deleted. It does not
guarantee that global resources (e.g. CRDs, CRs, CRBs) are deleted,
but it improves the tests, and would eventually allow the tests
to be run in parallel (assuming the installed extensions allow
for that).

Signed-off-by: Todd Short <tshort@redhat.com>
  • Loading branch information
tmshort committed Nov 6, 2024
1 parent 6bda277 commit 422ccf5
Showing 1 changed file with 65 additions and 33 deletions.
98 changes: 65 additions & 33 deletions test/e2e/cluster_extension_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ const (
var pollDuration = time.Minute
var pollInterval = time.Second

func createNamespace(ctx context.Context) (*corev1.Namespace, error) {
namespaceName := fmt.Sprintf("namespace-%s", rand.String(8))
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
},
}
err := c.Create(ctx, ns)
if err != nil {
return nil, err
}
return ns, nil
}

func createServiceAccount(ctx context.Context, name types.NamespacedName, clusterExtensionName string) (*corev1.ServiceAccount, error) {
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -177,8 +191,12 @@ func createClusterRoleAndBindingForSA(ctx context.Context, name string, sa *core
return nil
}

func testInit(t *testing.T) (*ocv1alpha1.ClusterExtension, *catalogd.ClusterCatalog, *corev1.ServiceAccount) {
func testInit(t *testing.T) (*ocv1alpha1.ClusterExtension, *catalogd.ClusterCatalog, *corev1.ServiceAccount, *corev1.Namespace) {
var err error

ns, err := createNamespace(context.Background())
require.NoError(t, err)

extensionCatalog, err := createTestCatalog(context.Background(), testCatalogName, os.Getenv(testCatalogRefEnvVar))
require.NoError(t, err)

Expand All @@ -191,30 +209,42 @@ func testInit(t *testing.T) (*ocv1alpha1.ClusterExtension, *catalogd.ClusterCata

defaultNamespace := types.NamespacedName{
Name: clusterExtensionName,
Namespace: "default",
Namespace: ns.GetName(),
}

sa, err := createServiceAccount(context.Background(), defaultNamespace, clusterExtensionName)
require.NoError(t, err)
return clusterExtension, extensionCatalog, sa
return clusterExtension, extensionCatalog, sa, ns
}

func testCleanup(t *testing.T, cat *catalogd.ClusterCatalog, clusterExtension *ocv1alpha1.ClusterExtension, sa *corev1.ServiceAccount) {
func testCleanup(t *testing.T, cat *catalogd.ClusterCatalog, clusterExtension *ocv1alpha1.ClusterExtension, sa *corev1.ServiceAccount, ns *corev1.Namespace) {
t.Logf("By deleting ClusterCatalog %q", cat.Name)
require.NoError(t, c.Delete(context.Background(), cat))
require.Eventually(t, func() bool {
err := c.Get(context.Background(), types.NamespacedName{Name: cat.Name}, &catalogd.ClusterCatalog{})
return errors.IsNotFound(err)
}, pollDuration, pollInterval)

t.Logf("By deleting ClusterExtension %q", clusterExtension.Name)
require.NoError(t, c.Delete(context.Background(), clusterExtension))
require.Eventually(t, func() bool {
err := c.Get(context.Background(), types.NamespacedName{Name: clusterExtension.Name}, &ocv1alpha1.ClusterExtension{})
return errors.IsNotFound(err)
}, pollDuration, pollInterval)

t.Logf("By deleting ServiceAccount %q", sa.Name)
require.NoError(t, c.Delete(context.Background(), sa))
require.Eventually(t, func() bool {
err := c.Get(context.Background(), types.NamespacedName{Name: sa.Name, Namespace: sa.Namespace}, &corev1.ServiceAccount{})
return errors.IsNotFound(err)
}, pollDuration, pollInterval)

t.Logf("By deleting Namespace %q", ns.Name)
require.NoError(t, c.Delete(context.Background(), ns))
require.Eventually(t, func() bool {
err := c.Get(context.Background(), types.NamespacedName{Name: ns.Name}, &corev1.Namespace{})
return errors.IsNotFound(err)
}, pollDuration, pollInterval)
}

func TestClusterExtensionInstallRegistry(t *testing.T) {
Expand All @@ -240,8 +270,8 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
t.Log("When a cluster extension is installed from a catalog")
t.Log("When the extension bundle format is registry+v1")

clusterExtension, extensionCatalog, sa := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa)
clusterExtension, extensionCatalog, sa, ns := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
defer getArtifactsOutput(t)

clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{
Expand All @@ -255,7 +285,7 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
},
},
Install: ocv1alpha1.ClusterExtensionInstallConfig{
Namespace: "default",
Namespace: ns.Name,
ServiceAccount: ocv1alpha1.ServiceAccountReference{
Name: sa.Name,
},
Expand Down Expand Up @@ -298,8 +328,8 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
func TestClusterExtensionInstallRegistryMultipleBundles(t *testing.T) {
t.Log("When a cluster extension is installed from a catalog")

clusterExtension, extensionCatalog, sa := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa)
clusterExtension, extensionCatalog, sa, ns := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
defer getArtifactsOutput(t)

clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{
Expand All @@ -310,7 +340,7 @@ func TestClusterExtensionInstallRegistryMultipleBundles(t *testing.T) {
},
},
Install: ocv1alpha1.ClusterExtensionInstallConfig{
Namespace: "default",
Namespace: ns.Name,
ServiceAccount: ocv1alpha1.ServiceAccountReference{
Name: sa.Name,
},
Expand Down Expand Up @@ -341,8 +371,8 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
t.Log("When a cluster extension is installed from a catalog")
t.Log("When resolving upgrade edges")

clusterExtension, extensionCatalog, sa := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa)
clusterExtension, extensionCatalog, sa, ns := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
defer getArtifactsOutput(t)

t.Log("By creating an ClusterExtension at a specified version")
Expand All @@ -356,7 +386,7 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
},
},
Install: ocv1alpha1.ClusterExtensionInstallConfig{
Namespace: "default",
Namespace: ns.Name,
ServiceAccount: ocv1alpha1.ServiceAccountReference{
Name: sa.Name,
},
Expand Down Expand Up @@ -406,8 +436,8 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
t.Log("When a cluster extension is installed from a catalog")
t.Log("When resolving upgrade edges")

clusterExtension, extensionCatalog, sa := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa)
clusterExtension, extensionCatalog, sa, ns := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
defer getArtifactsOutput(t)

t.Log("By creating an ClusterExtension at a specified version")
Expand All @@ -420,7 +450,7 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
},
},
Install: ocv1alpha1.ClusterExtensionInstallConfig{
Namespace: "default",
Namespace: ns.Name,
ServiceAccount: ocv1alpha1.ServiceAccountReference{
Name: sa.Name,
},
Expand Down Expand Up @@ -457,8 +487,8 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
t.Log("When a cluster extension is installed from a catalog")
t.Log("When resolving upgrade edges")
clusterExtension, extensionCatalog, sa := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa)
clusterExtension, extensionCatalog, sa, ns := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
defer getArtifactsOutput(t)

t.Log("By creating an ClusterExtension at a specified version")
Expand All @@ -471,7 +501,7 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
},
},
Install: ocv1alpha1.ClusterExtensionInstallConfig{
Namespace: "default",
Namespace: ns.Name,
ServiceAccount: ocv1alpha1.ServiceAccountReference{
Name: sa.Name,
},
Expand Down Expand Up @@ -507,8 +537,8 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched(t *testing.T) {
t.Log("When a cluster extension is installed from a catalog")
t.Log("It resolves again when a catalog is patched with new ImageRef")
clusterExtension, extensionCatalog, sa := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa)
clusterExtension, extensionCatalog, sa, ns := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
defer getArtifactsOutput(t)

clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{
Expand All @@ -528,7 +558,7 @@ func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched(t *testing.T) {
},
},
Install: ocv1alpha1.ClusterExtensionInstallConfig{
Namespace: "default",
Namespace: ns.Name,
ServiceAccount: ocv1alpha1.ServiceAccountReference{
Name: sa.Name,
},
Expand Down Expand Up @@ -593,9 +623,11 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
Name: clusterExtensionName,
},
}
sa, err := createServiceAccount(context.Background(), types.NamespacedName{Name: clusterExtensionName, Namespace: "default"}, clusterExtensionName)
ns, err := createNamespace(context.Background())
require.NoError(t, err)
sa, err := createServiceAccount(context.Background(), types.NamespacedName{Name: clusterExtensionName, Namespace: ns.Name}, clusterExtensionName)
require.NoError(t, err)
defer testCleanup(t, extensionCatalog, clusterExtension, sa)
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
defer getArtifactsOutput(t)

clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{
Expand All @@ -609,7 +641,7 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
},
},
Install: ocv1alpha1.ClusterExtensionInstallConfig{
Namespace: "default",
Namespace: ns.Name,
ServiceAccount: ocv1alpha1.ServiceAccountReference{
Name: sa.Name,
},
Expand Down Expand Up @@ -657,8 +689,8 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
func TestClusterExtensionInstallReResolvesWhenManagedContentChanged(t *testing.T) {
t.Log("When a cluster extension is installed from a catalog")
t.Log("It resolves again when managed content is changed")
clusterExtension, extensionCatalog, sa := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa)
clusterExtension, extensionCatalog, sa, ns := testInit(t)
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
defer getArtifactsOutput(t)

clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{
Expand All @@ -672,7 +704,7 @@ func TestClusterExtensionInstallReResolvesWhenManagedContentChanged(t *testing.T
},
},
Install: ocv1alpha1.ClusterExtensionInstallConfig{
Namespace: "default",
Namespace: ns.Name,
ServiceAccount: ocv1alpha1.ServiceAccountReference{
Name: sa.Name,
},
Expand Down Expand Up @@ -712,18 +744,18 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
t.Log("When a cluster extension is installed from a catalog")
t.Log("When the extension bundle format is registry+v1")

clusterExtension, extensionCatalog, _ := testInit(t)
clusterExtension, extensionCatalog, _, ns := testInit(t)

name := rand.String(10)
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "default",
Namespace: ns.Name,
},
}
err := c.Create(context.Background(), sa)
require.NoError(t, err)

defer testCleanup(t, extensionCatalog, clusterExtension, sa)
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
defer getArtifactsOutput(t)

clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{
Expand All @@ -737,7 +769,7 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
},
},
Install: ocv1alpha1.ClusterExtensionInstallConfig{
Namespace: "default",
Namespace: ns.Name,
ServiceAccount: ocv1alpha1.ServiceAccountReference{
Name: sa.Name,
},
Expand Down

0 comments on commit 422ccf5

Please sign in to comment.