-
Notifications
You must be signed in to change notification settings - Fork 22
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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. | ||||||||||
|
@@ -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" | ||||||||||
) | ||||||||||
|
||||||||||
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) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We expect users to run the e2e test from the root directory.
Suggested change
|
||||||||||
|
||||||||||
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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ahrtr, I did manually test using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, why the existing e2e test work without this? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||
|
||||||||||
// 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)) | ||||||||||
} |
There was a problem hiding this comment.
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
ortest_utils
?There was a problem hiding this comment.
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