Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support e2e integration tests w all webhooks & controller #326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/v2"
. "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