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 9dc0f63
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 102 deletions.
4 changes: 4 additions & 0 deletions api/v1alpha1/virtualmachine_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,8 @@ func restore_v1alpha2_VirtualMachineReadinessProbeSpec(

// ConvertTo converts this VirtualMachine to the Hub version.
func (src *VirtualMachine) ConvertTo(dstRaw conversion.Hub) error {
fmt.Println("v1a1/vm convert to")

dst := dstRaw.(*v1alpha2.VirtualMachine)
if err := Convert_v1alpha1_VirtualMachine_To_v1alpha2_VirtualMachine(src, dst, nil); err != nil {
return err
Expand All @@ -904,6 +906,8 @@ func (src *VirtualMachine) ConvertTo(dstRaw conversion.Hub) error {

// ConvertFrom converts the hub version to this VirtualMachine.
func (dst *VirtualMachine) ConvertFrom(srcRaw conversion.Hub) error {
fmt.Println("v1a1/vm convert from")

src := srcRaw.(*v1alpha2.VirtualMachine)
if err := Convert_v1alpha2_VirtualMachine_To_v1alpha1_VirtualMachine(src, dst, nil); err != nil {
return err
Expand Down
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
7 changes: 6 additions & 1 deletion api/v1alpha2/virtualmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

package v1alpha2

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

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

func (r *VirtualMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
fmt.Println("v1a2/vm convert")
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func intgTests() {
})
})

It("Reconciles after VirtualMachine creation", func() {
FIt("Reconciles after VirtualMachine creation", func() {
Expect(ctx.Client.Create(ctx, vm)).To(Succeed())

By("VirtualMachine should have finalizer added", func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,60 @@ import (

ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"

vmopv1a1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"

Check failure on line 13 in controllers/virtualmachine/v1alpha2/virtualmachine_controller_suite_test.go

View workflow job for this annotation

GitHub Actions / lint-go

import "github.com/vmware-tanzu/vm-operator/api/v1alpha1" imported as "vmopv1a1" but must be "vmopv1" according to config (importas)
vmopv1a2 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
virtualmachine "github.com/vmware-tanzu/vm-operator/controllers/virtualmachine/v1alpha2"
ctrlContext "github.com/vmware-tanzu/vm-operator/pkg/context"
"github.com/vmware-tanzu/vm-operator/pkg/lib"
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"
mutationv1a2 "github.com/vmware-tanzu/vm-operator/webhooks/virtualmachine/v1alpha2/mutation"
validationv1a2 "github.com/vmware-tanzu/vm-operator/webhooks/virtualmachine/v1alpha2/validation"
)

var intgFakeVMProvider = providerfake.NewVMProviderA2()

var suite = builder.NewTestSuiteForControllerWithFSS(
virtualmachine.AddToManager,
func(ctx *ctrlContext.ControllerManagerContext, _ ctrlmgr.Manager) error {
ctx.VMProviderA2 = intgFakeVMProvider
return nil
},
map[string]bool{lib.VMServiceV1Alpha2FSS: true})
var suite = builder.NewTestSuiteWithOptions(
builder.TestSuiteOptions{
InitProviderFn: func(ctx *ctrlContext.ControllerManagerContext, _ ctrlmgr.Manager) error {
ctx.VMProviderA2 = intgFakeVMProvider
return nil
},
FeatureStates: map[string]bool{lib.VMServiceV1Alpha2FSS: true},
Controllers: []pkgmgr.AddToManagerFunc{virtualmachine.AddToManager},
ConversionWebhooks: []builder.TestSuiteConversionWebhookOptions{
{
Name: "virtualmachines.vmoperator.vmware.com",
AddToManagerFn: []func(ctrlmgr.Manager) error{
(&vmopv1a1.VirtualMachine{}).SetupWebhookWithManager,
(&vmopv1a2.VirtualMachine{}).SetupWebhookWithManager,
},
},
},
MutationWebhooks: []builder.TestSuiteMutationWebhookOptions{
{
Name: "default.mutating.virtualmachine.v1alpha1.vmoperator.vmware.com",
AddToManagerFn: mutationv1a1.AddToManager,
},
{
Name: "default.mutating.virtualmachine.v1alpha2.vmoperator.vmware.com",
AddToManagerFn: mutationv1a2.AddToManager,
},
},
ValidationWebhooks: []builder.TestSuiteValidationWebhookOptions{
{
Name: "default.validating.virtualmachine.v1alpha1.vmoperator.vmware.com",
AddToManagerFn: validationv1a1.AddToManager,
},
{
Name: "default.validating.virtualmachine.v1alpha2.vmoperator.vmware.com",
AddToManagerFn: validationv1a2.AddToManager,
},
},
})

func TestVirtualMachine(t *testing.T) {
suite.Register(t, "VirtualMachine controller suite", intgTests, unitTests)
Expand Down
2 changes: 2 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -46,6 +47,7 @@ func New(opts Options) (Manager, error) {
opts.defaults()

_ = clientgoscheme.AddToScheme(opts.Scheme)
_ = apiextensionsv1.AddToScheme(opts.Scheme)
_ = vmopv1.AddToScheme(opts.Scheme)
_ = ncpv1alpha1.AddToScheme(opts.Scheme)
_ = cnsv1alpha1.AddToScheme(opts.Scheme)
Expand Down
6 changes: 3 additions & 3 deletions pkg/manager/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var InitializeProvidersNoopFn InitializeProvidersFunc = func(_ *context.Controll
return nil
}

// Options describes the options used to create a new GCM manager.
// Options describes the options used to create a new manager.
type Options struct {
// LeaderElectionEnabled is a flag that enables leader election.
LeaderElectionEnabled bool
Expand All @@ -46,8 +46,8 @@ type Options struct {
// locking resource when configuring leader election.
LeaderElectionID string

// HealthProbeBindAddress is the TCP address that the controller should bind to
// for serving health probes
// HealthProbeBindAddress is the TCP address to which the controller should
// bind for serving health probes.
HealthProbeBindAddress string

// SyncPeriod is the amount of time to wait between syncing the local
Expand Down
Loading

0 comments on commit 9dc0f63

Please sign in to comment.