ttlafterfinished_controller_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright 2018 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 ttlafterfinished
  14. import (
  15. "strings"
  16. "testing"
  17. "time"
  18. batch "k8s.io/api/batch/v1"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. utilpointer "k8s.io/utils/pointer"
  22. )
  23. func newJob(completionTime, failedTime metav1.Time, ttl *int32) *batch.Job {
  24. j := &batch.Job{
  25. TypeMeta: metav1.TypeMeta{Kind: "Job"},
  26. ObjectMeta: metav1.ObjectMeta{
  27. Name: "foobar",
  28. Namespace: metav1.NamespaceDefault,
  29. },
  30. Spec: batch.JobSpec{
  31. Selector: &metav1.LabelSelector{
  32. MatchLabels: map[string]string{"foo": "bar"},
  33. },
  34. Template: v1.PodTemplateSpec{
  35. ObjectMeta: metav1.ObjectMeta{
  36. Labels: map[string]string{
  37. "foo": "bar",
  38. },
  39. },
  40. Spec: v1.PodSpec{
  41. Containers: []v1.Container{
  42. {Image: "foo/bar"},
  43. },
  44. },
  45. },
  46. },
  47. }
  48. if !completionTime.IsZero() {
  49. c := batch.JobCondition{Type: batch.JobComplete, Status: v1.ConditionTrue, LastTransitionTime: completionTime}
  50. j.Status.Conditions = append(j.Status.Conditions, c)
  51. }
  52. if !failedTime.IsZero() {
  53. c := batch.JobCondition{Type: batch.JobFailed, Status: v1.ConditionTrue, LastTransitionTime: failedTime}
  54. j.Status.Conditions = append(j.Status.Conditions, c)
  55. }
  56. if ttl != nil {
  57. j.Spec.TTLSecondsAfterFinished = ttl
  58. }
  59. return j
  60. }
  61. func durationPointer(n int) *time.Duration {
  62. s := time.Duration(n) * time.Second
  63. return &s
  64. }
  65. func TestTimeLeft(t *testing.T) {
  66. now := metav1.Now()
  67. testCases := []struct {
  68. name string
  69. completionTime metav1.Time
  70. failedTime metav1.Time
  71. ttl *int32
  72. since *time.Time
  73. expectErr bool
  74. expectErrStr string
  75. expectedTimeLeft *time.Duration
  76. }{
  77. {
  78. name: "Error case: Job unfinished",
  79. ttl: utilpointer.Int32Ptr(100),
  80. since: &now.Time,
  81. expectErr: true,
  82. expectErrStr: "should not be cleaned up",
  83. },
  84. {
  85. name: "Error case: Job completed now, no TTL",
  86. completionTime: now,
  87. since: &now.Time,
  88. expectErr: true,
  89. expectErrStr: "should not be cleaned up",
  90. },
  91. {
  92. name: "Job completed now, 0s TTL",
  93. completionTime: now,
  94. ttl: utilpointer.Int32Ptr(0),
  95. since: &now.Time,
  96. expectedTimeLeft: durationPointer(0),
  97. },
  98. {
  99. name: "Job completed now, 10s TTL",
  100. completionTime: now,
  101. ttl: utilpointer.Int32Ptr(10),
  102. since: &now.Time,
  103. expectedTimeLeft: durationPointer(10),
  104. },
  105. {
  106. name: "Job completed 10s ago, 15s TTL",
  107. completionTime: metav1.NewTime(now.Add(-10 * time.Second)),
  108. ttl: utilpointer.Int32Ptr(15),
  109. since: &now.Time,
  110. expectedTimeLeft: durationPointer(5),
  111. },
  112. {
  113. name: "Error case: Job failed now, no TTL",
  114. failedTime: now,
  115. since: &now.Time,
  116. expectErr: true,
  117. expectErrStr: "should not be cleaned up",
  118. },
  119. {
  120. name: "Job failed now, 0s TTL",
  121. failedTime: now,
  122. ttl: utilpointer.Int32Ptr(0),
  123. since: &now.Time,
  124. expectedTimeLeft: durationPointer(0),
  125. },
  126. {
  127. name: "Job failed now, 10s TTL",
  128. failedTime: now,
  129. ttl: utilpointer.Int32Ptr(10),
  130. since: &now.Time,
  131. expectedTimeLeft: durationPointer(10),
  132. },
  133. {
  134. name: "Job failed 10s ago, 15s TTL",
  135. failedTime: metav1.NewTime(now.Add(-10 * time.Second)),
  136. ttl: utilpointer.Int32Ptr(15),
  137. since: &now.Time,
  138. expectedTimeLeft: durationPointer(5),
  139. },
  140. }
  141. for _, tc := range testCases {
  142. job := newJob(tc.completionTime, tc.failedTime, tc.ttl)
  143. gotTimeLeft, gotErr := timeLeft(job, tc.since)
  144. if tc.expectErr != (gotErr != nil) {
  145. t.Errorf("%s: expected error is %t, got %t, error: %v", tc.name, tc.expectErr, gotErr != nil, gotErr)
  146. }
  147. if tc.expectErr && len(tc.expectErrStr) == 0 {
  148. t.Errorf("%s: invalid test setup; error message must not be empty for error cases", tc.name)
  149. }
  150. if tc.expectErr && !strings.Contains(gotErr.Error(), tc.expectErrStr) {
  151. t.Errorf("%s: expected error message contains %q, got %v", tc.name, tc.expectErrStr, gotErr)
  152. }
  153. if !tc.expectErr {
  154. if *gotTimeLeft != *tc.expectedTimeLeft {
  155. t.Errorf("%s: expected time left %v, got %v", tc.name, tc.expectedTimeLeft, gotTimeLeft)
  156. }
  157. }
  158. }
  159. }