|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "os/exec" |
| 23 | + "testing" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + |
| 28 | + "github.com/validator-labs/validator-plugin-maas/tests/utils" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + // Optional Environment Variables: |
| 33 | + // - PROMETHEUS_INSTALL_SKIP=true: Skips Prometheus Operator installation during test setup. |
| 34 | + // - CERT_MANAGER_INSTALL_SKIP=true: Skips CertManager installation during test setup. |
| 35 | + // These variables are useful if Prometheus or CertManager is already installed, avoiding |
| 36 | + // re-installation and conflicts. |
| 37 | + skipPrometheusInstall = os.Getenv("PROMETHEUS_INSTALL_SKIP") == "true" |
| 38 | + skipCertManagerInstall = os.Getenv("CERT_MANAGER_INSTALL_SKIP") == "true" |
| 39 | + // isPrometheusOperatorAlreadyInstalled will be set true when prometheus CRDs be found on the cluster |
| 40 | + isPrometheusOperatorAlreadyInstalled = false |
| 41 | + // isCertManagerAlreadyInstalled will be set true when CertManager CRDs be found on the cluster |
| 42 | + isCertManagerAlreadyInstalled = false |
| 43 | + |
| 44 | + // projectImage is the name of the image which will be build and loaded |
| 45 | + // with the code source changes to be tested. |
| 46 | + projectImage = "quay.io/validator-labs/validator-plugin-maas:latest" |
| 47 | +) |
| 48 | + |
| 49 | +// TestE2E runs the end-to-end (e2e) test suite for the project. These tests execute in an isolated, |
| 50 | +// temporary environment to validate project changes with the the purposed to be used in CI jobs. |
| 51 | +// The default setup requires Kind, builds/loads the Manager Docker image locally, and installs |
| 52 | +// CertManager and Prometheus. |
| 53 | +func TestE2E(t *testing.T) { |
| 54 | + RegisterFailHandler(Fail) |
| 55 | + _, _ = fmt.Fprintf(GinkgoWriter, "Starting validator-plugin-maas integration test suite\n") |
| 56 | + RunSpecs(t, "e2e suite") |
| 57 | +} |
| 58 | + |
| 59 | +var _ = BeforeSuite(func() { |
| 60 | + By("Ensure that Prometheus is enabled") |
| 61 | + _ = utils.UncommentCode("config/default/kustomization.yaml", "#- ../prometheus", "#") |
| 62 | + |
| 63 | + By("generating files") |
| 64 | + cmd := exec.Command("make", "generate") |
| 65 | + _, err := utils.Run(cmd) |
| 66 | + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate") |
| 67 | + |
| 68 | + By("generating manifests") |
| 69 | + cmd = exec.Command("make", "manifests") |
| 70 | + _, err = utils.Run(cmd) |
| 71 | + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests") |
| 72 | + |
| 73 | + By("building the manager(Operator) image") |
| 74 | + cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage)) |
| 75 | + _, err = utils.Run(cmd) |
| 76 | + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image") |
| 77 | + |
| 78 | + // TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is |
| 79 | + // built and available before running the tests. Also, remove the following block. |
| 80 | + By("loading the manager(Operator) image on Kind") |
| 81 | + err = utils.LoadImageToKindClusterWithName(projectImage) |
| 82 | + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to load the manager(Operator) image into Kind") |
| 83 | + |
| 84 | + // The tests-e2e are intended to run on a temporary cluster that is created and destroyed for testing. |
| 85 | + // To prevent errors when tests run in environments with Prometheus or CertManager already installed, |
| 86 | + // we check for their presence before execution. |
| 87 | + // Setup Prometheus and CertManager before the suite if not skipped and if not already installed |
| 88 | + if !skipPrometheusInstall { |
| 89 | + By("checking if prometheus is installed already") |
| 90 | + isPrometheusOperatorAlreadyInstalled = utils.IsPrometheusCRDsInstalled() |
| 91 | + if !isPrometheusOperatorAlreadyInstalled { |
| 92 | + _, _ = fmt.Fprintf(GinkgoWriter, "Installing Prometheus Operator...\n") |
| 93 | + Expect(utils.InstallPrometheusOperator()).To(Succeed(), "Failed to install Prometheus Operator") |
| 94 | + } else { |
| 95 | + _, _ = fmt.Fprintf(GinkgoWriter, "WARNING: Prometheus Operator is already installed. Skipping installation...\n") |
| 96 | + } |
| 97 | + } |
| 98 | + if !skipCertManagerInstall { |
| 99 | + By("checking if cert manager is installed already") |
| 100 | + isCertManagerAlreadyInstalled = utils.IsCertManagerCRDsInstalled() |
| 101 | + if !isCertManagerAlreadyInstalled { |
| 102 | + _, _ = fmt.Fprintf(GinkgoWriter, "Installing CertManager...\n") |
| 103 | + Expect(utils.InstallCertManager()).To(Succeed(), "Failed to install CertManager") |
| 104 | + } else { |
| 105 | + _, _ = fmt.Fprintf(GinkgoWriter, "WARNING: CertManager is already installed. Skipping installation...\n") |
| 106 | + } |
| 107 | + } |
| 108 | +}) |
| 109 | + |
| 110 | +var _ = AfterSuite(func() { |
| 111 | + // Teardown Prometheus and CertManager after the suite if not skipped and if they were not already installed |
| 112 | + if !skipPrometheusInstall && !isPrometheusOperatorAlreadyInstalled { |
| 113 | + _, _ = fmt.Fprintf(GinkgoWriter, "Uninstalling Prometheus Operator...\n") |
| 114 | + utils.UninstallPrometheusOperator() |
| 115 | + } |
| 116 | + if !skipCertManagerInstall && !isCertManagerAlreadyInstalled { |
| 117 | + _, _ = fmt.Fprintf(GinkgoWriter, "Uninstalling CertManager...\n") |
| 118 | + utils.UninstallCertManager() |
| 119 | + } |
| 120 | +}) |
0 commit comments