pod_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 format
  14. import (
  15. "testing"
  16. "time"
  17. "github.com/stretchr/testify/assert"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. )
  22. func fakeCreatePod(name, namespace string, uid types.UID) *v1.Pod {
  23. return &v1.Pod{
  24. ObjectMeta: metav1.ObjectMeta{
  25. Name: name,
  26. Namespace: namespace,
  27. UID: uid,
  28. },
  29. }
  30. }
  31. func fakeCreatePodWithDeletionTimestamp(name, namespace string, uid types.UID, deletionTimestamp *metav1.Time) *v1.Pod {
  32. return &v1.Pod{
  33. ObjectMeta: metav1.ObjectMeta{
  34. Name: name,
  35. Namespace: namespace,
  36. UID: uid,
  37. DeletionTimestamp: deletionTimestamp,
  38. },
  39. }
  40. }
  41. func TestPod(t *testing.T) {
  42. testCases := []struct {
  43. caseName string
  44. pod *v1.Pod
  45. expectedValue string
  46. }{
  47. {"field_empty_case", fakeCreatePod("", "", ""), "_()"},
  48. {"field_normal_case", fakeCreatePod("test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75"), "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
  49. }
  50. for _, testCase := range testCases {
  51. realPod := Pod(testCase.pod)
  52. assert.Equalf(t, testCase.expectedValue, realPod, "Failed to test: %s", testCase.caseName)
  53. }
  54. }
  55. func TestPodAndPodDesc(t *testing.T) {
  56. testCases := []struct {
  57. caseName string
  58. podName string
  59. podNamesapce string
  60. podUID types.UID
  61. expectedValue string
  62. }{
  63. {"field_empty_case", "", "", "", "_()"},
  64. {"field_normal_case", "test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75", "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
  65. }
  66. for _, testCase := range testCases {
  67. realPodDesc := PodDesc(testCase.podName, testCase.podNamesapce, testCase.podUID)
  68. assert.Equalf(t, testCase.expectedValue, realPodDesc, "Failed to test: %s", testCase.caseName)
  69. }
  70. }
  71. func TestPodWithDeletionTimestamp(t *testing.T) {
  72. normalDeletionTime := metav1.Date(2017, time.September, 26, 14, 37, 50, 00, time.UTC)
  73. testCases := []struct {
  74. caseName string
  75. isdeletionTimestampNil bool
  76. deletionTimestamp metav1.Time
  77. expectedValue string
  78. }{
  79. {"timestamp_is_nil_case", true, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
  80. {"timestamp_is_normal_case", false, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z"},
  81. }
  82. for _, testCase := range testCases {
  83. fakePod := fakeCreatePodWithDeletionTimestamp("test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75", &testCase.deletionTimestamp)
  84. if testCase.isdeletionTimestampNil {
  85. fakePod.SetDeletionTimestamp(nil)
  86. }
  87. realPodWithDeletionTimestamp := PodWithDeletionTimestamp(fakePod)
  88. assert.Equalf(t, testCase.expectedValue, realPodWithDeletionTimestamp, "Failed to test: %s", testCase.caseName)
  89. }
  90. }
  91. func TestPods(t *testing.T) {
  92. pod1 := fakeCreatePod("pod1", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75")
  93. pod2 := fakeCreatePod("pod2", metav1.NamespaceDefault, "e84a99bf-d1f9-43c2-9fa5-044ac85f794b")
  94. testCases := []struct {
  95. caseName string
  96. pods []*v1.Pod
  97. expectedValue string
  98. }{
  99. {"input_nil_case", nil, ""},
  100. {"input_empty_case", []*v1.Pod{}, ""},
  101. {"input_length_one_case", []*v1.Pod{pod1}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
  102. {"input_length_more_than_one_case", []*v1.Pod{pod1, pod2}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75), pod2_default(e84a99bf-d1f9-43c2-9fa5-044ac85f794b)"},
  103. }
  104. for _, testCase := range testCases {
  105. realPods := Pods(testCase.pods)
  106. assert.Equalf(t, testCase.expectedValue, realPods, "Failed to test: %s", testCase.caseName)
  107. }
  108. }
  109. func TestPodsWithDeletionTimestamps(t *testing.T) {
  110. normalDeletionTime := metav1.Date(2017, time.September, 26, 14, 37, 50, 00, time.UTC)
  111. pod1 := fakeCreatePodWithDeletionTimestamp("pod1", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75", &normalDeletionTime)
  112. pod2 := fakeCreatePodWithDeletionTimestamp("pod2", metav1.NamespaceDefault, "e84a99bf-d1f9-43c2-9fa5-044ac85f794b", &normalDeletionTime)
  113. testCases := []struct {
  114. caseName string
  115. pods []*v1.Pod
  116. expectedValue string
  117. }{
  118. {"input_nil_case", nil, ""},
  119. {"input_empty_case", []*v1.Pod{}, ""},
  120. {"input_length_one_case", []*v1.Pod{pod1}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z"},
  121. {"input_length_more_than_one_case", []*v1.Pod{pod1, pod2}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z, pod2_default(e84a99bf-d1f9-43c2-9fa5-044ac85f794b):DeletionTimestamp=2017-09-26T14:37:50Z"},
  122. }
  123. for _, testCase := range testCases {
  124. realPods := PodsWithDeletionTimestamps(testCase.pods)
  125. assert.Equalf(t, testCase.expectedValue, realPods, "Failed to test: %s", testCase.caseName)
  126. }
  127. }