github.com/terraform-modules-krish/terratest@v0.29.0/modules/k8s/daemonset_test.go (about)

     1  // +build kubernetes
     2  
     3  // NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests. This is done because minikube
     4  // is heavy and can interfere with docker related tests in terratest. Specifically, many of the tests start to fail with
     5  // `connection refused` errors from `minikube`. To avoid overloading the system, we run the kubernetes tests and helm
     6  // tests separately from the others. This may not be necessary if you have a sufficiently powerful machine.  We
     7  // recommend at least 4 cores and 16GB of RAM if you want to run all the tests together.
     8  package k8s
     9  
    10  import (
    11  	"fmt"
    12  
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  
    15  	"strings"
    16  	"testing"
    17  
    18  	"github.com/terraform-modules-krish/terratest/modules/random"
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestGetDaemonSetEReturnsErrorForNonExistantDaemonSet(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	options := NewKubectlOptions("", "", "")
    26  	_, err := GetDaemonSetE(t, options, "sample-ds")
    27  	require.Error(t, err)
    28  }
    29  
    30  func TestGetDaemonSetEReturnsCorrectServiceInCorrectNamespace(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	uniqueID := strings.ToLower(random.UniqueId())
    34  	options := NewKubectlOptions("", "", uniqueID)
    35  	configData := fmt.Sprintf(EXAMPLE_DAEMONSET_YAML_TEMPLATE, uniqueID, uniqueID)
    36  	KubectlApplyFromString(t, options, configData)
    37  	defer KubectlDeleteFromString(t, options, configData)
    38  
    39  	daemonSet := GetDaemonSet(t, options, "sample-ds")
    40  	require.Equal(t, daemonSet.Name, "sample-ds")
    41  	require.Equal(t, daemonSet.Namespace, uniqueID)
    42  }
    43  
    44  func TestListDaemonSetsReturnsCorrectServiceInCorrectNamespace(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	uniqueID := strings.ToLower(random.UniqueId())
    48  	options := NewKubectlOptions("", "", uniqueID)
    49  	configData := fmt.Sprintf(EXAMPLE_DAEMONSET_YAML_TEMPLATE, uniqueID, uniqueID)
    50  	KubectlApplyFromString(t, options, configData)
    51  	defer KubectlDeleteFromString(t, options, configData)
    52  
    53  	daemonSets := ListDaemonSets(t, options, metav1.ListOptions{})
    54  	require.Equal(t, len(daemonSets), 1)
    55  
    56  	daemonSet := daemonSets[0]
    57  	require.Equal(t, daemonSet.Name, "sample-ds")
    58  	require.Equal(t, daemonSet.Namespace, uniqueID)
    59  }
    60  
    61  const EXAMPLE_DAEMONSET_YAML_TEMPLATE = `---
    62  apiVersion: v1
    63  kind: Namespace
    64  metadata:
    65    name: %s
    66  ---
    67  apiVersion: apps/v1
    68  kind: DaemonSet
    69  metadata:
    70    name: sample-ds
    71    namespace: %s
    72    labels:
    73      k8s-app: sample-ds
    74  spec:
    75    selector:
    76      matchLabels:
    77        name: sample-ds
    78    template:
    79      metadata:
    80        labels:
    81          name: sample-ds
    82      spec:
    83        tolerations:
    84        - key: node-role.kubernetes.io/master
    85          effect: NoSchedule
    86        containers:
    87        - name: alpine
    88          image: alpine:3.8
    89          command: ['sh', '-c', 'echo Hello Terratest! && sleep 99999']
    90  `