github.com/mponton/terratest@v0.44.0/modules/k8s/daemonset_test.go (about)

     1  //go:build kubernetes
     2  // +build 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  package k8s
    10  
    11  import (
    12  	"fmt"
    13  
    14  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    15  
    16  	"strings"
    17  	"testing"
    18  
    19  	"github.com/mponton/terratest/modules/random"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestGetDaemonSetEReturnsErrorForNonExistantDaemonSet(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	options := NewKubectlOptions("", "", "")
    27  	_, err := GetDaemonSetE(t, options, "sample-ds")
    28  	require.Error(t, err)
    29  }
    30  
    31  func TestGetDaemonSetEReturnsCorrectServiceInCorrectNamespace(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	uniqueID := strings.ToLower(random.UniqueId())
    35  	options := NewKubectlOptions("", "", uniqueID)
    36  	configData := fmt.Sprintf(EXAMPLE_DAEMONSET_YAML_TEMPLATE, uniqueID, uniqueID)
    37  	KubectlApplyFromString(t, options, configData)
    38  	defer KubectlDeleteFromString(t, options, configData)
    39  
    40  	daemonSet := GetDaemonSet(t, options, "sample-ds")
    41  	require.Equal(t, daemonSet.Name, "sample-ds")
    42  	require.Equal(t, daemonSet.Namespace, uniqueID)
    43  }
    44  
    45  func TestListDaemonSetsReturnsCorrectServiceInCorrectNamespace(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	uniqueID := strings.ToLower(random.UniqueId())
    49  	options := NewKubectlOptions("", "", uniqueID)
    50  	configData := fmt.Sprintf(EXAMPLE_DAEMONSET_YAML_TEMPLATE, uniqueID, uniqueID)
    51  	KubectlApplyFromString(t, options, configData)
    52  	defer KubectlDeleteFromString(t, options, configData)
    53  
    54  	daemonSets := ListDaemonSets(t, options, metav1.ListOptions{})
    55  	require.Equal(t, len(daemonSets), 1)
    56  
    57  	daemonSet := daemonSets[0]
    58  	require.Equal(t, daemonSet.Name, "sample-ds")
    59  	require.Equal(t, daemonSet.Namespace, uniqueID)
    60  }
    61  
    62  const EXAMPLE_DAEMONSET_YAML_TEMPLATE = `---
    63  apiVersion: v1
    64  kind: Namespace
    65  metadata:
    66    name: %s
    67  ---
    68  apiVersion: apps/v1
    69  kind: DaemonSet
    70  metadata:
    71    name: sample-ds
    72    namespace: %s
    73    labels:
    74      k8s-app: sample-ds
    75  spec:
    76    selector:
    77      matchLabels:
    78        name: sample-ds
    79    template:
    80      metadata:
    81        labels:
    82          name: sample-ds
    83      spec:
    84        tolerations:
    85        - key: node-role.kubernetes.io/master
    86          effect: NoSchedule
    87        containers:
    88        - name: alpine
    89          image: alpine:3.8
    90          command: ['sh', '-c', 'echo Hello Terratest! && sleep 99999']
    91  `