labels_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. Copyright 2016 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 kuberuntime
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/intstr"
  20. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  21. )
  22. func TestContainerLabels(t *testing.T) {
  23. deletionGracePeriod := int64(10)
  24. terminationGracePeriod := int64(10)
  25. lifecycle := &v1.Lifecycle{
  26. // Left PostStart as nil
  27. PreStop: &v1.Handler{
  28. Exec: &v1.ExecAction{
  29. Command: []string{"action1", "action2"},
  30. },
  31. HTTPGet: &v1.HTTPGetAction{
  32. Path: "path",
  33. Host: "host",
  34. Port: intstr.FromInt(8080),
  35. Scheme: "scheme",
  36. },
  37. TCPSocket: &v1.TCPSocketAction{
  38. Port: intstr.FromString("80"),
  39. },
  40. },
  41. }
  42. container := &v1.Container{
  43. Name: "test_container",
  44. TerminationMessagePath: "/somepath",
  45. Lifecycle: lifecycle,
  46. }
  47. pod := &v1.Pod{
  48. ObjectMeta: metav1.ObjectMeta{
  49. Name: "test_pod",
  50. Namespace: "test_pod_namespace",
  51. UID: "test_pod_uid",
  52. DeletionGracePeriodSeconds: &deletionGracePeriod,
  53. },
  54. Spec: v1.PodSpec{
  55. Containers: []v1.Container{*container},
  56. TerminationGracePeriodSeconds: &terminationGracePeriod,
  57. },
  58. }
  59. var tests = []struct {
  60. description string
  61. expected *labeledContainerInfo
  62. }{
  63. {
  64. "Regular containers",
  65. &labeledContainerInfo{
  66. PodName: pod.Name,
  67. PodNamespace: pod.Namespace,
  68. PodUID: pod.UID,
  69. ContainerName: container.Name,
  70. },
  71. },
  72. }
  73. // Test whether we can get right information from label
  74. for _, test := range tests {
  75. labels := newContainerLabels(container, pod)
  76. containerInfo := getContainerInfoFromLabels(labels)
  77. if !reflect.DeepEqual(containerInfo, test.expected) {
  78. t.Errorf("%v: expected %v, got %v", test.description, test.expected, containerInfo)
  79. }
  80. }
  81. }
  82. func TestContainerAnnotations(t *testing.T) {
  83. restartCount := 5
  84. deletionGracePeriod := int64(10)
  85. terminationGracePeriod := int64(10)
  86. opts := &kubecontainer.RunContainerOptions{
  87. Annotations: []kubecontainer.Annotation{
  88. {Name: "Foo", Value: "bar"},
  89. },
  90. }
  91. lifecycle := &v1.Lifecycle{
  92. // Left PostStart as nil
  93. PreStop: &v1.Handler{
  94. Exec: &v1.ExecAction{
  95. Command: []string{"action1", "action2"},
  96. },
  97. HTTPGet: &v1.HTTPGetAction{
  98. Path: "path",
  99. Host: "host",
  100. Port: intstr.FromInt(8080),
  101. Scheme: "scheme",
  102. },
  103. TCPSocket: &v1.TCPSocketAction{
  104. Port: intstr.FromString("80"),
  105. },
  106. },
  107. }
  108. containerPorts := []v1.ContainerPort{
  109. {
  110. Name: "http",
  111. HostPort: 80,
  112. ContainerPort: 8080,
  113. Protocol: v1.ProtocolTCP,
  114. },
  115. {
  116. Name: "https",
  117. HostPort: 443,
  118. ContainerPort: 6443,
  119. Protocol: v1.ProtocolTCP,
  120. },
  121. }
  122. container := &v1.Container{
  123. Name: "test_container",
  124. Ports: containerPorts,
  125. TerminationMessagePath: "/somepath",
  126. Lifecycle: lifecycle,
  127. }
  128. pod := &v1.Pod{
  129. ObjectMeta: metav1.ObjectMeta{
  130. Name: "test_pod",
  131. Namespace: "test_pod_namespace",
  132. UID: "test_pod_uid",
  133. DeletionGracePeriodSeconds: &deletionGracePeriod,
  134. },
  135. Spec: v1.PodSpec{
  136. Containers: []v1.Container{*container},
  137. TerminationGracePeriodSeconds: &terminationGracePeriod,
  138. },
  139. }
  140. expected := &annotatedContainerInfo{
  141. ContainerPorts: containerPorts,
  142. PodDeletionGracePeriod: pod.DeletionGracePeriodSeconds,
  143. PodTerminationGracePeriod: pod.Spec.TerminationGracePeriodSeconds,
  144. Hash: kubecontainer.HashContainer(container),
  145. RestartCount: restartCount,
  146. TerminationMessagePath: container.TerminationMessagePath,
  147. PreStopHandler: container.Lifecycle.PreStop,
  148. }
  149. // Test whether we can get right information from label
  150. annotations := newContainerAnnotations(container, pod, restartCount, opts)
  151. containerInfo := getContainerInfoFromAnnotations(annotations)
  152. if !reflect.DeepEqual(containerInfo, expected) {
  153. t.Errorf("expected %v, got %v", expected, containerInfo)
  154. }
  155. if v, ok := annotations[opts.Annotations[0].Name]; !ok || v != opts.Annotations[0].Value {
  156. t.Errorf("expected annotation %s to exist got %v, %v", opts.Annotations[0].Name, ok, v)
  157. }
  158. // Test when DeletionGracePeriodSeconds, TerminationGracePeriodSeconds and Lifecycle are nil,
  159. // the information got from annotations should also be nil
  160. container.Lifecycle = nil
  161. pod.DeletionGracePeriodSeconds = nil
  162. pod.Spec.TerminationGracePeriodSeconds = nil
  163. expected.PodDeletionGracePeriod = nil
  164. expected.PodTerminationGracePeriod = nil
  165. expected.PreStopHandler = nil
  166. // Because container is changed, the Hash should be updated
  167. expected.Hash = kubecontainer.HashContainer(container)
  168. annotations = newContainerAnnotations(container, pod, restartCount, opts)
  169. containerInfo = getContainerInfoFromAnnotations(annotations)
  170. if !reflect.DeepEqual(containerInfo, expected) {
  171. t.Errorf("expected %v, got %v", expected, containerInfo)
  172. }
  173. if v, ok := annotations[opts.Annotations[0].Name]; !ok || v != opts.Annotations[0].Value {
  174. t.Errorf("expected annotation %s to exist got %v, %v", opts.Annotations[0].Name, ok, v)
  175. }
  176. }
  177. func TestPodLabels(t *testing.T) {
  178. pod := &v1.Pod{
  179. ObjectMeta: metav1.ObjectMeta{
  180. Name: "test_pod",
  181. Namespace: "test_pod_namespace",
  182. UID: "test_pod_uid",
  183. Labels: map[string]string{"foo": "bar"},
  184. },
  185. Spec: v1.PodSpec{
  186. Containers: []v1.Container{},
  187. },
  188. }
  189. expected := &labeledPodSandboxInfo{
  190. Labels: pod.Labels,
  191. PodName: pod.Name,
  192. PodNamespace: pod.Namespace,
  193. PodUID: pod.UID,
  194. }
  195. // Test whether we can get right information from label
  196. labels := newPodLabels(pod)
  197. podSandboxInfo := getPodSandboxInfoFromLabels(labels)
  198. if !reflect.DeepEqual(podSandboxInfo, expected) {
  199. t.Errorf("expected %v, got %v", expected, podSandboxInfo)
  200. }
  201. }
  202. func TestPodAnnotations(t *testing.T) {
  203. pod := &v1.Pod{
  204. ObjectMeta: metav1.ObjectMeta{
  205. Name: "test_pod",
  206. Namespace: "test_pod_namespace",
  207. UID: "test_pod_uid",
  208. Annotations: map[string]string{"foo": "bar"},
  209. },
  210. Spec: v1.PodSpec{
  211. Containers: []v1.Container{},
  212. },
  213. }
  214. expected := &annotatedPodSandboxInfo{
  215. Annotations: map[string]string{"foo": "bar"},
  216. }
  217. // Test whether we can get right information from annotations
  218. annotations := newPodAnnotations(pod)
  219. podSandboxInfo := getPodSandboxInfoFromAnnotations(annotations)
  220. if !reflect.DeepEqual(podSandboxInfo, expected) {
  221. t.Errorf("expected %v, got %v", expected, podSandboxInfo)
  222. }
  223. }