ref_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ref
  14. import (
  15. "reflect"
  16. "testing"
  17. "github.com/stretchr/testify/require"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. "k8s.io/kubernetes/pkg/api/legacyscheme"
  22. api "k8s.io/kubernetes/pkg/apis/core"
  23. )
  24. type FakeAPIObject struct{}
  25. func (obj *FakeAPIObject) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
  26. func (obj *FakeAPIObject) DeepCopyObject() runtime.Object {
  27. if obj == nil {
  28. return nil
  29. }
  30. clone := *obj
  31. return &clone
  32. }
  33. type ExtensionAPIObject struct {
  34. metav1.TypeMeta
  35. metav1.ObjectMeta
  36. }
  37. func (obj *ExtensionAPIObject) DeepCopyObject() runtime.Object {
  38. panic("ExtensionAPIObject does not support DeepCopy")
  39. }
  40. func TestGetReference(t *testing.T) {
  41. // when vendoring kube, if you don't force the set of registered versions (like make test does)
  42. // then you run into trouble because the types aren't registered in the scheme by anything. This does the
  43. // register manually to allow unit test execution
  44. if _, _, err := legacyscheme.Scheme.ObjectKinds(&api.Pod{}); err != nil {
  45. require.NoError(t, api.AddToScheme(legacyscheme.Scheme))
  46. }
  47. table := map[string]struct {
  48. obj runtime.Object
  49. ref *api.ObjectReference
  50. fieldPath string
  51. shouldErr bool
  52. }{
  53. "pod": {
  54. obj: &api.Pod{
  55. ObjectMeta: metav1.ObjectMeta{
  56. Name: "foo",
  57. UID: "bar",
  58. ResourceVersion: "42",
  59. SelfLink: "/api/version1/pods/foo",
  60. },
  61. },
  62. fieldPath: ".desiredState.containers[0]",
  63. ref: &api.ObjectReference{
  64. Kind: "Pod",
  65. APIVersion: "version1",
  66. Name: "foo",
  67. UID: "bar",
  68. ResourceVersion: "42",
  69. FieldPath: ".desiredState.containers[0]",
  70. },
  71. },
  72. "serviceList": {
  73. obj: &api.ServiceList{
  74. ListMeta: metav1.ListMeta{
  75. ResourceVersion: "42",
  76. SelfLink: "/api/version2/services",
  77. },
  78. },
  79. ref: &api.ObjectReference{
  80. Kind: "ServiceList",
  81. APIVersion: "version2",
  82. ResourceVersion: "42",
  83. },
  84. },
  85. "extensionAPIObject": {
  86. obj: &ExtensionAPIObject{
  87. TypeMeta: metav1.TypeMeta{
  88. Kind: "ExtensionAPIObject",
  89. },
  90. ObjectMeta: metav1.ObjectMeta{
  91. Name: "foo",
  92. UID: "bar",
  93. ResourceVersion: "42",
  94. SelfLink: "/custom_prefix/version1/extensions/foo",
  95. },
  96. },
  97. ref: &api.ObjectReference{
  98. Kind: "ExtensionAPIObject",
  99. APIVersion: "version1",
  100. Name: "foo",
  101. UID: "bar",
  102. ResourceVersion: "42",
  103. },
  104. },
  105. "badSelfLink": {
  106. obj: &api.ServiceList{
  107. ListMeta: metav1.ListMeta{
  108. ResourceVersion: "42",
  109. SelfLink: "version2/services",
  110. },
  111. },
  112. shouldErr: true,
  113. },
  114. "error": {
  115. obj: &FakeAPIObject{},
  116. ref: nil,
  117. shouldErr: true,
  118. },
  119. "errorNil": {
  120. obj: nil,
  121. ref: nil,
  122. shouldErr: true,
  123. },
  124. }
  125. for name, item := range table {
  126. ref, err := GetPartialReference(legacyscheme.Scheme, item.obj, item.fieldPath)
  127. if e, a := item.shouldErr, (err != nil); e != a {
  128. t.Errorf("%v: expected %v, got %v, err %v", name, e, a, err)
  129. continue
  130. }
  131. if e, a := item.ref, ref; !reflect.DeepEqual(e, a) {
  132. t.Errorf("%v: expected %#v, got %#v", name, e, a)
  133. }
  134. }
  135. }