istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/serviceregistry/serviceentry/util_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 serviceentry
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"k8s.io/apimachinery/pkg/types"
    22  
    23  	networking "istio.io/api/networking/v1alpha3"
    24  	"istio.io/istio/pkg/config"
    25  	"istio.io/istio/pkg/config/schema/gvk"
    26  )
    27  
    28  func TestGetWorkloadServiceEntries(t *testing.T) {
    29  	se1 := config.Config{
    30  		Meta: config.Meta{GroupVersionKind: gvk.ServiceEntry, Namespace: "default", Name: "se-1"},
    31  		Spec: &networking.ServiceEntry{
    32  			Hosts: []string{"*.google.com"},
    33  			Ports: []*networking.ServicePort{
    34  				{Number: 80, Name: "http-number", Protocol: "http"},
    35  				{Number: 8080, Name: "http2-number", Protocol: "http2"},
    36  			},
    37  			WorkloadSelector: &networking.WorkloadSelector{
    38  				Labels: map[string]string{"app": "foo"},
    39  			},
    40  		},
    41  	}
    42  	se2 := config.Config{
    43  		Meta: config.Meta{GroupVersionKind: gvk.ServiceEntry, Namespace: "default", Name: "se-2"},
    44  		Spec: &networking.ServiceEntry{
    45  			Hosts: []string{"*.google.com"},
    46  			Ports: []*networking.ServicePort{
    47  				{Number: 80, Name: "http-number", Protocol: "http"},
    48  				{Number: 8080, Name: "http2-number", Protocol: "http2"},
    49  			},
    50  			WorkloadSelector: &networking.WorkloadSelector{
    51  				Labels: map[string]string{"app": "bar"},
    52  			},
    53  		},
    54  	}
    55  
    56  	se3 := config.Config{
    57  		Meta: config.Meta{GroupVersionKind: gvk.ServiceEntry, Namespace: "default", Name: "se-3"},
    58  		Spec: &networking.ServiceEntry{
    59  			Hosts: []string{"www.wikipedia.org"},
    60  			Ports: []*networking.ServicePort{
    61  				{Number: 80, Name: "http-number", Protocol: "http"},
    62  				{Number: 8080, Name: "http2-number", Protocol: "http2"},
    63  			},
    64  			WorkloadSelector: &networking.WorkloadSelector{
    65  				Labels: map[string]string{"app": "foo"},
    66  			},
    67  		},
    68  	}
    69  	ses := []config.Config{se1, se2, se3}
    70  
    71  	wle := &networking.WorkloadEntry{
    72  		Address: "2.3.4.5",
    73  		Labels: map[string]string{
    74  			"app":     "foo",
    75  			"version": "v1",
    76  		},
    77  		Ports: map[string]uint32{
    78  			"http-number":  8081,
    79  			"http2-number": 8088,
    80  		},
    81  	}
    82  
    83  	expected := map[types.NamespacedName]*config.Config{
    84  		{Namespace: "default", Name: "se-1"}: &se1,
    85  		{Namespace: "default", Name: "se-3"}: &se3,
    86  	}
    87  	got := getWorkloadServiceEntries(ses, wle)
    88  	if !reflect.DeepEqual(got, expected) {
    89  		t.Errorf("recv unexpected se: %v", got)
    90  	}
    91  }
    92  
    93  func TestCompareServiceEntries(t *testing.T) {
    94  	oldSes := map[types.NamespacedName]*config.Config{
    95  		{Namespace: "default", Name: "se-1"}: {},
    96  		{Namespace: "default", Name: "se-2"}: {},
    97  		{Namespace: "default", Name: "se-3"}: {},
    98  	}
    99  	currSes := map[types.NamespacedName]*config.Config{
   100  		{Namespace: "default", Name: "se-2"}: {},
   101  		{Namespace: "default", Name: "se-4"}: {},
   102  		{Namespace: "default", Name: "se-5"}: {},
   103  	}
   104  
   105  	expectedUnselected := map[types.NamespacedName]*config.Config{
   106  		{Namespace: "default", Name: "se-1"}: {},
   107  		{Namespace: "default", Name: "se-3"}: {},
   108  	}
   109  	unSelected := difference(oldSes, currSes)
   110  
   111  	if len(unSelected) != len(expectedUnselected) {
   112  		t.Errorf("got unexpected unSelected ses %v", unSelected)
   113  	}
   114  	for _, se := range unSelected {
   115  		if _, ok := expectedUnselected[se]; !ok {
   116  			t.Errorf("got unexpected unSelected se %v", se)
   117  		}
   118  	}
   119  }