istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/envoy/configdump/endpoint_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 TestPrintEndpointsSummary(t *testing.T) {
    29  	tests := []struct {
    30  		name   string
    31  		filter EndpointFilter
    32  	}{
    33  		{
    34  			name:   "emptyfilter",
    35  			filter: EndpointFilter{},
    36  		},
    37  		{
    38  			name: "portfilter",
    39  			filter: EndpointFilter{
    40  				Port: 8080,
    41  			},
    42  		},
    43  	}
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			gotOut := &bytes.Buffer{}
    47  			cw := &ConfigWriter{Stdout: gotOut}
    48  			cd, _ := os.ReadFile("testdata/endpoint/configdump.json")
    49  			cw.Prime(cd)
    50  			err := cw.PrintEndpointsSummary(tt.filter)
    51  			assert.NoError(t, err)
    52  
    53  			wantOutputFile := path.Join("testdata/endpoint", fmt.Sprintf("%s_output.txt", tt.name))
    54  			util.CompareContent(t, gotOut.Bytes(), wantOutputFile)
    55  		})
    56  	}
    57  }
    58  
    59  func TestPrintEndpoints(t *testing.T) {
    60  	tests := []struct {
    61  		name         string
    62  		outputFormat string
    63  		filter       EndpointFilter
    64  	}{
    65  		{
    66  			name:         "emptyfilter",
    67  			outputFormat: "json",
    68  			filter:       EndpointFilter{},
    69  		},
    70  		{
    71  			name:         "emptyfilter",
    72  			outputFormat: "yaml",
    73  			filter:       EndpointFilter{},
    74  		},
    75  		{
    76  			name:         "portfilter",
    77  			outputFormat: "json",
    78  			filter: EndpointFilter{
    79  				Port: 8080,
    80  			},
    81  		},
    82  		{
    83  			name:         "portfilter",
    84  			outputFormat: "yaml",
    85  			filter: EndpointFilter{
    86  				Port: 8080,
    87  			},
    88  		},
    89  	}
    90  	for _, tt := range tests {
    91  		t.Run(tt.name, func(t *testing.T) {
    92  			gotOut := &bytes.Buffer{}
    93  			cw := &ConfigWriter{Stdout: gotOut}
    94  			cd, _ := os.ReadFile("testdata/endpoint/configdump.json")
    95  			cw.Prime(cd)
    96  			err := cw.PrintEndpoints(tt.filter, tt.outputFormat)
    97  			assert.NoError(t, err)
    98  
    99  			wantOutputFile := path.Join("testdata/endpoint", fmt.Sprintf("%s_output.%s", tt.name, tt.outputFormat))
   100  			util.CompareContent(t, gotOut.Bytes(), wantOutputFile)
   101  		})
   102  	}
   103  }