istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/networking/grpcgen/lds_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 grpcgen
    16  
    17  import (
    18  	"fmt"
    19  	"sort"
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  
    24  	"istio.io/istio/pilot/pkg/model"
    25  	"istio.io/istio/pkg/istio-agent/grpcxds"
    26  	"istio.io/istio/pkg/util/sets"
    27  )
    28  
    29  var node = &model.Proxy{DNSDomain: "ns.svc.cluster.local", Metadata: &model.NodeMetadata{Namespace: "ns"}}
    30  
    31  func TestListenerNameFilter(t *testing.T) {
    32  	cases := map[string]struct {
    33  		in          []string
    34  		want        listenerNames
    35  		wantInbound []string
    36  	}{
    37  		"simple": {
    38  			in: []string{"foo.com:80", "foo.com:443", "wildcard.com"},
    39  			want: listenerNames{
    40  				"foo.com": {
    41  					RequestedNames: sets.New("foo.com"),
    42  					Ports:          sets.New("80", "443"),
    43  				},
    44  				"wildcard.com": {RequestedNames: sets.New("wildcard.com")},
    45  			},
    46  		},
    47  		"plain-host clears port-map": {
    48  			in:   []string{"foo.com:80", "foo.com"},
    49  			want: listenerNames{"foo.com": {RequestedNames: sets.New("foo.com")}},
    50  		},
    51  		"port-map stays clear": {
    52  			in: []string{"foo.com:80", "foo.com", "foo.com:443"},
    53  			want: listenerNames{"foo.com": {
    54  				RequestedNames: sets.New("foo.com"),
    55  			}},
    56  		},
    57  		"special listeners preserved exactly": {
    58  			in: []string{
    59  				"foo.com:80",
    60  				fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "foo:1234"),
    61  				fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "foo"),
    62  				fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "[::]:8076"),
    63  			},
    64  			want: listenerNames{
    65  				"foo.com": {
    66  					RequestedNames: sets.New("foo.com"),
    67  					Ports:          sets.New("80"),
    68  				},
    69  				fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "foo:1234"): {
    70  					RequestedNames: sets.New(fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "foo:1234")),
    71  				},
    72  				fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "foo"): {
    73  					RequestedNames: sets.New(fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "foo")),
    74  				},
    75  				fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "[::]:8076"): {
    76  					RequestedNames: sets.New(fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "[::]:8076")),
    77  				},
    78  			},
    79  			wantInbound: []string{
    80  				fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "foo:1234"),
    81  				fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "foo"),
    82  				fmt.Sprintf(grpcxds.ServerListenerNameTemplate, "[::]:8076"),
    83  			},
    84  		},
    85  		"expand shortnames": {
    86  			in: []string{
    87  				"bar",
    88  				"bar.ns",
    89  				"bar.ns.svc",
    90  				"bar.ns.svc.cluster.local",
    91  				"foo:80",
    92  				"foo.ns:81",
    93  				"foo.ns.svc:82",
    94  				"foo.ns.svc.cluster.local:83",
    95  			},
    96  			want: listenerNames{
    97  				"bar":        {RequestedNames: sets.New("bar")},
    98  				"bar.ns":     {RequestedNames: sets.New("bar.ns")},
    99  				"bar.ns.svc": {RequestedNames: sets.New("bar.ns.svc")},
   100  				"bar.ns.svc.cluster.local": {RequestedNames: sets.New(
   101  					"bar",
   102  					"bar.ns",
   103  					"bar.ns.svc",
   104  					"bar.ns.svc.cluster.local",
   105  				)},
   106  				"foo":        {RequestedNames: sets.New("foo"), Ports: sets.New("80")},
   107  				"foo.ns":     {RequestedNames: sets.New("foo.ns"), Ports: sets.New("81")},
   108  				"foo.ns.svc": {RequestedNames: sets.New("foo.ns.svc"), Ports: sets.New("82")},
   109  				"foo.ns.svc.cluster.local": {
   110  					RequestedNames: sets.New(
   111  						"foo",
   112  						"foo.ns",
   113  						"foo.ns.svc",
   114  						"foo.ns.svc.cluster.local",
   115  					),
   116  					Ports: sets.New("80", "81", "82", "83"),
   117  				},
   118  			},
   119  		},
   120  	}
   121  	for name, tt := range cases {
   122  		t.Run(name, func(t *testing.T) {
   123  			got := newListenerNameFilter(tt.in, node)
   124  			if diff := cmp.Diff(got, tt.want); diff != "" {
   125  				t.Fatal(diff)
   126  			}
   127  			gotInbound := got.inboundNames()
   128  			sort.Strings(gotInbound)
   129  			sort.Strings(tt.wantInbound)
   130  			if diff := cmp.Diff(gotInbound, tt.wantInbound); diff != "" {
   131  				t.Fatalf(diff)
   132  			}
   133  		})
   134  	}
   135  }
   136  
   137  func TestTryFindFQDN(t *testing.T) {
   138  	cases := []struct {
   139  		in   string
   140  		want string
   141  	}{
   142  		{"foo", "foo.ns.svc.cluster.local"},
   143  		{"foo.ns", "foo.ns.svc.cluster.local"},
   144  		{"foo.ns.svc", "foo.ns.svc.cluster.local"},
   145  		{"foo.ns.svc.cluster.local", ""},
   146  		{"foo.com", ""},
   147  		{"foo.ns.com", ""},
   148  		{"foo.ns.svc.notdnsdomain", ""},
   149  		{"foo.ns.svc.cluster.local.extra", ""},
   150  		{"xds.istio.io/grpc/lds/inbound/0.0.0.0:1234", ""},
   151  	}
   152  
   153  	for _, tc := range cases {
   154  		t.Run(tc.in, func(t *testing.T) {
   155  			if got := tryFindFQDN(tc.in, node); got != tc.want {
   156  				t.Errorf("want %q but got %q", tc.want, got)
   157  			}
   158  		})
   159  	}
   160  }