github.com/mponton/terratest@v0.44.0/modules/k8s/job_test.go (about) 1 //go:build kubeall || kubernetes 2 // +build kubeall kubernetes 3 4 // NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests. This is done because minikube 5 // is heavy and can interfere with docker related tests in terratest. Specifically, many of the tests start to fail with 6 // `connection refused` errors from `minikube`. To avoid overloading the system, we run the kubernetes tests and helm 7 // tests separately from the others. This may not be necessary if you have a sufficiently powerful machine. We 8 // recommend at least 4 cores and 16GB of RAM if you want to run all the tests together. 9 10 package k8s 11 12 import ( 13 "fmt" 14 "strings" 15 "testing" 16 "time" 17 18 "github.com/stretchr/testify/require" 19 batchv1 "k8s.io/api/batch/v1" 20 corev1 "k8s.io/api/core/v1" 21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 23 "github.com/mponton/terratest/modules/random" 24 ) 25 26 func TestListJobsReturnsJobsInNamespace(t *testing.T) { 27 t.Parallel() 28 29 uniqueID := strings.ToLower(random.UniqueId()) 30 options := NewKubectlOptions("", "", uniqueID) 31 configData := fmt.Sprintf(EXAMPLE_JOB_YAML_TEMPLATE, uniqueID, uniqueID) 32 defer KubectlDeleteFromString(t, options, configData) 33 KubectlApplyFromString(t, options, configData) 34 35 jobs := ListJobs(t, options, metav1.ListOptions{}) 36 require.Equal(t, len(jobs), 1) 37 job := jobs[0] 38 require.Equal(t, job.Name, "pi-job") 39 require.Equal(t, job.Namespace, uniqueID) 40 } 41 42 func TestGetJobEReturnsErrorForNonExistantJob(t *testing.T) { 43 t.Parallel() 44 45 options := NewKubectlOptions("", "", "default") 46 _, err := GetJobE(t, options, "pi-job") 47 require.Error(t, err) 48 } 49 50 func TestGetJobEReturnsCorrectJobInCorrectNamespace(t *testing.T) { 51 t.Parallel() 52 53 uniqueID := strings.ToLower(random.UniqueId()) 54 options := NewKubectlOptions("", "", uniqueID) 55 configData := fmt.Sprintf(EXAMPLE_JOB_YAML_TEMPLATE, uniqueID, uniqueID) 56 defer KubectlDeleteFromString(t, options, configData) 57 KubectlApplyFromString(t, options, configData) 58 59 job := GetJob(t, options, "pi-job") 60 require.Equal(t, job.Name, "pi-job") 61 require.Equal(t, job.Namespace, uniqueID) 62 } 63 64 func TestWaitUntilJobSucceedReturnsSuccessfully(t *testing.T) { 65 t.Parallel() 66 67 uniqueID := strings.ToLower(random.UniqueId()) 68 options := NewKubectlOptions("", "", uniqueID) 69 configData := fmt.Sprintf(EXAMPLE_JOB_YAML_TEMPLATE, uniqueID, uniqueID) 70 defer KubectlDeleteFromString(t, options, configData) 71 KubectlApplyFromString(t, options, configData) 72 73 WaitUntilJobSucceed(t, options, "pi-job", 60, 1*time.Second) 74 } 75 76 func TestIsJobSucceeded(t *testing.T) { 77 t.Parallel() 78 79 cases := []struct { 80 title string 81 job *batchv1.Job 82 expectedResult bool 83 }{ 84 { 85 title: "TestIsJobSucceeded", 86 job: &batchv1.Job{ 87 Status: batchv1.JobStatus{ 88 Conditions: []batchv1.JobCondition{ 89 batchv1.JobCondition{ 90 Type: batchv1.JobComplete, 91 Status: corev1.ConditionTrue, 92 }, 93 }, 94 }, 95 }, 96 expectedResult: true, 97 }, 98 { 99 title: "TestIsJobFailed", 100 job: &batchv1.Job{ 101 Status: batchv1.JobStatus{ 102 Conditions: []batchv1.JobCondition{ 103 batchv1.JobCondition{ 104 Type: batchv1.JobFailed, 105 Status: corev1.ConditionTrue, 106 }, 107 }, 108 }, 109 }, 110 expectedResult: false, 111 }, 112 { 113 title: "TestIsJobStarting", 114 job: &batchv1.Job{ 115 Status: batchv1.JobStatus{ 116 Conditions: []batchv1.JobCondition{}, 117 }, 118 }, 119 expectedResult: false, 120 }, 121 } 122 for _, tc := range cases { 123 tc := tc 124 t.Run(tc.title, func(t *testing.T) { 125 t.Parallel() 126 actualResult := IsJobSucceeded(tc.job) 127 require.Equal(t, tc.expectedResult, actualResult) 128 }) 129 } 130 } 131 132 const EXAMPLE_JOB_YAML_TEMPLATE = `--- 133 apiVersion: v1 134 kind: Namespace 135 metadata: 136 name: %s 137 --- 138 apiVersion: batch/v1 139 kind: Job 140 metadata: 141 name: pi-job 142 namespace: %s 143 spec: 144 template: 145 spec: 146 containers: 147 - name: pi 148 image: "perl:5.34.1" 149 command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] 150 restartPolicy: Never 151 backoffLimit: 4 152 `