From 422ccf5adde31d16624f8eff943d5889cb4006cb Mon Sep 17 00:00:00 2001 From: Todd Short Date: Wed, 6 Nov 2024 12:58:03 -0500 Subject: [PATCH] Use a separate namespace for each e2e test Fixes #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 --- test/e2e/cluster_extension_install_test.go | 98 ++++++++++++++-------- 1 file changed, 65 insertions(+), 33 deletions(-) diff --git a/test/e2e/cluster_extension_install_test.go b/test/e2e/cluster_extension_install_test.go index d03aacf5e..89b90613c 100644 --- a/test/e2e/cluster_extension_install_test.go +++ b/test/e2e/cluster_extension_install_test.go @@ -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{ @@ -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) @@ -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) { @@ -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{ @@ -255,7 +285,7 @@ func TestClusterExtensionInstallRegistry(t *testing.T) { }, }, Install: ocv1alpha1.ClusterExtensionInstallConfig{ - Namespace: "default", + Namespace: ns.Name, ServiceAccount: ocv1alpha1.ServiceAccountReference{ Name: sa.Name, }, @@ -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{ @@ -310,7 +340,7 @@ func TestClusterExtensionInstallRegistryMultipleBundles(t *testing.T) { }, }, Install: ocv1alpha1.ClusterExtensionInstallConfig{ - Namespace: "default", + Namespace: ns.Name, ServiceAccount: ocv1alpha1.ServiceAccountReference{ Name: sa.Name, }, @@ -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") @@ -356,7 +386,7 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) { }, }, Install: ocv1alpha1.ClusterExtensionInstallConfig{ - Namespace: "default", + Namespace: ns.Name, ServiceAccount: ocv1alpha1.ServiceAccountReference{ Name: sa.Name, }, @@ -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") @@ -420,7 +450,7 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) { }, }, Install: ocv1alpha1.ClusterExtensionInstallConfig{ - Namespace: "default", + Namespace: ns.Name, ServiceAccount: ocv1alpha1.ServiceAccountReference{ Name: sa.Name, }, @@ -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") @@ -471,7 +501,7 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) { }, }, Install: ocv1alpha1.ClusterExtensionInstallConfig{ - Namespace: "default", + Namespace: ns.Name, ServiceAccount: ocv1alpha1.ServiceAccountReference{ Name: sa.Name, }, @@ -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{ @@ -528,7 +558,7 @@ func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched(t *testing.T) { }, }, Install: ocv1alpha1.ClusterExtensionInstallConfig{ - Namespace: "default", + Namespace: ns.Name, ServiceAccount: ocv1alpha1.ServiceAccountReference{ Name: sa.Name, }, @@ -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{ @@ -609,7 +641,7 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) { }, }, Install: ocv1alpha1.ClusterExtensionInstallConfig{ - Namespace: "default", + Namespace: ns.Name, ServiceAccount: ocv1alpha1.ServiceAccountReference{ Name: sa.Name, }, @@ -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{ @@ -672,7 +704,7 @@ func TestClusterExtensionInstallReResolvesWhenManagedContentChanged(t *testing.T }, }, Install: ocv1alpha1.ClusterExtensionInstallConfig{ - Namespace: "default", + Namespace: ns.Name, ServiceAccount: ocv1alpha1.ServiceAccountReference{ Name: sa.Name, }, @@ -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{ @@ -737,7 +769,7 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes }, }, Install: ocv1alpha1.ClusterExtensionInstallConfig{ - Namespace: "default", + Namespace: ns.Name, ServiceAccount: ocv1alpha1.ServiceAccountReference{ Name: sa.Name, },