Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vr4manta committed Nov 14, 2024
1 parent 1602f4e commit d5ccb46
Show file tree
Hide file tree
Showing 8 changed files with 584 additions and 14 deletions.
8 changes: 8 additions & 0 deletions pkg/controller/vsphere/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

testutils "github.com/openshift/machine-api-operator/pkg/util/testing"

. "github.com/onsi/gomega"
configv1 "github.com/openshift/api/config/v1"
machinev1 "github.com/openshift/api/machine/v1beta1"
Expand Down Expand Up @@ -363,13 +365,19 @@ func TestMachineEvents(t *testing.T) {
}
gs.Eventually(getNode, timeout).Should(Succeed())

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

taskIDCache := make(map[string]string)
params := ActuatorParams{
Client: k8sClient,
EventRecorder: eventRecorder,
APIReader: k8sClient,
TaskIDCache: taskIDCache,
OpenshiftConfigNamespace: openshiftConfigNamespaceForTest,
FeatureGates: gate,
}

actuator := NewActuator(params)
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/vsphere/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2631,8 +2631,7 @@ func TestCreate(t *testing.T) {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

err = gates.Set(fmt.Sprintf("%v=%v", features.FeatureGateVSphereStaticIPs, tc.staticIPFeatureGateEnabled))
if err != nil {
if err = gates.Set(fmt.Sprintf("%v=%v", features.FeatureGateVSphereStaticIPs, tc.staticIPFeatureGateEnabled)); err != nil {
t.Errorf("Unexpected error setting static IP feature gates: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func NewMachineHealthCheck(name string) *machinev1.MachineHealthCheck {

func NewDefaultMutableFeatureGate() (featuregate.MutableFeatureGate, error) {
defaultMutableGate := feature.DefaultMutableFeatureGate
_, err := features.NewFeatureGateOptions(defaultMutableGate, openshiftfeatures.SelfManaged, openshiftfeatures.FeatureGateMachineAPIMigration)
_, err := features.NewFeatureGateOptions(defaultMutableGate, openshiftfeatures.SelfManaged, openshiftfeatures.FeatureGateMachineAPIMigration, openshiftfeatures.FeatureGateVSphereStaticIPs, openshiftfeatures.FeatureGateVSphereMultiDisk)
if err != nil {
return nil, fmt.Errorf("failed to set up default feature gate: %w", err)
}
Expand Down
70 changes: 61 additions & 9 deletions pkg/webhooks/machine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"reflect"
"testing"

testutils "github.com/openshift/machine-api-operator/pkg/util/testing"

. "github.com/onsi/gomega"
osconfigv1 "github.com/openshift/api/config/v1"
machinev1 "github.com/openshift/api/machine/v1"
Expand Down Expand Up @@ -1172,6 +1174,11 @@ func TestMachineCreation(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
gs := NewWithT(t)

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

mgr, err := manager.New(cfg, manager.Options{
Metrics: metricsserver.Options{
BindAddress: "0",
Expand Down Expand Up @@ -1201,7 +1208,7 @@ func TestMachineCreation(t *testing.T) {
dns.Spec.PublicZone = &osconfigv1.DNSZone{}
}
machineDefaulter := admission.WithCustomDefaulter(scheme.Scheme, &machinev1beta1.Machine{}, createMachineDefaulter(platformStatus, tc.clusterID))
machineValidator := admission.WithCustomValidator(scheme.Scheme, &machinev1beta1.Machine{}, createMachineValidator(infra, c, dns))
machineValidator := admission.WithCustomValidator(scheme.Scheme, &machinev1beta1.Machine{}, createMachineValidator(infra, c, dns, gate))
mgr.GetWebhookServer().Register(DefaultMachineMutatingHookPath, &webhook.Admission{Handler: machineDefaulter})
mgr.GetWebhookServer().Register(DefaultMachineValidatingHookPath, &webhook.Admission{Handler: machineValidator})

Expand Down Expand Up @@ -1974,6 +1981,11 @@ func TestMachineUpdate(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
gs := NewWithT(t)

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

mgr, err := manager.New(cfg, manager.Options{
Metrics: metricsserver.Options{
BindAddress: "0",
Expand All @@ -1999,7 +2011,7 @@ func TestMachineUpdate(t *testing.T) {
},
}
machineDefaulter := admission.WithCustomDefaulter(scheme.Scheme, &machinev1beta1.Machine{}, createMachineDefaulter(platformStatus, tc.clusterID))
machineValidator := admission.WithCustomValidator(scheme.Scheme, &machinev1beta1.Machine{}, createMachineValidator(infra, c, plainDNS))
machineValidator := admission.WithCustomValidator(scheme.Scheme, &machinev1beta1.Machine{}, createMachineValidator(infra, c, plainDNS, gate))
mgr.GetWebhookServer().Register(DefaultMachineMutatingHookPath, &webhook.Admission{Handler: machineDefaulter})
mgr.GetWebhookServer().Register(DefaultMachineValidatingHookPath, &webhook.Admission{Handler: machineValidator})

Expand Down Expand Up @@ -2380,7 +2392,13 @@ func TestValidateAWSProviderSpec(t *testing.T) {
infra := plainInfra.DeepCopy()
infra.Status.InfrastructureName = "clusterID"
infra.Status.PlatformStatus.Type = osconfigv1.AWSPlatformType
h := createMachineValidator(infra, c, plainDNS)

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

h := createMachineValidator(infra, c, plainDNS, gate)

for _, tc := range testCases {
t.Run(tc.testCase, func(t *testing.T) {
Expand Down Expand Up @@ -3050,7 +3068,12 @@ func TestValidateAzureProviderSpec(t *testing.T) {
infra.Status.PlatformStatus.Type = osconfigv1.AzurePlatformType
infra.Status.PlatformStatus.Azure = tc.azurePlatformStatus

h := createMachineValidator(infra, c, plainDNS)
gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

h := createMachineValidator(infra, c, plainDNS, gate)

// create a valid spec that will then be 'broken' by modifySpec
providerSpec := &machinev1beta1.AzureMachineProviderSpec{
Expand Down Expand Up @@ -3725,7 +3748,13 @@ func TestValidateGCPProviderSpec(t *testing.T) {
infra := plainInfra.DeepCopy()
infra.Status.InfrastructureName = "clusterID"
infra.Status.PlatformStatus.Type = osconfigv1.GCPPlatformType
h := createMachineValidator(infra, c, plainDNS)

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

h := createMachineValidator(infra, c, plainDNS, gate)

for _, tc := range testCases {
providerSpec := &machinev1beta1.GCPMachineProviderSpec{
Expand Down Expand Up @@ -4178,7 +4207,13 @@ func TestValidateVSphereProviderSpec(t *testing.T) {
infra := plainInfra.DeepCopy()
infra.Status.InfrastructureName = "clusterID"
infra.Status.PlatformStatus.Type = osconfigv1.VSpherePlatformType
h := createMachineValidator(infra, c, plainDNS)

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

h := createMachineValidator(infra, c, plainDNS, gate)

for _, tc := range testCases {
t.Run(tc.testCase, func(t *testing.T) {
Expand Down Expand Up @@ -4410,7 +4445,13 @@ func TestUpdateFinalizer(t *testing.T) {
infra := plainInfra.DeepCopy()
infra.Status.InfrastructureName = "clusterID"
infra.Status.PlatformStatus.Type = osconfigv1.AWSPlatformType
h := createMachineValidator(infra, c, plainDNS)

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

h := createMachineValidator(infra, c, plainDNS, gate)

providerSpec := &machinev1beta1.AWSMachineProviderConfig{
AMI: machinev1beta1.AWSResourceReference{
Expand Down Expand Up @@ -4839,7 +4880,12 @@ func TestValidatePowerVSProviderSpec(t *testing.T) {
infra := plainInfra.DeepCopy()
infra.Status.InfrastructureName = "clusterID"
infra.Status.PlatformStatus.Type = osconfigv1.PowerVSPlatformType
h := createMachineValidator(infra, c, plainDNS)
gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

h := createMachineValidator(infra, c, plainDNS, gate)

for _, tc := range testCases {
t.Run(tc.testCase, func(t *testing.T) {
Expand Down Expand Up @@ -5133,7 +5179,13 @@ func TestValidateNutanixProviderSpec(t *testing.T) {
infra := plainInfra.DeepCopy()
infra.Status.InfrastructureName = "clusterID"
infra.Status.PlatformStatus.Type = osconfigv1.NutanixPlatformType
h := createMachineValidator(infra, c, plainDNS)

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

h := createMachineValidator(infra, c, plainDNS, gate)

for _, tc := range testCases {
t.Run(tc.testCase, func(t *testing.T) {
Expand Down
16 changes: 14 additions & 2 deletions pkg/webhooks/machineset_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
testutils "github.com/openshift/machine-api-operator/pkg/util/testing"
"testing"

"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -509,8 +510,14 @@ func TestMachineSetCreation(t *testing.T) {
if !tc.disconnected {
dns.Spec.PublicZone = &osconfigv1.DNSZone{}
}

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

machineSetDefaulter := createMachineSetDefaulter(platformStatus, tc.clusterID)
machineSetValidator := createMachineSetValidator(infra, c, dns)
machineSetValidator := createMachineSetValidator(infra, c, dns, gate)
mgr.GetWebhookServer().Register(DefaultMachineSetMutatingHookPath, &webhook.Admission{Handler: machineSetDefaulter})
mgr.GetWebhookServer().Register(DefaultMachineSetValidatingHookPath, &webhook.Admission{Handler: machineSetValidator})

Expand Down Expand Up @@ -1193,8 +1200,13 @@ func TestMachineSetUpdate(t *testing.T) {
infra.Status.InfrastructureName = tc.clusterID
infra.Status.PlatformStatus = platformStatus

gate, err := testutils.NewDefaultMutableFeatureGate()
if err != nil {
t.Errorf("Unexpected error setting up feature gates: %v", err)
}

machineSetDefaulter := createMachineSetDefaulter(platformStatus, tc.clusterID)
machineSetValidator := createMachineSetValidator(infra, c, plainDNS)
machineSetValidator := createMachineSetValidator(infra, c, plainDNS, gate)
mgr.GetWebhookServer().Register(DefaultMachineSetMutatingHookPath, &webhook.Admission{Handler: machineSetDefaulter})
mgr.GetWebhookServer().Register(DefaultMachineSetValidatingHookPath, &webhook.Admission{Handler: machineSetValidator})

Expand Down
1 change: 1 addition & 0 deletions pkg/webhooks/v1beta1_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestMain(m *testing.M) {
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "install"),
filepath.Join("..", "..", "..", "..", "vendor", "github.com", "openshift", "api", "config", "v1", "zz_generated.crd-manifests"),
filepath.Join("..", "..", "..", "..", "third_party", "cluster-api", "crd"),
},
WebhookInstallOptions: envtest.WebhookInstallOptions{
MutatingWebhooks: []*admissionregistrationv1.MutatingWebhookConfiguration{NewMachineMutatingWebhookConfiguration()},
Expand Down
Loading

0 comments on commit d5ccb46

Please sign in to comment.