Unify fuzzers and roundtrip tests
Kubernetes-commit: ecc811d263894ae54bbe62a3b1ba14847a260e95
This commit is contained in:
parent
4906405ecb
commit
aaf3784254
|
|
@ -14,6 +14,7 @@ go_library(
|
|||
deps = [
|
||||
"//vendor/github.com/google/gofuzz:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/testing:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/testing/fuzzer:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
|
||||
"//vendor/k8s.io/apiserver/pkg/apis/example:go_default_library",
|
||||
|
|
|
|||
|
|
@ -17,18 +17,21 @@ limitations under the License.
|
|||
package fuzzer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/gofuzz"
|
||||
|
||||
apitesting "k8s.io/apimachinery/pkg/api/testing"
|
||||
"k8s.io/apimachinery/pkg/api/testing/fuzzer"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
|
||||
"k8s.io/apiserver/pkg/apis/example"
|
||||
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
|
||||
)
|
||||
|
||||
// overrideGenericFuncs override some generic fuzzer funcs from k8s.io/apiserver in order to have more realistic
|
||||
// overrideMetaFuncs override some generic fuzzer funcs from k8s.io/apiserver in order to have more realistic
|
||||
// values in a Kubernetes context.
|
||||
func overrideGenericFuncs(t apitesting.TestingCommon, codecs runtimeserializer.CodecFactory) []interface{} {
|
||||
func overrideMetaFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
|
||||
return []interface{}{
|
||||
func(j *runtime.Object, c fuzz.Continue) {
|
||||
// TODO: uncomment when round trip starts from a versioned object
|
||||
|
|
@ -54,7 +57,7 @@ func overrideGenericFuncs(t apitesting.TestingCommon, codecs runtimeserializer.C
|
|||
// Convert the object to raw bytes
|
||||
bytes, err := runtime.Encode(apitesting.TestCodec(codecs, examplev1.SchemeGroupVersion), obj)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to encode object: %v", err)
|
||||
panic(fmt.Sprintf("Failed to encode object: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -64,7 +67,7 @@ func overrideGenericFuncs(t apitesting.TestingCommon, codecs runtimeserializer.C
|
|||
}
|
||||
}
|
||||
|
||||
func exampleFuncs(t apitesting.TestingCommon) []interface{} {
|
||||
func exampleFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
|
||||
return []interface{}{
|
||||
func(s *example.PodSpec, c fuzz.Continue) {
|
||||
c.FuzzNoCustom(s)
|
||||
|
|
@ -90,10 +93,8 @@ func exampleFuncs(t apitesting.TestingCommon) []interface{} {
|
|||
}
|
||||
}
|
||||
|
||||
func Funcs(t apitesting.TestingCommon, codecs runtimeserializer.CodecFactory) []interface{} {
|
||||
return apitesting.MergeFuzzerFuncs(t,
|
||||
apitesting.GenericFuzzerFuncs(t, codecs),
|
||||
overrideGenericFuncs(t, codecs),
|
||||
exampleFuncs(t),
|
||||
)
|
||||
}
|
||||
// Funcs returns the fuzzer functions for the example api group.
|
||||
var Funcs = fuzzer.MergeFuzzerFuncs(
|
||||
overrideMetaFuncs,
|
||||
exampleFuncs,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ licenses(["notice"])
|
|||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
|
|
@ -19,3 +20,14 @@ go_library(
|
|||
"//vendor/k8s.io/apiserver/pkg/apis/example/v1:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["roundtrip_test.go"],
|
||||
library = ":go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/testing/roundtrip:go_default_library",
|
||||
"//vendor/k8s.io/apiserver/pkg/apis/example/fuzzer:go_default_library",
|
||||
],
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
Copyright 2017 The Kubernetes 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 install
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/testing/roundtrip"
|
||||
examplefuzzer "k8s.io/apiserver/pkg/apis/example/fuzzer"
|
||||
)
|
||||
|
||||
func TestRoundTrip(t *testing.T) {
|
||||
roundtrip.RoundTripTestForAPIGroup(t, Install, examplefuzzer.Funcs)
|
||||
}
|
||||
|
|
@ -25,7 +25,7 @@ go_test(
|
|||
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/testing:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/testing/fuzzer:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/internalversion:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ import (
|
|||
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||
apierrs "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
apitesting "k8s.io/apimachinery/pkg/api/testing"
|
||||
fuzzer "k8s.io/apimachinery/pkg/api/testing/fuzzer"
|
||||
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
|
|
@ -4104,7 +4104,7 @@ func newTestRequestInfoResolver() *request.RequestInfoFactory {
|
|||
const benchmarkSeed = 100
|
||||
|
||||
func benchmarkItems(b *testing.B) []example.Pod {
|
||||
clientapiObjectFuzzer := apitesting.FuzzerFor(examplefuzzer.Funcs(b, codecs), rand.NewSource(benchmarkSeed))
|
||||
clientapiObjectFuzzer := fuzzer.FuzzerFor(examplefuzzer.Funcs, rand.NewSource(benchmarkSeed), codecs)
|
||||
items := make([]example.Pod, 3)
|
||||
for i := range items {
|
||||
clientapiObjectFuzzer.Fuzz(&items[i])
|
||||
|
|
|
|||
Loading…
Reference in New Issue