github.com/mponton/terratest@v0.44.0/modules/k8s/persistent_volume_claim_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  	"strings"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/stretchr/testify/require"
    18  
    19  	corev1 "k8s.io/api/core/v1"
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	_ "k8s.io/client-go/plugin/pkg/client/auth"
    22  
    23  	"github.com/mponton/terratest/modules/random"
    24  )
    25  
    26  func TestListPersistentVolumeClaimsReturnsPersistentVolumeClaimsInNamespace(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	pvcName := "test-dummy-pvc"
    30  	namespace := strings.ToLower(random.UniqueId())
    31  	options := NewKubectlOptions("", "", namespace)
    32  	configData := renderFixtureYamlTemplate(namespace, pvcName)
    33  	defer KubectlDeleteFromString(t, options, configData)
    34  	KubectlApplyFromString(t, options, configData)
    35  
    36  	pvcs := ListPersistentVolumeClaims(t, options, metav1.ListOptions{})
    37  	require.Equal(t, len(pvcs), 1)
    38  	pvc := pvcs[0]
    39  	require.Equal(t, pvc.Name, pvcName)
    40  	require.Equal(t, pvc.Namespace, namespace)
    41  }
    42  
    43  func TestListPersistentVolumeClaimsReturnsZeroPersistentVolumeClaimsIfNoneCreated(t *testing.T) {
    44  	t.Parallel()
    45  
    46  	namespace := strings.ToLower(random.UniqueId())
    47  	options := NewKubectlOptions("", "", namespace)
    48  	CreateNamespace(t, options, namespace)
    49  	defer DeleteNamespace(t, options, namespace)
    50  
    51  	pvcs := ListPersistentVolumeClaims(t, options, metav1.ListOptions{})
    52  	require.Equal(t, len(pvcs), 0)
    53  }
    54  
    55  func TestGetPersistentVolumeClaimEReturnsErrorForNonExistantPersistentVolumeClaim(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	options := NewKubectlOptions("", "", "default")
    59  	_, err := GetPersistentVolumeClaimE(t, options, "non-existent")
    60  	require.Error(t, err)
    61  }
    62  
    63  func TestGetPersistentVolumeClaimReturnsCorrectPersistentVolumeClaimInCorrectNamespace(t *testing.T) {
    64  	t.Parallel()
    65  
    66  	pvcName := "test-dummy-pvc"
    67  	namespace := strings.ToLower(random.UniqueId())
    68  	options := NewKubectlOptions("", "", namespace)
    69  	configData := renderFixtureYamlTemplate(namespace, pvcName)
    70  	defer KubectlDeleteFromString(t, options, configData)
    71  	KubectlApplyFromString(t, options, configData)
    72  
    73  	pvc := GetPersistentVolumeClaim(t, options, pvcName)
    74  	require.Equal(t, pvc.Name, pvcName)
    75  	require.Equal(t, pvc.Namespace, namespace)
    76  }
    77  
    78  func TestWaitUntilPersistentVolumeClaimInGivenStatusPhase(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	pvcName := "test-dummy-pvc"
    82  	namespace := strings.ToLower(random.UniqueId())
    83  	pvcBoundStatusPhase := corev1.ClaimBound
    84  	options := NewKubectlOptions("", "", namespace)
    85  	configData := renderFixtureYamlTemplate(namespace, pvcName)
    86  	defer KubectlDeleteFromString(t, options, configData)
    87  	KubectlApplyFromString(t, options, configData)
    88  
    89  	WaitUntilPersistentVolumeClaimInStatus(t, options, pvcName, &pvcBoundStatusPhase, 60, 1*time.Second)
    90  }
    91  
    92  func TestWaitUntilPersistentVolumeClaimInStatusEReturnsErrorWhenWaitingForAnUnexistentPvc(t *testing.T) {
    93  	t.Parallel()
    94  
    95  	pvcBoundStatusPhase := corev1.ClaimBound
    96  	options := NewKubectlOptions("", "", "default")
    97  	err := WaitUntilPersistentVolumeClaimInStatusE(t, options, "non-existent", &pvcBoundStatusPhase, 3, 1*time.Second)
    98  	require.NotEqual(t, err, nil)
    99  }
   100  
   101  func TestWaitUntilPersistentVolumeClaimInStatusEReturnsErrorWhenTimesOut(t *testing.T) {
   102  	t.Parallel()
   103  
   104  	pvcName := "test-dummy-pvc"
   105  	pvcLostStatusPhase := corev1.ClaimLost
   106  	namespace := strings.ToLower(random.UniqueId())
   107  	options := NewKubectlOptions("", "", namespace)
   108  	configData := renderFixtureYamlTemplate(namespace, pvcName)
   109  	defer KubectlDeleteFromString(t, options, configData)
   110  	KubectlApplyFromString(t, options, configData)
   111  
   112  	err := WaitUntilPersistentVolumeClaimInStatusE(t, options, pvcName, &pvcLostStatusPhase, 5, 1*time.Second)
   113  	require.NotEqual(t, err, nil)
   114  }
   115  
   116  func TestIsPersistentVolumeClaimInStatusReturnsFalseIfPvcIsNil(t *testing.T) {
   117  	t.Parallel()
   118  
   119  	result := IsPersistentVolumeClaimInStatus(nil, nil)
   120  	require.Equal(t, result, false)
   121  }
   122  
   123  const pvcFixtureYamlTemplate = `---
   124  apiVersion: v1
   125  kind: Namespace
   126  metadata:
   127    name: __namespace__
   128  ---
   129  apiVersion: v1
   130  kind: PersistentVolume
   131  metadata:
   132    name: __namespace__
   133  spec:
   134    capacity:
   135      storage: 10Mi
   136    accessModes:
   137      - ReadWriteOnce
   138    hostPath:
   139      path: "/tmp/__namespace__"
   140  ---
   141  apiVersion: v1
   142  kind: PersistentVolumeClaim
   143  metadata:
   144      namespace: __namespace__
   145      name: __pvcName__
   146  spec:
   147      accessModes:
   148          - ReadWriteOnce
   149      resources:
   150          requests:
   151              storage: 10Mi
   152  ---
   153  apiVersion: v1
   154  kind: Pod
   155  metadata:
   156    name: test-pvc-pod
   157    namespace: __namespace__
   158  spec:
   159    volumes:
   160      - name: test-pvc-volume
   161        persistentVolumeClaim:
   162          claimName: __pvcName__
   163    containers:
   164      - name: test-pvc-image
   165        image: nginx
   166        volumeMounts:
   167          - mountPath: "/tmp/foo"
   168            name: test-pvc-volume
   169  `
   170  
   171  func renderFixtureYamlTemplate(namespace, pvcName string) string {
   172  	return strings.Replace(strings.Replace(pvcFixtureYamlTemplate, "__namespace__", namespace, -1), "__pvcName__", pvcName, -1)
   173  }