2015-04-29 18:37:19 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-04-29 18:37:19 +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 config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
2015-09-09 21:59:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2016-01-13 22:40:56 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apimachinery/registered"
|
2016-01-22 05:06:52 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/securitycontext"
|
2015-04-29 18:37:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func noDefault(*api.Pod) error { return nil }
|
|
|
|
|
|
|
|
func TestDecodeSinglePod(t *testing.T) {
|
2015-08-19 23:59:43 +00:00
|
|
|
grace := int64(30)
|
2015-04-29 18:37:19 +00:00
|
|
|
pod := &api.Pod{
|
2015-09-09 21:59:11 +00:00
|
|
|
TypeMeta: unversioned.TypeMeta{
|
2015-04-29 18:37:19 +00:00
|
|
|
APIVersion: "",
|
|
|
|
},
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
UID: "12345",
|
|
|
|
Namespace: "mynamespace",
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
2015-08-19 23:59:43 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
TerminationGracePeriodSeconds: &grace,
|
2015-04-29 18:37:19 +00:00
|
|
|
Containers: []api.Container{{
|
|
|
|
Name: "image",
|
|
|
|
Image: "test/image",
|
|
|
|
ImagePullPolicy: "IfNotPresent",
|
|
|
|
TerminationMessagePath: "/dev/termination-log",
|
2015-05-05 23:02:13 +00:00
|
|
|
SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults(),
|
2015-04-29 18:37:19 +00:00
|
|
|
}},
|
2015-09-14 21:56:51 +00:00
|
|
|
SecurityContext: &api.PodSecurityContext{},
|
2015-04-29 18:37:19 +00:00
|
|
|
},
|
|
|
|
}
|
2016-01-22 05:06:52 +00:00
|
|
|
json, err := runtime.Encode(testapi.Default.Codec(), pod)
|
2015-04-29 18:37:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
parsed, podOut, err := tryDecodeSinglePod(json, noDefault)
|
|
|
|
if !parsed {
|
|
|
|
t.Errorf("expected to have parsed file: (%s)", string(json))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v (%s)", err, string(json))
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(pod, podOut) {
|
|
|
|
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(json))
|
|
|
|
}
|
|
|
|
|
2015-12-19 05:08:34 +00:00
|
|
|
for _, gv := range registered.EnabledVersionsForGroup(api.GroupName) {
|
2016-10-12 20:55:28 +00:00
|
|
|
info, _ := runtime.SerializerInfoForMediaType(api.Codecs.SupportedMediaTypes(), "application/yaml")
|
|
|
|
encoder := api.Codecs.EncoderForVersion(info.Serializer, gv)
|
2016-01-22 05:06:52 +00:00
|
|
|
yaml, err := runtime.Encode(encoder, pod)
|
2015-06-03 23:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
parsed, podOut, err = tryDecodeSinglePod(yaml, noDefault)
|
|
|
|
if !parsed {
|
|
|
|
t.Errorf("expected to have parsed file: (%s)", string(yaml))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v (%s)", err, string(yaml))
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(pod, podOut) {
|
|
|
|
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(yaml))
|
|
|
|
}
|
2015-04-29 18:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodePodList(t *testing.T) {
|
2015-08-19 23:59:43 +00:00
|
|
|
grace := int64(30)
|
2015-04-29 18:37:19 +00:00
|
|
|
pod := &api.Pod{
|
2015-09-09 21:59:11 +00:00
|
|
|
TypeMeta: unversioned.TypeMeta{
|
2015-04-29 18:37:19 +00:00
|
|
|
APIVersion: "",
|
|
|
|
},
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
UID: "12345",
|
|
|
|
Namespace: "mynamespace",
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
2015-08-19 23:59:43 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
TerminationGracePeriodSeconds: &grace,
|
2015-04-29 18:37:19 +00:00
|
|
|
Containers: []api.Container{{
|
|
|
|
Name: "image",
|
|
|
|
Image: "test/image",
|
|
|
|
ImagePullPolicy: "IfNotPresent",
|
|
|
|
TerminationMessagePath: "/dev/termination-log",
|
2015-05-05 23:02:13 +00:00
|
|
|
SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults(),
|
2015-04-29 18:37:19 +00:00
|
|
|
}},
|
2015-09-14 21:56:51 +00:00
|
|
|
SecurityContext: &api.PodSecurityContext{},
|
2015-04-29 18:37:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
podList := &api.PodList{
|
|
|
|
Items: []api.Pod{*pod},
|
|
|
|
}
|
2016-01-22 05:06:52 +00:00
|
|
|
json, err := runtime.Encode(testapi.Default.Codec(), podList)
|
2015-04-29 18:37:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
parsed, podListOut, err := tryDecodePodList(json, noDefault)
|
|
|
|
if !parsed {
|
|
|
|
t.Errorf("expected to have parsed file: (%s)", string(json))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v (%s)", err, string(json))
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(podList, &podListOut) {
|
|
|
|
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", podList, &podListOut, string(json))
|
|
|
|
}
|
|
|
|
|
2015-12-19 05:08:34 +00:00
|
|
|
for _, gv := range registered.EnabledVersionsForGroup(api.GroupName) {
|
2016-10-12 20:55:28 +00:00
|
|
|
info, _ := runtime.SerializerInfoForMediaType(api.Codecs.SupportedMediaTypes(), "application/yaml")
|
|
|
|
encoder := api.Codecs.EncoderForVersion(info.Serializer, gv)
|
2016-01-22 05:06:52 +00:00
|
|
|
yaml, err := runtime.Encode(encoder, podList)
|
2015-06-03 23:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2015-04-29 18:37:19 +00:00
|
|
|
|
2015-06-03 23:07:00 +00:00
|
|
|
parsed, podListOut, err = tryDecodePodList(yaml, noDefault)
|
|
|
|
if !parsed {
|
2016-01-22 05:06:52 +00:00
|
|
|
t.Errorf("expected to have parsed file: (%s): %v", string(yaml), err)
|
|
|
|
continue
|
2015-06-03 23:07:00 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v (%s)", err, string(yaml))
|
2016-01-22 05:06:52 +00:00
|
|
|
continue
|
2015-06-03 23:07:00 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(podList, &podListOut) {
|
|
|
|
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, &podListOut, string(yaml))
|
|
|
|
}
|
2015-04-29 18:37:19 +00:00
|
|
|
}
|
|
|
|
}
|