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