sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     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  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    21  )
    22  
    23  var _ machinery.Template = &SuiteTest{}
    24  
    25  type Test struct {
    26  	machinery.TemplateMixin
    27  	machinery.BoilerplateMixin
    28  	machinery.RepositoryMixin
    29  	machinery.ProjectNameMixin
    30  }
    31  
    32  func (f *Test) SetTemplateDefaults() error {
    33  	if f.Path == "" {
    34  		f.Path = "test/e2e/e2e_test.go"
    35  	}
    36  
    37  	f.TemplateBody = TestTemplate
    38  	return nil
    39  }
    40  
    41  var TestTemplate = `{{ .Boilerplate }}
    42  
    43  
    44  package e2e
    45  
    46  import (
    47  	"fmt"
    48  	"os/exec"
    49  	"time"
    50  
    51  	. "github.com/onsi/ginkgo/v2"
    52  	. "github.com/onsi/gomega"
    53  	
    54  	"{{ .Repo }}/test/utils"
    55  )
    56  
    57  const namespace = "{{ .ProjectName }}-system"
    58  
    59  var _ = Describe("controller", Ordered, func() {
    60  	BeforeAll(func() {
    61  		By("installing prometheus operator")
    62  		Expect(utils.InstallPrometheusOperator()).To(Succeed())
    63  
    64  		By("installing the cert-manager")
    65  		Expect(utils.InstallCertManager()).To(Succeed())
    66  
    67  		By("creating manager namespace")
    68  		cmd := exec.Command("kubectl", "create", "ns", namespace)
    69  		_, _ = utils.Run(cmd)
    70  	})
    71  
    72  	AfterAll(func() {
    73  		By("uninstalling the Prometheus manager bundle")
    74  		utils.UninstallPrometheusOperator()
    75  
    76  		By("uninstalling the cert-manager bundle")
    77  		utils.UninstallCertManager()
    78  
    79  		By("removing manager namespace")
    80  		cmd := exec.Command("kubectl", "delete", "ns", namespace)
    81  		_, _ = utils.Run(cmd)
    82  	})
    83  
    84  	Context("Operator", func() {
    85  		It("should run successfully", func() {
    86  			var controllerPodName string
    87  			var err error
    88  
    89  			// projectimage stores the name of the image used in the example
    90  			var projectimage = "example.com/{{ .ProjectName }}:v0.0.1"
    91  
    92  			By("building the manager(Operator) image")
    93  			cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectimage))
    94  			_, err = utils.Run(cmd)
    95  			ExpectWithOffset(1, err).NotTo(HaveOccurred())
    96  
    97  			By("loading the the manager(Operator) image on Kind")
    98  			err = utils.LoadImageToKindClusterWithName(projectimage)
    99  			ExpectWithOffset(1, err).NotTo(HaveOccurred())
   100  
   101  			By("installing CRDs")
   102  			cmd = exec.Command("make", "install")
   103  			_, err = utils.Run(cmd)
   104  
   105  			By("deploying the controller-manager")
   106  			cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectimage))
   107  			_, err = utils.Run(cmd)
   108  			ExpectWithOffset(1, err).NotTo(HaveOccurred())
   109  
   110  			By("validating that the controller-manager pod is running as expected")
   111  			verifyControllerUp := func() error {
   112  				// Get pod name
   113  				
   114  				cmd = exec.Command("kubectl", "get",
   115  					"pods", "-l", "control-plane=controller-manager",
   116  					"-o", "go-template={{"{{"}} range .items {{"}}"}}" +
   117  					"{{"{{"}} if not .metadata.deletionTimestamp {{"}}"}}" + 
   118  					"{{"{{"}} .metadata.name {{"}}"}}"+
   119  					"{{"{{"}} \"\\n\" {{"}}"}}{{"{{"}} end {{"}}"}}{{"{{"}} end {{"}}"}}",
   120  					"-n", namespace,
   121  				)
   122  
   123  				podOutput, err := utils.Run(cmd)
   124  				ExpectWithOffset(2, err).NotTo(HaveOccurred())
   125  				podNames := utils.GetNonEmptyLines(string(podOutput))
   126  				if len(podNames) != 1 {
   127  					return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames))
   128  				}
   129  				controllerPodName = podNames[0]
   130  				ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))
   131  
   132  				// Validate pod status
   133  				cmd = exec.Command("kubectl", "get",
   134  					"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
   135  					"-n", namespace,
   136  				)
   137  				status, err := utils.Run(cmd)
   138  				ExpectWithOffset(2, err).NotTo(HaveOccurred())
   139  				if string(status) != "Running" {
   140  					return fmt.Errorf("controller pod in %s status", status)
   141  				}
   142  				return nil
   143  			}
   144  			EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
   145  
   146  		})
   147  	})
   148  })
   149  `