Improve serialization round trip test and add v1beta3

This commit is contained in:
Clayton Coleman 2014-12-12 16:57:25 -05:00
parent 8f65442c2f
commit 8262c30c97
3 changed files with 171 additions and 138 deletions

View File

@ -78,7 +78,6 @@ kube::log::status "Starting kube-apiserver"
APISERVER_PID=$! 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}/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 # Start controller manager
kube::log::status "Starting CONTROLLER-MANAGER" kube::log::status "Starting CONTROLLER-MANAGER"
@ -88,6 +87,7 @@ kube::log::status "Starting CONTROLLER-MANAGER"
CTLRMGR_PID=$! 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:${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_cmd=(
"${KUBE_OUTPUT_HOSTBIN}/kubectl" "${KUBE_OUTPUT_HOSTBIN}/kubectl"

View File

@ -18,6 +18,7 @@ package api_test
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"math/rand" "math/rand"
"reflect" "reflect"
@ -30,6 +31,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2" "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/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
@ -38,188 +40,220 @@ import (
"speter.net/go/exp/math/dec/inf" "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. // fuzzerFor can randomly populate api objects that are destined for version.
var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs( func fuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer {
func(j *runtime.PluginBase, c fuzz.Continue) { f := fuzz.New().NilChance(.5).NumElements(1, 1)
// Do nothing; this struct has only a Kind field and it must stay blank in memory. if src != nil {
}, f.RandSource(src)
func(j *runtime.TypeMeta, c fuzz.Continue) { }
// We have to customize the randomization of TypeMetas because their f.Funcs(
// APIVersion and Kind must remain blank in memory. func(j *runtime.PluginBase, c fuzz.Continue) {
j.APIVersion = "" // Do nothing; this struct has only a Kind field and it must stay blank in memory.
j.Kind = "" },
}, func(j *runtime.TypeMeta, c fuzz.Continue) {
func(j *api.TypeMeta, c fuzz.Continue) { // We have to customize the randomization of TypeMetas because their
// We have to customize the randomization of TypeMetas because their // APIVersion and Kind must remain blank in memory.
// APIVersion and Kind must remain blank in memory. j.APIVersion = ""
j.APIVersion = "" j.Kind = ""
j.Kind = "" },
}, func(j *api.TypeMeta, c fuzz.Continue) {
func(j *api.ObjectMeta, c fuzz.Continue) { // We have to customize the randomization of TypeMetas because their
j.Name = c.RandString() // APIVersion and Kind must remain blank in memory.
j.ResourceVersion = strconv.FormatUint(c.RandUint64(), 10) j.APIVersion = ""
j.SelfLink = c.RandString() j.Kind = ""
},
func(j *api.ObjectMeta, c fuzz.Continue) {
j.Name = c.RandString()
j.ResourceVersion = strconv.FormatUint(c.RandUint64(), 10)
j.SelfLink = c.RandString()
var sec, nsec int64 var sec, nsec int64
c.Fuzz(&sec) c.Fuzz(&sec)
c.Fuzz(&nsec) c.Fuzz(&nsec)
j.CreationTimestamp = util.Unix(sec, nsec).Rfc3339Copy() j.CreationTimestamp = util.Unix(sec, nsec).Rfc3339Copy()
}, },
func(j *api.ListMeta, c fuzz.Continue) { func(j *api.ListMeta, c fuzz.Continue) {
j.ResourceVersion = strconv.FormatUint(c.RandUint64(), 10) j.ResourceVersion = strconv.FormatUint(c.RandUint64(), 10)
j.SelfLink = c.RandString() j.SelfLink = c.RandString()
}, },
func(j *api.PodPhase, c fuzz.Continue) { func(j *api.PodPhase, c fuzz.Continue) {
statuses := []api.PodPhase{api.PodPending, api.PodRunning, api.PodFailed, api.PodUnknown} statuses := []api.PodPhase{api.PodPending, api.PodRunning, api.PodFailed, api.PodUnknown}
*j = statuses[c.Rand.Intn(len(statuses))] *j = statuses[c.Rand.Intn(len(statuses))]
}, },
func(j *api.ReplicationControllerSpec, c fuzz.Continue) { func(j *api.ReplicationControllerSpec, c fuzz.Continue) {
// TemplateRef must be nil for round trip // TemplateRef must be nil for round trip
c.Fuzz(&j.Template) c.Fuzz(&j.Template)
if j.Template == nil { if j.Template == nil {
// TODO: v1beta1/2 can't round trip a nil template correctly, fix by having v1beta1/2 // TODO: v1beta1/2 can't round trip a nil template correctly, fix by having v1beta1/2
// conversion compare converted object to nil via DeepEqual // conversion compare converted object to nil via DeepEqual
j.Template = &api.PodTemplateSpec{} j.Template = &api.PodTemplateSpec{}
}
j.Template.ObjectMeta = api.ObjectMeta{Labels: j.Template.ObjectMeta.Labels}
j.Template.Spec.NodeSelector = nil
c.Fuzz(&j.Selector)
j.Replicas = int(c.RandUint64())
},
func(j *api.ReplicationControllerStatus, c fuzz.Continue) {
// only replicas round trips
j.Replicas = int(c.RandUint64())
},
func(j *api.List, c fuzz.Continue) {
c.Fuzz(&j.ListMeta)
c.Fuzz(&j.Items)
if j.Items == nil {
j.Items = []runtime.Object{}
}
},
func(j *runtime.Object, c fuzz.Continue) {
if c.RandBool() {
*j = &runtime.Unknown{
TypeMeta: runtime.TypeMeta{Kind: "Something", APIVersion: "unknown"},
RawJSON: []byte(`{"apiVersion":"unknown","kind":"Something","someKey":"someValue"}`),
} }
} else { j.Template.ObjectMeta = api.ObjectMeta{Labels: j.Template.ObjectMeta.Labels}
types := []runtime.Object{&api.Pod{}, &api.ReplicationController{}} j.Template.Spec.NodeSelector = nil
t := types[c.Rand.Intn(len(types))] c.Fuzz(&j.Selector)
c.Fuzz(t) j.Replicas = int(c.RandUint64())
*j = t },
} func(j *api.ReplicationControllerStatus, c fuzz.Continue) {
}, // only replicas round trips
func(intstr *util.IntOrString, c fuzz.Continue) { j.Replicas = int(c.RandUint64())
// util.IntOrString will panic if its kind is set wrong. },
if c.RandBool() { func(j *api.List, c fuzz.Continue) {
intstr.Kind = util.IntstrInt c.Fuzz(&j.ListMeta)
intstr.IntVal = int(c.RandUint64()) c.Fuzz(&j.Items)
intstr.StrVal = "" if j.Items == nil {
} else { j.Items = []runtime.Object{}
intstr.Kind = util.IntstrString }
intstr.IntVal = 0 },
intstr.StrVal = c.RandString() func(j *runtime.Object, c fuzz.Continue) {
} if c.RandBool() {
}, *j = &runtime.Unknown{
func(pb map[docker.Port][]docker.PortBinding, c fuzz.Continue) { TypeMeta: runtime.TypeMeta{Kind: "Something", APIVersion: "unknown"},
// This is necessary because keys with nil values get omitted. RawJSON: []byte(`{"apiVersion":"unknown","kind":"Something","someKey":"someValue"}`),
// TODO: Is this a bug? }
pb[docker.Port(c.RandString())] = []docker.PortBinding{ } else {
{c.RandString(), c.RandString()}, types := []runtime.Object{&api.Pod{}, &api.ReplicationController{}}
{c.RandString(), c.RandString()}, t := types[c.Rand.Intn(len(types))]
} c.Fuzz(t)
}, *j = t
func(pm map[string]docker.PortMapping, c fuzz.Continue) { }
// This is necessary because keys with nil values get omitted. },
// TODO: Is this a bug? func(intstr *util.IntOrString, c fuzz.Continue) {
pm[c.RandString()] = docker.PortMapping{ // util.IntOrString will panic if its kind is set wrong.
c.RandString(): c.RandString(), if c.RandBool() {
} intstr.Kind = util.IntstrInt
}, intstr.IntVal = int(c.RandUint64())
intstr.StrVal = ""
} else {
intstr.Kind = util.IntstrString
intstr.IntVal = 0
intstr.StrVal = c.RandString()
}
},
func(pb map[docker.Port][]docker.PortBinding, c fuzz.Continue) {
// This is necessary because keys with nil values get omitted.
// TODO: Is this a bug?
pb[docker.Port(c.RandString())] = []docker.PortBinding{
{c.RandString(), c.RandString()},
{c.RandString(), c.RandString()},
}
},
func(pm map[string]docker.PortMapping, c fuzz.Continue) {
// This is necessary because keys with nil values get omitted.
// TODO: Is this a bug?
pm[c.RandString()] = docker.PortMapping{
c.RandString(): c.RandString(),
}
},
func(q *resource.Quantity, c fuzz.Continue) { func(q *resource.Quantity, c fuzz.Continue) {
// Real Quantity fuzz testing is done elsewhere; // Real Quantity fuzz testing is done elsewhere;
// this limited subset of functionality survives // this limited subset of functionality survives
// round-tripping to v1beta1/2. // round-tripping to v1beta1/2.
q.Amount = &inf.Dec{} q.Amount = &inf.Dec{}
q.Format = resource.DecimalExponent q.Format = resource.DecimalExponent
//q.Amount.SetScale(inf.Scale(-c.Intn(12))) //q.Amount.SetScale(inf.Scale(-c.Intn(12)))
q.Amount.SetUnscaled(c.Int63n(1000)) q.Amount.SetUnscaled(c.Int63n(1000))
}, },
) )
return f
}
func runTest(t *testing.T, codec runtime.Codec, source runtime.Object) { func fuzzInternalObject(t *testing.T, forVersion string, item runtime.Object, seed int64) runtime.Object {
name := reflect.TypeOf(source).Elem().Name() fuzzerFor(t, forVersion, rand.NewSource(seed)).Fuzz(item)
apiObjectFuzzer.Fuzz(source)
j, err := meta.Accessor(source) j, err := meta.Accessor(item)
if err != nil { if err != nil {
t.Fatalf("Unexpected error %v for %#v", err, source) t.Fatalf("Unexpected error %v for %#v", err, item)
} }
j.SetKind("") j.SetKind("")
j.SetAPIVersion("") 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 { if err != nil {
t.Errorf("%v: %v (%#v)", name, err, source) t.Errorf("%v: %v (%#v)", name, err, item)
return return
} }
obj2, err := codec.Decode(data) obj2, err := codec.Decode(data)
if err != nil { 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 return
} }
if !api.Semantic.DeepEqual(source, obj2) { if !api.Semantic.DeepEqual(item, obj2) {
t.Errorf("1: %v: diff: %v\nCodec: %v\nData: %s\nSource: %#v", name, util.ObjectGoPrintDiff(source, obj2), codec, string(data), source) 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 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) err = codec.DecodeInto(data, obj3)
if err != nil { if err != nil {
t.Errorf("2: %v: %v", name, err) t.Errorf("2: %v: %v", name, err)
return return
} }
if !api.Semantic.DeepEqual(source, obj3) { if !api.Semantic.DeepEqual(item, obj3) {
t.Errorf("3: %v: diff: %v\nCodec: %v", name, util.ObjectDiff(source, obj3), codec) t.Errorf("3: %v: diff: %v\nCodec: %v", name, util.ObjectDiff(item, obj3), codec)
return 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 // For debugging problems
func TestSpecificKind(t *testing.T) { func TestSpecificKind(t *testing.T) {
api.Scheme.Log(t) api.Scheme.Log(t)
defer api.Scheme.Log(nil)
kind := "PodList" kind := "PodList"
item, err := api.Scheme.New("", kind) item, err := api.Scheme.New("", kind)
if err != nil { if err != nil {
t.Errorf("Couldn't make a %v? %v", kind, err) t.Errorf("Couldn't make a %v? %v", kind, err)
return return
} }
runTest(t, v1beta1.Codec, item) roundTripSame(t, item)
runTest(t, v1beta2.Codec, item)
api.Scheme.Log(nil)
} }
func TestList(t *testing.T) { func TestList(t *testing.T) {
api.Scheme.Log(t) api.Scheme.Log(t)
defer api.Scheme.Log(nil)
kind := "List" kind := "List"
item, err := api.Scheme.New("", kind) item, err := api.Scheme.New("", kind)
if err != nil { if err != nil {
t.Errorf("Couldn't make a %v? %v", kind, err) t.Errorf("Couldn't make a %v? %v", kind, err)
return return
} }
runTest(t, v1beta1.Codec, item) roundTripSame(t, item)
runTest(t, v1beta2.Codec, item)
api.Scheme.Log(nil)
} }
var nonRoundTrippableTypes = util.NewStringSet("ContainerManifest") var nonRoundTrippableTypes = util.NewStringSet("ContainerManifest", "ContainerManifestList")
var nonInternalRoundTrippableTypes = util.NewStringSet("List") var nonInternalRoundTrippableTypes = util.NewStringSet("List")
func TestRoundTripTypes(t *testing.T) { func TestRoundTripTypes(t *testing.T) {
// api.Scheme.Log(t)
// defer api.Scheme.Log(nil)
for kind := range api.Scheme.KnownTypes("") { for kind := range api.Scheme.KnownTypes("") {
if nonRoundTrippableTypes.Has(kind) { if nonRoundTrippableTypes.Has(kind) {
continue continue
@ -233,10 +267,9 @@ func TestRoundTripTypes(t *testing.T) {
if _, err := meta.Accessor(item); err != nil { 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) t.Fatalf("%q is not a TypeMeta and cannot be tested - add it to nonRoundTrippableTypes: %v", kind, err)
} }
runTest(t, v1beta1.Codec, item) roundTripSame(t, item)
runTest(t, v1beta2.Codec, item)
if !nonInternalRoundTrippableTypes.Has(kind) { 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) { func BenchmarkEncode(b *testing.B) {
pod := api.Pod{} pod := api.Pod{}
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed)) apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
apiObjectFuzzer.Fuzz(&pod) apiObjectFuzzer.Fuzz(&pod)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
latest.Codec.Encode(&pod) latest.Codec.Encode(&pod)
@ -291,7 +324,7 @@ func BenchmarkEncode(b *testing.B) {
// BenchmarkEncodeJSON provides a baseline for regular JSON encode performance // BenchmarkEncodeJSON provides a baseline for regular JSON encode performance
func BenchmarkEncodeJSON(b *testing.B) { func BenchmarkEncodeJSON(b *testing.B) {
pod := api.Pod{} pod := api.Pod{}
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed)) apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
apiObjectFuzzer.Fuzz(&pod) apiObjectFuzzer.Fuzz(&pod)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
json.Marshal(&pod) json.Marshal(&pod)
@ -300,7 +333,7 @@ func BenchmarkEncodeJSON(b *testing.B) {
func BenchmarkDecode(b *testing.B) { func BenchmarkDecode(b *testing.B) {
pod := api.Pod{} pod := api.Pod{}
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed)) apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
apiObjectFuzzer.Fuzz(&pod) apiObjectFuzzer.Fuzz(&pod)
data, _ := latest.Codec.Encode(&pod) data, _ := latest.Codec.Encode(&pod)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -310,7 +343,7 @@ func BenchmarkDecode(b *testing.B) {
func BenchmarkDecodeInto(b *testing.B) { func BenchmarkDecodeInto(b *testing.B) {
pod := api.Pod{} pod := api.Pod{}
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed)) apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
apiObjectFuzzer.Fuzz(&pod) apiObjectFuzzer.Fuzz(&pod)
data, _ := latest.Codec.Encode(&pod) data, _ := latest.Codec.Encode(&pod)
for i := 0; i < b.N; i++ { 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 // BenchmarkDecodeJSON provides a baseline for regular JSON decode performance
func BenchmarkDecodeJSON(b *testing.B) { func BenchmarkDecodeJSON(b *testing.B) {
pod := api.Pod{} pod := api.Pod{}
apiObjectFuzzer.RandSource(rand.NewSource(benchmarkSeed)) apiObjectFuzzer := fuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
apiObjectFuzzer.Fuzz(&pod) apiObjectFuzzer.Fuzz(&pod)
data, _ := latest.Codec.Encode(&pod) data, _ := latest.Codec.Encode(&pod)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {

View File

@ -40,7 +40,7 @@ type Interface interface {
} }
func (c *Client) ReplicationControllers(namespace string) ReplicationControllerInterface { func (c *Client) ReplicationControllers(namespace string) ReplicationControllerInterface {
return newReplicationControllers(c, namespace, c.preV1Beta3) return newReplicationControllers(c, namespace)
} }
func (c *Client) Nodes() NodeInterface { func (c *Client) Nodes() NodeInterface {