...

Source file src/github.com/linkerd/linkerd2/pkg/k8s/resource/resource_test.go

Documentation: github.com/linkerd/linkerd2/pkg/k8s/resource

     1  package resource
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/linkerd/linkerd2/pkg/k8s"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  )
    10  
    11  func TestRenderRBACResource(t *testing.T) {
    12  	// Given
    13  	// RBAC object in the cluster
    14  	k8sCfg := []string{
    15  		`apiVersion: rbac.authorization.k8s.io/v1
    16  kind: ClusterRoleBinding
    17  metadata:
    18    annotations:
    19      kubectl.kubernetes.io/last-applied-configuration: |
    20        {"apiVersion":"rbac.authorization.k8s.io/v1","kind":"ClusterRoleBinding","metadata":{"annotations":{},"labels":{"linkerd.io/control-plane-component":"web","linkerd.io/control-plane-ns":"linkerd"},"name":"linkerd-linkerd-web-admin"},"roleRef":{"apiGroup":"rbac.authorization.k8s.io","kind":"ClusterRole","name":"linkerd-linkerd-tap-admin"},"subjects":[{"kind":"ServiceAccount","name":"linkerd-web","namespace":"linkerd"}]}
    21    creationTimestamp: "2020-03-28T20:33:00Z"
    22    labels:
    23      linkerd.io/control-plane-component: web
    24      linkerd.io/control-plane-ns: linkerd
    25    name: linkerd-linkerd-web-admin
    26    resourceVersion: "5512995"`,
    27  	}
    28  	// A clientset to get the resource from the cluster
    29  	fakeK8sAPI, err := k8s.NewFakeAPI(k8sCfg...)
    30  	if err != nil {
    31  		t.Fatalf("Unexpected error creating fake k8s clientset:%v", err)
    32  	}
    33  
    34  	// When we fetch the resources using our fake client
    35  	resources, err := fetchClusterRoleBindings(context.Background(), fakeK8sAPI, metav1.ListOptions{LabelSelector: k8s.ControllerNSLabel})
    36  	if err != nil {
    37  		t.Fatalf("Unexpected error fetching resources from mock client:%v", err)
    38  	}
    39  
    40  	// Then
    41  	expResources := 1
    42  	if len(resources) != expResources {
    43  		t.Errorf("mismatch in resource slice size: expected %d and got %d", expResources, len(resources))
    44  	}
    45  
    46  	rbacResource := resources[0]
    47  	expKind := "ClusterRoleBinding"
    48  	if rbacResource.Kind != expKind {
    49  		t.Errorf("mismatch in resource kind: expected %s and got %s", expKind, rbacResource.Kind)
    50  	}
    51  
    52  	expVersion := "rbac.authorization.k8s.io/v1"
    53  	if rbacResource.APIVersion != expVersion {
    54  		t.Errorf("mismatch in resource apiVersion: expected %s and got %s", expVersion, rbacResource.APIVersion)
    55  	}
    56  
    57  	expName := "linkerd-linkerd-web-admin"
    58  	if rbacResource.Name != expName {
    59  		t.Errorf("mismatch in resource name: expected %s and got %s", expName, rbacResource.Name)
    60  	}
    61  }
    62  

View as plain text