Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert existing e2e tests from Gingko to e2e framework #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
10 changes: 1 addition & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,7 @@ test: manifests generate fmt vet envtest ## Run tests.
# - CERT_MANAGER_INSTALL_SKIP=true
.PHONY: test-e2e
test-e2e: manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind.
@command -v kind >/dev/null 2>&1 || { \
echo "Kind is not installed. Please install Kind manually."; \
exit 1; \
}
@kind get clusters | grep -q 'kind' || { \
echo "No Kind cluster is running. Please start a Kind cluster before running the e2e tests."; \
exit 1; \
}
go test ./test/e2e/ -v -ginkgo.v
go test ./test/e2e/

.PHONY: lint
lint: golangci-lint ## Run golangci-lint linter
Expand Down
2 changes: 1 addition & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ spec:
args:
- --leader-elect
- --health-probe-bind-address=:8081
image: controller:latest
image: etcd-operator-controller:current
name: manager
securityContext:
allowPrivilegeEscalation: false
Expand Down
325 changes: 234 additions & 91 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -17,104 +16,248 @@ limitations under the License.
package e2e

import (
"context"
"fmt"
"log"
"os"
"os/exec"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
"sigs.k8s.io/e2e-framework/support/kind"
"sigs.k8s.io/e2e-framework/support/utils"

"go.etcd.io/etcd-operator/test/utils"
kubebuilder_utils "go.etcd.io/etcd-operator/test/utils"

Choose a reason for hiding this comment

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

Does it make more sense to name it as e2e_utils or test_utils?

Copy link
Author

Choose a reason for hiding this comment

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

I think test_utils would make sense. I'll change it up

)

var (
// Optional Environment Variables:
// - PROMETHEUS_INSTALL_SKIP=true: Skips Prometheus Operator installation during test setup.
// - CERT_MANAGER_INSTALL_SKIP=true: Skips CertManager installation during test setup.
// These variables are useful if Prometheus or CertManager is already installed, avoiding
// re-installation and conflicts.
skipPrometheusInstall = os.Getenv("PROMETHEUS_INSTALL_SKIP") == "true"
skipCertManagerInstall = os.Getenv("CERT_MANAGER_INSTALL_SKIP") == "true"
// isPrometheusOperatorAlreadyInstalled will be set true when prometheus CRDs be found on the cluster
isPrometheusOperatorAlreadyInstalled = false
// isCertManagerAlreadyInstalled will be set true when CertManager CRDs be found on the cluster
isCertManagerAlreadyInstalled = false

// projectImage is the name of the image which will be build and loaded
// with the code source changes to be tested.
projectImage = "example.com/etcd-operator:v0.0.1"
testEnv env.Environment
dockerImage = "etcd-operator-controller:current"
)

// TestE2E runs the end-to-end (e2e) test suite for the project. These tests execute in an isolated,
// temporary environment to validate project changes with the the purposed to be used in CI jobs.
// The default setup requires Kind, builds/loads the Manager Docker image locally, and installs
// CertManager and Prometheus.
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
_, _ = fmt.Fprintf(GinkgoWriter, "Starting etcd-operator integration test suite\n")
RunSpecs(t, "e2e suite")
}
func TestMain(m *testing.M) {
testEnv = env.New()
kindClusterName := "etcd-cluster"
kindCluster := kind.NewCluster(kindClusterName)

Choose a reason for hiding this comment

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

Should we keep the option for which version of Kubernetes the kind cluster should spin up with? Default can use the latest available.
Ref: https://github.com/kubernetes-sigs/e2e-framework/blob/main/support/kind/kind.go#L28

Copy link
Author

Choose a reason for hiding this comment

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

I don't think spinning a cluster with the latest k8s version should be a problem?


log.Println("Creating KinD cluster...")
origWd, _ := os.Getwd()
testEnv.Setup(
// setup up environment
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
// create KinD cluster
var err error
ctx, err = envfuncs.CreateCluster(kindCluster, kindClusterName)(ctx, cfg)
if err != nil {
log.Printf("failed to create cluster: %s", err)
return ctx, err
}

return ctx, nil
},

// prepare the resources
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
// change dir for Make file
if err := os.Chdir("../../"); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}
Comment on lines +66 to +69
Copy link
Member

Choose a reason for hiding this comment

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

We expect users to run the e2e test from the root directory. go test ./test/e2e/

Suggested change
if err := os.Chdir("../../"); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}


log.Println("Installing bin tools...")
if p := utils.RunCommand(
`make kustomize`,
); p.Err() != nil {
log.Printf("Failed to install kustomize binary: %s: %s", p.Err(), p.Out())
return ctx, p.Err()
}
if p := utils.RunCommand(
`make controller-gen`,
); p.Err() != nil {
log.Printf("Failed to install controller-gen binary: %s: %s", p.Err(), p.Out())
return ctx, p.Err()
}

// gen manifest files
log.Println("Generate manifests...")
if p := utils.RunCommand(
`make manifests`,
); p.Err() != nil {
log.Printf("Failed to generate manifests: %s: %s", p.Err(), p.Out())
return ctx, p.Err()
}

// install crd
log.Println("Install crd...")
if p := utils.RunCommand(
`make install`,
); p.Err() != nil {
log.Printf("Failed to generate manifests: %s: %s", p.Err(), p.Out())
return ctx, p.Err()
}

// Build docker image
log.Println("Building docker image...")
if p := utils.RunCommand(fmt.Sprintf("docker build -t %s .", dockerImage)); p.Err() != nil {
log.Printf("Failed to build docker image: %s: %s", p.Err(), p.Out())
return ctx, p.Err()
}

// Load docker image into kind
log.Println("Loading docker image into kind cluster...")
if err := kindCluster.LoadImage(ctx, dockerImage); err != nil {
log.Printf("Failed to load image into kind: %s", err)
return ctx, err
}

// set working directory test/e2e
if err := os.Chdir(origWd); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}

return ctx, nil
},

// install prometheus and cert-manager
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
log.Println("Installing prometheus operator...")
if err := kubebuilder_utils.InstallPrometheusOperator(); err != nil {
log.Printf("Unable to install Prometheus operator: %s", err)
}

log.Println("Installing cert-manager...")
if err := kubebuilder_utils.InstallCertManager(); err != nil {
log.Printf("Unable to install Cert Manager: %s", err)
}

// set working directory test/e2e
if err := os.Chdir(origWd); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}

return ctx, nil
},

// set up environment
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// set up environment
// create namespace and deploy the etcd-operator

func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
abdurrehman107 marked this conversation as resolved.
Show resolved Hide resolved
// change working directory for Make file
if err := os.Chdir("../../"); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}
Comment on lines +150 to +153
Copy link
Member

Choose a reason for hiding this comment

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

We expect users to run the e2e test from the root directory. go test ./test/e2e/

Suggested change
if err := os.Chdir("../../"); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}

Copy link
Author

Choose a reason for hiding this comment

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

@ahrtr, I did manually test using go test ./test/e2e from the root directory, the tests fail if I do not move out of the directory. The go test ./test/e2e/ makes them start from the directory test/e2e.

Copy link
Member

Choose a reason for hiding this comment

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

Again, why the existing e2e test work without this?

cc @ArkaSaha30 @hakman

Copy link
Author

@abdurrehman107 abdurrehman107 Feb 11, 2025

Choose a reason for hiding this comment

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

The existing e2e test works because it uses the Run() function mentioned in the utils file. I don't use that specific function because it uses Gingko. I've used the RunCommand() function given by the e2e-framework. Since we wish to completely remove the use of Gingko, I suggest we use this. Below is the function used in the existing tests:

func Run(cmd *exec.Cmd) (string, error) {
	dir, _ := GetProjectDir()
	cmd.Dir = dir

	if err := os.Chdir(cmd.Dir); err != nil {
		_, _ = fmt.Fprintf(GinkgoWriter, "chdir dir: %s\n", err)
	}

	cmd.Env = append(os.Environ(), "GO111MODULE=on")
	command := strings.Join(cmd.Args, " ")
	_, _ = fmt.Fprintf(GinkgoWriter, "running: %s\n", command)
	output, err := cmd.CombinedOutput()
	if err != nil {
		return string(output), fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
	}

	return string(output), nil
}


// Deploy components
log.Println("Deploying components...")
log.Println("Deploying controller-manager resources...")
if p := utils.RunCommand(
`make deploy`,
); p.Err() != nil {
log.Printf("Failed to deploy resource configurations: %s: %s", p.Err(), p.Out())
return ctx, p.Err()
}

var _ = BeforeSuite(func() {
By("Ensure that Prometheus is enabled")
_ = utils.UncommentCode("config/default/kustomization.yaml", "#- ../prometheus", "#")

By("generating files")
cmd := exec.Command("make", "generate")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate")

By("generating manifests")
cmd = exec.Command("make", "manifests")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests")

By("building the manager(Operator) image")
cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, 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)
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 Prometheus or CertManager already installed,
// we check for their presence before execution.
// Setup Prometheus and CertManager before the suite if not skipped and if not already installed
if !skipPrometheusInstall {
By("checking if prometheus is installed already")
isPrometheusOperatorAlreadyInstalled = utils.IsPrometheusCRDsInstalled()
if !isPrometheusOperatorAlreadyInstalled {
_, _ = fmt.Fprintf(GinkgoWriter, "Installing Prometheus Operator...\n")
Expect(utils.InstallPrometheusOperator()).To(Succeed(), "Failed to install Prometheus Operator")
} else {
_, _ = fmt.Fprintf(GinkgoWriter, "WARNING: Prometheus Operator is already installed. Skipping installation...\n")
}
}
if !skipCertManagerInstall {
By("checking if cert manager is installed already")
isCertManagerAlreadyInstalled = utils.IsCertManagerCRDsInstalled()
if !isCertManagerAlreadyInstalled {
_, _ = fmt.Fprintf(GinkgoWriter, "Installing CertManager...\n")
Expect(utils.InstallCertManager()).To(Succeed(), "Failed to install CertManager")
} else {
_, _ = fmt.Fprintf(GinkgoWriter, "WARNING: CertManager is already installed. Skipping installation...\n")
}
}
})

var _ = AfterSuite(func() {
// Teardown Prometheus and CertManager after the suite if not skipped and if they were not already installed
if !skipPrometheusInstall && !isPrometheusOperatorAlreadyInstalled {
_, _ = fmt.Fprintf(GinkgoWriter, "Uninstalling Prometheus Operator...\n")
utils.UninstallPrometheusOperator()
}
if !skipCertManagerInstall && !isCertManagerAlreadyInstalled {
_, _ = fmt.Fprintf(GinkgoWriter, "Uninstalling CertManager...\n")
utils.UninstallCertManager()
}
})
// wait for controller to get ready
log.Println("Waiting for controller-manager deployment to be available...")
client := cfg.Client()
if err := wait.For(
conditions.New(client.Resources()).DeploymentAvailable("etcd-operator-controller-manager", "etcd-operator-system"),
wait.WithTimeout(3*time.Minute),
wait.WithInterval(10*time.Second),
); err != nil {
log.Printf("Timed out while waiting for etcd-operator-controller-manager deployment: %s", err)
return ctx, err
}
// set working directory test/e2e
if err := os.Chdir(origWd); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}

return ctx, nil
},
)

// Use the Environment.Finish method to define clean up steps
testEnv.Finish(
abdurrehman107 marked this conversation as resolved.
Show resolved Hide resolved
// cleanup environment
func(ctx context.Context, c *envconf.Config) (context.Context, error) {
log.Println("Finishing tests, cleaning cluster ...")

// change working directory for Make file
if err := os.Chdir("../../"); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}

// uninstall crd
log.Println("Uninstalling crd...")
if p := utils.RunCommand(
`make uninstall ignore-not-found=true`,
); p.Err() != nil {
log.Printf("Warning: Failed to uninstall crd: %s: %s", p.Err(), p.Out())
}

// undeploy etcd operator
log.Println("Undeploy etcd controller...")
if p := utils.RunCommand(
`make undeploy ignore-not-found=true`,
); p.Err() != nil {
log.Printf("Warning: Failed to undeploy controller: %s: %s", p.Err(), p.Out())
}

// set working directory test/e2e
if err := os.Chdir(origWd); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}

return ctx, nil
},
// remove the installed dependencies
func(ctx context.Context, c *envconf.Config) (context.Context, error) {
// change working directory for Make file
if err := os.Chdir("../../"); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}

log.Println("Removing dependencies...")

// remove prometheus
kubebuilder_utils.UninstallPrometheusOperator()

// remove cert-manager
kubebuilder_utils.UninstallCertManager()

// set working directory test/e2e
if err := os.Chdir(origWd); err != nil {
log.Printf("Unable to set working directory: %s", err)
return ctx, err
}

return ctx, nil
},

// Destroy environment
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
log.Println("Destroying cluster...")

var err error
ctx, err = envfuncs.DestroyCluster(kindClusterName)(ctx, cfg)
if err != nil {
log.Printf("failed to delete cluster: %s", err)
}

return ctx, nil
},
)

// Use Environment.Run to launch the test
os.Exit(testEnv.Run(m))
}
Loading
Loading