2015-04-29 03:15:16 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-04-29 03:15:16 +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.
|
|
|
|
*/
|
|
|
|
|
2017-01-05 20:53:49 +00:00
|
|
|
package tests
|
2015-04-29 03:15:16 +00:00
|
|
|
|
|
|
|
import (
|
2015-07-01 06:36:39 +00:00
|
|
|
"fmt"
|
2016-01-20 23:24:30 +00:00
|
|
|
"reflect"
|
2015-11-07 04:13:17 +00:00
|
|
|
"strings"
|
2015-04-29 03:15:16 +00:00
|
|
|
"testing"
|
2016-04-25 20:34:35 +00:00
|
|
|
"time"
|
2015-04-29 03:15:16 +00:00
|
|
|
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apimachinery/registered"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
2015-11-07 04:13:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/validation"
|
2015-04-29 03:15:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeUnstructured(t *testing.T) {
|
2016-10-04 21:20:07 +00:00
|
|
|
groupVersionString := registered.GroupOrDie(api.GroupName).GroupVersion.String()
|
2015-11-30 20:28:48 +00:00
|
|
|
rawJson := fmt.Sprintf(`{"kind":"Pod","apiVersion":"%s","metadata":{"name":"test"}}`, groupVersionString)
|
2015-04-29 03:15:16 +00:00
|
|
|
pl := &api.List{
|
|
|
|
Items: []runtime.Object{
|
|
|
|
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
|
2016-03-16 08:03:33 +00:00
|
|
|
&runtime.Unknown{
|
|
|
|
TypeMeta: runtime.TypeMeta{Kind: "Pod", APIVersion: groupVersionString},
|
|
|
|
Raw: []byte(rawJson),
|
|
|
|
ContentType: runtime.ContentTypeJSON,
|
|
|
|
},
|
|
|
|
&runtime.Unknown{
|
|
|
|
TypeMeta: runtime.TypeMeta{Kind: "", APIVersion: groupVersionString},
|
|
|
|
Raw: []byte(rawJson),
|
|
|
|
ContentType: runtime.ContentTypeJSON,
|
|
|
|
},
|
2016-11-03 01:54:13 +00:00
|
|
|
&unstructured.Unstructured{
|
2016-04-25 20:34:35 +00:00
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "Foo",
|
|
|
|
"apiVersion": "Bar",
|
|
|
|
"test": "value",
|
|
|
|
},
|
|
|
|
},
|
2015-04-29 03:15:16 +00:00
|
|
|
},
|
|
|
|
}
|
2016-11-03 01:54:13 +00:00
|
|
|
if errs := runtime.DecodeList(pl.Items, unstructured.UnstructuredJSONScheme); len(errs) == 1 {
|
2015-04-29 03:15:16 +00:00
|
|
|
t.Fatalf("unexpected error %v", errs)
|
|
|
|
}
|
2016-11-03 01:54:13 +00:00
|
|
|
if pod, ok := pl.Items[1].(*unstructured.Unstructured); !ok || pod.Object["kind"] != "Pod" || pod.Object["metadata"].(map[string]interface{})["name"] != "test" {
|
2015-04-29 03:15:16 +00:00
|
|
|
t.Errorf("object not converted: %#v", pl.Items[1])
|
|
|
|
}
|
2016-11-03 01:54:13 +00:00
|
|
|
if pod, ok := pl.Items[2].(*unstructured.Unstructured); !ok || pod.Object["kind"] != "Pod" || pod.Object["metadata"].(map[string]interface{})["name"] != "test" {
|
2015-12-21 05:08:33 +00:00
|
|
|
t.Errorf("object not converted: %#v", pl.Items[2])
|
2015-04-29 03:15:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 23:24:30 +00:00
|
|
|
|
|
|
|
func TestDecode(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
json []byte
|
|
|
|
want runtime.Object
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
json: []byte(`{"apiVersion": "test", "kind": "test_kind"}`),
|
2016-11-03 01:54:13 +00:00
|
|
|
want: &unstructured.Unstructured{
|
2016-01-20 23:24:30 +00:00
|
|
|
Object: map[string]interface{}{"apiVersion": "test", "kind": "test_kind"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
json: []byte(`{"apiVersion": "test", "kind": "test_list", "items": []}`),
|
2016-11-03 01:54:13 +00:00
|
|
|
want: &unstructured.UnstructuredList{
|
2016-04-25 20:34:35 +00:00
|
|
|
Object: map[string]interface{}{"apiVersion": "test", "kind": "test_list"},
|
2016-01-20 23:24:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
json: []byte(`{"items": [{"metadata": {"name": "object1"}, "apiVersion": "test", "kind": "test_kind"}, {"metadata": {"name": "object2"}, "apiVersion": "test", "kind": "test_kind"}], "apiVersion": "test", "kind": "test_list"}`),
|
2016-11-03 01:54:13 +00:00
|
|
|
want: &unstructured.UnstructuredList{
|
2016-04-25 20:34:35 +00:00
|
|
|
Object: map[string]interface{}{"apiVersion": "test", "kind": "test_list"},
|
2016-11-03 01:54:13 +00:00
|
|
|
Items: []*unstructured.Unstructured{
|
2016-01-20 23:24:30 +00:00
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{"name": "object1"},
|
|
|
|
"apiVersion": "test",
|
|
|
|
"kind": "test_kind",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{"name": "object2"},
|
|
|
|
"apiVersion": "test",
|
|
|
|
"kind": "test_kind",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
2016-11-03 01:54:13 +00:00
|
|
|
got, _, err := unstructured.UnstructuredJSONScheme.Decode(tc.json, nil, nil)
|
2016-01-20 23:24:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error for %q: %v", string(tc.json), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, tc.want) {
|
|
|
|
t.Errorf("Decode(%q) want: %v\ngot: %v", string(tc.json), tc.want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-07 04:13:17 +00:00
|
|
|
|
2016-04-25 20:34:35 +00:00
|
|
|
func TestUnstructuredGetters(t *testing.T) {
|
2016-11-03 01:54:13 +00:00
|
|
|
unstruct := unstructured.Unstructured{
|
2016-04-25 20:34:35 +00:00
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "test_kind",
|
|
|
|
"apiVersion": "test_version",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "test_name",
|
|
|
|
"namespace": "test_namespace",
|
|
|
|
"generateName": "test_generateName",
|
|
|
|
"uid": "test_uid",
|
|
|
|
"resourceVersion": "test_resourceVersion",
|
|
|
|
"selfLink": "test_selfLink",
|
|
|
|
"creationTimestamp": "2009-11-10T23:00:00Z",
|
|
|
|
"deletionTimestamp": "2010-11-10T23:00:00Z",
|
|
|
|
"labels": map[string]interface{}{
|
|
|
|
"test_label": "test_value",
|
|
|
|
},
|
|
|
|
"annotations": map[string]interface{}{
|
|
|
|
"test_annotation": "test_value",
|
|
|
|
},
|
2016-05-04 05:31:26 +00:00
|
|
|
"ownerReferences": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"kind": "Pod",
|
|
|
|
"name": "poda",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"uid": "1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"kind": "Pod",
|
|
|
|
"name": "podb",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"uid": "2",
|
|
|
|
},
|
|
|
|
},
|
2016-05-18 03:24:42 +00:00
|
|
|
"finalizers": []interface{}{
|
|
|
|
"finalizer.1",
|
|
|
|
"finalizer.2",
|
|
|
|
},
|
2016-07-13 02:50:18 +00:00
|
|
|
"clusterName": "cluster123",
|
2016-04-25 20:34:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetAPIVersion(), "test_version"; got != want {
|
|
|
|
t.Errorf("GetAPIVersions() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetKind(), "test_kind"; got != want {
|
|
|
|
t.Errorf("GetKind() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetNamespace(), "test_namespace"; got != want {
|
|
|
|
t.Errorf("GetNamespace() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetName(), "test_name"; got != want {
|
|
|
|
t.Errorf("GetName() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetGenerateName(), "test_generateName"; got != want {
|
|
|
|
t.Errorf("GetGenerateName() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetUID(), types.UID("test_uid"); got != want {
|
|
|
|
t.Errorf("GetUID() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetResourceVersion(), "test_resourceVersion"; got != want {
|
|
|
|
t.Errorf("GetResourceVersion() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetSelfLink(), "test_selfLink"; got != want {
|
|
|
|
t.Errorf("GetSelfLink() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
2016-12-03 18:57:26 +00:00
|
|
|
if got, want := unstruct.GetCreationTimestamp(), metav1.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC); !got.Equal(want) {
|
2016-04-25 20:34:35 +00:00
|
|
|
t.Errorf("GetCreationTimestamp() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
2016-12-03 18:57:26 +00:00
|
|
|
if got, want := unstruct.GetDeletionTimestamp(), metav1.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC); got == nil || !got.Equal(want) {
|
2016-04-25 20:34:35 +00:00
|
|
|
t.Errorf("GetDeletionTimestamp() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetLabels(), map[string]string{"test_label": "test_value"}; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("GetLabels() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetAnnotations(), map[string]string{"test_annotation": "test_value"}; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("GetAnnotations() = %s, want %s", got, want)
|
|
|
|
}
|
2016-05-04 05:31:26 +00:00
|
|
|
refs := unstruct.GetOwnerReferences()
|
2016-12-04 03:42:29 +00:00
|
|
|
expectedOwnerReferences := []metav1.OwnerReference{
|
2016-05-04 05:31:26 +00:00
|
|
|
{
|
|
|
|
Kind: "Pod",
|
|
|
|
Name: "poda",
|
|
|
|
APIVersion: "v1",
|
|
|
|
UID: "1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: "Pod",
|
|
|
|
Name: "podb",
|
|
|
|
APIVersion: "v1",
|
|
|
|
UID: "2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if got, want := refs, expectedOwnerReferences; !reflect.DeepEqual(got, want) {
|
2016-05-18 03:24:42 +00:00
|
|
|
t.Errorf("GetOwnerReferences()=%v, want %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := unstruct.GetFinalizers(), []string{"finalizer.1", "finalizer.2"}; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("GetFinalizers()=%v, want %v", got, want)
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
2016-07-13 02:50:18 +00:00
|
|
|
if got, want := unstruct.GetClusterName(), "cluster123"; got != want {
|
|
|
|
t.Errorf("GetClusterName()=%v, want %v", got, want)
|
|
|
|
}
|
2016-04-25 20:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnstructuredSetters(t *testing.T) {
|
2016-11-03 01:54:13 +00:00
|
|
|
unstruct := unstructured.Unstructured{}
|
2016-05-20 12:38:19 +00:00
|
|
|
trueVar := true
|
2016-04-25 20:34:35 +00:00
|
|
|
|
2016-11-03 01:54:13 +00:00
|
|
|
want := unstructured.Unstructured{
|
2016-04-25 20:34:35 +00:00
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "test_kind",
|
|
|
|
"apiVersion": "test_version",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "test_name",
|
|
|
|
"namespace": "test_namespace",
|
|
|
|
"generateName": "test_generateName",
|
|
|
|
"uid": "test_uid",
|
|
|
|
"resourceVersion": "test_resourceVersion",
|
|
|
|
"selfLink": "test_selfLink",
|
|
|
|
"creationTimestamp": "2009-11-10T23:00:00Z",
|
|
|
|
"deletionTimestamp": "2010-11-10T23:00:00Z",
|
|
|
|
"labels": map[string]interface{}{
|
|
|
|
"test_label": "test_value",
|
|
|
|
},
|
|
|
|
"annotations": map[string]interface{}{
|
|
|
|
"test_annotation": "test_value",
|
|
|
|
},
|
2016-05-04 05:31:26 +00:00
|
|
|
"ownerReferences": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"kind": "Pod",
|
|
|
|
"name": "poda",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"uid": "1",
|
2016-05-20 12:38:19 +00:00
|
|
|
"controller": (*bool)(nil),
|
2016-05-04 05:31:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"kind": "Pod",
|
|
|
|
"name": "podb",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"uid": "2",
|
2016-05-20 12:38:19 +00:00
|
|
|
"controller": &trueVar,
|
2016-05-04 05:31:26 +00:00
|
|
|
},
|
|
|
|
},
|
2016-05-18 03:24:42 +00:00
|
|
|
"finalizers": []interface{}{
|
|
|
|
"finalizer.1",
|
|
|
|
"finalizer.2",
|
|
|
|
},
|
2016-07-13 02:50:18 +00:00
|
|
|
"clusterName": "cluster123",
|
2016-04-25 20:34:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
unstruct.SetAPIVersion("test_version")
|
|
|
|
unstruct.SetKind("test_kind")
|
|
|
|
unstruct.SetNamespace("test_namespace")
|
|
|
|
unstruct.SetName("test_name")
|
|
|
|
unstruct.SetGenerateName("test_generateName")
|
|
|
|
unstruct.SetUID(types.UID("test_uid"))
|
|
|
|
unstruct.SetResourceVersion("test_resourceVersion")
|
|
|
|
unstruct.SetSelfLink("test_selfLink")
|
2016-12-03 18:57:26 +00:00
|
|
|
unstruct.SetCreationTimestamp(metav1.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))
|
|
|
|
date := metav1.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)
|
2016-04-25 20:34:35 +00:00
|
|
|
unstruct.SetDeletionTimestamp(&date)
|
|
|
|
unstruct.SetLabels(map[string]string{"test_label": "test_value"})
|
|
|
|
unstruct.SetAnnotations(map[string]string{"test_annotation": "test_value"})
|
2016-12-04 03:42:29 +00:00
|
|
|
newOwnerReferences := []metav1.OwnerReference{
|
2016-05-04 05:31:26 +00:00
|
|
|
{
|
|
|
|
Kind: "Pod",
|
|
|
|
Name: "poda",
|
|
|
|
APIVersion: "v1",
|
|
|
|
UID: "1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: "Pod",
|
|
|
|
Name: "podb",
|
|
|
|
APIVersion: "v1",
|
|
|
|
UID: "2",
|
2016-05-20 12:38:19 +00:00
|
|
|
Controller: &trueVar,
|
2016-05-04 05:31:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
unstruct.SetOwnerReferences(newOwnerReferences)
|
2016-05-18 03:24:42 +00:00
|
|
|
unstruct.SetFinalizers([]string{"finalizer.1", "finalizer.2"})
|
2016-07-13 02:50:18 +00:00
|
|
|
unstruct.SetClusterName("cluster123")
|
2016-04-25 20:34:35 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(unstruct, want) {
|
2016-05-04 05:31:26 +00:00
|
|
|
t.Errorf("Wanted: \n%s\n Got:\n%s", want, unstruct)
|
2016-04-25 20:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnstructuredListGetters(t *testing.T) {
|
2016-11-03 01:54:13 +00:00
|
|
|
unstruct := unstructured.UnstructuredList{
|
2016-04-25 20:34:35 +00:00
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "test_kind",
|
|
|
|
"apiVersion": "test_version",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"resourceVersion": "test_resourceVersion",
|
|
|
|
"selfLink": "test_selfLink",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetAPIVersion(), "test_version"; got != want {
|
|
|
|
t.Errorf("GetAPIVersions() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetKind(), "test_kind"; got != want {
|
|
|
|
t.Errorf("GetKind() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetResourceVersion(), "test_resourceVersion"; got != want {
|
|
|
|
t.Errorf("GetResourceVersion() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := unstruct.GetSelfLink(), "test_selfLink"; got != want {
|
|
|
|
t.Errorf("GetSelfLink() = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnstructuredListSetters(t *testing.T) {
|
2016-11-03 01:54:13 +00:00
|
|
|
unstruct := unstructured.UnstructuredList{}
|
2016-04-25 20:34:35 +00:00
|
|
|
|
2016-11-03 01:54:13 +00:00
|
|
|
want := unstructured.UnstructuredList{
|
2016-04-25 20:34:35 +00:00
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "test_kind",
|
|
|
|
"apiVersion": "test_version",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"resourceVersion": "test_resourceVersion",
|
|
|
|
"selfLink": "test_selfLink",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
unstruct.SetAPIVersion("test_version")
|
|
|
|
unstruct.SetKind("test_kind")
|
|
|
|
unstruct.SetResourceVersion("test_resourceVersion")
|
|
|
|
unstruct.SetSelfLink("test_selfLink")
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(unstruct, want) {
|
|
|
|
t.Errorf("Wanted: \n%s\n Got:\n%s", unstruct, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-07 04:13:17 +00:00
|
|
|
func TestDecodeNumbers(t *testing.T) {
|
|
|
|
|
|
|
|
// Start with a valid pod
|
|
|
|
originalJSON := []byte(`{
|
|
|
|
"kind":"Pod",
|
|
|
|
"apiVersion":"v1",
|
|
|
|
"metadata":{"name":"pod","namespace":"foo"},
|
|
|
|
"spec":{
|
|
|
|
"containers":[{"name":"container","image":"container"}],
|
|
|
|
"activeDeadlineSeconds":1000030003
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
pod := &api.Pod{}
|
|
|
|
|
|
|
|
// Decode with structured codec
|
|
|
|
codec, err := testapi.GetCodecForObject(pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
err = runtime.DecodeInto(codec, originalJSON, pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
// ensure pod is valid
|
|
|
|
if errs := validation.ValidatePod(pod); len(errs) > 0 {
|
|
|
|
t.Fatalf("pod should be valid: %v", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Round-trip with unstructured codec
|
2016-11-03 01:54:13 +00:00
|
|
|
unstructuredObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, originalJSON)
|
2015-11-07 04:13:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2016-11-03 01:54:13 +00:00
|
|
|
roundtripJSON, err := runtime.Encode(unstructured.UnstructuredJSONScheme, unstructuredObj)
|
2015-11-07 04:13:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we serialize back out in int form
|
|
|
|
if !strings.Contains(string(roundtripJSON), `"activeDeadlineSeconds":1000030003`) {
|
|
|
|
t.Errorf("Expected %s, got %s", `"activeDeadlineSeconds":1000030003`, string(roundtripJSON))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode with structured codec again
|
|
|
|
obj2, err := runtime.Decode(codec, roundtripJSON)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
// ensure pod is still valid
|
|
|
|
pod2, ok := obj2.(*api.Pod)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected an *api.Pod, got %#v", obj2)
|
|
|
|
}
|
|
|
|
if errs := validation.ValidatePod(pod2); len(errs) > 0 {
|
|
|
|
t.Fatalf("pod should be valid: %v", errs)
|
|
|
|
}
|
|
|
|
// ensure round-trip preserved large integers
|
|
|
|
if !reflect.DeepEqual(pod, pod2) {
|
|
|
|
t.Fatalf("Expected\n\t%#v, got \n\t%#v", pod, pod2)
|
|
|
|
}
|
|
|
|
}
|