github.com/mponton/terratest@v0.44.0/modules/k8s/namespace_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  
    16  	"github.com/stretchr/testify/require"
    17  	corev1 "k8s.io/api/core/v1"
    18  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    19  
    20  	"github.com/mponton/terratest/modules/random"
    21  )
    22  
    23  func TestNamespaces(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	uniqueId := random.UniqueId()
    27  	namespaceName := strings.ToLower(uniqueId)
    28  	options := NewKubectlOptions("", "", namespaceName)
    29  	CreateNamespace(t, options, namespaceName)
    30  	defer func() {
    31  		DeleteNamespace(t, options, namespaceName)
    32  		namespace := GetNamespace(t, options, namespaceName)
    33  		require.Equal(t, namespace.Status.Phase, corev1.NamespaceTerminating)
    34  	}()
    35  
    36  	namespace := GetNamespace(t, options, namespaceName)
    37  	require.Equal(t, namespace.Name, namespaceName)
    38  }
    39  
    40  func TestNamespaceWithMetadata(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	uniqueId := random.UniqueId()
    44  	namespaceName := strings.ToLower(uniqueId)
    45  	options := NewKubectlOptions("", "", namespaceName)
    46  	namespaceLabels := map[string]string{"foo": "bar"}
    47  	namespaceObjectMetaWithLabels := metav1.ObjectMeta{
    48  		Name:   namespaceName,
    49  		Labels: namespaceLabels,
    50  	}
    51  	CreateNamespaceWithMetadata(t, options, namespaceObjectMetaWithLabels)
    52  	defer func() {
    53  		DeleteNamespace(t, options, namespaceName)
    54  		namespace := GetNamespace(t, options, namespaceName)
    55  		require.Equal(t, namespace.Status.Phase, corev1.NamespaceTerminating)
    56  	}()
    57  
    58  	namespace := GetNamespace(t, options, namespaceName)
    59  	require.Equal(t, namespace.Name, namespaceName)
    60  	require.Equal(t, namespace.Labels, namespaceLabels)
    61  }