-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1981 from shiftstack/issue1980
🐛 Consolidate and fix v1beta1 fuzzer funcs
- Loading branch information
Showing
3 changed files
with
112 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package helpers | ||
|
||
import ( | ||
"strings" | ||
|
||
fuzz "github.com/google/gofuzz" | ||
|
||
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1" | ||
) | ||
|
||
func filterInvalidTags(tags []infrav1.NeutronTag) []infrav1.NeutronTag { | ||
var ret []infrav1.NeutronTag | ||
for i := range tags { | ||
s := string(tags[i]) | ||
if len(s) > 0 && !strings.Contains(s, ",") { | ||
ret = append(ret, tags[i]) | ||
} | ||
} | ||
return ret | ||
} | ||
|
||
// InfraV1FuzzerFuncs returns fuzzer funcs for v1beta1 OpenStack types which: | ||
// * Constrain the output in ways which are validated by the API server | ||
// * Add additional test coverage where it is not generated by the default fuzzer. | ||
func InfraV1FuzzerFuncs() []interface{} { | ||
return []interface{}{ | ||
func(spec *infrav1.OpenStackClusterSpec, c fuzz.Continue) { | ||
c.FuzzNoCustom(spec) | ||
|
||
// The fuzzer only seems to generate Subnets of | ||
// length 1, but we need to also test length 2. | ||
// Ensure it is occasionally generated. | ||
if len(spec.Subnets) == 1 && c.RandBool() { | ||
subnet := infrav1.SubnetFilter{} | ||
c.Fuzz(&subnet) | ||
spec.Subnets = append(spec.Subnets, subnet) | ||
} | ||
}, | ||
|
||
func(spec *infrav1.SubnetSpec, c fuzz.Continue) { | ||
c.FuzzNoCustom(spec) | ||
|
||
// CIDR is required and API validates that it's present, so | ||
// we force it to always be set. | ||
for spec.CIDR == "" { | ||
spec.CIDR = c.RandString() | ||
} | ||
}, | ||
|
||
func(pool *infrav1.AllocationPool, c fuzz.Continue) { | ||
c.FuzzNoCustom(pool) | ||
|
||
// Start and End are required properties, let's make sure both are set | ||
for pool.Start == "" { | ||
pool.Start = c.RandString() | ||
} | ||
|
||
for pool.End == "" { | ||
pool.End = c.RandString() | ||
} | ||
}, | ||
|
||
// v1beta1 filter tags cannot contain commas and can't be empty. | ||
func(filter *infrav1.FilterByNeutronTags, c fuzz.Continue) { | ||
c.FuzzNoCustom(filter) | ||
|
||
// Sometimes add an additional tag to ensure we get test coverage of multiple tags | ||
if c.RandBool() { | ||
filter.Tags = append(filter.Tags, infrav1.NeutronTag(c.RandString())) | ||
} | ||
if c.RandBool() { | ||
filter.TagsAny = append(filter.TagsAny, infrav1.NeutronTag(c.RandString())) | ||
} | ||
if c.RandBool() { | ||
filter.NotTags = append(filter.NotTags, infrav1.NeutronTag(c.RandString())) | ||
} | ||
if c.RandBool() { | ||
filter.NotTagsAny = append(filter.NotTagsAny, infrav1.NeutronTag(c.RandString())) | ||
} | ||
|
||
// Remove empty tags and tags with commas | ||
filter.Tags = filterInvalidTags(filter.Tags) | ||
filter.TagsAny = filterInvalidTags(filter.TagsAny) | ||
filter.NotTags = filterInvalidTags(filter.NotTags) | ||
filter.NotTagsAny = filterInvalidTags(filter.NotTagsAny) | ||
}, | ||
} | ||
} |