mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Improve serialization round trip test and add v1beta3
This commit is contained in:
parent
8f65442c2f
commit
8262c30c97
@ -78,7 +78,6 @@ kube::log::status "Starting kube-apiserver"
|
||||
APISERVER_PID=$!
|
||||
|
||||
kube::util::wait_for_url "http://127.0.0.1:${API_PORT}/healthz" "apiserver: "
|
||||
kube::util::wait_for_url "http://127.0.0.1:${API_PORT}/api/v1beta1/minions/127.0.0.1" "apiserver(minions): "
|
||||
|
||||
# Start controller manager
|
||||
kube::log::status "Starting CONTROLLER-MANAGER"
|
||||
@ -88,6 +87,7 @@ kube::log::status "Starting CONTROLLER-MANAGER"
|
||||
CTLRMGR_PID=$!
|
||||
|
||||
kube::util::wait_for_url "http://127.0.0.1:${CTLRMGR_PORT}/healthz" "controller-manager: "
|
||||
kube::util::wait_for_url "http://127.0.0.1:${API_PORT}/api/v1beta1/minions/127.0.0.1" "apiserver(minions): "
|
||||
|
||||
kube_cmd=(
|
||||
"${KUBE_OUTPUT_HOSTBIN}/kubectl"
|
||||
|
@ -18,6 +18,7 @@ package api_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"flag"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
@ -30,6 +31,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
|
||||
@ -38,10 +40,15 @@ import (
|
||||
"speter.net/go/exp/math/dec/inf"
|
||||
)
|
||||
|
||||
var fuzzIters = flag.Int("fuzz_iters", 40, "How many fuzzing iterations to do.")
|
||||
var fuzzIters = flag.Int("fuzz_iters", 20, "How many fuzzing iterations to do.")
|
||||
|
||||
// apiObjectFuzzer can randomly populate api objects.
|
||||
var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs(
|
||||
// fuzzerFor can randomly populate api objects that are destined for version.
|
||||
func fuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer {
|
||||
f := fuzz.New().NilChance(.5).NumElements(1, 1)
|
||||
if src != nil {
|
||||
f.RandSource(src)
|
||||
}
|
||||
f.Funcs(
|
||||
func(j *runtime.PluginBase, c fuzz.Continue) {
|
||||
// Do nothing; this struct has only a Kind field and it must stay blank in memory.
|
||||
},
|
||||
@ -149,77 +156,104 @@ var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs(
|
||||
//q.Amount.SetScale(inf.Scale(-c.Intn(12)))
|
||||
q.Amount.SetUnscaled(c.Int63n(1000))
|
||||
},
|
||||
)
|
||||
)
|
||||
return f
|
||||
}
|
||||
|
||||
func runTest(t *testing.T, codec runtime.Codec, source runtime.Object) {
|
||||
name := reflect.TypeOf(source).Elem().Name()
|
||||
apiObjectFuzzer.Fuzz(source)
|
||||
j, err := meta.Accessor(source)
|
||||
func fuzzInternalObject(t *testing.T, forVersion string, item runtime.Object, seed int64) runtime.Object {
|
||||
fuzzerFor(t, forVersion, rand.NewSource(seed)).Fuzz(item)
|
||||
|
||||
j, err := meta.Accessor(item)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error %v for %#v", err, source)
|
||||
t.Fatalf("Unexpected error %v for %#v", err, item)
|
||||
}
|
||||
j.SetKind("")
|
||||
j.SetAPIVersion("")
|
||||
|
||||
data, err := codec.Encode(source)
|
||||
return item
|
||||
}
|
||||
|
||||
func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {
|
||||
name := reflect.TypeOf(item).Elem().Name()
|
||||
data, err := codec.Encode(item)
|
||||
if err != nil {
|
||||
t.Errorf("%v: %v (%#v)", name, err, source)
|
||||
t.Errorf("%v: %v (%#v)", name, err, item)
|
||||
return
|
||||
}
|
||||
|
||||
obj2, err := codec.Decode(data)
|
||||
if err != nil {
|
||||
t.Errorf("0: %v: %v\nCodec: %v\nData: %s\nSource: %#v", name, err, codec, string(data), source)
|
||||
t.Errorf("0: %v: %v\nCodec: %v\nData: %s\nSource: %#v", name, err, codec, string(data), item)
|
||||
return
|
||||
}
|
||||
if !api.Semantic.DeepEqual(source, obj2) {
|
||||
t.Errorf("1: %v: diff: %v\nCodec: %v\nData: %s\nSource: %#v", name, util.ObjectGoPrintDiff(source, obj2), codec, string(data), source)
|
||||
if !api.Semantic.DeepEqual(item, obj2) {
|
||||
t.Errorf("1: %v: diff: %v\nCodec: %v\nData: %s\nSource: %#v\nFinal: %#v", name, util.ObjectGoPrintDiff(item, obj2), codec, string(data), item, obj2)
|
||||
return
|
||||
}
|
||||
|
||||
obj3 := reflect.New(reflect.TypeOf(source).Elem()).Interface().(runtime.Object)
|
||||
obj3 := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
|
||||
err = codec.DecodeInto(data, obj3)
|
||||
if err != nil {
|
||||
t.Errorf("2: %v: %v", name, err)
|
||||
return
|
||||
}
|
||||
if !api.Semantic.DeepEqual(source, obj3) {
|
||||
t.Errorf("3: %v: diff: %v\nCodec: %v", name, util.ObjectDiff(source, obj3), codec)
|
||||
if !api.Semantic.DeepEqual(item, obj3) {
|
||||
t.Errorf("3: %v: diff: %v\nCodec: %v", name, util.ObjectDiff(item, obj3), codec)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// roundTripSame verifies the same source object is tested in all API versions.
|
||||
func roundTripSame(t *testing.T, item runtime.Object) {
|
||||
seed := rand.Int63()
|
||||
fuzzInternalObject(t, "", item, seed)
|
||||
roundTrip(t, v1beta1.Codec, item)
|
||||
roundTrip(t, v1beta2.Codec, item)
|
||||
fuzzInternalObject(t, "v1beta3", item, seed)
|
||||
roundTrip(t, v1beta3.Codec, item)
|
||||
}
|
||||
|
||||
func roundTripAll(t *testing.T, item runtime.Object) {
|
||||
seed := rand.Int63()
|
||||
roundTrip(t, v1beta1.Codec, fuzzInternalObject(t, "v1beta1", item, seed))
|
||||
roundTrip(t, v1beta2.Codec, fuzzInternalObject(t, "v1beta2", item, seed))
|
||||
roundTrip(t, v1beta3.Codec, fuzzInternalObject(t, "v1beta3", item, seed))
|
||||
}
|
||||
|
||||
// For debugging problems
|
||||
func TestSpecificKind(t *testing.T) {
|
||||
api.Scheme.Log(t)
|
||||
defer api.Scheme.Log(nil)
|
||||
|
||||
kind := "PodList"
|
||||
item, err := api.Scheme.New("", kind)
|
||||
if err != nil {
|
||||
t.Errorf("Couldn't make a %v? %v", kind, err)
|
||||
return
|
||||
}
|
||||
runTest(t, v1beta1.Codec, item)
|
||||
runTest(t, v1beta2.Codec, item)
|
||||
api.Scheme.Log(nil)
|
||||
roundTripSame(t, item)
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
api.Scheme.Log(t)
|
||||
defer api.Scheme.Log(nil)
|
||||
|
||||
kind := "List"
|
||||
item, err := api.Scheme.New("", kind)
|
||||
if err != nil {
|
||||
t.Errorf("Couldn't make a %v? %v", kind, err)
|
||||
return
|
||||
}
|
||||
runTest(t, v1beta1.Codec, item)
|
||||
runTest(t, v1beta2.Codec, item)
|
||||
api.Scheme.Log(nil)
|
||||
roundTripSame(t, item)
|
||||
}
|
||||
|
||||
var nonRoundTrippableTypes = util.NewStringSet("ContainerManifest")
|
||||
var nonRoundTrippableTypes = util.NewStringSet("ContainerManifest", "ContainerManifestList")
|
||||
var nonInternalRoundTrippableTypes = util.NewStringSet("List")
|
||||
|
||||
func TestRoundTripTypes(t *testing.T) {
|
||||
// api.Scheme.Log(t)
|
||||
// defer api.Scheme.Log(nil)
|
||||
|
||||
for kind := range api.Scheme.KnownTypes("") {
|
||||
if nonRoundTrippableTypes.Has(kind) {
|
||||
continue
|
||||
@ -233,10 +267,9 @@ func TestRoundTripTypes(t *testing.T) {
|
||||
if _, err := meta.Accessor(item); err != nil {
|
||||
t.Fatalf("%q is not a TypeMeta and cannot be tested - add it to nonRoundTrippableTypes: %v", kind, err)
|
||||
}
|
||||
runTest(t, v1beta1.Codec, item)
|
||||
runTest(t, v1beta2.Codec, item)
|
||||
roundTripSame(t, item)
|
||||
if !nonInternalRoundTrippableTypes.Has(kind) {
|
||||
runTest(t, api.Codec, item)
|
||||
roundTrip(t, api.Codec, fuzzInternalObject(t, "", item, rand.Int63()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -281,7 +314,7 @@ const benchmarkSeed = 100
|
||||
|
||||
func BenchmarkEncode(b *testing.B) {
|
||||
pod := api.Pod{}
|
||||
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer.Fuzz(&pod)
|
||||
for i := 0; i < b.N; i++ {
|
||||
latest.Codec.Encode(&pod)
|
||||
@ -291,7 +324,7 @@ func BenchmarkEncode(b *testing.B) {
|
||||
// BenchmarkEncodeJSON provides a baseline for regular JSON encode performance
|
||||
func BenchmarkEncodeJSON(b *testing.B) {
|
||||
pod := api.Pod{}
|
||||
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer.Fuzz(&pod)
|
||||
for i := 0; i < b.N; i++ {
|
||||
json.Marshal(&pod)
|
||||
@ -300,7 +333,7 @@ func BenchmarkEncodeJSON(b *testing.B) {
|
||||
|
||||
func BenchmarkDecode(b *testing.B) {
|
||||
pod := api.Pod{}
|
||||
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer.Fuzz(&pod)
|
||||
data, _ := latest.Codec.Encode(&pod)
|
||||
for i := 0; i < b.N; i++ {
|
||||
@ -310,7 +343,7 @@ func BenchmarkDecode(b *testing.B) {
|
||||
|
||||
func BenchmarkDecodeInto(b *testing.B) {
|
||||
pod := api.Pod{}
|
||||
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer.Fuzz(&pod)
|
||||
data, _ := latest.Codec.Encode(&pod)
|
||||
for i := 0; i < b.N; i++ {
|
||||
@ -322,7 +355,7 @@ func BenchmarkDecodeInto(b *testing.B) {
|
||||
// BenchmarkDecodeJSON provides a baseline for regular JSON decode performance
|
||||
func BenchmarkDecodeJSON(b *testing.B) {
|
||||
pod := api.Pod{}
|
||||
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
||||
apiObjectFuzzer.Fuzz(&pod)
|
||||
data, _ := latest.Codec.Encode(&pod)
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
@ -40,7 +40,7 @@ type Interface interface {
|
||||
}
|
||||
|
||||
func (c *Client) ReplicationControllers(namespace string) ReplicationControllerInterface {
|
||||
return newReplicationControllers(c, namespace, c.preV1Beta3)
|
||||
return newReplicationControllers(c, namespace)
|
||||
}
|
||||
|
||||
func (c *Client) Nodes() NodeInterface {
|
||||
|
Loading…
Reference in New Issue
Block a user