labels_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2015 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 dockertools
  14. import (
  15. "reflect"
  16. "strconv"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/testapi"
  20. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  21. "k8s.io/kubernetes/pkg/kubelet/util/format"
  22. "k8s.io/kubernetes/pkg/runtime"
  23. "k8s.io/kubernetes/pkg/util/intstr"
  24. )
  25. func TestLabels(t *testing.T) {
  26. restartCount := 5
  27. deletionGracePeriod := int64(10)
  28. terminationGracePeriod := int64(10)
  29. lifecycle := &api.Lifecycle{
  30. // Left PostStart as nil
  31. PreStop: &api.Handler{
  32. Exec: &api.ExecAction{
  33. Command: []string{"action1", "action2"},
  34. },
  35. HTTPGet: &api.HTTPGetAction{
  36. Path: "path",
  37. Host: "host",
  38. Port: intstr.FromInt(8080),
  39. Scheme: "scheme",
  40. },
  41. TCPSocket: &api.TCPSocketAction{
  42. Port: intstr.FromString("80"),
  43. },
  44. },
  45. }
  46. containerPorts := []api.ContainerPort{
  47. {
  48. Name: "http",
  49. HostPort: 80,
  50. ContainerPort: 8080,
  51. Protocol: api.ProtocolTCP,
  52. },
  53. {
  54. Name: "https",
  55. HostPort: 443,
  56. ContainerPort: 6443,
  57. Protocol: api.ProtocolTCP,
  58. },
  59. }
  60. container := &api.Container{
  61. Name: "test_container",
  62. Ports: containerPorts,
  63. TerminationMessagePath: "/somepath",
  64. Lifecycle: lifecycle,
  65. }
  66. pod := &api.Pod{
  67. ObjectMeta: api.ObjectMeta{
  68. Name: "test_pod",
  69. Namespace: "test_pod_namespace",
  70. UID: "test_pod_uid",
  71. DeletionGracePeriodSeconds: &deletionGracePeriod,
  72. },
  73. Spec: api.PodSpec{
  74. Containers: []api.Container{*container},
  75. TerminationGracePeriodSeconds: &terminationGracePeriod,
  76. },
  77. }
  78. expected := &labelledContainerInfo{
  79. PodName: pod.Name,
  80. PodNamespace: pod.Namespace,
  81. PodUID: pod.UID,
  82. PodDeletionGracePeriod: pod.DeletionGracePeriodSeconds,
  83. PodTerminationGracePeriod: pod.Spec.TerminationGracePeriodSeconds,
  84. Name: container.Name,
  85. Hash: strconv.FormatUint(kubecontainer.HashContainer(container), 16),
  86. RestartCount: restartCount,
  87. TerminationMessagePath: container.TerminationMessagePath,
  88. PreStopHandler: container.Lifecycle.PreStop,
  89. Ports: containerPorts,
  90. }
  91. // Test whether we can get right information from label
  92. labels := newLabels(container, pod, restartCount, false)
  93. containerInfo := getContainerInfoFromLabel(labels)
  94. if !reflect.DeepEqual(containerInfo, expected) {
  95. t.Errorf("expected %v, got %v", expected, containerInfo)
  96. }
  97. // Test when DeletionGracePeriodSeconds, TerminationGracePeriodSeconds and Lifecycle are nil,
  98. // the information got from label should also be nil
  99. container.Lifecycle = nil
  100. pod.DeletionGracePeriodSeconds = nil
  101. pod.Spec.TerminationGracePeriodSeconds = nil
  102. expected.PodDeletionGracePeriod = nil
  103. expected.PodTerminationGracePeriod = nil
  104. expected.PreStopHandler = nil
  105. // Because container is changed, the Hash should be updated
  106. expected.Hash = strconv.FormatUint(kubecontainer.HashContainer(container), 16)
  107. labels = newLabels(container, pod, restartCount, false)
  108. containerInfo = getContainerInfoFromLabel(labels)
  109. if !reflect.DeepEqual(containerInfo, expected) {
  110. t.Errorf("expected %v, got %v", expected, containerInfo)
  111. }
  112. // Test when DeletionGracePeriodSeconds, TerminationGracePeriodSeconds and Lifecycle are nil,
  113. // but the old label kubernetesPodLabel is set, the information got from label should also be set
  114. pod.DeletionGracePeriodSeconds = &deletionGracePeriod
  115. pod.Spec.TerminationGracePeriodSeconds = &terminationGracePeriod
  116. container.Lifecycle = lifecycle
  117. data, err := runtime.Encode(testapi.Default.Codec(), pod)
  118. if err != nil {
  119. t.Fatalf("Failed to encode pod %q into string: %v", format.Pod(pod), err)
  120. }
  121. labels[kubernetesPodLabel] = string(data)
  122. expected.PodDeletionGracePeriod = pod.DeletionGracePeriodSeconds
  123. expected.PodTerminationGracePeriod = pod.Spec.TerminationGracePeriodSeconds
  124. expected.PreStopHandler = container.Lifecycle.PreStop
  125. // Do not update expected.Hash here, because we directly use the labels in last test, so we never
  126. // changed the kubernetesContainerHashLabel in this test, the expected.Hash shouldn't be changed.
  127. containerInfo = getContainerInfoFromLabel(labels)
  128. if !reflect.DeepEqual(containerInfo, expected) {
  129. t.Errorf("expected %v, got %v", expected, containerInfo)
  130. }
  131. }