Skip to content

Commit

Permalink
Simplify webhooks suites
Browse files Browse the repository at this point in the history
- Generate manifests only for the webhooks we are about to test
- Do not wire in unrelated webhooks in the test suite

Co-authored-by: Georgi Sabev <[email protected]>
  • Loading branch information
danail-branekov and georgethebeatle committed Dec 20, 2024
1 parent a2de0f7 commit 8a98e19
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 385 deletions.
76 changes: 0 additions & 76 deletions controllers/api/v1alpha1/cforg_types_test.go

This file was deleted.

94 changes: 0 additions & 94 deletions controllers/api/v1alpha1/cfspace_types_test.go

This file was deleted.

32 changes: 8 additions & 24 deletions controllers/api/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,22 @@ package v1alpha1_test

import (
"context"
"os"
"path/filepath"
"testing"
"time"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/shared"
"code.cloudfoundry.org/korifi/controllers/coordination"
"code.cloudfoundry.org/korifi/controllers/webhooks/finalizer"
"code.cloudfoundry.org/korifi/controllers/webhooks/networking/domains"
"code.cloudfoundry.org/korifi/controllers/webhooks/networking/routes"
"code.cloudfoundry.org/korifi/controllers/webhooks/validation"
"code.cloudfoundry.org/korifi/controllers/webhooks/version"
"code.cloudfoundry.org/korifi/controllers/webhooks/workloads/apps"
"code.cloudfoundry.org/korifi/controllers/webhooks/workloads/orgs"
packageswebhook "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/packages"
"code.cloudfoundry.org/korifi/controllers/webhooks/workloads/spaces"
"code.cloudfoundry.org/korifi/tests/helpers"

"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -79,11 +72,17 @@ var _ = BeforeSuite(func() {

ctx, stopManager = context.WithCancel(context.TODO())

webhookManifestsPath := helpers.GenerateWebhookManifest(
"code.cloudfoundry.org/korifi/controllers/api/v1alpha1",
)
DeferCleanup(func() {
Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed())
})
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "crds")},
ErrorIfCRDPathMissing: false,
WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")},
Paths: []string{webhookManifestsPath},
},
}

Expand All @@ -92,13 +91,9 @@ var _ = BeforeSuite(func() {
_, err := testEnv.Start()
Expect(err).NotTo(HaveOccurred())

Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(korifiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(admissionv1beta1.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(coordinationv1.AddToScheme(scheme.Scheme)).To(Succeed())

k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml"))
Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed())

adminClient, stopClientCache = helpers.NewCachedClient(testEnv.Config)

Expand All @@ -124,17 +119,6 @@ var _ = BeforeSuite(func() {

Expect((&korifiv1alpha1.CFBuild{}).SetupWebhookWithManager(k8sManager)).To(Succeed())

orgNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, orgs.CFOrgEntityType))
orgPlacementValidator := validation.NewPlacementValidator(uncachedClient, namespace)
Expect(orgs.NewValidator(orgNameDuplicateValidator, orgPlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed())

spaceNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, spaces.CFSpaceEntityType))
spacePlacementValidator := validation.NewPlacementValidator(uncachedClient, namespace)
Expect(spaces.NewValidator(spaceNameDuplicateValidator, spacePlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed())
version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager)
finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager)
Expect(packageswebhook.NewValidator().SetupWebhookWithManager(k8sManager)).To(Succeed())

Expect(adminClient.Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Expand Down
69 changes: 8 additions & 61 deletions controllers/webhooks/finalizer/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,17 @@ package finalizer_test

import (
"context"
"os"
"path/filepath"
"testing"
"time"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/shared"
"code.cloudfoundry.org/korifi/controllers/coordination"
"code.cloudfoundry.org/korifi/controllers/webhooks/finalizer"
"code.cloudfoundry.org/korifi/controllers/webhooks/networking/domains"
"code.cloudfoundry.org/korifi/controllers/webhooks/networking/routes"
bindingswebhook "code.cloudfoundry.org/korifi/controllers/webhooks/services/bindings"
"code.cloudfoundry.org/korifi/controllers/webhooks/services/instances"
"code.cloudfoundry.org/korifi/controllers/webhooks/validation"
"code.cloudfoundry.org/korifi/controllers/webhooks/version"
"code.cloudfoundry.org/korifi/controllers/webhooks/workloads/apps"
"code.cloudfoundry.org/korifi/controllers/webhooks/workloads/orgs"
"code.cloudfoundry.org/korifi/controllers/webhooks/workloads/packages"
"code.cloudfoundry.org/korifi/controllers/webhooks/workloads/spaces"
"code.cloudfoundry.org/korifi/tests/helpers"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -59,63 +46,33 @@ func TestWorkloadsWebhooks(t *testing.T) {
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

webhookManifestsPath := helpers.GenerateWebhookManifest(
"code.cloudfoundry.org/korifi/controllers/webhooks/finalizer",
)
DeferCleanup(func() {
Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed())
})
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "crds"),
},
ErrorIfCRDPathMissing: true,
WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")},
Paths: []string{webhookManifestsPath},
},
}

_, err := testEnv.Start()
Expect(err).NotTo(HaveOccurred())

Expect(korifiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(admissionv1beta1.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(coordinationv1.AddToScheme(scheme.Scheme)).To(Succeed())

k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml"))
Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed())

adminClient, stopClientCache = helpers.NewCachedClient(testEnv.Config)

finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager)

version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager)
Expect((&korifiv1alpha1.CFApp{}).SetupWebhookWithManager(k8sManager)).To(Succeed())

uncachedClient := helpers.NewUncachedClient(k8sManager.GetConfig())
Expect(apps.NewValidator(
validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, apps.AppEntityType)),
).SetupWebhookWithManager(k8sManager)).To(Succeed())

orgNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, orgs.CFOrgEntityType))
orgPlacementValidator := validation.NewPlacementValidator(uncachedClient, rootNamespace)
Expect(orgs.NewValidator(orgNameDuplicateValidator, orgPlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed())

spaceNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, spaces.CFSpaceEntityType))
spacePlacementValidator := validation.NewPlacementValidator(uncachedClient, rootNamespace)
Expect(spaces.NewValidator(spaceNameDuplicateValidator, spacePlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed())

Expect(domains.NewValidator(uncachedClient).SetupWebhookWithManager(k8sManager)).To(Succeed())

Expect((&korifiv1alpha1.CFPackage{}).SetupWebhookWithManager(k8sManager)).To(Succeed())

Expect((&korifiv1alpha1.CFRoute{}).SetupWebhookWithManager(k8sManager)).To(Succeed())
Expect(routes.NewValidator(
validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, routes.RouteEntityType)),
rootNamespace,
uncachedClient,
).SetupWebhookWithManager(k8sManager)).To(Succeed())
Expect(packages.NewValidator().SetupWebhookWithManager(k8sManager)).To(Succeed())
Expect(instances.NewValidator(validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, instances.ServiceInstanceEntityType))).SetupWebhookWithManager(k8sManager)).To(Succeed())
Expect(bindingswebhook.NewCFServiceBindingValidator(
validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, bindingswebhook.ServiceBindingEntityType)),
).SetupWebhookWithManager(k8sManager)).To(Succeed())

stopManager = helpers.StartK8sManager(k8sManager)

ctx := context.Background()
Expand All @@ -124,16 +81,6 @@ var _ = BeforeSuite(func() {
Name: rootNamespace,
},
})).To(Succeed())

Expect(adminClient.Create(ctx, &korifiv1alpha1.CFDomain{
ObjectMeta: metav1.ObjectMeta{
Namespace: rootNamespace,
Name: defaultDomainName,
},
Spec: korifiv1alpha1.CFDomainSpec{
Name: "my.domain",
},
})).To(Succeed())
})

var _ = AfterSuite(func() {
Expand Down
Loading

0 comments on commit 8a98e19

Please sign in to comment.