k3s/pkg/api/ref/ref_test.go

151 lines
3.8 KiB
Go
Raw Normal View History

2014-09-27 00:29:46 +00:00
/*
Copyright 2014 The Kubernetes Authors.
2014-09-27 00:29:46 +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-04-10 21:20:02 +00:00
package ref
2014-09-27 00:29:46 +00:00
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
2017-01-11 14:09:48 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
2017-10-16 11:41:50 +00:00
"k8s.io/kubernetes/pkg/api/legacyscheme"
api "k8s.io/kubernetes/pkg/apis/core"
2014-09-27 00:29:46 +00:00
)
type FakeAPIObject struct{}
2016-11-21 02:55:31 +00:00
func (obj *FakeAPIObject) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (obj *FakeAPIObject) DeepCopyObject() runtime.Object {
if obj == nil {
return nil
}
clone := *obj
return &clone
}
2014-09-27 00:29:46 +00:00
type ExtensionAPIObject struct {
2016-12-03 18:57:26 +00:00
metav1.TypeMeta
metav1.ObjectMeta
}
func (obj *ExtensionAPIObject) DeepCopyObject() runtime.Object {
panic("ExtensionAPIObject does not support DeepCopy")
}
2014-09-27 00:29:46 +00:00
func TestGetReference(t *testing.T) {
// when vendoring kube, if you don't force the set of registered versions (like make test does)
// then you run into trouble because the types aren't registered in the scheme by anything. This does the
// register manually to allow unit test execution
2017-10-16 11:41:50 +00:00
if _, _, err := legacyscheme.Scheme.ObjectKinds(&api.Pod{}); err != nil {
require.NoError(t, api.AddToScheme(legacyscheme.Scheme))
}
2014-09-27 00:29:46 +00:00
table := map[string]struct {
obj runtime.Object
2017-04-10 21:20:02 +00:00
ref *api.ObjectReference
fieldPath string
2014-09-27 00:29:46 +00:00
shouldErr bool
}{
"pod": {
2017-04-10 21:20:02 +00:00
obj: &api.Pod{
ObjectMeta: metav1.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: "foo",
2014-10-23 02:59:15 +00:00
UID: "bar",
ResourceVersion: "42",
SelfLink: "/api/version1/pods/foo",
2014-09-27 00:29:46 +00:00
},
},
fieldPath: ".desiredState.containers[0]",
2017-04-10 21:20:02 +00:00
ref: &api.ObjectReference{
2014-09-27 00:29:46 +00:00
Kind: "Pod",
APIVersion: "version1",
2014-09-27 00:29:46 +00:00
Name: "foo",
2014-10-23 02:59:15 +00:00
UID: "bar",
ResourceVersion: "42",
FieldPath: ".desiredState.containers[0]",
2014-09-27 00:29:46 +00:00
},
},
"serviceList": {
2017-04-10 21:20:02 +00:00
obj: &api.ServiceList{
2016-12-03 18:57:26 +00:00
ListMeta: metav1.ListMeta{
ResourceVersion: "42",
SelfLink: "/api/version2/services",
2014-09-27 00:29:46 +00:00
},
},
2017-04-10 21:20:02 +00:00
ref: &api.ObjectReference{
2014-09-27 00:29:46 +00:00
Kind: "ServiceList",
APIVersion: "version2",
ResourceVersion: "42",
2014-09-27 00:29:46 +00:00
},
},
"extensionAPIObject": {
obj: &ExtensionAPIObject{
2016-12-03 18:57:26 +00:00
TypeMeta: metav1.TypeMeta{
Kind: "ExtensionAPIObject",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
UID: "bar",
ResourceVersion: "42",
SelfLink: "/custom_prefix/version1/extensions/foo",
},
},
2017-04-10 21:20:02 +00:00
ref: &api.ObjectReference{
Kind: "ExtensionAPIObject",
APIVersion: "version1",
Name: "foo",
UID: "bar",
ResourceVersion: "42",
},
},
2014-09-27 00:29:46 +00:00
"badSelfLink": {
2017-04-10 21:20:02 +00:00
obj: &api.ServiceList{
2016-12-03 18:57:26 +00:00
ListMeta: metav1.ListMeta{
ResourceVersion: "42",
SelfLink: "version2/services",
2014-09-27 00:29:46 +00:00
},
},
shouldErr: true,
},
"error": {
obj: &FakeAPIObject{},
ref: nil,
shouldErr: true,
},
"errorNil": {
obj: nil,
ref: nil,
shouldErr: true,
},
2014-09-27 00:29:46 +00:00
}
for name, item := range table {
2017-10-16 11:41:50 +00:00
ref, err := GetPartialReference(legacyscheme.Scheme, item.obj, item.fieldPath)
2014-09-27 00:29:46 +00:00
if e, a := item.shouldErr, (err != nil); e != a {
t.Errorf("%v: expected %v, got %v, err %v", name, e, a, err)
2014-09-27 00:29:46 +00:00
continue
}
if e, a := item.ref, ref; !reflect.DeepEqual(e, a) {
t.Errorf("%v: expected %#v, got %#v", name, e, a)
}
}
}