ref_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 api
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/runtime"
  19. )
  20. type FakeAPIObject struct{}
  21. func (obj *FakeAPIObject) GetObjectKind() unversioned.ObjectKind { return unversioned.EmptyObjectKind }
  22. type ExtensionAPIObject struct {
  23. unversioned.TypeMeta
  24. ObjectMeta
  25. }
  26. func (obj *ExtensionAPIObject) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta }
  27. func TestGetReference(t *testing.T) {
  28. // when vendoring kube, if you don't force the set of registered versions (like make test does)
  29. // then you run into trouble because the types aren't registered in the scheme by anything. This does the
  30. // register manually to allow unit test execution
  31. if _, _, err := Scheme.ObjectKinds(&Pod{}); err != nil {
  32. AddToScheme(Scheme)
  33. }
  34. table := map[string]struct {
  35. obj runtime.Object
  36. ref *ObjectReference
  37. fieldPath string
  38. shouldErr bool
  39. }{
  40. "pod": {
  41. obj: &Pod{
  42. ObjectMeta: ObjectMeta{
  43. Name: "foo",
  44. UID: "bar",
  45. ResourceVersion: "42",
  46. SelfLink: "/api/version1/pods/foo",
  47. },
  48. },
  49. fieldPath: ".desiredState.containers[0]",
  50. ref: &ObjectReference{
  51. Kind: "Pod",
  52. APIVersion: "version1",
  53. Name: "foo",
  54. UID: "bar",
  55. ResourceVersion: "42",
  56. FieldPath: ".desiredState.containers[0]",
  57. },
  58. },
  59. "serviceList": {
  60. obj: &ServiceList{
  61. ListMeta: unversioned.ListMeta{
  62. ResourceVersion: "42",
  63. SelfLink: "/api/version2/services",
  64. },
  65. },
  66. ref: &ObjectReference{
  67. Kind: "ServiceList",
  68. APIVersion: "version2",
  69. ResourceVersion: "42",
  70. },
  71. },
  72. "extensionAPIObject": {
  73. obj: &ExtensionAPIObject{
  74. TypeMeta: unversioned.TypeMeta{
  75. Kind: "ExtensionAPIObject",
  76. },
  77. ObjectMeta: ObjectMeta{
  78. Name: "foo",
  79. UID: "bar",
  80. ResourceVersion: "42",
  81. SelfLink: "/custom_prefix/version1/extensions/foo",
  82. },
  83. },
  84. ref: &ObjectReference{
  85. Kind: "ExtensionAPIObject",
  86. APIVersion: "version1",
  87. Name: "foo",
  88. UID: "bar",
  89. ResourceVersion: "42",
  90. },
  91. },
  92. "badSelfLink": {
  93. obj: &ServiceList{
  94. ListMeta: unversioned.ListMeta{
  95. ResourceVersion: "42",
  96. SelfLink: "version2/services",
  97. },
  98. },
  99. shouldErr: true,
  100. },
  101. "error": {
  102. obj: &FakeAPIObject{},
  103. ref: nil,
  104. shouldErr: true,
  105. },
  106. "errorNil": {
  107. obj: nil,
  108. ref: nil,
  109. shouldErr: true,
  110. },
  111. }
  112. for name, item := range table {
  113. ref, err := GetPartialReference(item.obj, item.fieldPath)
  114. if e, a := item.shouldErr, (err != nil); e != a {
  115. t.Errorf("%v: expected %v, got %v, err %v", name, e, a, err)
  116. continue
  117. }
  118. if e, a := item.ref, ref; !reflect.DeepEqual(e, a) {
  119. t.Errorf("%v: expected %#v, got %#v", name, e, a)
  120. }
  121. }
  122. }