istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/injector/injector-list_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 injector 16 17 import ( 18 "context" 19 "fmt" 20 "testing" 21 22 corev1 "k8s.io/api/core/v1" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/runtime" 25 26 "istio.io/api/annotation" 27 "istio.io/api/label" 28 "istio.io/istio/pkg/config/constants" 29 "istio.io/istio/pkg/kube" 30 "istio.io/istio/pkg/test/util/assert" 31 "istio.io/istio/pkg/util/sets" 32 ) 33 34 func Test_extractRevisionFromPod(t *testing.T) { 35 cases := []struct { 36 name string 37 pod *corev1.Pod 38 expectedRevision string 39 }{ 40 { 41 name: "no rev", 42 pod: &corev1.Pod{}, 43 expectedRevision: "", 44 }, 45 { 46 name: "has rev annotation", 47 pod: &corev1.Pod{ 48 ObjectMeta: metav1.ObjectMeta{ 49 Annotations: map[string]string{ 50 annotation.SidecarStatus.Name: `{"revision": "test-anno"}`, 51 }, 52 }, 53 }, 54 expectedRevision: "test-anno", 55 }, 56 { 57 name: "has both rev label and annotation, use label", 58 pod: &corev1.Pod{ 59 ObjectMeta: metav1.ObjectMeta{ 60 Labels: map[string]string{ 61 label.IoIstioRev.Name: "test-label", // don't care about the label 62 }, 63 Annotations: map[string]string{ 64 annotation.SidecarStatus.Name: `{"revision":"test-anno"}`, 65 }, 66 }, 67 }, 68 expectedRevision: "test-anno", 69 }, 70 } 71 for i, c := range cases { 72 t.Run(fmt.Sprintf("case %d %s", i, c.name), func(t *testing.T) { 73 assert.Equal(t, c.expectedRevision, extractRevisionFromPod(c.pod)) 74 }) 75 } 76 } 77 78 func Test_getNamespaces(t *testing.T) { 79 createNamespace := func(name string, labels map[string]string) *corev1.Namespace { 80 return &corev1.Namespace{ 81 ObjectMeta: metav1.ObjectMeta{ 82 Name: name, 83 Labels: labels, 84 }, 85 } 86 } 87 nss := []runtime.Object{ 88 createNamespace("default", nil), 89 createNamespace("kube-system", nil), 90 createNamespace("istio-system", nil), 91 createNamespace("ambient", map[string]string{ 92 constants.DataplaneModeLabel: constants.DataplaneModeAmbient, 93 }), 94 createNamespace("no-ambient", map[string]string{ 95 constants.DataplaneModeLabel: constants.DataplaneModeAmbient, 96 "istio-injection": "enabled", 97 }), 98 } 99 100 client := kube.NewFakeClient(nss...) 101 expected := sets.New[string]("default", "no-ambient") 102 actual, err := getNamespaces(context.TODO(), client, "istio-system") 103 assert.NoError(t, err) 104 for _, ns := range actual { 105 assert.Equal(t, true, expected.Contains(ns.Name)) 106 } 107 }