istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/envoy/configdump/configdump_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  	"os"
    20  	"testing"
    21  
    22  	"istio.io/istio/pilot/test/util"
    23  	"istio.io/istio/pkg/test/util/assert"
    24  )
    25  
    26  func TestConfigWriter_Prime(t *testing.T) {
    27  	tests := []struct {
    28  		name        string
    29  		wantConfigs int
    30  		inputFile   string
    31  		wantErr     bool
    32  	}{
    33  		{
    34  			name:        "errors if unable to unmarshal bytes",
    35  			inputFile:   "",
    36  			wantConfigs: 0,
    37  			wantErr:     true,
    38  		},
    39  	}
    40  	for _, tt := range tests {
    41  		t.Run(tt.name, func(t *testing.T) {
    42  			cw := &ConfigWriter{}
    43  			cd, _ := os.ReadFile(tt.inputFile)
    44  			err := cw.Prime(cd)
    45  			if cw.configDump == nil {
    46  				if tt.wantConfigs != 0 {
    47  					t.Errorf("wanted some configs loaded but config dump was nil")
    48  				}
    49  			} else if len(cw.configDump.Configs) != tt.wantConfigs {
    50  				t.Errorf("wanted %v configs loaded in got %v", tt.wantConfigs, len(cw.configDump.Configs))
    51  			}
    52  			if tt.wantErr {
    53  				assert.Error(t, err)
    54  			} else {
    55  				assert.NoError(t, err)
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func TestConfigWriter_PrintBootstrapDump(t *testing.T) {
    62  	tests := []struct {
    63  		name           string
    64  		wantOutputFile string
    65  		callPrime      bool
    66  		wantErr        bool
    67  	}{
    68  		{
    69  			name:           "returns expected bootstrap dump from Envoy onto Stdout",
    70  			callPrime:      true,
    71  			wantOutputFile: "testdata/bootstrapdump.json",
    72  		},
    73  		{
    74  			name:    "errors if config dump is not primed",
    75  			wantErr: true,
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			gotOut := &bytes.Buffer{}
    81  			cw := &ConfigWriter{Stdout: gotOut}
    82  			cd, _ := os.ReadFile("testdata/configdump.json")
    83  			if tt.callPrime {
    84  				cw.Prime(cd)
    85  			}
    86  			err := cw.PrintBootstrapDump("json")
    87  			if tt.wantOutputFile != "" {
    88  				util.CompareContent(t, gotOut.Bytes(), tt.wantOutputFile)
    89  			}
    90  			if err == nil && tt.wantErr {
    91  				t.Errorf("PrintBootstrapDump (%v) did not produce expected err", tt.name)
    92  			} else if err != nil && !tt.wantErr {
    93  				t.Errorf("PrintBootstrapDump (%v) produced unexpected err: %v", tt.name, err)
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func TestConfigWriter_PrintVersionSummary(t *testing.T) {
   100  	tests := []struct {
   101  		name           string
   102  		wantOutputFile string
   103  		callPrime      bool
   104  		wantErr        bool
   105  	}{
   106  		{
   107  			name:           "returns expected version summary onto Stdout",
   108  			callPrime:      true,
   109  			wantOutputFile: "testdata/versionsummary.txt",
   110  		},
   111  		{
   112  			name:    "errors if config dump is not primed",
   113  			wantErr: true,
   114  		},
   115  	}
   116  	for _, tt := range tests {
   117  		t.Run(tt.name, func(t *testing.T) {
   118  			gotOut := &bytes.Buffer{}
   119  			cw := &ConfigWriter{Stdout: gotOut}
   120  			cd, _ := os.ReadFile("testdata/configdump.json")
   121  			if tt.callPrime {
   122  				cw.Prime(cd)
   123  			}
   124  			err := cw.PrintBootstrapSummary()
   125  			if tt.wantOutputFile != "" {
   126  				util.CompareContent(t, gotOut.Bytes(), tt.wantOutputFile)
   127  			}
   128  			if err == nil && tt.wantErr {
   129  				t.Errorf("PrintVersionSummary (%v) did not produce expected err", tt.name)
   130  			} else if err != nil && !tt.wantErr {
   131  				t.Errorf("PrintVersionSummary (%v) produced unexpected err: %v", tt.name, err)
   132  			}
   133  		})
   134  	}
   135  }