github.com/mponton/terratest@v0.44.0/modules/k8s/persistent_volume.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 // ListPersistentVolumes will look for PersistentVolumes 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 ListPersistentVolumes(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) []corev1.PersistentVolume { 21 pvs, err := ListPersistentVolumesE(t, options, filters) 22 require.NoError(t, err) 23 return pvs 24 } 25 26 // ListPersistentVolumesE will look for PersistentVolumes that match the given filters and return them. 27 func ListPersistentVolumesE(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) ([]corev1.PersistentVolume, error) { 28 clientset, err := GetKubernetesClientFromOptionsE(t, options) 29 if err != nil { 30 return nil, err 31 } 32 33 resp, err := clientset.CoreV1().PersistentVolumes().List(context.Background(), filters) 34 if err != nil { 35 return nil, err 36 } 37 return resp.Items, nil 38 } 39 40 // GetPersistentVolume returns a Kubernetes PersistentVolume resource with the given name. This will fail the test if there is an error. 41 func GetPersistentVolume(t testing.TestingT, options *KubectlOptions, name string) *corev1.PersistentVolume { 42 pv, err := GetPersistentVolumeE(t, options, name) 43 require.NoError(t, err) 44 return pv 45 } 46 47 // GetPersistentVolumeE returns a Kubernetes PersistentVolume resource with the given name. 48 func GetPersistentVolumeE(t testing.TestingT, options *KubectlOptions, name string) (*corev1.PersistentVolume, error) { 49 clientset, err := GetKubernetesClientFromOptionsE(t, options) 50 if err != nil { 51 return nil, err 52 } 53 return clientset.CoreV1().PersistentVolumes().Get(context.Background(), name, metav1.GetOptions{}) 54 } 55 56 // WaitUntilPersistentVolumeInStatus waits until the given Persistent Volume is the given status phase, 57 // retrying the check for the specified amount of times, sleeping 58 // for the provided duration between each try. 59 // This will fail the test if there is an error. 60 func WaitUntilPersistentVolumeInStatus(t testing.TestingT, options *KubectlOptions, pvName string, pvStatusPhase *corev1.PersistentVolumePhase, retries int, sleepBetweenRetries time.Duration) { 61 require.NoError(t, WaitUntilPersistentVolumeInStatusE(t, options, pvName, pvStatusPhase, retries, sleepBetweenRetries)) 62 } 63 64 // WaitUntilPersistentVolumeInStatusE waits until the given PersistentVolume is in the given status phase, 65 // retrying the check for the specified amount of times, sleeping 66 // for the provided duration between each try. 67 func WaitUntilPersistentVolumeInStatusE( 68 t testing.TestingT, 69 options *KubectlOptions, 70 pvName string, 71 pvStatusPhase *corev1.PersistentVolumePhase, 72 retries int, 73 sleepBetweenRetries time.Duration, 74 ) error { 75 statusMsg := fmt.Sprintf("Wait for Persistent Volume %s to be '%s'", pvName, *pvStatusPhase) 76 message, err := retry.DoWithRetryE( 77 t, 78 statusMsg, 79 retries, 80 sleepBetweenRetries, 81 func() (string, error) { 82 pv, err := GetPersistentVolumeE(t, options, pvName) 83 if err != nil { 84 return "", err 85 } 86 if !IsPersistentVolumeInStatus(pv, pvStatusPhase) { 87 return "", NewPersistentVolumeNotInStatusError(pv, pvStatusPhase) 88 } 89 return fmt.Sprintf("Persistent Volume is now '%s'", *pvStatusPhase), nil 90 }, 91 ) 92 if err != nil { 93 logger.Logf(t, "Timeout waiting for PersistentVolume to be '%s': %s", *pvStatusPhase, err) 94 return err 95 } 96 logger.Logf(t, message) 97 return nil 98 } 99 100 // IsPersistentVolumeInStatus returns true if the given PersistentVolume is in the given status phase 101 func IsPersistentVolumeInStatus(pv *corev1.PersistentVolume, pvStatusPhase *corev1.PersistentVolumePhase) bool { 102 return pv != nil && pv.Status.Phase == *pvStatusPhase 103 }