2014-09-11 17:02:53 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-09-11 17:02:53 +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 latest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
internal "k8s.io/kubernetes/pkg/api"
|
2014-09-11 17:02:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceVersioner(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
pod := internal.Pod{ObjectMeta: internal.ObjectMeta{ResourceVersion: "10"}}
|
2015-03-05 05:17:29 +00:00
|
|
|
version, err := accessor.ResourceVersion(&pod)
|
2014-09-11 17:02:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2014-10-07 20:51:28 +00:00
|
|
|
if version != "10" {
|
2014-10-15 18:56:19 +00:00
|
|
|
t.Errorf("unexpected version %v", version)
|
2014-09-11 17:02:53 +00:00
|
|
|
}
|
2014-10-23 20:51:34 +00:00
|
|
|
|
|
|
|
podList := internal.PodList{ListMeta: internal.ListMeta{ResourceVersion: "10"}}
|
2015-03-05 05:17:29 +00:00
|
|
|
version, err = accessor.ResourceVersion(&podList)
|
2014-10-23 20:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if version != "10" {
|
|
|
|
t.Errorf("unexpected version %v", version)
|
|
|
|
}
|
2014-09-11 17:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCodec(t *testing.T) {
|
|
|
|
pod := internal.Pod{}
|
|
|
|
data, err := Codec.Encode(&pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
other := internal.Pod{}
|
|
|
|
if err := json.Unmarshal(data, &other); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if other.APIVersion != Version || other.Kind != "Pod" {
|
|
|
|
t.Errorf("unexpected unmarshalled object %#v", other)
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 23:01:29 +00:00
|
|
|
|
|
|
|
func TestInterfacesFor(t *testing.T) {
|
2014-09-25 22:08:09 +00:00
|
|
|
if _, err := InterfacesFor(""); err == nil {
|
2014-09-11 23:01:29 +00:00
|
|
|
t.Fatalf("unexpected non-error: %v", err)
|
|
|
|
}
|
|
|
|
for i, version := range append([]string{Version, OldestVersion}, Versions...) {
|
2014-09-25 22:08:09 +00:00
|
|
|
if vi, err := InterfacesFor(version); err != nil || vi == nil {
|
2014-09-11 23:01:29 +00:00
|
|
|
t.Fatalf("%d: unexpected result: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 16:33:46 +00:00
|
|
|
|
|
|
|
func TestRESTMapper(t *testing.T) {
|
2015-06-04 22:07:41 +00:00
|
|
|
if v, k, err := RESTMapper.VersionAndKindForResource("replicationcontrollers"); err != nil || v != "v1" || k != "ReplicationController" {
|
2014-10-29 16:33:46 +00:00
|
|
|
t.Errorf("unexpected version mapping: %s %s %v", v, k, err)
|
|
|
|
}
|
|
|
|
|
2015-06-04 22:07:41 +00:00
|
|
|
if m, err := RESTMapper.RESTMapping("PodTemplate", ""); err != nil || m.APIVersion != "v1" || m.Resource != "podtemplates" {
|
2015-03-04 00:54:17 +00:00
|
|
|
t.Errorf("unexpected version mapping: %#v %v", m, err)
|
|
|
|
}
|
|
|
|
|
2014-10-29 16:33:46 +00:00
|
|
|
for _, version := range Versions {
|
2014-12-12 19:05:27 +00:00
|
|
|
mapping, err := RESTMapper.RESTMapping("ReplicationController", version)
|
2014-10-29 16:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mapping.Resource != "replicationControllers" && mapping.Resource != "replicationcontrollers" {
|
|
|
|
t.Errorf("incorrect resource name: %#v", mapping)
|
|
|
|
}
|
|
|
|
if mapping.APIVersion != version {
|
|
|
|
t.Errorf("incorrect version: %v", mapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
interfaces, _ := InterfacesFor(version)
|
|
|
|
if mapping.Codec != interfaces.Codec {
|
2015-05-05 23:53:22 +00:00
|
|
|
t.Errorf("unexpected codec: %#v, expected: %#v", mapping, interfaces)
|
2014-10-29 16:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc := &internal.ReplicationController{ObjectMeta: internal.ObjectMeta{Name: "foo"}}
|
|
|
|
name, err := mapping.MetadataAccessor.Name(rc)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if name != "foo" {
|
|
|
|
t.Errorf("unable to retrieve object meta with: %v", mapping.MetadataAccessor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|