Repository: knative/pkg
Pull request #1770 (2020-10-05 09:54:41)

add fuzzers

knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1770#" title="Author self-approved">vaikas</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[apis/duck/OWNERS](https://github.com/knative/pkg/blob/master/apis/duck/OWNERS)~~ [vaikas]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/knative/pkg/pull/1770?src=pr&el=h1) Report
> Merging [#1770](https://codecov.io/gh/knative/pkg/pull/1770?src=pr&el=desc) into [master](https://codecov.io/gh/knative/pkg/commit/ea7374e811058d08c0fe8592fb94e9d12ccd5cf2?el=desc) will **increase** coverage by `0.08%`.
> The diff coverage is `84.61%`.

[![Impacted file tree graph](https://codecov.io/gh/knative/pkg/pull/1770/graphs/tree.svg?width=650&height=150&src=pr&token=rlqpDnfxbO)](https://codecov.io/gh/knative/pkg/pull/1770?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1770      +/-   ##
==========================================
+ Coverage   68.51%   68.59%   +0.08%     
==========================================
  Files         215      218       +3     
  Lines        9216     9229      +13     
==========================================
+ Hits         6314     6331      +17     
+ Misses       2585     2584       -1     
+ Partials      317      314       -3     
```


| [Impacted Files](https://codecov.io/gh/knative/pkg/pull/1770?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [apis/duck/v1alpha1/fuzzer.go](https://codecov.io/gh/knative/pkg/pull/1770/diff?src=pr&el=tree#diff-YXBpcy9kdWNrL3YxYWxwaGExL2Z1enplci5nbw==) | `60.00% <60.00%> (ø)` | |
| [apis/duck/v1/fuzzer.go](https://codecov.io/gh/knative/pkg/pull/1770/diff?src=pr&el=tree#diff-YXBpcy9kdWNrL3YxL2Z1enplci5nbw==) | `100.00% <100.00%> (ø)` | |
| [apis/duck/v1beta1/fuzzer.go](https://codecov.io/gh/knative/pkg/pull/1770/diff?src=pr&el=tree#diff-YXBpcy9kdWNrL3YxYmV0YTEvZnV6emVyLmdv) | `100.00% <100.00%> (ø)` | |
| [test/gcs/mock/mock.go](https://codecov.io/gh/knative/pkg/pull/1770/diff?src=pr&el=tree#diff-dGVzdC9nY3MvbW9jay9tb2NrLmdv) | `93.54% <0.00%> (+3.22%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/knative/pkg/pull/1770?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/knative/pkg/pull/1770?src=pr&el=footer). Last update [f9bf419...ff3cf63](https://codecov.io/gh/knative/pkg/pull/1770?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

vaikas: Thanks @dprotaso and also for mentioning the status, I'll do that as well then, wasn't aware of the open issue.
vaikas: Fixes: #1358 

Patch:
diff --git a/apis/duck/v1/fuzzer.go b/apis/duck/v1/fuzzer.go
new file mode 100644
index 000000000..4ef66e589
--- /dev/null
+++ b/apis/duck/v1/fuzzer.go
@@ -0,0 +1,44 @@
+/*
+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"
+	"knative.dev/pkg/apis"
+	pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
+)
+
+var testConditions = apis.Conditions{{Type: apis.ConditionReady}, {Type: apis.ConditionSucceeded}}
+
+// 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(status *Status, c fuzz.Continue) {
+				c.FuzzNoCustom(status) // fuzz the Status
+				status.SetConditions(testConditions)
+				pkgfuzzer.FuzzConditions(status, c)
+			},
+		}
+	},
+)
diff --git a/apis/duck/v1beta1/fuzzer.go b/apis/duck/v1beta1/fuzzer.go
new file mode 100644
index 000000000..efc8be973
--- /dev/null
+++ b/apis/duck/v1beta1/fuzzer.go
@@ -0,0 +1,43 @@
+/*
+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"
+	"knative.dev/pkg/apis"
+	pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
+)
+
+var testConditions = apis.Conditions{{Type: apis.ConditionReady}, {Type: apis.ConditionSucceeded}}
+
+// FuzzerFuncs includes fuzzing funcs for knative.dev/duck v1beta1 Status 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(status *Status, c fuzz.Continue) {
+				c.FuzzNoCustom(status) // fuzz the Status
+				status.SetConditions(testConditions)
+				pkgfuzzer.FuzzConditions(status, c)
+			},
+		}
+	},
+)


Test patch:
diff --git a/apis/duck/v1/roundtrip_test.go b/apis/duck/v1/roundtrip_test.go
new file mode 100644
index 000000000..25bca151b
--- /dev/null
+++ b/apis/duck/v1/roundtrip_test.go
@@ -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 v1
+
+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 TestEventingRoundTripTypesToJSON(t *testing.T) {
+	scheme := runtime.NewScheme()
+	utilruntime.Must(AddToScheme(scheme))
+
+	fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
+		pkgfuzzer.Funcs,
+		FuzzerFuncs,
+	)
+	roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
+}
diff --git a/apis/duck/v1alpha1/roundtrip_test.go b/apis/duck/v1alpha1/roundtrip_test.go
new file mode 100644
index 000000000..c40197e50
--- /dev/null
+++ b/apis/duck/v1alpha1/roundtrip_test.go
@@ -0,0 +1,37 @@
+/*
+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"
+)
+
+func TestEventingRoundTripTypesToJSON(t *testing.T) {
+	scheme := runtime.NewScheme()
+	utilruntime.Must(AddToScheme(scheme))
+
+	fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
+		pkgfuzzer.Funcs,
+	)
+	roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
+}
diff --git a/apis/duck/v1beta1/roundtrip_test.go b/apis/duck/v1beta1/roundtrip_test.go
new file mode 100644
index 000000000..f727740ae
--- /dev/null
+++ b/apis/duck/v1beta1/roundtrip_test.go
@@ -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 TestEventingRoundTripTypesToJSON(t *testing.T) {
+	scheme := runtime.NewScheme()
+	utilruntime.Must(AddToScheme(scheme))
+
+	fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
+		pkgfuzzer.Funcs,
+		FuzzerFuncs,
+	)
+	roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
+}