istio.io/istio@v0.0.0-20240520182934-d79c90f27776/cni/pkg/util/podutil_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"testing"
    19  
    20  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  
    23  	"istio.io/api/annotation"
    24  	"istio.io/istio/pkg/config/constants"
    25  	"istio.io/istio/pkg/test/util/assert"
    26  )
    27  
    28  func TestGetPodIPIfPodIPPresent(t *testing.T) {
    29  	pod := &corev1.Pod{
    30  		ObjectMeta: metav1.ObjectMeta{
    31  			Name:      "test",
    32  			Namespace: "test",
    33  		},
    34  		Spec: corev1.PodSpec{
    35  			NodeName: "derp",
    36  		},
    37  		Status: corev1.PodStatus{
    38  			PodIP: "11.1.1.12",
    39  		},
    40  	}
    41  
    42  	podIPs := GetPodIPsIfPresent(pod)
    43  	assert.Equal(t, len(podIPs), 1)
    44  }
    45  
    46  func TestGetPodIPsIfPodIPPresent(t *testing.T) {
    47  	pod := &corev1.Pod{
    48  		ObjectMeta: metav1.ObjectMeta{
    49  			Name:      "test",
    50  			Namespace: "test",
    51  		},
    52  		Spec: corev1.PodSpec{
    53  			NodeName: "derp",
    54  		},
    55  		Status: corev1.PodStatus{
    56  			PodIP:  "2.2.2.2",
    57  			PodIPs: []corev1.PodIP{{IP: "2.2.2.2"}, {IP: "3.3.3.3"}},
    58  		},
    59  	}
    60  
    61  	podIPs := GetPodIPsIfPresent(pod)
    62  	assert.Equal(t, len(podIPs), 2)
    63  }
    64  
    65  func TestGetPodIPsIfNoPodIPPresent(t *testing.T) {
    66  	pod := &corev1.Pod{
    67  		ObjectMeta: metav1.ObjectMeta{
    68  			Name:      "test",
    69  			Namespace: "test",
    70  		},
    71  		Spec: corev1.PodSpec{
    72  			NodeName: "derp",
    73  		},
    74  		Status: corev1.PodStatus{},
    75  	}
    76  
    77  	podIPs := GetPodIPsIfPresent(pod)
    78  	assert.Equal(t, len(podIPs), 0)
    79  }
    80  
    81  func TestPodRedirectionEnabled(t *testing.T) {
    82  	var (
    83  		ambientEnabledLabel     = map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient}
    84  		ambientDisabledLabel    = map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeNone}
    85  		sidecarStatusAnnotation = map[string]string{annotation.SidecarStatus.Name: "test"}
    86  
    87  		namespaceWithAmbientEnabledLabel = &corev1.Namespace{
    88  			ObjectMeta: metav1.ObjectMeta{
    89  				Name:   "test",
    90  				Labels: ambientEnabledLabel,
    91  			},
    92  		}
    93  
    94  		unlabelledNamespace = &corev1.Namespace{
    95  			ObjectMeta: metav1.ObjectMeta{
    96  				Name: "test",
    97  			},
    98  		}
    99  
   100  		podWithAmbientEnabledLabel = &corev1.Pod{
   101  			ObjectMeta: metav1.ObjectMeta{
   102  				Name:      "test",
   103  				Namespace: "test",
   104  				Labels:    ambientEnabledLabel,
   105  			},
   106  		}
   107  
   108  		unlabelledPod = &corev1.Pod{
   109  			ObjectMeta: metav1.ObjectMeta{
   110  				Name:      "test",
   111  				Namespace: "test",
   112  			},
   113  		}
   114  
   115  		podWithSidecar = &corev1.Pod{
   116  			ObjectMeta: metav1.ObjectMeta{
   117  				Name:        "test",
   118  				Namespace:   "test",
   119  				Annotations: sidecarStatusAnnotation,
   120  			},
   121  		}
   122  
   123  		podWithAmbientDisabledLabel = &corev1.Pod{
   124  			ObjectMeta: metav1.ObjectMeta{
   125  				Name:      "test",
   126  				Namespace: "test",
   127  				Labels:    ambientDisabledLabel,
   128  			},
   129  		}
   130  
   131  		podWithSidecarAndAmbientEnabledLabel = &corev1.Pod{
   132  			ObjectMeta: metav1.ObjectMeta{
   133  				Name:        "test",
   134  				Namespace:   "test",
   135  				Labels:      ambientEnabledLabel,
   136  				Annotations: sidecarStatusAnnotation,
   137  			},
   138  		}
   139  	)
   140  
   141  	type args struct {
   142  		namespace *corev1.Namespace
   143  		pod       *corev1.Pod
   144  	}
   145  	tests := []struct {
   146  		name string
   147  		args args
   148  		want bool
   149  	}{
   150  		{
   151  			name: "ambient mode enabled for namespace",
   152  			args: args{
   153  				namespace: namespaceWithAmbientEnabledLabel,
   154  				pod:       unlabelledPod,
   155  			},
   156  			want: true,
   157  		},
   158  		{
   159  			name: "ambient mode enabled for pod",
   160  			args: args{
   161  				namespace: unlabelledNamespace,
   162  				pod:       podWithAmbientEnabledLabel,
   163  			},
   164  			want: true,
   165  		},
   166  		{
   167  			name: "ambient mode enabled for both namespace and pod",
   168  			args: args{
   169  				namespace: namespaceWithAmbientEnabledLabel,
   170  				pod:       podWithAmbientEnabledLabel,
   171  			},
   172  			want: true,
   173  		},
   174  		{
   175  			name: "ambient mode enabled for neither namespace nor pod",
   176  			args: args{
   177  				namespace: unlabelledNamespace,
   178  				pod:       unlabelledPod,
   179  			},
   180  			want: false,
   181  		},
   182  		{
   183  			name: "pod has sidecar and namespace has ambient enabled",
   184  			args: args{
   185  				namespace: namespaceWithAmbientEnabledLabel,
   186  				pod:       podWithSidecar,
   187  			},
   188  			want: false,
   189  		},
   190  		{
   191  			name: "pod has label to disable ambient redirection",
   192  			args: args{
   193  				namespace: namespaceWithAmbientEnabledLabel,
   194  				pod:       podWithAmbientDisabledLabel,
   195  			},
   196  			want: false,
   197  		},
   198  		{
   199  			name: "pod has sidecar, pod has ambient mode label",
   200  			args: args{
   201  				namespace: unlabelledNamespace,
   202  				pod:       podWithSidecarAndAmbientEnabledLabel,
   203  			},
   204  			want: false,
   205  		},
   206  	}
   207  	for _, tt := range tests {
   208  		t.Run(tt.name, func(t *testing.T) {
   209  			if got := PodRedirectionEnabled(tt.args.namespace, tt.args.pod); got != tt.want {
   210  				t.Errorf("PodRedirectionEnabled() = %v, want %v", got, tt.want)
   211  			}
   212  		})
   213  	}
   214  }