fixtures.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/apimachinery/pkg/util/rand"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. )
  21. // NewTestJob returns a Job which does one of several testing behaviors. notTerminate starts a Job that will run
  22. // effectively forever. fail starts a Job that will fail immediately. succeed starts a Job that will succeed
  23. // immediately. randomlySucceedOrFail starts a Job that will succeed or fail randomly. failOnce fails the Job the
  24. // first time it is run and succeeds subsequently. name is the Name of the Job. RestartPolicy indicates the restart
  25. // policy of the containers in which the Pod is running. Parallelism is the Job's parallelism, and completions is the
  26. // Job's required number of completions.
  27. func NewTestJob(behavior, name string, rPol v1.RestartPolicy, parallelism, completions int32, activeDeadlineSeconds *int64, backoffLimit int32) *batchv1.Job {
  28. anyNode := ""
  29. return NewTestJobOnNode(behavior, name, rPol, parallelism, completions, activeDeadlineSeconds, backoffLimit, anyNode)
  30. }
  31. // NewTestJobOnNode is similar to NewTestJob but supports specifying a Node on which the Job's Pods will run.
  32. // Empty nodeName means no node selection constraints.
  33. func NewTestJobOnNode(behavior, name string, rPol v1.RestartPolicy, parallelism, completions int32, activeDeadlineSeconds *int64, backoffLimit int32, nodeName string) *batchv1.Job {
  34. manualSelector := false
  35. job := &batchv1.Job{
  36. ObjectMeta: metav1.ObjectMeta{
  37. Name: name,
  38. },
  39. TypeMeta: metav1.TypeMeta{
  40. Kind: "Job",
  41. },
  42. Spec: batchv1.JobSpec{
  43. ActiveDeadlineSeconds: activeDeadlineSeconds,
  44. Parallelism: &parallelism,
  45. Completions: &completions,
  46. BackoffLimit: &backoffLimit,
  47. ManualSelector: &manualSelector,
  48. Template: v1.PodTemplateSpec{
  49. ObjectMeta: metav1.ObjectMeta{
  50. Labels: map[string]string{JobSelectorKey: name},
  51. },
  52. Spec: v1.PodSpec{
  53. RestartPolicy: rPol,
  54. Volumes: []v1.Volume{
  55. {
  56. Name: "data",
  57. VolumeSource: v1.VolumeSource{
  58. EmptyDir: &v1.EmptyDirVolumeSource{},
  59. },
  60. },
  61. },
  62. Containers: []v1.Container{
  63. {
  64. Name: "c",
  65. Image: framework.BusyBoxImage,
  66. Command: []string{},
  67. VolumeMounts: []v1.VolumeMount{
  68. {
  69. MountPath: "/data",
  70. Name: "data",
  71. },
  72. },
  73. SecurityContext: &v1.SecurityContext{},
  74. },
  75. },
  76. NodeName: nodeName,
  77. },
  78. },
  79. },
  80. }
  81. switch behavior {
  82. case "notTerminate":
  83. job.Spec.Template.Spec.Containers[0].Command = []string{"sleep", "1000000"}
  84. case "fail":
  85. job.Spec.Template.Spec.Containers[0].Command = []string{"/bin/sh", "-c", "exit 1"}
  86. case "succeed":
  87. job.Spec.Template.Spec.Containers[0].Command = []string{"/bin/sh", "-c", "exit 0"}
  88. case "randomlySucceedOrFail":
  89. // Bash's $RANDOM generates pseudorandom int in range 0 - 32767.
  90. // Dividing by 16384 gives roughly 50/50 chance of success.
  91. job.Spec.Template.Spec.Containers[0].Command = []string{"/bin/sh", "-c", "exit $(( $RANDOM / 16384 ))"}
  92. case "failOnce":
  93. // Fail the first the container of the pod is run, and
  94. // succeed the second time. Checks for file on a data volume.
  95. // If present, succeed. If not, create but fail.
  96. // If RestartPolicy is Never, the nodeName should be set to
  97. // ensure all job pods run on a single node and the volume
  98. // will be mounted from a hostPath instead.
  99. if len(nodeName) > 0 {
  100. randomDir := "/tmp/job-e2e/" + rand.String(10)
  101. hostPathType := v1.HostPathDirectoryOrCreate
  102. job.Spec.Template.Spec.Volumes[0].VolumeSource = v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: randomDir, Type: &hostPathType}}
  103. // Tests involving r/w operations on hostPath volume needs to run in
  104. // privileged mode for SELinux enabled distro, while Windows platform
  105. // neither supports nor needs privileged mode.
  106. privileged := !framework.NodeOSDistroIs("windows")
  107. job.Spec.Template.Spec.Containers[0].SecurityContext.Privileged = &privileged
  108. }
  109. 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"}
  110. }
  111. return job
  112. }