github.com/Azure/aad-pod-identity@v1.8.17/pkg/pod/pod_test.go (about)

     1  package pod
     2  
     3  import (
     4  	"testing"
     5  
     6  	internalaadpodid "github.com/Azure/aad-pod-identity/pkg/apis/aadpodidentity"
     7  
     8  	corev1 "k8s.io/api/core/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/klog/v2"
    11  )
    12  
    13  type TestPodClient struct {
    14  	pods []*corev1.Pod
    15  }
    16  
    17  func (c TestPodClient) Start(exit <-chan struct{}) {
    18  	klog.Info("start called from the test interface")
    19  }
    20  
    21  func (c TestPodClient) GetPods() ([]*corev1.Pod, error) {
    22  	// TODO: Add label matching. For now we add only pods which we want to add.
    23  	return c.pods, nil
    24  }
    25  
    26  func (c *TestPodClient) AddPod(podName string, podNs string, nodeName string, binding string) {
    27  	labels := make(map[string]string)
    28  	labels[internalaadpodid.CRDLabelKey] = binding
    29  	pod := &corev1.Pod{
    30  		ObjectMeta: metav1.ObjectMeta{
    31  			Name:      podName,
    32  			Namespace: podNs,
    33  			Labels:    labels,
    34  		},
    35  		Spec: corev1.PodSpec{
    36  			NodeName: nodeName,
    37  		},
    38  	}
    39  	c.pods = append(c.pods, pod)
    40  }
    41  
    42  func (c *TestPodClient) DeletePod(podName string, podNs string) {
    43  	var newPods []*corev1.Pod
    44  	changed := false
    45  	for _, pod := range c.pods {
    46  		if pod.Name == podName && pod.Namespace == podNs {
    47  			changed = true
    48  			continue
    49  		} else {
    50  			newPods = append(newPods, pod)
    51  		}
    52  	}
    53  	if changed {
    54  		c.pods = newPods
    55  	}
    56  }
    57  
    58  func TestIsPodExcepted(t *testing.T) {
    59  	cases := []struct {
    60  		podLabels        map[string]string
    61  		exceptionList    []internalaadpodid.AzurePodIdentityException
    62  		shouldBeExcepted bool
    63  	}{
    64  		{
    65  			podLabels:        map[string]string{"foo": "bar"},
    66  			exceptionList:    nil,
    67  			shouldBeExcepted: false,
    68  		},
    69  		{
    70  			podLabels: map[string]string{"foo": "except"},
    71  			exceptionList: []internalaadpodid.AzurePodIdentityException{
    72  				{
    73  					ObjectMeta: metav1.ObjectMeta{
    74  						Name: "exception1",
    75  					},
    76  					Spec: internalaadpodid.AzurePodIdentityExceptionSpec{
    77  						PodLabels: map[string]string{"foo": "notexcept"},
    78  					},
    79  				},
    80  			},
    81  			shouldBeExcepted: false,
    82  		},
    83  		{
    84  			podLabels: map[string]string{"foo": "except"},
    85  			exceptionList: []internalaadpodid.AzurePodIdentityException{
    86  				{
    87  					ObjectMeta: metav1.ObjectMeta{
    88  						Name: "exception1",
    89  					},
    90  					Spec: internalaadpodid.AzurePodIdentityExceptionSpec{
    91  						PodLabels: map[string]string{"foo": "notexcept"},
    92  					},
    93  				},
    94  				{
    95  					ObjectMeta: metav1.ObjectMeta{
    96  						Name: "exception2",
    97  					},
    98  					Spec: internalaadpodid.AzurePodIdentityExceptionSpec{
    99  						PodLabels: map[string]string{"foo": "except"},
   100  					},
   101  				},
   102  			},
   103  			shouldBeExcepted: true,
   104  		},
   105  	}
   106  
   107  	for _, tc := range cases {
   108  		isExcepted := IsPodExcepted(tc.podLabels, tc.exceptionList)
   109  		if isExcepted != tc.shouldBeExcepted {
   110  			t.Fatalf("expected: %v, got %v", tc.shouldBeExcepted, isExcepted)
   111  		}
   112  	}
   113  }