Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vr4manta committed Sep 18, 2024
1 parent f1a2ff8 commit b05d938
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 11 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
18 changes: 17 additions & 1 deletion pkg/controller/vsphere/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"strings"
"testing"

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

. "github.com/onsi/gomega"

"github.com/vmware/govmomi/object"
Expand Down Expand Up @@ -178,6 +180,7 @@ func TestClone(t *testing.T) {
}

getMachineScope := func(providerSpec *machinev1.VSphereMachineProviderSpec) *machineScope {
gates, _ := testutils.NewDefaultMutableFeatureGate()
return &machineScope{
Context: context.TODO(),
machine: &machinev1.Machine{
Expand All @@ -193,6 +196,7 @@ func TestClone(t *testing.T) {
session: session,
providerStatus: &machinev1.VSphereMachineProviderStatus{},
client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(&credentialsSecret, &userDataSecret).Build(),
featureGates: gates,
}
}

Expand Down Expand Up @@ -1953,6 +1957,11 @@ func TestDelete(t *testing.T) {
t.Fatal(err)
}

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

client := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(
simParams.secret,
tc.machine(t, simParams.host),
Expand All @@ -1965,6 +1974,7 @@ func TestDelete(t *testing.T) {
machine: tc.machine(t, simParams.host),
apiReader: client,
openshiftConfigNameSpace: openshiftConfigNamespaceForTest,
featureGates: gates,
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -2613,13 +2623,19 @@ func TestCreate(t *testing.T) {
}

client := builder.Build()
//TODO: NAG - Add feature gates in here

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

machineScope, err := newMachineScope(machineScopeParams{
client: client,
Context: context.Background(),
machine: machine,
apiReader: client,
openshiftConfigNameSpace: openshiftConfigNamespaceForTest,
featureGates: gates,
})
if err != nil {
t.Fatal(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 @@ -1130,6 +1132,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 @@ -1159,7 +1166,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 @@ -1932,6 +1939,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 @@ -1957,7 +1969,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 @@ -2338,7 +2350,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 @@ -3008,7 +3026,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 @@ -3683,7 +3706,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 @@ -4136,7 +4165,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 @@ -4368,7 +4403,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 @@ -4797,7 +4838,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 @@ -5091,7 +5137,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

0 comments on commit b05d938

Please sign in to comment.