labels_test.go 6.2 KB

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