github.com/someshkoli/terratest@v0.41.1/modules/k8s/job.go (about)

     1  package k8s
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  	batchv1 "k8s.io/api/batch/v1"
    10  	corev1 "k8s.io/api/core/v1"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  
    13  	"github.com/gruntwork-io/terratest/modules/logger"
    14  	"github.com/gruntwork-io/terratest/modules/retry"
    15  	"github.com/gruntwork-io/terratest/modules/testing"
    16  )
    17  
    18  // ListJobs will look for Jobs in the given namespace that match the given filters and return them. This will fail the
    19  // test if there is an error.
    20  func ListJobs(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) []batchv1.Job {
    21  	jobs, err := ListJobsE(t, options, filters)
    22  	require.NoError(t, err)
    23  	return jobs
    24  }
    25  
    26  // ListJobsE will look for jobs in the given namespace that match the given filters and return them.
    27  func ListJobsE(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) ([]batchv1.Job, error) {
    28  	clientset, err := GetKubernetesClientFromOptionsE(t, options)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	resp, err := clientset.BatchV1().Jobs(options.Namespace).List(context.Background(), filters)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	return resp.Items, nil
    38  }
    39  
    40  // GetJob returns a Kubernetes job resource in the provided namespace with the given name. This will
    41  // fail the test if there is an error.
    42  func GetJob(t testing.TestingT, options *KubectlOptions, jobName string) *batchv1.Job {
    43  	job, err := GetJobE(t, options, jobName)
    44  	require.NoError(t, err)
    45  	return job
    46  }
    47  
    48  // GetJobE returns a Kubernetes job resource in the provided namespace with the given name.
    49  func GetJobE(t testing.TestingT, options *KubectlOptions, jobName string) (*batchv1.Job, error) {
    50  	clientset, err := GetKubernetesClientFromOptionsE(t, options)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return clientset.BatchV1().Jobs(options.Namespace).Get(context.Background(), jobName, metav1.GetOptions{})
    55  }
    56  
    57  // WaitUntilJobSucceed waits until requested job is suceeded, retrying the check for the specified amount of times, sleeping
    58  // for the provided duration between each try. This will fail the test if there is an error or if the check times out.
    59  func WaitUntilJobSucceed(t testing.TestingT, options *KubectlOptions, jobName string, retries int, sleepBetweenRetries time.Duration) {
    60  	require.NoError(t, WaitUntilJobSucceedE(t, options, jobName, retries, sleepBetweenRetries))
    61  }
    62  
    63  // WaitUntilJobSucceedE waits until requested job is succeeded, retrying the check for the specified amount of times, sleeping
    64  // for the provided duration between each try.
    65  func WaitUntilJobSucceedE(t testing.TestingT, options *KubectlOptions, jobName string, retries int, sleepBetweenRetries time.Duration) error {
    66  	statusMsg := fmt.Sprintf("Wait for job %s to be provisioned.", jobName)
    67  	message, err := retry.DoWithRetryE(
    68  		t,
    69  		statusMsg,
    70  		retries,
    71  		sleepBetweenRetries,
    72  		func() (string, error) {
    73  			job, err := GetJobE(t, options, jobName)
    74  			if err != nil {
    75  				return "", err
    76  			}
    77  			if !IsJobSucceeded(job) {
    78  				return "", NewJobNotSucceeded(job)
    79  			}
    80  			return "Job is now Succeeded", nil
    81  		},
    82  	)
    83  	if err != nil {
    84  		logger.Logf(t, "Timed out waiting for Job to be provisioned: %s", err)
    85  		return err
    86  	}
    87  	logger.Logf(t, message)
    88  	return nil
    89  }
    90  
    91  // IsJobSucceeded returns true when the job status condition "Complete" is true. This behavior is documented in the kubernetes API reference:
    92  // https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/job-v1/#JobStatus
    93  func IsJobSucceeded(job *batchv1.Job) bool {
    94  	for _, condition := range job.Status.Conditions {
    95  		if condition.Type == batchv1.JobComplete && condition.Status == corev1.ConditionTrue {
    96  			return true
    97  		}
    98  	}
    99  	return false
   100  }