fixtures.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2019 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 job
  14. import (
  15. batchv1 "k8s.io/api/batch/v1"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/kubernetes/test/e2e/framework"
  19. )
  20. // NewTestJob returns a Job which does one of several testing behaviors. notTerminate starts a Job that will run
  21. // effectively forever. fail starts a Job that will fail immediately. succeed starts a Job that will succeed
  22. // immediately. randomlySucceedOrFail starts a Job that will succeed or fail randomly. failOnce fails the Job the
  23. // first time it is run and succeeds subsequently. name is the Name of the Job. RestartPolicy indicates the restart
  24. // policy of the containers in which the Pod is running. Parallelism is the Job's parallelism, and completions is the
  25. // Job's required number of completions.
  26. func NewTestJob(behavior, name string, rPol v1.RestartPolicy, parallelism, completions int32, activeDeadlineSeconds *int64, backoffLimit int32) *batchv1.Job {
  27. manualSelector := false
  28. job := &batchv1.Job{
  29. ObjectMeta: metav1.ObjectMeta{
  30. Name: name,
  31. },
  32. TypeMeta: metav1.TypeMeta{
  33. Kind: "Job",
  34. },
  35. Spec: batchv1.JobSpec{
  36. ActiveDeadlineSeconds: activeDeadlineSeconds,
  37. Parallelism: &parallelism,
  38. Completions: &completions,
  39. BackoffLimit: &backoffLimit,
  40. ManualSelector: &manualSelector,
  41. Template: v1.PodTemplateSpec{
  42. ObjectMeta: metav1.ObjectMeta{
  43. Labels: map[string]string{JobSelectorKey: name},
  44. },
  45. Spec: v1.PodSpec{
  46. RestartPolicy: rPol,
  47. Volumes: []v1.Volume{
  48. {
  49. Name: "data",
  50. VolumeSource: v1.VolumeSource{
  51. EmptyDir: &v1.EmptyDirVolumeSource{},
  52. },
  53. },
  54. },
  55. Containers: []v1.Container{
  56. {
  57. Name: "c",
  58. Image: framework.BusyBoxImage,
  59. Command: []string{},
  60. VolumeMounts: []v1.VolumeMount{
  61. {
  62. MountPath: "/data",
  63. Name: "data",
  64. },
  65. },
  66. SecurityContext: &v1.SecurityContext{},
  67. },
  68. },
  69. },
  70. },
  71. },
  72. }
  73. switch behavior {
  74. case "notTerminate":
  75. job.Spec.Template.Spec.Containers[0].Command = []string{"sleep", "1000000"}
  76. case "fail":
  77. job.Spec.Template.Spec.Containers[0].Command = []string{"/bin/sh", "-c", "exit 1"}
  78. case "succeed":
  79. job.Spec.Template.Spec.Containers[0].Command = []string{"/bin/sh", "-c", "exit 0"}
  80. case "randomlySucceedOrFail":
  81. // Bash's $RANDOM generates pseudorandom int in range 0 - 32767.
  82. // Dividing by 16384 gives roughly 50/50 chance of success.
  83. job.Spec.Template.Spec.Containers[0].Command = []string{"/bin/sh", "-c", "exit $(( $RANDOM / 16384 ))"}
  84. case "failOnce":
  85. // Fail the first the container of the pod is run, and
  86. // succeed the second time. Checks for file on emptydir.
  87. // If present, succeed. If not, create but fail.
  88. // Note that this cannot be used with RestartNever because
  89. // it always fails the first time for a pod.
  90. job.Spec.Template.Spec.Containers[0].Command = []string{"/bin/sh", "-c", "if [[ -r /data/foo ]] ; then exit 0 ; else touch /data/foo ; exit 1 ; fi"}
  91. }
  92. return job
  93. }
  94. // FinishTime returns finish time of the specified job.
  95. func FinishTime(finishedJob *batchv1.Job) metav1.Time {
  96. var finishTime metav1.Time
  97. for _, c := range finishedJob.Status.Conditions {
  98. if (c.Type == batchv1.JobComplete || c.Type == batchv1.JobFailed) && c.Status == v1.ConditionTrue {
  99. return c.LastTransitionTime
  100. }
  101. }
  102. return finishTime
  103. }