helpers_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright 2017 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 pods
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. utilfeature "k8s.io/apiserver/pkg/util/feature"
  19. featuregatetesting "k8s.io/component-base/featuregate/testing"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. "k8s.io/kubernetes/pkg/features"
  22. )
  23. func TestVisitContainersWithPath(t *testing.T) {
  24. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.EphemeralContainers, true)()
  25. testCases := []struct {
  26. description string
  27. haveSpec *api.PodSpec
  28. wantNames []string
  29. }{
  30. {
  31. "empty podspec",
  32. &api.PodSpec{},
  33. []string{},
  34. },
  35. {
  36. "regular containers",
  37. &api.PodSpec{
  38. Containers: []api.Container{
  39. {Name: "c1"},
  40. {Name: "c2"},
  41. },
  42. },
  43. []string{"spec.containers[0]", "spec.containers[1]"},
  44. },
  45. {
  46. "init containers",
  47. &api.PodSpec{
  48. InitContainers: []api.Container{
  49. {Name: "i1"},
  50. {Name: "i2"},
  51. },
  52. },
  53. []string{"spec.initContainers[0]", "spec.initContainers[1]"},
  54. },
  55. {
  56. "regular and init containers",
  57. &api.PodSpec{
  58. Containers: []api.Container{
  59. {Name: "c1"},
  60. {Name: "c2"},
  61. },
  62. InitContainers: []api.Container{
  63. {Name: "i1"},
  64. {Name: "i2"},
  65. },
  66. },
  67. []string{"spec.initContainers[0]", "spec.initContainers[1]", "spec.containers[0]", "spec.containers[1]"},
  68. },
  69. {
  70. "ephemeral containers",
  71. &api.PodSpec{
  72. Containers: []api.Container{
  73. {Name: "c1"},
  74. {Name: "c2"},
  75. },
  76. EphemeralContainers: []api.EphemeralContainer{
  77. {EphemeralContainerCommon: api.EphemeralContainerCommon{Name: "e1"}},
  78. },
  79. },
  80. []string{"spec.containers[0]", "spec.containers[1]", "spec.ephemeralContainers[0]"},
  81. },
  82. {
  83. "all container types",
  84. &api.PodSpec{
  85. Containers: []api.Container{
  86. {Name: "c1"},
  87. {Name: "c2"},
  88. },
  89. InitContainers: []api.Container{
  90. {Name: "i1"},
  91. {Name: "i2"},
  92. },
  93. EphemeralContainers: []api.EphemeralContainer{
  94. {EphemeralContainerCommon: api.EphemeralContainerCommon{Name: "e1"}},
  95. {EphemeralContainerCommon: api.EphemeralContainerCommon{Name: "e2"}},
  96. },
  97. },
  98. []string{"spec.initContainers[0]", "spec.initContainers[1]", "spec.containers[0]", "spec.containers[1]", "spec.ephemeralContainers[0]", "spec.ephemeralContainers[1]"},
  99. },
  100. }
  101. for _, tc := range testCases {
  102. gotNames := []string{}
  103. VisitContainersWithPath(tc.haveSpec, func(c *api.Container, p *field.Path) bool {
  104. gotNames = append(gotNames, p.String())
  105. return true
  106. })
  107. if !reflect.DeepEqual(gotNames, tc.wantNames) {
  108. t.Errorf("VisitContainersWithPath() for test case %q visited containers %q, wanted to visit %q", tc.description, gotNames, tc.wantNames)
  109. }
  110. }
  111. }
  112. func TestConvertDownwardAPIFieldLabel(t *testing.T) {
  113. testCases := []struct {
  114. version string
  115. label string
  116. value string
  117. expectedErr bool
  118. expectedLabel string
  119. expectedValue string
  120. }{
  121. {
  122. version: "v2",
  123. label: "metadata.name",
  124. value: "test-pod",
  125. expectedErr: true,
  126. },
  127. {
  128. version: "v1",
  129. label: "invalid-label",
  130. value: "value",
  131. expectedErr: true,
  132. },
  133. {
  134. version: "v1",
  135. label: "metadata.name",
  136. value: "test-pod",
  137. expectedLabel: "metadata.name",
  138. expectedValue: "test-pod",
  139. },
  140. {
  141. version: "v1",
  142. label: "metadata.annotations",
  143. value: "myValue",
  144. expectedLabel: "metadata.annotations",
  145. expectedValue: "myValue",
  146. },
  147. {
  148. version: "v1",
  149. label: "metadata.annotations['myKey']",
  150. value: "myValue",
  151. expectedLabel: "metadata.annotations['myKey']",
  152. expectedValue: "myValue",
  153. },
  154. {
  155. version: "v1",
  156. label: "spec.host",
  157. value: "127.0.0.1",
  158. expectedLabel: "spec.nodeName",
  159. expectedValue: "127.0.0.1",
  160. },
  161. {
  162. version: "v1",
  163. label: "status.podIPs",
  164. value: "10.244.0.6,fd00::6",
  165. expectedLabel: "status.podIPs",
  166. expectedValue: "10.244.0.6,fd00::6",
  167. },
  168. {
  169. version: "v1",
  170. label: "status.podIPs",
  171. value: "10.244.0.6",
  172. expectedLabel: "status.podIPs",
  173. expectedValue: "10.244.0.6",
  174. },
  175. }
  176. for _, tc := range testCases {
  177. label, value, err := ConvertDownwardAPIFieldLabel(tc.version, tc.label, tc.value)
  178. if err != nil {
  179. if tc.expectedErr {
  180. continue
  181. }
  182. t.Errorf("ConvertDownwardAPIFieldLabel(%s, %s, %s) failed: %s",
  183. tc.version, tc.label, tc.value, err)
  184. }
  185. if tc.expectedLabel != label || tc.expectedValue != value {
  186. t.Errorf("ConvertDownwardAPIFieldLabel(%s, %s, %s) = (%s, %s, nil), expected (%s, %s, nil)",
  187. tc.version, tc.label, tc.value, label, value, tc.expectedLabel, tc.expectedValue)
  188. }
  189. }
  190. }