istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/envoy/configdump/route_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 configdump
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"os"
    21  	"path"
    22  	"testing"
    23  
    24  	"istio.io/istio/pilot/test/util"
    25  	"istio.io/istio/pkg/test/util/assert"
    26  )
    27  
    28  func TestDescribeRouteDomains(t *testing.T) {
    29  	tests := []struct {
    30  		desc     string
    31  		domains  []string
    32  		expected string
    33  	}{
    34  		{
    35  			desc:     "test zero domain",
    36  			domains:  []string{},
    37  			expected: "",
    38  		},
    39  		{
    40  			desc:     "test only one domain",
    41  			domains:  []string{"example.com"},
    42  			expected: "example.com",
    43  		},
    44  		{
    45  			desc:     "test domains with port",
    46  			domains:  []string{"example.com", "example.com:8080"},
    47  			expected: "example.com",
    48  		},
    49  		{
    50  			desc:     "test domains with ipv4 addresses",
    51  			domains:  []string{"example.com", "example.com:8080", "1.2.3.4", "1.2.3.4:8080"},
    52  			expected: "example.com, 1.2.3.4",
    53  		},
    54  		{
    55  			desc:     "test domains with ipv6 addresses",
    56  			domains:  []string{"example.com", "example.com:8080", "[fd00:10:96::7fc7]", "[fd00:10:96::7fc7]:8080"},
    57  			expected: "example.com, [fd00:10:96::7fc7]",
    58  		},
    59  		{
    60  			desc:     "test with more domains",
    61  			domains:  []string{"example.com", "example.com:8080", "www.example.com", "www.example.com:8080", "[fd00:10:96::7fc7]", "[fd00:10:96::7fc7]:8080"},
    62  			expected: "example.com, www.example.com + 1 more...",
    63  		},
    64  	}
    65  
    66  	for _, tt := range tests {
    67  		t.Run(tt.desc, func(t *testing.T) {
    68  			if got := describeRouteDomains(tt.domains); got != tt.expected {
    69  				t.Errorf("%s: expect %v got %v", tt.desc, tt.expected, got)
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func TestPrintRoutesSummary(t *testing.T) {
    76  	tests := []struct {
    77  		name string
    78  	}{
    79  		{
    80  			name: "empty-gateway",
    81  		},
    82  		{
    83  			name: "istio-gateway-http-route-prefix",
    84  		},
    85  		{
    86  			name: "k8s-gateway-http-route-path-prefix",
    87  		},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			gotOut := &bytes.Buffer{}
    92  			cw := &ConfigWriter{Stdout: gotOut}
    93  			cd, _ := os.ReadFile(fmt.Sprintf("testdata/routes/%s/configdump.json", tt.name))
    94  			if err := cw.Prime(cd); err != nil {
    95  				t.Errorf("failed to parse config dump: %s", err)
    96  			}
    97  			err := cw.PrintRouteSummary(RouteFilter{Verbose: true})
    98  			assert.NoError(t, err)
    99  
   100  			wantOutputFile := path.Join("testdata/routes", tt.name, "output.txt")
   101  			util.CompareContent(t, gotOut.Bytes(), wantOutputFile)
   102  		})
   103  	}
   104  }