Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ var (

// imagePrefix is the name of the image which will be build and loaded
// with the code source changes to be tested.
imagePrefix = "controller"
imagePrefix = utils.GetEnvOrDefault("IMG_PREFIX", "controller")
// imageTag is the tag of the image which will be build and loaded
// with the code source changes to be tested.
imageTag = "latest"
imageTag = utils.GetEnvOrDefault("IMG_TAG", "latest")
// skipBuild will skip the docker-build and kind load steps if set to true.
skipBuild = os.Getenv("SKIP_RESOURCE_BUILD") == "true"
)

// TestE2E runs the end-to-end (e2e) test suite for the project. These tests execute in an isolated,
Expand All @@ -59,17 +61,19 @@ func TestE2E(t *testing.T) {
}

var _ = BeforeSuite(func() {
By("building the manager(Operator) image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG_PREFIX=%s", imagePrefix), fmt.Sprintf("IMG_TAG=%s", imageTag))
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")
if !skipBuild {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason why we want to skip the build? am assuming this is for the case where image is already built and loaded in cluster, mainly when running e2e in local env

By("building the manager(Operator) image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG_PREFIX=%s", imagePrefix), fmt.Sprintf("IMG_TAG=%s", imageTag))
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")

// TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is
// built and available before running the tests. Also, remove the following block.
By("loading the manager(Operator) image on Kind")
//err = utils.LoadImageToKindClusterWithName(projectImage)
err = utils.LoadImageToKindClusterWithName(fmt.Sprintf("%s:%s", imagePrefix, imageTag))
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to load the manager(Operator) image into Kind")
// TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is
// built and available before running the tests. Also, remove the following block.
By("loading the manager(Operator) image on Kind")
//err = utils.LoadImageToKindClusterWithName(projectImage)
err = utils.LoadImageToKindClusterWithName(fmt.Sprintf("%s:%s", imagePrefix, imageTag))
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to load the manager(Operator) image into Kind")
}

// The tests-e2e are intended to run on a temporary cluster that is created and destroyed for testing.
// To prevent errors when tests run in environments with CertManager already installed,
Expand Down
56 changes: 41 additions & 15 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var _ = Describe("Manager", Ordered, func() {
// enforce the restricted security policy to the namespace, installing CRDs,
// and deploying the controller.
BeforeAll(func() {
useReleaseArtifacts := os.Getenv("USE_RELEASE_ARTIFACTS") == "true"

By("creating manager namespace")
cmd := exec.Command("kubectl", "create", "ns", namespace)
_, err := utils.Run(cmd)
Expand All @@ -64,33 +66,57 @@ var _ = Describe("Manager", Ordered, func() {
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred(), "Failed to label namespace with restricted policy")

By("installing CRDs")
cmd = exec.Command("make", "install")
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred(), "Failed to install CRDs")
if useReleaseArtifacts {
By("installing CRDs from dist/crds.yaml")
cmd = exec.Command("kubectl", "apply", "-f", "dist/crds.yaml")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would mean that dist/crds.yaml already exists. Should we add a check here or some logic to ensure it is always there when running e2e-tests?

_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred(), "Failed to install CRDs from dist/")

By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG_PREFIX=%s", imagePrefix), fmt.Sprintf("IMG_TAG=%s", imageTag))
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
By("deploying the controller-manager from dist/install.yaml")
cmd = exec.Command("kubectl", "apply", "-f", "dist/install.yaml")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, this needs install.yaml

_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred(), "Failed to deploy from dist/")
} else {
By("installing CRDs")
cmd = exec.Command("make", "install")
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred(), "Failed to install CRDs")

By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG_PREFIX=%s", imagePrefix), fmt.Sprintf("IMG_TAG=%s", imageTag))
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
}

Expect(err).NotTo(HaveOccurred(), "Failed to patch deployment")
})

// After all tests have been executed, clean up by undeploying the controller, uninstalling CRDs,
// and deleting the namespace.
AfterAll(func() {
useReleaseArtifacts := os.Getenv("USE_RELEASE_ARTIFACTS") == "true"

By("cleaning up the curl pod for metrics")
cmd := exec.Command("kubectl", "delete", "pod", "curl-metrics", "-n", namespace)
_, _ = utils.Run(cmd)

By("undeploying the controller-manager")
cmd = exec.Command("make", "undeploy")
_, _ = utils.Run(cmd)

By("uninstalling CRDs")
cmd = exec.Command("make", "uninstall")
_, _ = utils.Run(cmd)
if useReleaseArtifacts {
By("undeploying the controller-manager from dist/install.yaml")
cmd = exec.Command("kubectl", "delete", "-f", "dist/install.yaml", "--ignore-not-found")
_, _ = utils.Run(cmd)

By("uninstalling CRDs from dist/crds.yaml")
cmd = exec.Command("kubectl", "delete", "-f", "dist/crds.yaml", "--ignore-not-found")
_, _ = utils.Run(cmd)
} else {
By("undeploying the controller-manager")
cmd = exec.Command("make", "undeploy")
_, _ = utils.Run(cmd)

By("uninstalling CRDs")
cmd = exec.Command("make", "uninstall")
_, _ = utils.Run(cmd)
}

By("removing manager namespace")
cmd = exec.Command("kubectl", "delete", "ns", namespace)
Expand Down
9 changes: 9 additions & 0 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ func LoadImageToKindClusterWithName(name string) error {
return err
}

// GetEnvOrDefault returns the value of the environment variable if it exists,
// otherwise it returns the provided default value.
func GetEnvOrDefault(key, defaultValue string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}
return defaultValue
}

// GetNonEmptyLines converts given command output string into individual objects
// according to line breakers, and ignores the empty elements in it.
func GetNonEmptyLines(output string) []string {
Expand Down