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

add roundtrip tests #3574

Merged
merged 5 commits into from
Jul 15, 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/golang/protobuf v1.4.2
github.com/google/go-cmp v0.5.0
github.com/google/gofuzz v1.1.0
github.com/google/mako v0.0.0-20190821191249-122f8dcef9e3
github.com/google/uuid v1.1.1
github.com/influxdata/tdigest v0.0.0-20191024211133-5d87a7585faa // indirect
Expand Down
49 changes: 49 additions & 0 deletions pkg/apis/duck/v1/fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2020 The Knative 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 v1

import (
"math/rand"

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}

// FuzzerFuncs includes fuzzing funcs for knative.dev/duck v1 types
// In particular it makes sure that Delivery has only valid BackoffPolicyType in it.
//
// 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(ds *DeliverySpec, c fuzz.Continue) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means it won’t fuzz the other fields of the delivery spec - you may want to fuzz it here (no custom) and just override the back off policy after

c.FuzzNoCustom(ds) // fuzz the DeliverySpec
if ds.BackoffPolicy != nil && *ds.BackoffPolicy == "" {
ds.BackoffPolicy = nil
} else {
ds.BackoffPolicy = bops[rand.Intn(3)]
}
},
}
},
)
5 changes: 5 additions & 0 deletions pkg/apis/duck/v1beta1/delivery_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (source *DeliverySpec) ConvertTo(ctx context.Context, to apis.Convertible)
} else if *source.BackoffPolicy == BackoffPolicyExponential {
exponential := eventingduckv1.BackoffPolicyExponential
sink.BackoffPolicy = &exponential
} else {
return fmt.Errorf("unknown BackoffPolicy, got: %q", *source.BackoffPolicy)
}
}
sink.DeadLetterSink = source.DeadLetterSink
Expand All @@ -60,7 +62,10 @@ func (sink *DeliverySpec) ConvertFrom(ctx context.Context, from apis.Convertible
} else if *source.BackoffPolicy == eventingduckv1.BackoffPolicyExponential {
exponential := BackoffPolicyExponential
sink.BackoffPolicy = &exponential
} else {
return fmt.Errorf("unknown BackoffPolicy, got: %q", *source.BackoffPolicy)
}

}
sink.DeadLetterSink = source.DeadLetterSink
return nil
Expand Down
236 changes: 236 additions & 0 deletions pkg/apis/duck/v1beta1/delivery_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ package v1beta1
import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
v1 "knative.dev/eventing/pkg/apis/duck/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
pkgduck "knative.dev/pkg/apis/duck/v1"
)

func TestDeliverySpecConversionBadType(t *testing.T) {
Expand All @@ -44,3 +50,233 @@ func TestDeliveryStatusConversionBadType(t *testing.T) {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

// Test v1beta1 -> v1 -> v1beta1
func TestDeliverySpecConversion(t *testing.T) {
var retryCount int32 = 10
var backoffPolicy BackoffPolicyType = BackoffPolicyLinear
var backoffPolicyExp BackoffPolicyType = BackoffPolicyExponential
var backoffPolicyBad BackoffPolicyType = "garbage"
badPolicyString := `unknown BackoffPolicy, got: "garbage"`

tests := []struct {
name string
in *DeliverySpec
err *string
}{{
name: "min configuration",
in: &DeliverySpec{
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
}, {
name: "with retry",
in: &DeliverySpec{
Retry: &retryCount,
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
}, {
name: "with linear backoff",
in: &DeliverySpec{
Retry: &retryCount,
BackoffPolicy: &backoffPolicy,
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
}, {
name: "with exp backoff",
in: &DeliverySpec{
Retry: &retryCount,
BackoffPolicy: &backoffPolicyExp,
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
}, {
name: "with bad backoff",
in: &DeliverySpec{
Retry: &retryCount,
BackoffPolicy: &backoffPolicyBad,
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
err: &badPolicyString,
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ver := &v1.DeliverySpec{}
err := test.in.ConvertTo(context.Background(), ver)
if err != nil {
if test.err == nil || *test.err != err.Error() {
t.Errorf("ConvertTo() = %v", err)
}
return
}
got := &DeliverySpec{}
if err := got.ConvertFrom(context.Background(), ver); err != nil {
t.Errorf("ConvertFrom() = %v", err)
}
if diff := cmp.Diff(test.in, got); diff != "" {
t.Errorf("roundtrip (-want, +got) = %v", diff)
}
})
}
}

// Test v1 -> v1beta1 -> v1
func TestDeliverySpecConversionV1(t *testing.T) {
var retryCount int32 = 10
var backoffPolicy v1.BackoffPolicyType = v1.BackoffPolicyLinear
var backoffPolicyExp v1.BackoffPolicyType = v1.BackoffPolicyExponential
var backoffPolicyBad v1.BackoffPolicyType = "garbage"
badPolicyString := `unknown BackoffPolicy, got: "garbage"`

tests := []struct {
name string
in *v1.DeliverySpec
err *string
}{{
name: "min configuration",
in: &v1.DeliverySpec{
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
}, {
name: "with retry",
in: &v1.DeliverySpec{
Retry: &retryCount,
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
}, {
name: "with linear backoff",
in: &v1.DeliverySpec{
Retry: &retryCount,
BackoffPolicy: &backoffPolicy,
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
}, {
name: "with exp backoff",
in: &v1.DeliverySpec{
Retry: &retryCount,
BackoffPolicy: &backoffPolicyExp,
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
}, {
name: "with bad backoff",
in: &v1.DeliverySpec{
Retry: &retryCount,
BackoffPolicy: &backoffPolicyBad,
DeadLetterSink: &pkgduck.Destination{
URI: apis.HTTP("example.com"),
},
},
err: &badPolicyString,
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ver := &DeliverySpec{}
err := ver.ConvertFrom(context.Background(), test.in)
if err != nil {
if test.err == nil || *test.err != err.Error() {
t.Errorf("ConvertFrom() = %v", err)
}
return
}
got := &v1.DeliverySpec{}
if err := ver.ConvertTo(context.Background(), got); err != nil {
t.Errorf("ConvertTo() = %v", err)
}
if diff := cmp.Diff(test.in, got); diff != "" {
t.Errorf("roundtrip (-want, +got) = %v", diff)
}
})
}
}

// Test v1beta1 -> v1 -> v1beta1
func TestDeliveryStatusConversion(t *testing.T) {
tests := []struct {
name string
in *DeliveryStatus
err *string
}{{
name: "min configuration",
in: &DeliveryStatus{
DeadLetterChannel: &duckv1.KReference{
Kind: "dlKind",
Namespace: "dlNamespace",
Name: "dlName",
APIVersion: "dlAPIVersion",
},
},
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ver := &v1.DeliveryStatus{}
err := test.in.ConvertTo(context.Background(), ver)
if err != nil {
if test.err == nil || *test.err != err.Error() {
t.Errorf("ConvertTo() = %v", err)
}
return
}
got := &DeliveryStatus{}
if err := got.ConvertFrom(context.Background(), ver); err != nil {
t.Errorf("ConvertFrom() = %v", err)
}
if diff := cmp.Diff(test.in, got); diff != "" {
t.Errorf("roundtrip (-want, +got) = %v", diff)
}
})
}
}

// Test v1 -> v1beta1 -> v1
func TestDeliveryStatusConversionV1(t *testing.T) {
tests := []struct {
name string
in *v1.DeliveryStatus
err *string
}{{
name: "min configuration",
in: &v1.DeliveryStatus{
DeadLetterChannel: &duckv1.KReference{
Kind: "dlKind",
Namespace: "dlNamespace",
Name: "dlName",
APIVersion: "dlAPIVersion",
},
},
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ver := &DeliveryStatus{}
err := ver.ConvertFrom(context.Background(), test.in)
if err != nil {
if test.err == nil || *test.err != err.Error() {
t.Errorf("ConvertFrom() = %v", err)
}
return
}
got := &v1.DeliveryStatus{}
if err := ver.ConvertTo(context.Background(), got); err != nil {
t.Errorf("ConvertTo() = %v", err)
}
if diff := cmp.Diff(test.in, got); diff != "" {
t.Errorf("roundtrip (-want, +got) = %v", diff)
}
})
}
}
55 changes: 55 additions & 0 deletions pkg/apis/eventing/v1/fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2020 The Knative 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 v1

import (
fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
"k8s.io/apimachinery/pkg/runtime/serializer"
pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
)

// 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)
},
}
},
)
Loading