github.com/mponton/terratest@v0.44.0/modules/k8s/persistent_volume_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 20 corev1 "k8s.io/api/core/v1" 21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 _ "k8s.io/client-go/plugin/pkg/client/auth" 23 24 "github.com/mponton/terratest/modules/random" 25 ) 26 27 func TestListPersistentVolumesReturnsAllPersistentVolumes(t *testing.T) { 28 t.Parallel() 29 30 numPvFound := 0 31 pvNames := map[string]struct{}{ 32 strings.ToLower(random.UniqueId()): {}, 33 strings.ToLower(random.UniqueId()): {}, 34 strings.ToLower(random.UniqueId()): {}, 35 } 36 37 options := NewKubectlOptions("", "", "") 38 for pvName := range pvNames { 39 pv := fmt.Sprintf(PvFixtureYamlTemplate, pvName, pvName) 40 defer KubectlDeleteFromString(t, options, pv) 41 KubectlApplyFromString(t, options, pv) 42 } 43 44 pvs := ListPersistentVolumes(t, options, metav1.ListOptions{}) 45 for _, pv := range pvs { 46 if _, ok := pvNames[pv.Name]; ok { 47 numPvFound++ 48 } 49 } 50 51 require.Equal(t, numPvFound, len(pvNames)) 52 } 53 54 func TestListPersistentVolumesReturnsZeroPersistentVolumesIfNoneCreated(t *testing.T) { 55 t.Parallel() 56 57 options := NewKubectlOptions("", "", "") 58 pvs := ListPersistentVolumes(t, options, metav1.ListOptions{}) 59 require.Equal(t, 0, len(pvs)) 60 } 61 62 func TestGetPersistentVolumeEReturnsErrorForNonExistentPersistentVolumes(t *testing.T) { 63 t.Parallel() 64 65 options := NewKubectlOptions("", "", "") 66 _, err := GetPersistentVolumeE(t, options, "non-existent") 67 require.Error(t, err) 68 } 69 70 func TestGetPersistentVolumeReturnsCorrectPersistentVolume(t *testing.T) { 71 t.Parallel() 72 73 pvName := strings.ToLower(random.UniqueId()) 74 options := NewKubectlOptions("", "", "") 75 configData := fmt.Sprintf(PvFixtureYamlTemplate, pvName, pvName) 76 defer KubectlDeleteFromString(t, options, configData) 77 KubectlApplyFromString(t, options, configData) 78 79 pv := GetPersistentVolume(t, options, pvName) 80 require.Equal(t, pv.Name, pvName) 81 } 82 83 func TestWaitUntilPersistentVolumeInTheGivenStatusPhase(t *testing.T) { 84 t.Parallel() 85 86 pvName := strings.ToLower(random.UniqueId()) 87 pvAvailableStatusPhase := corev1.VolumeAvailable 88 89 options := NewKubectlOptions("", "", pvName) 90 configData := fmt.Sprintf(PvFixtureYamlTemplate, pvName, pvName) 91 KubectlApplyFromString(t, options, configData) 92 defer KubectlDeleteFromString(t, options, configData) 93 94 WaitUntilPersistentVolumeInStatus(t, options, pvName, &pvAvailableStatusPhase, 60, 1*time.Second) 95 } 96 97 const PvFixtureYamlTemplate = `--- 98 apiVersion: v1 99 kind: PersistentVolume 100 metadata: 101 name: %s 102 spec: 103 capacity: 104 storage: 10Mi 105 accessModes: 106 - ReadWriteOnce 107 hostPath: 108 path: "/tmp/%s" 109 `