Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Jan 22, 2024
1 parent 0e22981 commit 8477f64
Showing 1 changed file with 31 additions and 128 deletions.
159 changes: 31 additions & 128 deletions test/e2e/v4/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@ var _ = Describe("kubebuilder", func() {
" with restricted pods", func() {
kbc.IsRestricted = true
GenerateV4(kbc)
Run(kbc, true)
Run(kbc, true, false)
})
It("should generate a runnable project without webhooks"+
" with restricted pods", func() {
kbc.IsRestricted = true
GenerateV4WithoutWebhooks(kbc)
Run(kbc, false)
Run(kbc, false, false)
})
It("should generate a runnable project"+
" with the Installer", func() {
GenerateV4(kbc)
RunWithInstaller(kbc)
Run(kbc, false, true)
})
})
})
Expand All @@ -107,123 +107,8 @@ var _ = Describe("kubebuilder", func() {
// Also we can encapsulate the checks such as: checkManager, checkPrometheus, checkCertManager so that we can reuse them
// when or not is required and create many scenarios.

// RunWithInstaller runs a set of e2e tests for a scaffolded project defined by a TestContext.
func RunWithInstaller(kbc *utils.TestContext) {
var controllerPodName string
var err error

By("creating manager namespace")
err = kbc.CreateManagerNamespace()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("updating the go.mod")
err = kbc.Tidy()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("run make manifests")
err = kbc.Make("manifests")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("run make generate")
err = kbc.Make("generate")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("building the controller image")
err = kbc.Make("docker-build", "IMG="+kbc.ImageName)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("loading the controller docker image into the kind cluster")
err = kbc.LoadImageToKindCluster()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("building the installer")
err = kbc.Make("build-installer", "IMG="+kbc.ImageName)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
// --clusterrole=cluster-admin --user=myname@mycompany.com
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
By("deploying the controller-manager with the installer")

_, err = kbc.Kubectl.Apply(true, "-f", "dist/install.yaml")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get pod name
podOutput, err := kbc.Kubectl.Get(
true,
"pods", "-l", "control-plane=controller-manager",
"-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}"+
"{{ \"\\n\" }}{{ end }}{{ end }}")
ExpectWithOffset(2, err).NotTo(HaveOccurred())
podNames := util.GetNonEmptyLines(podOutput)
if len(podNames) != 1 {
return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames))
}
controllerPodName = podNames[0]
ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))

// Validate pod status
status, err := kbc.Kubectl.Get(
true,
"pods", controllerPodName, "-o", "jsonpath={.status.phase}")
ExpectWithOffset(2, err).NotTo(HaveOccurred())
if status != "Running" {
return fmt.Errorf("controller pod in %s status", status)
}
return nil
}
defer func() {
out, err := kbc.Kubectl.CommandInNamespace("describe", "all")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
fmt.Fprintln(GinkgoWriter, out)
}()
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())

By("granting permissions to access the metrics")
_, err = kbc.Kubectl.Command(
"create", "clusterrolebinding", fmt.Sprintf("metrics-%s", kbc.TestSuffix),
fmt.Sprintf("--clusterrole=e2e-%s-metrics-reader", kbc.TestSuffix),
fmt.Sprintf("--serviceaccount=%s:%s", kbc.Kubectl.Namespace, kbc.Kubectl.ServiceAccount))
ExpectWithOffset(1, err).NotTo(HaveOccurred())

_ = curlMetrics(kbc)

By("validating that cert-manager has provisioned the certificate Secret")
EventuallyWithOffset(1, func() error {
_, err := kbc.Kubectl.Get(
true,
"secrets", "webhook-server-cert")
return err
}, time.Minute, time.Second).Should(Succeed())

By("validating that the Prometheus manager has provisioned the Service")
EventuallyWithOffset(1, func() error {
_, err := kbc.Kubectl.Get(
false,
"Service", "prometheus-operator")
return err
}, time.Minute, time.Second).Should(Succeed())

By("validating that the ServiceMonitor for Prometheus is applied in the namespace")
_, err = kbc.Kubectl.Get(
true,
"ServiceMonitor")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("validating that the created resource object gets reconciled in the controller")
metricsOutput := curlMetrics(kbc)
ExpectWithOffset(1, metricsOutput).To(ContainSubstring(fmt.Sprintf(
`controller_runtime_reconcile_total{controller="%s",result="success"} 1`,
strings.ToLower(kbc.Kind),
)))
}

// Run runs a set of e2e tests for a scaffolded project defined by a TestContext.
func Run(kbc *utils.TestContext, hasWebhook bool) {
func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller bool) {
var controllerPodName string
var err error

Expand Down Expand Up @@ -255,16 +140,34 @@ func Run(kbc *utils.TestContext, hasWebhook bool) {
err = kbc.LoadImageToKindCluster()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
// --clusterrole=cluster-admin --user=myname@mycompany.com
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
By("deploying the controller-manager")
var output []byte
if !isToUseInstaller {

cmd := exec.Command("make", "deploy", "IMG="+kbc.ImageName)
output, err := kbc.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
// --clusterrole=cluster-admin --user=myname@mycompany.com
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
By("deploying the controller-manager")

cmd := exec.Command("make", "deploy", "IMG="+kbc.ImageName)
output, err = kbc.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
} else {
By("building the installer")
err = kbc.Make("build-installer", "IMG="+kbc.ImageName)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
// --clusterrole=cluster-admin --user=myname@mycompany.com
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
By("deploying the controller-manager with the installer")

_, err = kbc.Kubectl.Apply(true, "-f", "dist/install.yaml")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

if kbc.IsRestricted {
By("validating that manager Pod/container(s) are restricted")
Expand Down

0 comments on commit 8477f64

Please sign in to comment.