Skip to content

Commit ad801fd

Browse files
committed
conversion: fix fuzz tests to consider labels
1 parent 3a3b740 commit ad801fd

File tree

4 files changed

+229
-126
lines changed

4 files changed

+229
-126
lines changed

pkg/conversion/mapi2capi/aws_fuzz_test.go

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ var _ = Describe("AWS Fuzz (mapi2capi)", func() {
6969
return capi2mapi.FromMachineAndAWSMachineAndAWSCluster(machine, awsMachine, awsCluster)
7070
}
7171

72+
f := &awsProviderFuzzer{}
73+
7274
conversiontest.MAPI2CAPIMachineRoundTripFuzzTest(
7375
scheme,
7476
infra,
@@ -77,7 +79,7 @@ var _ = Describe("AWS Fuzz (mapi2capi)", func() {
7779
fromMachineAndAWSMachineAndAWSCluster,
7880
conversiontest.ObjectMetaFuzzerFuncs(mapiNamespace),
7981
conversiontest.MAPIMachineFuzzerFuncs(&mapiv1beta1.AWSMachineProviderConfig{}, &mapiv1beta1.AWSMachineProviderStatus{}, awsProviderIDFuzzer),
80-
awsProviderSpecFuzzerFuncs,
82+
f.FuzzerFuncsMachine,
8183
)
8284
})
8385

@@ -92,6 +94,8 @@ var _ = Describe("AWS Fuzz (mapi2capi)", func() {
9294
return capi2mapi.FromMachineSetAndAWSMachineTemplateAndAWSCluster(machineSet, awsMachineTemplate, awsCluster)
9395
}
9496

97+
f := &awsProviderFuzzer{}
98+
9599
conversiontest.MAPI2CAPIMachineSetRoundTripFuzzTest(
96100
scheme,
97101
infra,
@@ -101,7 +105,7 @@ var _ = Describe("AWS Fuzz (mapi2capi)", func() {
101105
conversiontest.ObjectMetaFuzzerFuncs(mapiNamespace),
102106
conversiontest.MAPIMachineFuzzerFuncs(&mapiv1beta1.AWSMachineProviderConfig{}, &mapiv1beta1.AWSMachineProviderStatus{}, awsProviderIDFuzzer),
103107
conversiontest.MAPIMachineSetFuzzerFuncs(),
104-
awsProviderSpecFuzzerFuncs,
108+
f.FuzzerFuncsMachineSet,
105109
)
106110
})
107111
})
@@ -110,8 +114,59 @@ func awsProviderIDFuzzer(c randfill.Continue) string {
110114
return "aws:///us-west-2a/i-" + strings.ReplaceAll(c.String(0), "/", "")
111115
}
112116

113-
//nolint:funlen,cyclop
114-
func awsProviderSpecFuzzerFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
117+
type awsProviderFuzzer struct {
118+
conversiontest.MAPIMachineFuzzer
119+
}
120+
121+
func (f *awsProviderFuzzer) fuzzProviderConfig(ps *mapiv1beta1.AWSMachineProviderConfig, c randfill.Continue) {
122+
c.FillNoCustom(ps)
123+
124+
// The type meta is always set to these values by the conversion.
125+
ps.APIVersion = mapiv1beta1.GroupVersion.String()
126+
ps.Kind = awsProviderSpecKind
127+
128+
// region must match the input AWSCluster so force it here.
129+
ps.Placement.Region = "us-east-1"
130+
131+
// Only one value here is valid in terms of fuzzing, so it is hardcoded.
132+
ps.CredentialsSecret = &corev1.LocalObjectReference{
133+
Name: mapi2capi.DefaultCredentialsSecretName,
134+
}
135+
136+
// Clear fields that are not supported in the provider spec.
137+
ps.DeviceIndex = 0
138+
ps.LoadBalancers = nil
139+
ps.ObjectMeta = metav1.ObjectMeta{}
140+
// TODO(OCPCLOUD-2713): remove this, temporarily hardcoded for AWS to make the migration to work.
141+
ps.CredentialsSecret = &corev1.LocalObjectReference{Name: "aws-cloud-credentials"}
142+
143+
// At least one device mapping must have no device name.
144+
rootFound := false
145+
146+
for i := range ps.BlockDevices {
147+
if ps.BlockDevices[i].DeviceName == nil {
148+
rootFound = true
149+
break
150+
}
151+
}
152+
153+
if !rootFound && len(ps.BlockDevices) > 0 {
154+
ps.BlockDevices[0].DeviceName = nil
155+
}
156+
157+
// Clear pointers to empty structs.
158+
if ps.UserDataSecret != nil && ps.UserDataSecret.Name == "" {
159+
ps.UserDataSecret = nil
160+
}
161+
162+
// Copy instance-type, region and zone to the struct so they can be set at the machine labels too.
163+
f.MAPIMachineFuzzer.InstanceType = ps.InstanceType
164+
f.MAPIMachineFuzzer.Region = ps.Placement.Region
165+
f.MAPIMachineFuzzer.Zone = ps.Placement.AvailabilityZone
166+
}
167+
168+
//nolint:funlen
169+
func (f *awsProviderFuzzer) FuzzerFuncsMachineSet(codecs runtimeserializer.CodecFactory) []interface{} {
115170
return []interface{}{
116171
func(nit *mapiv1beta1.AWSNetworkInterfaceType, c randfill.Continue) {
117172
switch c.Int31n(3) {
@@ -204,45 +259,13 @@ func awsProviderSpecFuzzerFuncs(codecs runtimeserializer.CodecFactory) []interfa
204259
// resulting in a documented lossy rountrip conversion, which would make the test to fail.
205260
}
206261
},
207-
func(ps *mapiv1beta1.AWSMachineProviderConfig, c randfill.Continue) {
208-
c.FillNoCustom(ps)
209-
210-
// The type meta is always set to these values by the conversion.
211-
ps.APIVersion = mapiv1beta1.GroupVersion.String()
212-
ps.Kind = awsProviderSpecKind
213-
214-
// region must match the input AWSCluster so force it here.
215-
ps.Placement.Region = "us-east-1"
216-
217-
// Only one value here is valid in terms of fuzzing, so it is hardcoded.
218-
ps.CredentialsSecret = &corev1.LocalObjectReference{
219-
Name: mapi2capi.DefaultCredentialsSecretName,
220-
}
221-
222-
// Clear fields that are not supported in the provider spec.
223-
ps.DeviceIndex = 0
224-
ps.LoadBalancers = nil
225-
ps.ObjectMeta = metav1.ObjectMeta{}
226-
// TODO(OCPCLOUD-2713): remove this, temporarily hardcoded for AWS to make the migration to work.
227-
ps.CredentialsSecret = &corev1.LocalObjectReference{Name: "aws-cloud-credentials"}
228-
229-
// At least one device mapping must have no device name.
230-
rootFound := false
231-
for i := range ps.BlockDevices {
232-
if ps.BlockDevices[i].DeviceName == nil {
233-
rootFound = true
234-
break
235-
}
236-
}
237-
238-
if !rootFound && len(ps.BlockDevices) > 0 {
239-
ps.BlockDevices[0].DeviceName = nil
240-
}
241-
242-
// Clear pointers to empty structs.
243-
if ps.UserDataSecret != nil && ps.UserDataSecret.Name == "" {
244-
ps.UserDataSecret = nil
245-
}
246-
},
262+
f.fuzzProviderConfig,
247263
}
248264
}
265+
266+
func (f *awsProviderFuzzer) FuzzerFuncsMachine(codecs runtimeserializer.CodecFactory) []interface{} {
267+
return append(
268+
f.FuzzerFuncsMachineSet(codecs),
269+
f.FuzzMachine,
270+
)
271+
}

pkg/conversion/mapi2capi/openstack_fuzz_test.go

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ var _ = Describe("OpenStack Fuzz (mapi2capi)", func() {
6666
return capi2mapi.FromMachineAndOpenStackMachineAndOpenStackCluster(machine, openstackMachine, openstackCluster)
6767
}
6868

69+
f := &openstackProviderFuzzer{}
70+
6971
conversiontest.MAPI2CAPIMachineRoundTripFuzzTest(
7072
scheme,
7173
infra,
@@ -74,7 +76,7 @@ var _ = Describe("OpenStack Fuzz (mapi2capi)", func() {
7476
fromMachineAndOpenStackMachineAndOpenStackCluster,
7577
conversiontest.ObjectMetaFuzzerFuncs(mapiNamespace),
7678
conversiontest.MAPIMachineFuzzerFuncs(&mapiv1alpha1.OpenstackProviderSpec{}, nil, openstackProviderIDFuzzer),
77-
openstackProviderSpecFuzzerFuncs,
79+
f.FuzzerFuncsMachine,
7880
)
7981
})
8082

@@ -89,6 +91,8 @@ var _ = Describe("OpenStack Fuzz (mapi2capi)", func() {
8991
return capi2mapi.FromMachineSetAndOpenStackMachineTemplateAndOpenStackCluster(machineSet, openstackMachineTemplate, openstackCluster)
9092
}
9193

94+
f := &openstackProviderFuzzer{}
95+
9296
conversiontest.MAPI2CAPIMachineSetRoundTripFuzzTest(
9397
scheme,
9498
infra,
@@ -98,7 +102,7 @@ var _ = Describe("OpenStack Fuzz (mapi2capi)", func() {
98102
conversiontest.ObjectMetaFuzzerFuncs(mapiNamespace),
99103
conversiontest.MAPIMachineFuzzerFuncs(&mapiv1alpha1.OpenstackProviderSpec{}, nil, openstackProviderIDFuzzer),
100104
conversiontest.MAPIMachineSetFuzzerFuncs(),
101-
openstackProviderSpecFuzzerFuncs,
105+
f.FuzzerFuncsMachineSet,
102106
)
103107
})
104108
})
@@ -107,8 +111,62 @@ func openstackProviderIDFuzzer(c randfill.Continue) string {
107111
return "openstack://" + uuid.NewString()
108112
}
109113

114+
type openstackProviderFuzzer struct {
115+
conversiontest.MAPIMachineFuzzer
116+
}
117+
118+
func (f *openstackProviderFuzzer) fuzzProviderSpec(providerSpec *mapiv1alpha1.OpenstackProviderSpec, c randfill.Continue) {
119+
c.FillNoCustom(providerSpec)
120+
121+
// The type meta is always set to these values by the conversion.
122+
providerSpec.APIVersion = mapiv1alpha1.GroupVersion.String()
123+
providerSpec.Kind = openstackProviderSpecKind
124+
125+
// Clear fields that are not supported in the provider spec.
126+
providerSpec.ObjectMeta = metav1.ObjectMeta{}
127+
providerSpec.FloatingIP = ""
128+
providerSpec.PrimarySubnet = ""
129+
providerSpec.SshUserName = ""
130+
131+
// Clear namespace fields, since these are intentionally not copied
132+
if providerSpec.UserDataSecret != nil {
133+
providerSpec.UserDataSecret.Namespace = ""
134+
}
135+
136+
if providerSpec.CloudsSecret != nil {
137+
providerSpec.CloudsSecret.Namespace = ""
138+
139+
if providerSpec.CloudsSecret.Name == "" {
140+
providerSpec.CloudsSecret = nil
141+
}
142+
}
143+
144+
// Clear fields that depend on other, unset fields or cannot coexist
145+
if providerSpec.CloudsSecret == nil {
146+
providerSpec.CloudName = ""
147+
}
148+
149+
switch c.Int31n(2) {
150+
case 0:
151+
providerSpec.ServerGroupID = uuid.NewString()
152+
providerSpec.ServerGroupName = ""
153+
case 1:
154+
providerSpec.ServerGroupID = ""
155+
providerSpec.ServerGroupName = uuid.NewString()
156+
}
157+
158+
// Clear pointers to empty structs.
159+
if providerSpec.UserDataSecret != nil && providerSpec.UserDataSecret.Name == "" {
160+
providerSpec.UserDataSecret = nil
161+
}
162+
163+
// Copy instance-type, region and zone to the struct so they can be set at the machine labels too.
164+
f.MAPIMachineFuzzer.InstanceType = providerSpec.Flavor
165+
f.MAPIMachineFuzzer.Zone = providerSpec.AvailabilityZone
166+
}
167+
110168
//nolint:funlen
111-
func openstackProviderSpecFuzzerFuncs(codecs runtimeserializer.CodecFactory) []any {
169+
func (f *openstackProviderFuzzer) FuzzerFuncsMachineSet(codecs runtimeserializer.CodecFactory) []any {
112170
return []any{
113171
func(bdm *mapiv1alpha1.BlockDeviceStorage, c randfill.Continue) {
114172
switch c.Int31n(2) {
@@ -194,54 +252,17 @@ func openstackProviderSpecFuzzerFuncs(codecs runtimeserializer.CodecFactory) []a
194252
securityGroup.Filter.NotTagsAny = generateFakeTags()
195253
}
196254
},
197-
func(providerSpec *mapiv1alpha1.OpenstackProviderSpec, c randfill.Continue) {
198-
c.FillNoCustom(providerSpec)
199-
200-
// The type meta is always set to these values by the conversion.
201-
providerSpec.APIVersion = mapiv1alpha1.GroupVersion.String()
202-
providerSpec.Kind = openstackProviderSpecKind
203-
204-
// Clear fields that are not supported in the provider spec.
205-
providerSpec.ObjectMeta = metav1.ObjectMeta{}
206-
providerSpec.FloatingIP = ""
207-
providerSpec.PrimarySubnet = ""
208-
providerSpec.SshUserName = ""
209-
210-
// Clear namespace fields, since these are intentionally not copied
211-
if providerSpec.UserDataSecret != nil {
212-
providerSpec.UserDataSecret.Namespace = ""
213-
}
214-
215-
if providerSpec.CloudsSecret != nil {
216-
providerSpec.CloudsSecret.Namespace = ""
217-
218-
if providerSpec.CloudsSecret.Name == "" {
219-
providerSpec.CloudsSecret = nil
220-
}
221-
}
222-
223-
// Clear fields that depend on other, unset fields or cannot coexist
224-
if providerSpec.CloudsSecret == nil {
225-
providerSpec.CloudName = ""
226-
}
227-
228-
switch c.Int31n(2) {
229-
case 0:
230-
providerSpec.ServerGroupID = uuid.NewString()
231-
providerSpec.ServerGroupName = ""
232-
case 1:
233-
providerSpec.ServerGroupID = ""
234-
providerSpec.ServerGroupName = uuid.NewString()
235-
}
236-
237-
// Clear pointers to empty structs.
238-
if providerSpec.UserDataSecret != nil && providerSpec.UserDataSecret.Name == "" {
239-
providerSpec.UserDataSecret = nil
240-
}
241-
},
255+
f.fuzzProviderSpec,
242256
}
243257
}
244258

259+
func (f *openstackProviderFuzzer) FuzzerFuncsMachine(codecs runtimeserializer.CodecFactory) []interface{} {
260+
return append(
261+
f.FuzzerFuncsMachineSet(codecs),
262+
f.FuzzMachine,
263+
)
264+
}
265+
245266
// generateFakeTags generate a fake alphanumeric CSV string for use in a tags field.
246267
func generateFakeTags() string {
247268
var buffer bytes.Buffer

0 commit comments

Comments
 (0)