Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Aug 31, 2024
1 parent d1a0cb8 commit 8d4ea33
Show file tree
Hide file tree
Showing 19 changed files with 352 additions and 38 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/test-e2e-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ jobs:
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '51s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '55,151s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4/
go mod tidy
make generate
make manifests
- name: Testing make test-e2e for project-v4
working-directory: testdata/project-v4/
Expand All @@ -57,8 +56,6 @@ jobs:
sed -i '55,151s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4-with-deploy-image/
go mod tidy
make generate
make manifests
- name: Testing make test-e2e for project-v4-with-deploy-image
working-directory: testdata/project-v4-with-deploy-image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) {
}

var _ = BeforeSuite(func() {
By("building the manager(Operator) image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
By("running go mod tidy")
cmd := exec.Command("go", "mod", "tidy")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy")

By("running 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const namespace = "project-system"

// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// controllerPodName stores the name of the controller pod
var controllerPodName string

// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand Down Expand Up @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() {
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down Expand Up @@ -108,6 +109,29 @@ var _ = Describe("controller", Ordered, func() {
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})

It("cert-manager should be successfully provisioned", func() {
By("validating that cert-manager has provisioned the certificate Secret")
EventuallyWithOffset(1, func() error {
cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert")
_, err := utils.Run(cmd)
return err
}, time.Minute, time.Second).Should(Succeed())

By("validating that the mutating|validating webhooks have the CA injected")
verifyCAInjection := func() error {
cmd := exec.Command("kubectl", "get",
"mutatingwebhookconfigurations.admissionregistration.k8s.io",
"project-mutating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
output, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
ExpectWithOffset(2, len(output)).To(BeNumerically(">", 10))

return nil
}
EventuallyWithOffset(1, verifyCAInjection, time.Minute, time.Second).Should(Succeed())
}) // +kubebuilder:scaffold:webhookchecks

// TODO(user): Customize the e2e test suite to include
// additional scenarios specific to your project.
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) {
}

var _ = BeforeSuite(func() {
By("building the manager(Operator) image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
By("running go mod tidy")
cmd := exec.Command("go", "mod", "tidy")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy")

By("running 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const namespace = "project-system"

// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// controllerPodName stores the name of the controller pod
var controllerPodName string

// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand Down Expand Up @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() {
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down Expand Up @@ -108,6 +109,8 @@ var _ = Describe("controller", Ordered, func() {
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})

// +kubebuilder:scaffold:webhookchecks

// TODO(user): Customize the e2e test suite to include
// additional scenarios specific to your project.
})
Expand Down
1 change: 1 addition & 0 deletions pkg/plugins/golang/v4/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (s *initScaffolder) Scaffold() error {
&templates.Readme{},
&templates.Golangci{},
&e2e.Test{},
&e2e.WebhookTestUpdater{WireWebhook: false},
&e2e.SuiteTest{},
&utils.Utils{},
&templates.DevContainer{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,24 @@ func TestE2E(t *testing.T) {
}
var _ = BeforeSuite(func() {
By("building the manager(Operator) image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
By("running go mod tidy")
cmd := exec.Command("go", "mod", "tidy")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy")
By("running 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ limitations under the License.
package e2e

import (
"fmt"
"path/filepath"
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
)

var _ machinery.Template = &SuiteTest{}
var _ machinery.Template = &Test{}
var _ machinery.Inserter = &WebhookTestUpdater{}

// Test defines the basic setup for the e2e test
type Test struct {
machinery.TemplateMixin
machinery.BoilerplateMixin
Expand All @@ -31,13 +35,74 @@ type Test struct {

func (f *Test) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = "test/e2e/e2e_test.go"
f.Path = filepath.Join("test", "e2e", "e2e_test.go")
}

// This is where the template body is defined with markers
f.TemplateBody = TestTemplate

return nil
}

// WebhookTestUpdater updates e2e_test.go to insert additional webhook validation tests
type WebhookTestUpdater struct {
machinery.RepositoryMixin
machinery.ProjectNameMixin
WireWebhook bool
}

// GetPath implements file.Builder
func (*WebhookTestUpdater) GetPath() string {
return filepath.Join("test", "e2e", "e2e_test.go")
}

// GetIfExistsAction implements file.Builder
func (*WebhookTestUpdater) GetIfExistsAction() machinery.IfExistsAction {
return machinery.OverwriteFile // Ensures only the marker is replaced
}

// GetMarkers implements file.Inserter
func (f *WebhookTestUpdater) GetMarkers() []machinery.Marker {
return []machinery.Marker{
machinery.NewMarkerFor(f.GetPath(), "webhookchecks"),
}
}

// GetCodeFragments implements file.Inserter
func (f *WebhookTestUpdater) GetCodeFragments() machinery.CodeFragmentsMap {
if !f.WireWebhook {
return nil
}

code := fmt.Sprintf(webhookchecksFragment, f.ProjectName)
return machinery.CodeFragmentsMap{
machinery.NewMarkerFor(f.GetPath(), "webhookchecks"): []string{code},
}
}

const webhookchecksFragment = `It("cert-manager should be successfully provisioned", func() {
By("validating that cert-manager has provisioned the certificate Secret")
EventuallyWithOffset(1, func() error {
cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert")
_, err := utils.Run(cmd)
return err
}, time.Minute, time.Second).Should(Succeed())
By("validating that the mutating|validating webhooks have the CA injected")
verifyCAInjection := func() error {
cmd := exec.Command("kubectl", "get",
"mutatingwebhookconfigurations.admissionregistration.k8s.io",
"%s-mutating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
output, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
ExpectWithOffset(2, len(output)).To(BeNumerically(">", 10))
return nil
}
EventuallyWithOffset(1, verifyCAInjection, time.Minute, time.Second).Should(Succeed())
})`

var TestTemplate = `{{ .Boilerplate }}
Expand All @@ -58,6 +123,9 @@ const namespace = "{{ .ProjectName }}-system"
// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// controllerPodName stores the name of the controller pod
var controllerPodName string
// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand Down Expand Up @@ -96,8 +164,6 @@ var _ = Describe("controller", Ordered, func() {
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string
By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down Expand Up @@ -135,6 +201,8 @@ var _ = Describe("controller", Ordered, func() {
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})
// +kubebuilder:scaffold:webhookchecks
// TODO(user): Customize the e2e test suite to include
// additional scenarios specific to your project.
})
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package scaffolds

import (
"fmt"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e"

log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
Expand Down Expand Up @@ -86,6 +87,7 @@ func (s *webhookScaffolder) Scaffold() error {

if err := scaffold.Execute(
&api.Webhook{Force: s.force},
&e2e.WebhookTestUpdater{WireWebhook: true},
&templates.MainUpdater{WireWebhook: true},
&api.WebhookTest{Force: s.force},
); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,24 @@ func TestE2E(t *testing.T) {
}

var _ = BeforeSuite(func() {
By("building the manager(Operator) image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
By("running go mod tidy")
cmd := exec.Command("go", "mod", "tidy")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run go mod tidy")

By("running 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const namespace = "project-v4-multigroup-with-deploy-image-system"

// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// controllerPodName stores the name of the controller pod
var controllerPodName string

// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand Down Expand Up @@ -69,8 +72,6 @@ var _ = Describe("controller", Ordered, func() {
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down Expand Up @@ -108,6 +109,29 @@ var _ = Describe("controller", Ordered, func() {
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})

It("cert-manager should be successfully provisioned", func() {
By("validating that cert-manager has provisioned the certificate Secret")
EventuallyWithOffset(1, func() error {
cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert")
_, err := utils.Run(cmd)
return err
}, time.Minute, time.Second).Should(Succeed())

By("validating that the mutating|validating webhooks have the CA injected")
verifyCAInjection := func() error {
cmd := exec.Command("kubectl", "get",
"mutatingwebhookconfigurations.admissionregistration.k8s.io",
"project-v4-multigroup-with-deploy-image-mutating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
output, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
ExpectWithOffset(2, len(output)).To(BeNumerically(">", 10))

return nil
}
EventuallyWithOffset(1, verifyCAInjection, time.Minute, time.Second).Should(Succeed())
}) // +kubebuilder:scaffold:webhookchecks

// TODO(user): Customize the e2e test suite to include
// additional scenarios specific to your project.
})
Expand Down
Loading

0 comments on commit 8d4ea33

Please sign in to comment.