github.com/mponton/terratest@v0.44.0/modules/k8s/cluster_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  	"testing"
    14  
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestGetClusterRoleEReturnsErrorForNonExistantClusterRole(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	options := NewKubectlOptions("", "", "default")
    22  	_, err := GetClusterRoleE(t, options, "non-existing-role")
    23  	require.Error(t, err)
    24  }
    25  
    26  func TestGetClusterRoleEReturnsCorrectClusterRoleInCorrectNamespace(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	options := NewKubectlOptions("", "", "default")
    30  	defer KubectlDeleteFromString(t, options, EXAMPLE_CLUSTER_ROLE_YAML_TEMPLATE)
    31  	KubectlApplyFromString(t, options, EXAMPLE_CLUSTER_ROLE_YAML_TEMPLATE)
    32  
    33  	role := GetClusterRole(t, options, "terratest-cluster-role")
    34  	require.Equal(t, role.Name, "terratest-cluster-role")
    35  }
    36  
    37  const EXAMPLE_CLUSTER_ROLE_YAML_TEMPLATE = `---
    38  apiVersion: rbac.authorization.k8s.io/v1
    39  kind: ClusterRole
    40  metadata:
    41    name: 'terratest-cluster-role'
    42  rules:
    43  - apiGroups:
    44    - '*'
    45    resources:
    46    - '*'
    47    verbs:
    48    - '*'
    49  `