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 23, 2024
1 parent 2bdbb7e commit da0d90c
Show file tree
Hide file tree
Showing 7 changed files with 571 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
3 changes: 2 additions & 1 deletion pkg/controller/vsphere/machine_scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ func TestNodeGetter(t *testing.T) {
testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "install"),
filepath.Join("..", "..", "..", "vendor", "github.com", "openshift", "api", "config", "v1", "zz_generated.crd-manifests")},
filepath.Join("..", "..", "..", "vendor", "github.com", "openshift", "api", "config", "v1", "zz_generated.crd-manifests"),
filepath.Join("..", "..", "..", "third_party", "cluster-api", "crds")},
}

cfg, err := testEnv.Start()
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
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 da0d90c

Please sign in to comment.