github.com/mponton/terratest@v0.44.0/modules/k8s/replicaset_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  
    15  	"strings"
    16  	"testing"
    17  
    18  	"github.com/mponton/terratest/modules/random"
    19  	"github.com/stretchr/testify/require"
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  )
    22  
    23  func TestGetReplicaSetEReturnsError(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	options := NewKubectlOptions("", "", "")
    27  	_, err := GetReplicaSetE(t, options, "sample-rs")
    28  	require.Error(t, err)
    29  }
    30  
    31  func TestGetReplicaSets(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	uniqueID := strings.ToLower(random.UniqueId())
    35  	options := NewKubectlOptions("", "", uniqueID)
    36  	configData := fmt.Sprintf(EXAMPLE_REPLICASET_YAML_TEMPLATE, uniqueID, uniqueID)
    37  	defer KubectlDeleteFromString(t, options, configData)
    38  	KubectlApplyFromString(t, options, configData)
    39  
    40  	replicaSet := GetReplicaSet(t, options, "sample-rs")
    41  	require.Equal(t, replicaSet.Name, "sample-rs")
    42  	require.Equal(t, replicaSet.Namespace, uniqueID)
    43  }
    44  
    45  func TestListReplicaSets(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	uniqueID := strings.ToLower(random.UniqueId())
    49  	options := NewKubectlOptions("", "", uniqueID)
    50  	configData := fmt.Sprintf(EXAMPLE_REPLICASET_YAML_TEMPLATE, uniqueID, uniqueID)
    51  	defer KubectlDeleteFromString(t, options, configData)
    52  	KubectlApplyFromString(t, options, configData)
    53  
    54  	replicaSets := ListReplicaSets(t, options, metav1.ListOptions{})
    55  	require.Equal(t, len(replicaSets), 1)
    56  
    57  	replicaSet := replicaSets[0]
    58  	require.Equal(t, replicaSet.Name, "sample-rs")
    59  	require.Equal(t, replicaSet.Namespace, uniqueID)
    60  }
    61  
    62  const EXAMPLE_REPLICASET_YAML_TEMPLATE = `---
    63  apiVersion: v1
    64  kind: Namespace
    65  metadata:
    66    name: %s
    67  ---
    68  apiVersion: apps/v1
    69  kind: ReplicaSet
    70  metadata:
    71    name: sample-rs
    72    namespace: %s
    73    labels:
    74      app: sample-rs
    75  spec:
    76    selector:
    77      matchLabels:
    78        name: sample-rs
    79    template:
    80      metadata:
    81        labels:
    82          name: sample-rs
    83      spec:
    84        containers:
    85        - name: alpine
    86          image: alpine:3.8
    87          command: ['sh', '-c', 'echo Hello Terratest! && sleep 99999']
    88  `