2014-08-30 00:58:25 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-08-30 00:58:25 +00:00
|
|
|
|
|
|
|
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 api_test
|
|
|
|
|
|
|
|
import (
|
2014-09-19 01:27:33 +00:00
|
|
|
"encoding/json"
|
2014-12-12 21:57:25 +00:00
|
|
|
|
2014-09-19 01:27:33 +00:00
|
|
|
"math/rand"
|
2014-08-30 00:58:25 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/latest"
|
|
|
|
"k8s.io/kubernetes/pkg/api/meta"
|
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
|
|
|
apitesting "k8s.io/kubernetes/pkg/api/testing"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2015-01-03 03:10:03 +00:00
|
|
|
|
2015-01-13 19:22:02 +00:00
|
|
|
flag "github.com/spf13/pflag"
|
2014-08-30 00:58:25 +00:00
|
|
|
)
|
|
|
|
|
2014-12-12 21:57:25 +00:00
|
|
|
var fuzzIters = flag.Int("fuzz_iters", 20, "How many fuzzing iterations to do.")
|
|
|
|
|
|
|
|
func fuzzInternalObject(t *testing.T, forVersion string, item runtime.Object, seed int64) runtime.Object {
|
2015-01-29 00:57:33 +00:00
|
|
|
apitesting.FuzzerFor(t, forVersion, rand.NewSource(seed)).Fuzz(item)
|
2014-12-12 21:57:25 +00:00
|
|
|
|
2015-01-19 02:22:55 +00:00
|
|
|
j, err := meta.TypeAccessor(item)
|
2014-08-30 00:58:25 +00:00
|
|
|
if err != nil {
|
2014-12-12 21:57:25 +00:00
|
|
|
t.Fatalf("Unexpected error %v for %#v", err, item)
|
2014-08-30 00:58:25 +00:00
|
|
|
}
|
|
|
|
j.SetKind("")
|
|
|
|
j.SetAPIVersion("")
|
|
|
|
|
2014-12-12 21:57:25 +00:00
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {
|
2015-03-06 04:55:32 +00:00
|
|
|
printer := spew.ConfigState{DisableMethods: true}
|
|
|
|
|
2014-12-12 21:57:25 +00:00
|
|
|
name := reflect.TypeOf(item).Elem().Name()
|
|
|
|
data, err := codec.Encode(item)
|
2014-08-30 00:58:25 +00:00
|
|
|
if err != nil {
|
2015-03-06 04:55:32 +00:00
|
|
|
t.Errorf("%v: %v (%s)", name, err, printer.Sprintf("%#v", item))
|
2014-08-30 00:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-11 17:02:53 +00:00
|
|
|
obj2, err := codec.Decode(data)
|
2014-08-30 00:58:25 +00:00
|
|
|
if err != nil {
|
2015-03-06 04:55:32 +00:00
|
|
|
t.Errorf("0: %v: %v\nCodec: %v\nData: %s\nSource: %#v", name, err, codec, string(data), printer.Sprintf("%#v", item))
|
2014-08-30 00:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2014-12-12 21:57:25 +00:00
|
|
|
if !api.Semantic.DeepEqual(item, obj2) {
|
2015-03-06 04:55:32 +00:00
|
|
|
t.Errorf("1: %v: diff: %v\nCodec: %v\nData: %s\nSource: %#v\nFinal: %#v", name, util.ObjectGoPrintDiff(item, obj2), codec, string(data), printer.Sprintf("%#v", item), printer.Sprintf("%#v", obj2))
|
2014-11-13 12:01:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-12 21:57:25 +00:00
|
|
|
obj3 := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
|
2014-09-11 17:02:53 +00:00
|
|
|
err = codec.DecodeInto(data, obj3)
|
2014-08-30 00:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("2: %v: %v", name, err)
|
|
|
|
return
|
2014-11-13 12:01:25 +00:00
|
|
|
}
|
2014-12-12 21:57:25 +00:00
|
|
|
if !api.Semantic.DeepEqual(item, obj3) {
|
|
|
|
t.Errorf("3: %v: diff: %v\nCodec: %v", name, util.ObjectDiff(item, obj3), codec)
|
2014-11-13 12:01:25 +00:00
|
|
|
return
|
2014-08-30 00:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 21:57:25 +00:00
|
|
|
// roundTripSame verifies the same source object is tested in all API versions.
|
2015-03-04 00:54:17 +00:00
|
|
|
func roundTripSame(t *testing.T, item runtime.Object, except ...string) {
|
|
|
|
set := util.NewStringSet(except...)
|
2014-12-12 21:57:25 +00:00
|
|
|
seed := rand.Int63()
|
|
|
|
fuzzInternalObject(t, "", item, seed)
|
2015-06-30 02:30:14 +00:00
|
|
|
version := testapi.Version()
|
|
|
|
if !set.Has(version) {
|
|
|
|
fuzzInternalObject(t, version, item, seed)
|
|
|
|
roundTrip(t, testapi.Codec(), item)
|
2015-06-03 17:31:26 +00:00
|
|
|
}
|
2014-12-12 21:57:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:17:25 +00:00
|
|
|
// For debugging problems
|
|
|
|
func TestSpecificKind(t *testing.T) {
|
|
|
|
api.Scheme.Log(t)
|
2014-12-12 21:57:25 +00:00
|
|
|
defer api.Scheme.Log(nil)
|
|
|
|
|
2014-10-05 21:17:25 +00:00
|
|
|
kind := "PodList"
|
|
|
|
item, err := api.Scheme.New("", kind)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Couldn't make a %v? %v", kind, err)
|
|
|
|
return
|
|
|
|
}
|
2014-12-12 21:57:25 +00:00
|
|
|
roundTripSame(t, item)
|
2014-10-05 21:17:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 02:19:10 +00:00
|
|
|
func TestList(t *testing.T) {
|
|
|
|
api.Scheme.Log(t)
|
2014-12-12 21:57:25 +00:00
|
|
|
defer api.Scheme.Log(nil)
|
|
|
|
|
2014-12-08 02:19:10 +00:00
|
|
|
kind := "List"
|
|
|
|
item, err := api.Scheme.New("", kind)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Couldn't make a %v? %v", kind, err)
|
|
|
|
return
|
|
|
|
}
|
2014-12-12 21:57:25 +00:00
|
|
|
roundTripSame(t, item)
|
2014-12-08 02:19:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 19:04:30 +00:00
|
|
|
var nonRoundTrippableTypes = util.NewStringSet()
|
2015-07-28 22:56:27 +00:00
|
|
|
var nonInternalRoundTrippableTypes = util.NewStringSet("List", "ListOptions", "PodExecOptions", "PodAttachOptions")
|
2015-05-22 15:20:27 +00:00
|
|
|
var nonRoundTrippableTypesByVersion = map[string][]string{}
|
2014-11-07 02:09:46 +00:00
|
|
|
|
|
|
|
func TestRoundTripTypes(t *testing.T) {
|
2014-12-12 21:57:25 +00:00
|
|
|
// api.Scheme.Log(t)
|
|
|
|
// defer api.Scheme.Log(nil)
|
|
|
|
|
2014-09-24 21:35:34 +00:00
|
|
|
for kind := range api.Scheme.KnownTypes("") {
|
2014-11-07 02:09:46 +00:00
|
|
|
if nonRoundTrippableTypes.Has(kind) {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-30 00:58:25 +00:00
|
|
|
// Try a few times, since runTest uses random values.
|
|
|
|
for i := 0; i < *fuzzIters; i++ {
|
2014-09-24 21:35:34 +00:00
|
|
|
item, err := api.Scheme.New("", kind)
|
|
|
|
if err != nil {
|
2014-11-07 02:09:46 +00:00
|
|
|
t.Fatalf("Couldn't make a %v? %v", kind, err)
|
2014-09-24 21:35:34 +00:00
|
|
|
}
|
2015-01-19 02:22:55 +00:00
|
|
|
if _, err := meta.TypeAccessor(item); err != nil {
|
2014-11-07 02:09:46 +00:00
|
|
|
t.Fatalf("%q is not a TypeMeta and cannot be tested - add it to nonRoundTrippableTypes: %v", kind, err)
|
2014-10-08 19:56:02 +00:00
|
|
|
}
|
2015-03-04 00:54:17 +00:00
|
|
|
roundTripSame(t, item, nonRoundTrippableTypesByVersion[kind]...)
|
2014-12-08 02:19:10 +00:00
|
|
|
if !nonInternalRoundTrippableTypes.Has(kind) {
|
2014-12-12 21:57:25 +00:00
|
|
|
roundTrip(t, api.Codec, fuzzInternalObject(t, "", item, rand.Int63()))
|
2014-12-08 02:19:10 +00:00
|
|
|
}
|
2014-08-30 00:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncode_Ptr(t *testing.T) {
|
|
|
|
pod := &api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{"name": "foo"},
|
|
|
|
},
|
2015-01-26 17:52:50 +00:00
|
|
|
Spec: api.PodSpec{
|
2015-03-14 01:38:07 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
2015-01-26 17:52:50 +00:00
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
},
|
2014-08-30 00:58:25 +00:00
|
|
|
}
|
2014-09-06 01:16:28 +00:00
|
|
|
obj := runtime.Object(pod)
|
2014-09-16 19:56:26 +00:00
|
|
|
data, err := latest.Codec.Encode(obj)
|
|
|
|
obj2, err2 := latest.Codec.Decode(data)
|
2014-08-30 00:58:25 +00:00
|
|
|
if err != nil || err2 != nil {
|
|
|
|
t.Fatalf("Failure: '%v' '%v'", err, err2)
|
|
|
|
}
|
|
|
|
if _, ok := obj2.(*api.Pod); !ok {
|
|
|
|
t.Fatalf("Got wrong type")
|
|
|
|
}
|
2015-01-05 21:38:39 +00:00
|
|
|
if !api.Semantic.DeepEqual(obj2, pod) {
|
2015-01-26 17:52:50 +00:00
|
|
|
t.Errorf("Expected:\n %#v,\n Got:\n %#v", pod, obj2)
|
2014-08-30 00:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadJSONRejection(t *testing.T) {
|
|
|
|
badJSONMissingKind := []byte(`{ }`)
|
2014-09-16 19:56:26 +00:00
|
|
|
if _, err := latest.Codec.Decode(badJSONMissingKind); err == nil {
|
2014-08-30 00:58:25 +00:00
|
|
|
t.Errorf("Did not reject despite lack of kind field: %s", badJSONMissingKind)
|
|
|
|
}
|
|
|
|
badJSONUnknownType := []byte(`{"kind": "bar"}`)
|
2014-09-16 19:56:26 +00:00
|
|
|
if _, err1 := latest.Codec.Decode(badJSONUnknownType); err1 == nil {
|
2014-08-30 00:58:25 +00:00
|
|
|
t.Errorf("Did not reject despite use of unknown type: %s", badJSONUnknownType)
|
|
|
|
}
|
|
|
|
/*badJSONKindMismatch := []byte(`{"kind": "Pod"}`)
|
|
|
|
if err2 := DecodeInto(badJSONKindMismatch, &Minion{}); err2 == nil {
|
|
|
|
t.Errorf("Kind is set but doesn't match the object type: %s", badJSONKindMismatch)
|
|
|
|
}*/
|
|
|
|
}
|
2014-09-19 01:27:33 +00:00
|
|
|
|
|
|
|
const benchmarkSeed = 100
|
|
|
|
|
|
|
|
func BenchmarkEncode(b *testing.B) {
|
|
|
|
pod := api.Pod{}
|
2015-01-29 00:57:33 +00:00
|
|
|
apiObjectFuzzer := apitesting.FuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
2014-09-19 01:27:33 +00:00
|
|
|
apiObjectFuzzer.Fuzz(&pod)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
latest.Codec.Encode(&pod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkEncodeJSON provides a baseline for regular JSON encode performance
|
|
|
|
func BenchmarkEncodeJSON(b *testing.B) {
|
|
|
|
pod := api.Pod{}
|
2015-01-29 00:57:33 +00:00
|
|
|
apiObjectFuzzer := apitesting.FuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
2014-09-19 01:27:33 +00:00
|
|
|
apiObjectFuzzer.Fuzz(&pod)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
json.Marshal(&pod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecode(b *testing.B) {
|
|
|
|
pod := api.Pod{}
|
2015-01-29 00:57:33 +00:00
|
|
|
apiObjectFuzzer := apitesting.FuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
2014-09-19 01:27:33 +00:00
|
|
|
apiObjectFuzzer.Fuzz(&pod)
|
|
|
|
data, _ := latest.Codec.Encode(&pod)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
latest.Codec.Decode(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecodeInto(b *testing.B) {
|
|
|
|
pod := api.Pod{}
|
2015-01-29 00:57:33 +00:00
|
|
|
apiObjectFuzzer := apitesting.FuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
2014-09-19 01:27:33 +00:00
|
|
|
apiObjectFuzzer.Fuzz(&pod)
|
|
|
|
data, _ := latest.Codec.Encode(&pod)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
obj := api.Pod{}
|
|
|
|
latest.Codec.DecodeInto(data, &obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkDecodeJSON provides a baseline for regular JSON decode performance
|
|
|
|
func BenchmarkDecodeJSON(b *testing.B) {
|
|
|
|
pod := api.Pod{}
|
2015-01-29 00:57:33 +00:00
|
|
|
apiObjectFuzzer := apitesting.FuzzerFor(nil, "", rand.NewSource(benchmarkSeed))
|
2014-09-19 01:27:33 +00:00
|
|
|
apiObjectFuzzer.Fuzz(&pod)
|
|
|
|
data, _ := latest.Codec.Encode(&pod)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
obj := api.Pod{}
|
|
|
|
json.Unmarshal(data, &obj)
|
|
|
|
}
|
|
|
|
}
|