Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Sep 14, 2024
1 parent 79a06e5 commit 3c3f847
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 266 deletions.
50 changes: 48 additions & 2 deletions test/e2e/deployimage/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ limitations under the License.
package deployimage

import (
"fmt"
"path/filepath"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"

//nolint:golint
// nolint:revive
Expand All @@ -36,10 +41,18 @@ func GenerateDeployImageWithOptions(kbc *utils.TestContext) {
// GenerateDeployImage implements a go/v4 plugin project and scaffold an API using the deploy image plugin
func GenerateDeployImage(kbc *utils.TestContext) {
initTheProject(kbc)
creatingAPI(kbc)
createAPI(kbc)
}

// GenerateWithWebhooks
func GenerateWithWebhook(kbc *utils.TestContext) {
initTheProject(kbc)
createAPI(kbc)
createWebhook(kbc)
enableMetricsCertManager(kbc)
}

func creatingAPI(kbc *utils.TestContext) {
func createAPI(kbc *utils.TestContext) {
By("creating API definition with deploy-image/v1-alpha plugin")
err := kbc.CreateAPI(
"--group", kbc.Group,
Expand Down Expand Up @@ -81,3 +94,36 @@ func initTheProject(kbc *utils.TestContext) {
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

func createWebhook(kbc *utils.TestContext) {
By("creating Webhook for the API")
err := kbc.CreateWebhook(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--defaulting",
"--programmatic-validation",
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("implementing the mutating and validating webhooks")
webhookFilePath := filepath.Join(
kbc.Dir, "api", kbc.Version,
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind)))
err = utils.ImplementWebhooks(webhookFilePath, strings.ToLower(kbc.Kind))
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

func enableMetricsCertManager(kbc *utils.TestContext) {
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../certmanager", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../prometheus", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- path: webhookcainjection_patch.yaml", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
utils.CertManagerTarget, "#")).To(Succeed())
}
54 changes: 51 additions & 3 deletions test/e2e/deployimage/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -52,18 +53,23 @@ var _ = Describe("kubebuilder", func() {

It("should generate a runnable project with deploy-image/v1-alpha options ", func() {
GenerateDeployImageWithOptions(kbc)
Run(kbc)
Run(kbc, false)
})

It("should generate a runnable project with deploy-image/v1-alpha without options ", func() {
GenerateDeployImage(kbc)
Run(kbc)
Run(kbc, false)
})

It("should generate a runnable project with deploy-image/v1-alpha and webhooks for the API", func() {
GenerateWithWebhook(kbc)
Run(kbc, true)
})
})
})

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

Expand Down Expand Up @@ -148,6 +154,48 @@ func Run(kbc *utils.TestContext) {
}
Eventually(verifyAvailableStatus).Should(Succeed())

if hasWebhooks {
By("configuring metrics and create curl Pod")
utils.EnableMetricsCreateCurlPod(kbc)

By("creating an instance of the CR not in the manager namespace")
sampleFile := filepath.Join("config", "samples",
fmt.Sprintf("%s_%s_%s.yaml", kbc.Group, kbc.Version, strings.ToLower(kbc.Kind)))

sampleFilePath, err := filepath.Abs(filepath.Join(fmt.Sprintf("e2e-%s", kbc.TestSuffix), sampleFile))
Expect(err).To(Not(HaveOccurred()))

Eventually(func(g Gomega) {
g.Expect(kbc.Kubectl.Apply(false, "-f", sampleFilePath)).Error().NotTo(HaveOccurred())
}).Should(Succeed())

By("validating webhooks not in the default namespace")
cnt, err := kbc.Kubectl.Get(
false,
"-f", sampleFile,
"-o", "go-template={{ .spec.size }}")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
size, err := strconv.Atoi(cnt)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, size).To(BeNumerically("==", 5))

By("checking the metrics values gets reconciled")
metricsOutput := utils.GetMetricsOutput(kbc)
ExpectWithOffset(1, metricsOutput).To(ContainSubstring(fmt.Sprintf(
`controller_runtime_reconcile_total{controller="%s",result="success"}`,
strings.ToLower(kbc.Kind),
)))

By("removing Curl Pod")
utils.RemoveCurlPod(kbc)

By("removing cr applied not in the default namespace")
Eventually(func(g Gomega) {
g.Expect(kbc.Kubectl.Delete(false,
"-f", sampleFilePath)).Error().NotTo(HaveOccurred())
}).Should(Succeed())
}

By("validating the finalizer")
Eventually(func(g Gomega) {
g.Expect(kbc.Kubectl.Delete(true, "-f", sampleFilePath)).Error().NotTo(HaveOccurred())
Expand Down
100 changes: 100 additions & 0 deletions test/e2e/utils/kustomize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package utils

//nolint:lll
const CertManagerTarget = `#replacements:
# - source: # Add cert-manager annotation to ValidatingWebhookConfiguration, MutatingWebhookConfiguration and CRDs
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
# fieldPath: .metadata.namespace # namespace of the certificate CR
# targets:
# - select:
# kind: ValidatingWebhookConfiguration
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 0
# create: true
# - select:
# kind: MutatingWebhookConfiguration
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 0
# create: true
# - select:
# kind: CustomResourceDefinition
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 0
# create: true
# - source:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
# fieldPath: .metadata.name
# targets:
# - select:
# kind: ValidatingWebhookConfiguration
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 1
# create: true
# - select:
# kind: MutatingWebhookConfiguration
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 1
# create: true
# - select:
# kind: CustomResourceDefinition
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 1
# create: true
# - source: # Add cert-manager annotation to the webhook Service
# kind: Service
# version: v1
# name: webhook-service
# fieldPath: .metadata.name # namespace of the service
# targets:
# - select:
# kind: Certificate
# group: cert-manager.io
# version: v1
# fieldPaths:
# - .spec.dnsNames.0
# - .spec.dnsNames.1
# options:
# delimiter: '.'
# index: 0
# create: true
# - source:
# kind: Service
# version: v1
# name: webhook-service
# fieldPath: .metadata.namespace # namespace of the service
# targets:
# - select:
# kind: Certificate
# group: cert-manager.io
# version: v1
# fieldPaths:
# - .spec.dnsNames.0
# - .spec.dnsNames.1
# options:
# delimiter: '.'
# index: 1
# create: true`
Loading

0 comments on commit 3c3f847

Please sign in to comment.