Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Commit

Permalink
DeepCopy the entire set of conditions (#128) (#1645)
Browse files Browse the repository at this point in the history
* DeepCopy the entire set of conditions

* remove unused function

* add fuzzing testing
  • Loading branch information
lionelvillard authored Oct 23, 2020
1 parent d8743c5 commit 4f0b170
Show file tree
Hide file tree
Showing 14 changed files with 1,856 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/golang/protobuf v1.4.2
github.com/google/go-cmp v0.5.2
github.com/google/gofuzz v1.1.0
github.com/google/uuid v1.1.1
github.com/gorilla/websocket v1.4.2
github.com/influxdata/tdigest v0.0.1 // indirect
Expand Down
19 changes: 15 additions & 4 deletions kafka/source/pkg/apis/sources/v1alpha1/kafka_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ func (source *KafkaSource) ConvertTo(ctx context.Context, obj apis.Convertible)
Topics: source.Spec.Topics,
ConsumerGroup: source.Spec.ConsumerGroup,
}
sink.Status.Status = source.Status.Status
source.Status.Status.ConvertTo(ctx, &sink.Status.Status)
source.Status.Status.DeepCopyInto(&sink.Status.Status)

// Optionals
if source.Spec.Sink != nil {
sink.Spec.Sink = *source.Spec.Sink.DeepCopy()
}

if source.Spec.CloudEventOverrides != nil {
sink.Spec.CloudEventOverrides = source.Spec.CloudEventOverrides.DeepCopy()
}

if source.Status.SinkURI != nil {
sink.Status.SinkURI = source.Status.SinkURI.DeepCopy()
}
Expand Down Expand Up @@ -83,12 +88,18 @@ func (sink *KafkaSource) ConvertFrom(ctx context.Context, obj apis.Convertible)
if reflect.DeepEqual(*sink.Spec.Sink, duckv1.Destination{}) {
sink.Spec.Sink = nil
}
sink.Status.Status = source.Status.Status
source.Status.Status.ConvertTo(ctx, &source.Status.Status)

source.Status.Status.DeepCopyInto(&sink.Status.Status)

// Optionals
if source.Status.SinkURI != nil {
sink.Status.SinkURI = source.Status.SinkURI.DeepCopy()
}

if source.Spec.CloudEventOverrides != nil {
sink.Spec.CloudEventOverrides = source.Spec.CloudEventOverrides.DeepCopy()
}

if source.Status.CloudEventAttributes != nil {
sink.Status.CloudEventAttributes = make([]duckv1.CloudEventAttributes, len(source.Status.CloudEventAttributes))
copy(sink.Status.CloudEventAttributes, source.Status.CloudEventAttributes)
Expand Down
6 changes: 6 additions & 0 deletions kafka/source/pkg/apis/sources/v1alpha1/kafka_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ type KafkaSourceSpec struct {
// Resource limits and Request specifications of the Receive Adapter Deployment
// Deprecated: v1beta1 drops this field.
Resources KafkaResourceSpec `json:"resources,omitempty"`

// CloudEventOverrides defines overrides to control the output format and
// modifications of the event sent to the sink.
// +optional
// Needed for supporting round-tripping
CloudEventOverrides *duckv1.CloudEventOverrides `json:"ceOverrides,omitempty"`
}

const (
Expand Down
63 changes: 63 additions & 0 deletions kafka/source/pkg/apis/sources/v1alpha1/roundtrip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 v1alpha1

import (
"testing"

"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"

"knative.dev/eventing-contrib/kafka/source/pkg/apis/sources/v1beta1"
)

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

fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
pkgfuzzer.Funcs,
v1beta1.FuzzerFuncs,
)
roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
}

func TestSourceRoundTripTypesToAlphaHub(t *testing.T) {
scheme := runtime.NewScheme()

sb := runtime.SchemeBuilder{
AddToScheme,
v1beta1.AddToScheme,
}

utilruntime.Must(sb.AddToScheme(scheme))

hubs := runtime.NewScheme()
hubs.AddKnownTypes(SchemeGroupVersion,
&KafkaSource{},
)

fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
pkgfuzzer.Funcs,
v1beta1.FuzzerFuncs,
)

roundtrip.ExternalTypesViaHub(t, scheme, hubs, fuzzerFuncs)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions kafka/source/pkg/apis/sources/v1beta1/fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
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 v1beta1

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 sources.knative.dev v1beta1 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 *KafkaSourceStatus, 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)
},
}
},
)
38 changes: 38 additions & 0 deletions kafka/source/pkg/apis/sources/v1beta1/roundtrip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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 v1beta1

import (
"testing"

"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"
)

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

fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
pkgfuzzer.Funcs,
FuzzerFuncs,
)
roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
}
5 changes: 0 additions & 5 deletions test/lib/listers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package lib
import (
"k8s.io/apimachinery/pkg/runtime"
fakekubeclientset "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
fakeeventingclientset "knative.dev/eventing/pkg/client/clientset/versioned/fake"
"knative.dev/pkg/reconciler/testing"
)
Expand Down Expand Up @@ -58,10 +57,6 @@ func NewListers(objs []runtime.Object) Listers {
return ls
}

func (l Listers) indexerFor(obj runtime.Object) cache.Indexer {
return l.sorter.IndexerForObjectType(obj)
}

func (l Listers) GetKubeObjects() []runtime.Object {
return l.sorter.ObjectsForSchemeFunc(fakekubeclientset.AddToScheme)
}
Expand Down
116 changes: 116 additions & 0 deletions vendor/k8s.io/apimachinery/pkg/api/apitesting/codec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4f0b170

Please sign in to comment.