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

Commit 4f0b170

Browse files
DeepCopy the entire set of conditions (#128) (#1645)
* DeepCopy the entire set of conditions * remove unused function * add fuzzing testing
1 parent d8743c5 commit 4f0b170

File tree

14 files changed

+1856
-9
lines changed

14 files changed

+1856
-9
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/davecgh/go-spew v1.1.1
1010
github.com/golang/protobuf v1.4.2
1111
github.com/google/go-cmp v0.5.2
12+
github.com/google/gofuzz v1.1.0
1213
github.com/google/uuid v1.1.1
1314
github.com/gorilla/websocket v1.4.2
1415
github.com/influxdata/tdigest v0.0.1 // indirect

kafka/source/pkg/apis/sources/v1alpha1/kafka_conversion.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,17 @@ func (source *KafkaSource) ConvertTo(ctx context.Context, obj apis.Convertible)
4444
Topics: source.Spec.Topics,
4545
ConsumerGroup: source.Spec.ConsumerGroup,
4646
}
47-
sink.Status.Status = source.Status.Status
48-
source.Status.Status.ConvertTo(ctx, &sink.Status.Status)
47+
source.Status.Status.DeepCopyInto(&sink.Status.Status)
48+
4949
// Optionals
5050
if source.Spec.Sink != nil {
5151
sink.Spec.Sink = *source.Spec.Sink.DeepCopy()
5252
}
53+
54+
if source.Spec.CloudEventOverrides != nil {
55+
sink.Spec.CloudEventOverrides = source.Spec.CloudEventOverrides.DeepCopy()
56+
}
57+
5358
if source.Status.SinkURI != nil {
5459
sink.Status.SinkURI = source.Status.SinkURI.DeepCopy()
5560
}
@@ -83,12 +88,18 @@ func (sink *KafkaSource) ConvertFrom(ctx context.Context, obj apis.Convertible)
8388
if reflect.DeepEqual(*sink.Spec.Sink, duckv1.Destination{}) {
8489
sink.Spec.Sink = nil
8590
}
86-
sink.Status.Status = source.Status.Status
87-
source.Status.Status.ConvertTo(ctx, &source.Status.Status)
91+
92+
source.Status.Status.DeepCopyInto(&sink.Status.Status)
93+
8894
// Optionals
8995
if source.Status.SinkURI != nil {
9096
sink.Status.SinkURI = source.Status.SinkURI.DeepCopy()
9197
}
98+
99+
if source.Spec.CloudEventOverrides != nil {
100+
sink.Spec.CloudEventOverrides = source.Spec.CloudEventOverrides.DeepCopy()
101+
}
102+
92103
if source.Status.CloudEventAttributes != nil {
93104
sink.Status.CloudEventAttributes = make([]duckv1.CloudEventAttributes, len(source.Status.CloudEventAttributes))
94105
copy(sink.Status.CloudEventAttributes, source.Status.CloudEventAttributes)

kafka/source/pkg/apis/sources/v1alpha1/kafka_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ type KafkaSourceSpec struct {
8989
// Resource limits and Request specifications of the Receive Adapter Deployment
9090
// Deprecated: v1beta1 drops this field.
9191
Resources KafkaResourceSpec `json:"resources,omitempty"`
92+
93+
// CloudEventOverrides defines overrides to control the output format and
94+
// modifications of the event sent to the sink.
95+
// +optional
96+
// Needed for supporting round-tripping
97+
CloudEventOverrides *duckv1.CloudEventOverrides `json:"ceOverrides,omitempty"`
9298
}
9399

94100
const (
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2020 The Knative Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
25+
pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
26+
"knative.dev/pkg/apis/testing/roundtrip"
27+
28+
"knative.dev/eventing-contrib/kafka/source/pkg/apis/sources/v1beta1"
29+
)
30+
31+
func TestSourcesRoundTripTypesToJSON(t *testing.T) {
32+
scheme := runtime.NewScheme()
33+
utilruntime.Must(AddToScheme(scheme))
34+
35+
fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
36+
pkgfuzzer.Funcs,
37+
v1beta1.FuzzerFuncs,
38+
)
39+
roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
40+
}
41+
42+
func TestSourceRoundTripTypesToAlphaHub(t *testing.T) {
43+
scheme := runtime.NewScheme()
44+
45+
sb := runtime.SchemeBuilder{
46+
AddToScheme,
47+
v1beta1.AddToScheme,
48+
}
49+
50+
utilruntime.Must(sb.AddToScheme(scheme))
51+
52+
hubs := runtime.NewScheme()
53+
hubs.AddKnownTypes(SchemeGroupVersion,
54+
&KafkaSource{},
55+
)
56+
57+
fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
58+
pkgfuzzer.Funcs,
59+
v1beta1.FuzzerFuncs,
60+
)
61+
62+
roundtrip.ExternalTypesViaHub(t, scheme, hubs, fuzzerFuncs)
63+
}

kafka/source/pkg/apis/sources/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2020 The Knative Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
fuzz "github.com/google/gofuzz"
21+
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
22+
"k8s.io/apimachinery/pkg/runtime/serializer"
23+
pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
24+
)
25+
26+
// FuzzerFuncs includes fuzzing funcs for sources.knative.dev v1beta1 types
27+
//
28+
// For other examples see
29+
// https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/fuzzer/fuzzer.go
30+
var FuzzerFuncs = fuzzer.MergeFuzzerFuncs(
31+
func(codecs serializer.CodecFactory) []interface{} {
32+
return []interface{}{
33+
func(s *KafkaSourceStatus, c fuzz.Continue) {
34+
c.FuzzNoCustom(s) // fuzz the status object
35+
36+
// Clear the random fuzzed condition
37+
s.Status.SetConditions(nil)
38+
39+
// Fuzz the known conditions except their type value
40+
s.InitializeConditions()
41+
pkgfuzzer.FuzzConditions(&s.Status, c)
42+
},
43+
}
44+
},
45+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2020 The Knative Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
25+
pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
26+
"knative.dev/pkg/apis/testing/roundtrip"
27+
)
28+
29+
func TestSourcesRoundTripTypesToJSON(t *testing.T) {
30+
scheme := runtime.NewScheme()
31+
utilruntime.Must(AddToScheme(scheme))
32+
33+
fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
34+
pkgfuzzer.Funcs,
35+
FuzzerFuncs,
36+
)
37+
roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
38+
}

test/lib/listers.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package lib
1919
import (
2020
"k8s.io/apimachinery/pkg/runtime"
2121
fakekubeclientset "k8s.io/client-go/kubernetes/fake"
22-
"k8s.io/client-go/tools/cache"
2322
fakeeventingclientset "knative.dev/eventing/pkg/client/clientset/versioned/fake"
2423
"knative.dev/pkg/reconciler/testing"
2524
)
@@ -58,10 +57,6 @@ func NewListers(objs []runtime.Object) Listers {
5857
return ls
5958
}
6059

61-
func (l Listers) indexerFor(obj runtime.Object) cache.Indexer {
62-
return l.sorter.IndexerForObjectType(obj)
63-
}
64-
6560
func (l Listers) GetKubeObjects() []runtime.Object {
6661
return l.sorter.ObjectsForSchemeFunc(fakekubeclientset.AddToScheme)
6762
}

vendor/k8s.io/apimachinery/pkg/api/apitesting/codec.go

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)