Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move fuzzer code to _test files #4399

Merged
merged 2 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pkg/apis/duck/v1/fuzzer.go → pkg/apis/duck/v1/test/fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1
package test

import (
"math/rand"

v1 "knative.dev/eventing/pkg/apis/duck/v1"

fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
"k8s.io/apimachinery/pkg/runtime/serializer"
)

var linear = BackoffPolicyLinear
var exponential = BackoffPolicyExponential
var bops = []*BackoffPolicyType{nil, &linear, &exponential}
var linear = v1.BackoffPolicyLinear
var exponential = v1.BackoffPolicyExponential
var bops = []*v1.BackoffPolicyType{nil, &linear, &exponential}

// FuzzerFuncs includes fuzzing funcs for knative.dev/duck v1 types
// In particular it makes sure that Delivery has only valid BackoffPolicyType in it.
Expand All @@ -36,7 +38,7 @@ var bops = []*BackoffPolicyType{nil, &linear, &exponential}
var FuzzerFuncs = fuzzer.MergeFuzzerFuncs(
func(codecs serializer.CodecFactory) []interface{} {
return []interface{}{
func(ds *DeliverySpec, c fuzz.Continue) {
func(ds *v1.DeliverySpec, c fuzz.Continue) {
c.FuzzNoCustom(ds) // fuzz the DeliverySpec
if ds.BackoffPolicy != nil && *ds.BackoffPolicy == "" {
ds.BackoffPolicy = nil
Expand Down
55 changes: 0 additions & 55 deletions pkg/apis/eventing/v1/fuzzer.go

This file was deleted.

34 changes: 34 additions & 0 deletions pkg/apis/eventing/v1/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,47 @@ package v1
import (
"testing"

fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/runtime/serializer"

"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
"knative.dev/pkg/apis/testing/roundtrip"
)

// FuzzerFuncs includes fuzzing funcs for knative.dev/eventing v1 types
//
// For other examples see
// https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/fuzzer/fuzzer.go
var FuzzerFuncs = fuzzer.MergeFuzzerFuncs(
func(codecs serializer.CodecFactory) []interface{} {
return []interface{}{
func(s *TriggerStatus, c fuzz.Continue) {
c.FuzzNoCustom(s) // fuzz the status object

// Clear the random fuzzed condition
s.Status.SetConditions(nil)

// Fuzz the known conditions except their type value
s.InitializeConditions()
pkgfuzzer.FuzzConditions(&s.Status, c)
},
func(s *BrokerStatus, c fuzz.Continue) {
c.FuzzNoCustom(s) // fuzz the status object

// Clear the random fuzzed condition
s.Status.SetConditions(nil)

// Fuzz the known conditions except their type value
s.InitializeConditions()
pkgfuzzer.FuzzConditions(&s.Status, c)
},
}
},
)

func TestEventingRoundTripTypesToJSON(t *testing.T) {
scheme := runtime.NewScheme()
utilruntime.Must(AddToScheme(scheme))
Expand Down
42 changes: 38 additions & 4 deletions pkg/apis/eventing/v1beta1/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,56 @@ package v1beta1
import (
"testing"

fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/runtime/serializer"
"knative.dev/eventing/pkg/apis/duck/v1/test"

"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
duckerfuzzer "knative.dev/eventing/pkg/apis/duck/v1"
v1 "knative.dev/eventing/pkg/apis/eventing/v1"
pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
"knative.dev/pkg/apis/testing/roundtrip"
)

// FuzzerFuncs includes fuzzing funcs for knative.dev/eventing v1 types
//
// For other examples see
// https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/fuzzer/fuzzer.go
var FuzzerFuncs = fuzzer.MergeFuzzerFuncs(
func(codecs serializer.CodecFactory) []interface{} {
return []interface{}{
func(s *v1.TriggerStatus, c fuzz.Continue) {
c.FuzzNoCustom(s) // fuzz the status object

// Clear the random fuzzed condition
s.Status.SetConditions(nil)

// Fuzz the known conditions except their type value
s.InitializeConditions()
pkgfuzzer.FuzzConditions(&s.Status, c)
},
func(s *v1.BrokerStatus, c fuzz.Continue) {
c.FuzzNoCustom(s) // fuzz the status object

// Clear the random fuzzed condition
s.Status.SetConditions(nil)

// Fuzz the known conditions except their type value
s.InitializeConditions()
pkgfuzzer.FuzzConditions(&s.Status, c)
},
}
},
)

func TestEventingRoundTripTypesToJSON(t *testing.T) {
scheme := runtime.NewScheme()
utilruntime.Must(AddToScheme(scheme))

fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
pkgfuzzer.Funcs,
v1.FuzzerFuncs,
FuzzerFuncs,
)
roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
}
Expand All @@ -58,8 +92,8 @@ func TestEventingRoundTripTypesToBetaHub(t *testing.T) {

fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
pkgfuzzer.Funcs,
v1.FuzzerFuncs,
duckerfuzzer.FuzzerFuncs,
FuzzerFuncs,
test.FuzzerFuncs,
)

roundtrip.ExternalTypesViaHub(t, scheme, hubs, fuzzerFuncs)
Expand Down
55 changes: 0 additions & 55 deletions pkg/apis/flows/v1/fuzzer.go

This file was deleted.

34 changes: 34 additions & 0 deletions pkg/apis/flows/v1/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,47 @@ package v1
import (
"testing"

fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/runtime/serializer"

"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
"knative.dev/pkg/apis/testing/roundtrip"
)

// FuzzerFuncs includes fuzzing funcs for knative.dev/flows v1 types
//
// For other examples see
// https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/fuzzer/fuzzer.go
var FuzzerFuncs = fuzzer.MergeFuzzerFuncs(
func(codecs serializer.CodecFactory) []interface{} {
return []interface{}{
func(s *SequenceStatus, c fuzz.Continue) {
c.FuzzNoCustom(s) // fuzz the status object

// Clear the random fuzzed condition
s.Status.SetConditions(nil)

// Fuzz the known conditions except their type value
s.InitializeConditions()
pkgfuzzer.FuzzConditions(&s.Status, c)
},
func(s *ParallelStatus, c fuzz.Continue) {
c.FuzzNoCustom(s) // fuzz the status object

// Clear the random fuzzed condition
s.Status.SetConditions(nil)

// Fuzz the known conditions except their type value
s.InitializeConditions()
pkgfuzzer.FuzzConditions(&s.Status, c)
},
}
},
)

func TestEventingRoundTripTypesToJSON(t *testing.T) {
scheme := runtime.NewScheme()
utilruntime.Must(AddToScheme(scheme))
Expand Down
42 changes: 38 additions & 4 deletions pkg/apis/flows/v1beta1/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,56 @@ package v1beta1
import (
"testing"

fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/runtime/serializer"
"knative.dev/eventing/pkg/apis/duck/v1/test"

"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
duckerfuzzer "knative.dev/eventing/pkg/apis/duck/v1"
v1 "knative.dev/eventing/pkg/apis/flows/v1"
pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
"knative.dev/pkg/apis/testing/roundtrip"
)

// FuzzerFuncs includes fuzzing funcs for knative.dev/flows v1 types
//
// For other examples see
// https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/fuzzer/fuzzer.go
var FuzzerFuncs = fuzzer.MergeFuzzerFuncs(
func(codecs serializer.CodecFactory) []interface{} {
return []interface{}{
func(s *v1.SequenceStatus, c fuzz.Continue) {
c.FuzzNoCustom(s) // fuzz the status object

// Clear the random fuzzed condition
s.Status.SetConditions(nil)

// Fuzz the known conditions except their type value
s.InitializeConditions()
pkgfuzzer.FuzzConditions(&s.Status, c)
},
func(s *v1.ParallelStatus, c fuzz.Continue) {
c.FuzzNoCustom(s) // fuzz the status object

// Clear the random fuzzed condition
s.Status.SetConditions(nil)

// Fuzz the known conditions except their type value
s.InitializeConditions()
pkgfuzzer.FuzzConditions(&s.Status, c)
},
}
},
)

func TestFlowsRoundTripTypesToJSON(t *testing.T) {
scheme := runtime.NewScheme()
utilruntime.Must(AddToScheme(scheme))

fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
pkgfuzzer.Funcs,
v1.FuzzerFuncs,
FuzzerFuncs,
)
roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
}
Expand All @@ -57,8 +91,8 @@ func TestFlowsRoundTripTypesToBetaHub(t *testing.T) {

fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
pkgfuzzer.Funcs,
v1.FuzzerFuncs,
duckerfuzzer.FuzzerFuncs,
FuzzerFuncs,
test.FuzzerFuncs,
)

roundtrip.ExternalTypesViaHub(t, scheme, hubs, fuzzerFuncs)
Expand Down
Loading