Repository: rancher/fleet
Pull request #1975 (2023-11-28 12:52:18)

Switch bundle diff test to use random target namespaces
The test was flaky, probably since both specs used the same namespace. The specs tried to work around that by waiting, but that's probably not effective.

The bundle-diffs.yaml was used in another test and was cloned, so that test doesn't have to change.


Patch:
diff --git a/.github/workflows/fleet-upgrade.yml b/.github/workflows/fleet-upgrade.yml
index 58fa9c92..06437c8b 100644
--- a/.github/workflows/fleet-upgrade.yml
+++ b/.github/workflows/fleet-upgrade.yml
@@ -62,7 +62,7 @@ jobs:
       -
         name: Create example workload
         run: |
-          kubectl apply -n fleet-local -f e2e/assets/single-cluster/bundle-diffs.yaml
+          kubectl apply -n fleet-local -f e2e/assets/installation/bundle-diffs.yaml
       -
         name: Build Fleet Binaries
         run: |


Test patch:
diff --git a/e2e/assets/fleet-upgrade/gitrepo-simple.yaml b/e2e/assets/fleet-upgrade/gitrepo-simple.yaml
index fa2bac06..9bc4534a 100644
--- a/e2e/assets/fleet-upgrade/gitrepo-simple.yaml
+++ b/e2e/assets/fleet-upgrade/gitrepo-simple.yaml
@@ -3,8 +3,8 @@ apiVersion: fleet.cattle.io/v1alpha1
 metadata:
   name: test-simple
 spec:
-  repo: https://github.com/rancher/fleet-examples
-  branch: test-simple
+  repo: https://github.com/rancher/fleet-test-data
+  branch: master
   paths:
     - simple-chart
     - simple-manifest
diff --git a/e2e/assets/installation/bundle-diffs.yaml b/e2e/assets/installation/bundle-diffs.yaml
new file mode 100644
index 00000000..6c683557
--- /dev/null
+++ b/e2e/assets/installation/bundle-diffs.yaml
@@ -0,0 +1,9 @@
+kind: GitRepo
+apiVersion: fleet.cattle.io/v1alpha1
+metadata:
+  name: bundle-diffs-test
+spec:
+  repo: https://github.com/rancher/fleet-test-data
+  branch: master
+  paths:
+  - bundle-diffs
diff --git a/e2e/assets/single-cluster/bundle-diffs.yaml b/e2e/assets/single-cluster/bundle-diffs.yaml
index 6c683557..c3239bd4 100644
--- a/e2e/assets/single-cluster/bundle-diffs.yaml
+++ b/e2e/assets/single-cluster/bundle-diffs.yaml
@@ -1,8 +1,9 @@
 kind: GitRepo
 apiVersion: fleet.cattle.io/v1alpha1
 metadata:
-  name: bundle-diffs-test
+  name: {{.Name}}
 spec:
+  targetNamespace: {{.TargetNamespace}}
   repo: https://github.com/rancher/fleet-test-data
   branch: master
   paths:
diff --git a/e2e/single-cluster/bundle_diffs_test.go b/e2e/single-cluster/bundle_diffs_test.go
index ec29eb19..9a3b25f0 100644
--- a/e2e/single-cluster/bundle_diffs_test.go
+++ b/e2e/single-cluster/bundle_diffs_test.go
@@ -2,10 +2,13 @@ package singlecluster_test
 
 import (
 	"encoding/json"
+	"os"
+	"path"
 	"time"
 
 	"github.com/rancher/fleet/e2e/testenv"
 	"github.com/rancher/fleet/e2e/testenv/kubectl"
+	"github.com/rancher/fleet/integrationtests/utils"
 	"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
 
 	apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -16,7 +19,11 @@ import (
 
 var _ = Describe("BundleDiffs", func() {
 	var (
-		asset    string
+		asset           string
+		tmpdir          string
+		name            string
+		targetNamespace string
+
 		k        kubectl.Command
 		interval = 2 * time.Second
 		duration = 30 * time.Second
@@ -43,20 +50,40 @@ var _ = Describe("BundleDiffs", func() {
 
 	BeforeEach(func() {
 		k = env.Kubectl.Namespace(env.Namespace)
+
+		var err error
+		targetNamespace, err = utils.NewNamespaceName()
+		Expect(err).ToNot(HaveOccurred())
+		name = "bundle-diff-" + targetNamespace
 	})
 
 	JustBeforeEach(func() {
-		out, err := k.Apply("-f", testenv.AssetPath(asset))
+		tmpdir, _ = os.MkdirTemp("", "fleet-")
+		gitrepo := path.Join(tmpdir, "gitrepo.yaml")
+		err := testenv.Template(gitrepo, testenv.AssetPath(asset), struct {
+			Name            string
+			TargetNamespace string
+		}{
+			name,
+			targetNamespace,
+		})
+		Expect(err).ToNot(HaveOccurred())
+
+		out, err := k.Apply("-f", gitrepo)
 		Expect(err).ToNot(HaveOccurred(), out)
 
 		DeferCleanup(func() {
-			out, err := k.Delete("-f", testenv.AssetPath(asset))
+			out, err := k.Delete("-f", gitrepo)
 			Expect(err).ToNot(HaveOccurred(), out)
 
+			os.RemoveAll(tmpdir)
+
+			_, _ = k.Delete("namespace", targetNamespace)
+
 			// test cases use the same namespace, so we have to
 			// make sure resources are cleaned up
 			Eventually(func() bool {
-				_, err := bundleDeploymentStatus("bundle-diffs-test")
+				_, err := bundleDeploymentStatus(name)
 				if err != nil && apierrors.IsNotFound(err) {
 					return true
 				}
@@ -74,7 +101,7 @@ var _ = Describe("BundleDiffs", func() {
 		JustBeforeEach(func() {
 			By("waiting for resources to be ready", func() {
 				Eventually(func() bool {
-					status, err := bundleDeploymentStatus("bundle-diffs-test")
+					status, err := bundleDeploymentStatus(name)
 					if err != nil || status == nil {
 						return false
 					}
@@ -82,7 +109,7 @@ var _ = Describe("BundleDiffs", func() {
 				}).Should(BeTrue())
 
 				Eventually(func() string {
-					out, _ := k.Namespace("bundle-diffs-example").Get("services")
+					out, _ := k.Namespace(targetNamespace).Get("services")
 
 					return out
 				}).Should(ContainSubstring("app-service"))
@@ -92,7 +119,7 @@ var _ = Describe("BundleDiffs", func() {
 		Context("modifying a ignored resource", func() {
 			JustBeforeEach(func() {
 				By("modifying the workload resources", func() {
-					kw := k.Namespace("bundle-diffs-example")
+					kw := k.Namespace(targetNamespace)
 					out, err := kw.Patch(
 						"service", "app-service",
 						"--type=json",
@@ -111,7 +138,7 @@ var _ = Describe("BundleDiffs", func() {
 
 			It("ignores changes", func() {
 				Consistently(func() bool {
-					status, err := bundleDeploymentStatus("bundle-diffs-test")
+					status, err := bundleDeploymentStatus(name)
 					if err != nil || status == nil {
 						return false
 					}
@@ -124,7 +151,7 @@ var _ = Describe("BundleDiffs", func() {
 		Context("modifying a monitored resource", func() {
 			JustBeforeEach(func() {
 				By("modifying a monitored value", func() {
-					kw := k.Namespace("bundle-diffs-example")
+					kw := k.Namespace(targetNamespace)
 					out, err := kw.Patch(
 						"service", "app-service",
 						"--type=json",
@@ -136,7 +163,7 @@ var _ = Describe("BundleDiffs", func() {
 
 			It("detects modifications", func() {
 				Eventually(func() bool {
-					status, err := bundleDeploymentStatus("bundle-diffs-test")
+					status, err := bundleDeploymentStatus(name)
 					if err != nil || status == nil {
 						return false
 					}