Skip to content

Commit

Permalink
Support e2e integration tests w all webhooks & controller
Browse files Browse the repository at this point in the history
This patch introduces the new function "NewTestSuiteWithOptions" for
creating a test suite that supports all webhooks, both conversion and
admission, as well as controllers. This enables end-to-end testing of
a controller via integration tests where the production webhooks are
installed and part of the reconciliation.
  • Loading branch information
akutz committed Dec 27, 2023
1 parent b76f234 commit 8ed73e6
Show file tree
Hide file tree
Showing 12 changed files with 744 additions and 235 deletions.
4 changes: 3 additions & 1 deletion api/v1alpha1/virtualmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

package v1alpha1

import ctrl "sigs.k8s.io/controller-runtime"
import (
ctrl "sigs.k8s.io/controller-runtime"
)

func (r *VirtualMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
Expand Down
4 changes: 3 additions & 1 deletion api/v1alpha2/virtualmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

package v1alpha2

import ctrl "sigs.k8s.io/controller-runtime"
import (
ctrl "sigs.k8s.io/controller-runtime"
)

func (r *VirtualMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
resourcev1 "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

Expand All @@ -28,11 +31,36 @@ func intgTests() {

vm *vmopv1.VirtualMachine
vmKey types.NamespacedName

storageClass *storagev1.StorageClass
resourceQuota *corev1.ResourceQuota
)

BeforeEach(func() {
ctx = suite.NewIntegrationTestContext()

// The validation webhook expects there to be a storage class associated
// with the namespace where the VM is located.
storageClass = &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "dummy-storage-class-",
},
Provisioner: "dummy-provisioner",
}
Expect(ctx.Client.Create(ctx, storageClass)).To(Succeed())
resourceQuota = &corev1.ResourceQuota{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "dummy-resource-quota-",
Namespace: ctx.Namespace,
},
Spec: corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{
corev1.ResourceName(storageClass.Name + ".storageclass.storage.k8s.io/dummy"): resourcev1.MustParse("0"),
},
},
}
Expect(ctx.Client.Create(ctx, resourceQuota)).To(Succeed())

vm = &vmopv1.VirtualMachine{
ObjectMeta: metav1.ObjectMeta{
Namespace: ctx.Namespace,
Expand All @@ -42,7 +70,7 @@ func intgTests() {
ImageName: "dummy-image",
ClassName: "dummy-class",
PowerState: vmopv1.VirtualMachinePoweredOn,
StorageClass: "dummy-storageclass",
StorageClass: storageClass.Name,
VmMetadata: &vmopv1.VirtualMachineMetadata{
Transport: vmopv1.VirtualMachineMetadataOvfEnvTransport,
ConfigMapName: "dummy-configmap",
Expand All @@ -53,6 +81,11 @@ func intgTests() {
})

AfterEach(func() {
Expect(ctx.Client.Delete(ctx, resourceQuota)).To(Succeed())
resourceQuota = nil
Expect(ctx.Client.Delete(ctx, storageClass)).To(Succeed())
storageClass = nil

ctx.AfterEach()
ctx = nil
intgFakeVMProvider.Reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,35 @@ import (

virtualmachine "github.com/vmware-tanzu/vm-operator/controllers/virtualmachine/v1alpha1"
ctrlContext "github.com/vmware-tanzu/vm-operator/pkg/context"
pkgmgr "github.com/vmware-tanzu/vm-operator/pkg/manager"
providerfake "github.com/vmware-tanzu/vm-operator/pkg/vmprovider/fake"
"github.com/vmware-tanzu/vm-operator/test/builder"
mutationv1a1 "github.com/vmware-tanzu/vm-operator/webhooks/virtualmachine/v1alpha1/mutation"
validationv1a1 "github.com/vmware-tanzu/vm-operator/webhooks/virtualmachine/v1alpha1/validation"
)

var intgFakeVMProvider = providerfake.NewVMProvider()

var suite = builder.NewTestSuiteForController(
virtualmachine.AddToManager,
func(ctx *ctrlContext.ControllerManagerContext, _ ctrlmgr.Manager) error {
ctx.VMProvider = intgFakeVMProvider
return nil
},
)
var suite = builder.NewTestSuiteWithOptions(
builder.TestSuiteOptions{
InitProviderFn: func(ctx *ctrlContext.ControllerManagerContext, _ ctrlmgr.Manager) error {
ctx.VMProvider = intgFakeVMProvider
return nil
},
Controllers: []pkgmgr.AddToManagerFunc{virtualmachine.AddToManager},
MutationWebhooks: []builder.TestSuiteMutationWebhookOptions{
{
Name: "default.mutating.virtualmachine.v1alpha1.vmoperator.vmware.com",
AddToManagerFn: mutationv1a1.AddToManager,
},
},
ValidationWebhooks: []builder.TestSuiteValidationWebhookOptions{
{
Name: "default.validating.virtualmachine.v1alpha1.vmoperator.vmware.com",
AddToManagerFn: validationv1a1.AddToManager,
},
},
})

func TestVirtualMachine(t *testing.T) {
suite.Register(t, "VirtualMachine controller suite", intgTests, unitTests)
Expand Down
Loading

0 comments on commit 8ed73e6

Please sign in to comment.