Skip to content

Commit

Permalink
Add e2e tests for the deleteNamespace feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rubhanazeem committed Apr 30, 2024
1 parent 732ebb4 commit d1c9796
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
18 changes: 18 additions & 0 deletions e2e/assets/single-cluster/delete-namespace/gitrepo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
kind: GitRepo
apiVersion: fleet.cattle.io/v1alpha1
metadata:
name: my-gitrepo
spec:
repo: https://github.com/rancher/fleet-test-data
branch: master
paths:
- helm-verify
targetNamespace: {{.TargetNamespace}}
deleteNamespace: {{.DeleteNamespace}}
targets:
- clusterSelector:
matchExpressions:
- key: provider.cattle.io
operator: NotIn
values:
- harvester
107 changes: 107 additions & 0 deletions e2e/single-cluster/delete_namespaces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package singlecluster_test

import (
"errors"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancher/fleet/e2e/testenv"
"github.com/rancher/fleet/e2e/testenv/kubectl"
)

var _ = Describe("delete namespaces", func() {
var (
k kubectl.Command
targetNamespace string
deleteNamespace bool
)

type TemplateData struct {
TargetNamespace string
DeleteNamespace bool
}

BeforeEach(func() {
k = env.Kubectl.Namespace(env.Namespace)
deleteNamespace = false

DeferCleanup(func() {
_, _ = k.Delete("ns", "my-custom-namespace", "--wait=false")
})
})

JustBeforeEach(func() {
err := testenv.ApplyTemplate(k, testenv.AssetPath("single-cluster/delete-namespace/gitrepo.yaml"),
TemplateData{targetNamespace, deleteNamespace})

Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
out, err := k.Namespace(targetNamespace).Get("configmaps")
if err != nil {
return err
}

if !strings.Contains(out, "app-config") {
return errors.New("expected configmap is not found")
}

return nil
}).ShouldNot(HaveOccurred())
})

When("delete namespaces is false", func() {
BeforeEach(func() {
targetNamespace = "my-custom-namespace"
})

It("targetNamespace is preserved when GitRepo is deleted", func() {
out, err := k.Delete("gitrepo", "my-gitrepo", "-n", "fleet-local")
Expect(err).ToNot(HaveOccurred(), out)

Eventually(func() string {
out, _ := k.Get("namespaces", targetNamespace)
return out
}).Should(ContainSubstring(targetNamespace))
})
})

When("delete namespaces is true", func() {
BeforeEach(func() {
deleteNamespace = true
})

It("targetNamespace is deleted after deleting gitRepo", func() {
_, err := k.Get("namespaces", targetNamespace)
Expect(err).To(BeNil())

out, err := k.Delete("gitrepo", "my-gitrepo", "-n", "fleet-local")
Expect(err).ToNot(HaveOccurred(), out)

Eventually(func() error {
_, err = k.Get("namespaces", targetNamespace)
return err
}).ShouldNot(BeNil())
})
})

When("delete namespaces is true but resources are deployed in default namespace", func() {
BeforeEach(func() {
deleteNamespace = true
targetNamespace = "default"
})

It("default namespace exists", func() {
out, err := k.Delete("gitrepo", "my-gitrepo", "-n", "fleet-local")
Expect(err).ToNot(HaveOccurred(), out)

Eventually(func() string {
out, _ = k.Namespace(targetNamespace).Get("configmap", "app-config", "-o", "yaml")
return out
}).Should(ContainSubstring("Error from server (NotFound)"))

_, err = k.Get("namespaces", targetNamespace)
Expect(err).ToNot(HaveOccurred())
})
})
})

0 comments on commit d1c9796

Please sign in to comment.