github.com/mponton/terratest@v0.44.0/modules/k8s/persistent_volume_claim.go (about) 1 package k8s 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "github.com/stretchr/testify/require" 9 10 corev1 "k8s.io/api/core/v1" 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 13 "github.com/mponton/terratest/modules/logger" 14 "github.com/mponton/terratest/modules/retry" 15 "github.com/mponton/terratest/modules/testing" 16 ) 17 18 // ListPersistentVolumeClaims will look for PersistentVolumeClaims 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 ListPersistentVolumeClaims(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) []corev1.PersistentVolumeClaim { 21 pvcs, err := ListPersistentVolumeClaimsE(t, options, filters) 22 require.NoError(t, err) 23 return pvcs 24 } 25 26 // ListPersistentVolumeClaimsE will look for PersistentVolumeClaims in the given namespace that match the given filters and return them. 27 func ListPersistentVolumeClaimsE(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) ([]corev1.PersistentVolumeClaim, error) { 28 clientset, err := GetKubernetesClientFromOptionsE(t, options) 29 if err != nil { 30 return nil, err 31 } 32 33 resp, err := clientset.CoreV1().PersistentVolumeClaims(options.Namespace).List(context.Background(), filters) 34 if err != nil { 35 return nil, err 36 } 37 return resp.Items, nil 38 } 39 40 // GetPersistentVolumeClaim returns a Kubernetes PersistentVolumeClaim resource in the provided namespace with the given name. This will 41 // fail the test if there is an error. 42 func GetPersistentVolumeClaim(t testing.TestingT, options *KubectlOptions, pvcName string) *corev1.PersistentVolumeClaim { 43 pvc, err := GetPersistentVolumeClaimE(t, options, pvcName) 44 require.NoError(t, err) 45 return pvc 46 } 47 48 // GetPersistentVolumeClaimE returns a Kubernetes PersistentVolumeClaim resource in the provided namespace with the given name. 49 func GetPersistentVolumeClaimE(t testing.TestingT, options *KubectlOptions, pvcName string) (*corev1.PersistentVolumeClaim, error) { 50 clientset, err := GetKubernetesClientFromOptionsE(t, options) 51 if err != nil { 52 return nil, err 53 } 54 return clientset.CoreV1().PersistentVolumeClaims(options.Namespace).Get(context.Background(), pvcName, metav1.GetOptions{}) 55 } 56 57 // WaitUntilPersistentVolumeClaimInStatus waits until the given PersistentVolumeClaim is the given status phase, 58 // retrying the check for the specified amount of times, sleeping 59 // for the provided duration between each try. 60 // This will fail the test if there is an error. 61 func WaitUntilPersistentVolumeClaimInStatus(t testing.TestingT, options *KubectlOptions, pvcName string, pvcStatusPhase *corev1.PersistentVolumeClaimPhase, retries int, sleepBetweenRetries time.Duration) { 62 require.NoError(t, WaitUntilPersistentVolumeClaimInStatusE(t, options, pvcName, pvcStatusPhase, retries, sleepBetweenRetries)) 63 } 64 65 // WaitUntilPersistentVolumeClaimInStatusE waits until the given PersistentVolumeClaim is the given status phase, 66 // retrying the check for the specified amount of times, sleeping 67 // for the provided duration between each try. 68 // This will fail the test if there is an error. 69 func WaitUntilPersistentVolumeClaimInStatusE(t testing.TestingT, options *KubectlOptions, pvcName string, pvcStatusPhase *corev1.PersistentVolumeClaimPhase, retries int, sleepBetweenRetries time.Duration) error { 70 statusMsg := fmt.Sprintf("Wait for PersistentVolumeClaim %s to be '%s'.", pvcName, *pvcStatusPhase) 71 message, err := retry.DoWithRetryE( 72 t, 73 statusMsg, 74 retries, 75 sleepBetweenRetries, 76 func() (string, error) { 77 pvc, err := GetPersistentVolumeClaimE(t, options, pvcName) 78 if err != nil { 79 return "", err 80 } 81 if !IsPersistentVolumeClaimInStatus(pvc, pvcStatusPhase) { 82 return "", NewPersistentVolumeClaimNotInStatusError(pvc, pvcStatusPhase) 83 } 84 return fmt.Sprintf("PersistentVolumeClaim is now '%s'", *pvcStatusPhase), nil 85 }, 86 ) 87 if err != nil { 88 logger.Default.Logf(t, "Timeout waiting for PersistentVolumeClaim to be '%s': %s", *pvcStatusPhase, err) 89 return err 90 } 91 logger.Default.Logf(t, message) 92 return nil 93 } 94 95 // IsPersistentVolumeClaimInStatus returns true if the given PersistentVolumeClaim is in the given status phase 96 func IsPersistentVolumeClaimInStatus(pvc *corev1.PersistentVolumeClaim, pvcStatusPhase *corev1.PersistentVolumeClaimPhase) bool { 97 return pvc != nil && pvc.Status.Phase == *pvcStatusPhase 98 }