github.com/mponton/terratest@v0.44.0/modules/k8s/role_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  	"strings"
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/require"
    18  
    19  	"github.com/mponton/terratest/modules/random"
    20  )
    21  
    22  func TestGetRoleEReturnsErrorForNonExistantRole(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	options := NewKubectlOptions("", "", "default")
    26  	_, err := GetRoleE(t, options, "non-existing-role")
    27  	require.Error(t, err)
    28  }
    29  
    30  func TestGetRoleEReturnsCorrectRoleInCorrectNamespace(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	uniqueID := strings.ToLower(random.UniqueId())
    34  	options := NewKubectlOptions("", "", uniqueID)
    35  	configData := fmt.Sprintf(EXAMPLE_ROLE_YAML_TEMPLATE, uniqueID, uniqueID)
    36  	defer KubectlDeleteFromString(t, options, configData)
    37  	KubectlApplyFromString(t, options, configData)
    38  
    39  	role := GetRole(t, options, "terratest-role")
    40  	require.Equal(t, role.Name, "terratest-role")
    41  	require.Equal(t, role.Namespace, uniqueID)
    42  }
    43  
    44  const EXAMPLE_ROLE_YAML_TEMPLATE = `---
    45  apiVersion: v1
    46  kind: Namespace
    47  metadata:
    48    name: '%s'
    49  ---
    50  apiVersion: rbac.authorization.k8s.io/v1
    51  kind: Role
    52  metadata:
    53    name: 'terratest-role'
    54    namespace: '%s'
    55  rules:
    56  - apiGroups:
    57    - '*'
    58    resources:
    59    - '*'
    60    verbs:
    61    - '*'
    62  `