k3s/pkg/api/meta/meta_test.go

207 lines
5.8 KiB
Go
Raw Normal View History

/*
Copyright 2014 Google Inc. All rights reserved.
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 meta
import (
"reflect"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
func TestGenericTypeMeta(t *testing.T) {
type TypeMeta struct {
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
2014-10-22 17:02:02 +00:00
Name string `json:"name,omitempty" yaml:"name,omitempty"`
2014-10-23 02:59:15 +00:00
UID string `json:"uid,omitempty" yaml:"uid,omitempty"`
CreationTimestamp util.Time `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"`
SelfLink string `json:"selfLink,omitempty" yaml:"selfLink,omitempty"`
ResourceVersion string `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
}
j := TypeMeta{
2014-10-22 17:02:02 +00:00
Name: "foo",
2014-10-23 02:59:15 +00:00
UID: "uid",
APIVersion: "a",
Kind: "b",
ResourceVersion: "1",
2014-09-25 21:57:41 +00:00
SelfLink: "some/place/only/we/know",
}
g, err := newGenericTypeMeta(reflect.ValueOf(&j).Elem())
if err != nil {
t.Fatalf("new err: %v", err)
}
// Prove g supports Accessor.
jbi := Accessor(g)
if e, a := "foo", jbi.Name(); e != a {
2014-08-03 22:36:36 +00:00
t.Errorf("expected %v, got %v", e, a)
}
2014-10-23 02:59:15 +00:00
if e, a := "uid", jbi.UID(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := "a", jbi.APIVersion(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := "b", jbi.Kind(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := "1", jbi.ResourceVersion(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
2014-09-25 21:57:41 +00:00
if e, a := "some/place/only/we/know", jbi.SelfLink(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
jbi.SetName("bar")
2014-10-23 02:59:15 +00:00
jbi.SetUID("other")
jbi.SetAPIVersion("c")
jbi.SetKind("d")
jbi.SetResourceVersion("2")
2014-09-25 21:57:41 +00:00
jbi.SetSelfLink("google.com")
2014-08-03 22:36:36 +00:00
// Prove that jbi changes the original object.
2014-10-22 17:02:02 +00:00
if e, a := "bar", j.Name; e != a {
2014-08-03 22:36:36 +00:00
t.Errorf("expected %v, got %v", e, a)
}
2014-10-23 02:59:15 +00:00
if e, a := "other", j.UID; e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := "c", j.APIVersion; e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := "d", j.Kind; e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := "2", j.ResourceVersion; e != a {
t.Errorf("expected %v, got %v", e, a)
}
2014-09-25 21:57:41 +00:00
if e, a := "google.com", j.SelfLink; e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
type MyAPIObject struct {
runtime.TypeMeta `yaml:",inline" json:",inline"`
}
func (*MyAPIObject) IsAnAPIObject() {}
type MyIncorrectlyMarkedAsAPIObject struct {
}
func (*MyIncorrectlyMarkedAsAPIObject) IsAnAPIObject() {}
func TestResourceVersionerOfAPI(t *testing.T) {
type T struct {
runtime.Object
Expected string
}
testCases := map[string]T{
"empty api object": {&MyAPIObject{}, ""},
"api object with version": {&MyAPIObject{TypeMeta: runtime.TypeMeta{ResourceVersion: "1"}}, "1"},
"pointer to api object with version": {&MyAPIObject{TypeMeta: runtime.TypeMeta{ResourceVersion: "1"}}, "1"},
}
versioning := NewResourceVersioner()
for key, testCase := range testCases {
actual, err := versioning.ResourceVersion(testCase.Object)
if err != nil {
t.Errorf("%s: unexpected error %#v", key, err)
}
if actual != testCase.Expected {
t.Errorf("%s: expected %v, got %v", key, testCase.Expected, actual)
}
}
2014-08-11 04:11:47 +00:00
failingCases := map[string]struct {
runtime.Object
Expected string
2014-08-11 04:11:47 +00:00
}{
"not a valid object to try": {&MyIncorrectlyMarkedAsAPIObject{}, "1"},
}
for key, testCase := range failingCases {
_, err := versioning.ResourceVersion(testCase.Object)
if err == nil {
t.Errorf("%s: expected error, got nil", key)
}
}
2014-08-11 04:11:47 +00:00
setCases := map[string]struct {
runtime.Object
Expected string
2014-08-11 04:11:47 +00:00
}{
"pointer to api object with version": {&MyAPIObject{TypeMeta: runtime.TypeMeta{ResourceVersion: "1"}}, "1"},
2014-08-11 04:11:47 +00:00
}
for key, testCase := range setCases {
if err := versioning.SetResourceVersion(testCase.Object, "5"); err != nil {
2014-08-11 04:11:47 +00:00
t.Errorf("%s: unexpected error %#v", key, err)
}
actual, err := versioning.ResourceVersion(testCase.Object)
if err != nil {
t.Errorf("%s: unexpected error %#v", key, err)
}
if actual != "5" {
t.Errorf("%s: expected %v, got %v", key, "5", actual)
2014-08-11 04:11:47 +00:00
}
}
}
2014-09-25 21:57:41 +00:00
func TestTypeMetaSelfLinker(t *testing.T) {
2014-09-25 21:57:41 +00:00
table := map[string]struct {
obj runtime.Object
2014-09-25 21:57:41 +00:00
expect string
try string
succeed bool
}{
"normal": {
obj: &MyAPIObject{TypeMeta: runtime.TypeMeta{SelfLink: "foobar"}},
2014-09-25 21:57:41 +00:00
expect: "foobar",
try: "newbar",
succeed: true,
},
"fail": {
obj: &MyIncorrectlyMarkedAsAPIObject{},
succeed: false,
},
}
linker := NewSelfLinker()
2014-09-25 21:57:41 +00:00
for name, item := range table {
got, err := linker.SelfLink(item.obj)
if e, a := item.succeed, err == nil; e != a {
t.Errorf("%v: expected %v, got %v", name, e, a)
}
if e, a := item.expect, got; item.succeed && e != a {
t.Errorf("%v: expected %v, got %v", name, e, a)
}
err = linker.SetSelfLink(item.obj, item.try)
if e, a := item.succeed, err == nil; e != a {
t.Errorf("%v: expected %v, got %v", name, e, a)
}
if item.succeed {
got, err := linker.SelfLink(item.obj)
if err != nil {
t.Errorf("%v: expected no err, got %v", name, err)
}
if e, a := item.try, got; e != a {
t.Errorf("%v: expected %v, got %v", name, e, a)
}
}
}
}