istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/ztunnel/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 name: "loads valid ztunnel config_dump", 41 inputFile: "testdata/dump.json", 42 wantConfigs: 27, 43 wantErr: false, 44 }, 45 } 46 for _, tt := range tests { 47 t.Run(tt.name, func(t *testing.T) { 48 cw := &ConfigWriter{} 49 cd, _ := os.ReadFile(tt.inputFile) 50 err := cw.Prime(cd) 51 if cw.ztunnelDump == nil { 52 if tt.wantConfigs != 0 { 53 t.Errorf("wanted some configs loaded but config dump was nil") 54 } 55 } else if len(cw.ztunnelDump.Workloads) != tt.wantConfigs { 56 t.Errorf("wanted %v configs loaded in got %v", tt.wantConfigs, len(cw.ztunnelDump.Workloads)) 57 } 58 if tt.wantErr { 59 assert.Error(t, err) 60 } else { 61 assert.NoError(t, err) 62 } 63 }) 64 } 65 } 66 67 func TestConfigWriter_PrintSecretSummary(t *testing.T) { 68 tests := []struct { 69 name string 70 wantOutputSecret string 71 wantOutputWorkload string 72 configNamespace string 73 callPrime bool 74 wantErr bool 75 }{ 76 { 77 name: "returns expected secret summary onto Stdout", 78 callPrime: true, 79 wantOutputSecret: "testdata/secretsummary.txt", 80 }, 81 { 82 name: "errors if config dump is not primed", 83 wantErr: true, 84 }, 85 { 86 name: "returns expected workload summary onto Stdout", 87 callPrime: true, 88 wantOutputWorkload: "testdata/workloadsummary.txt", 89 }, 90 { 91 name: "returns expected workload summary with the default namespace", 92 callPrime: true, 93 configNamespace: "default", 94 wantOutputWorkload: "testdata/workloadsummary_default.txt", 95 }, 96 } 97 for _, tt := range tests { 98 t.Run(tt.name, func(t *testing.T) { 99 gotOut := &bytes.Buffer{} 100 cw := &ConfigWriter{Stdout: gotOut} 101 cd, _ := os.ReadFile("testdata/dump.json") 102 if tt.callPrime { 103 cw.Prime(cd) 104 } 105 if tt.wantOutputSecret != "" { 106 err := cw.PrintSecretSummary() 107 if err == nil && tt.wantErr { 108 t.Errorf("PrintSecretSummary (%v) did not produce expected err", tt.name) 109 } else if err != nil && !tt.wantErr { 110 t.Errorf("PrintSecretSummary (%v) produced unexpected err: %v", tt.name, err) 111 } 112 util.CompareContent(t, gotOut.Bytes(), tt.wantOutputSecret) 113 } 114 if tt.wantOutputWorkload != "" { 115 wf := WorkloadFilter{Verbose: true} 116 if tt.configNamespace != "" { 117 wf.Namespace = tt.configNamespace 118 } 119 err := cw.PrintWorkloadSummary(wf) 120 if err == nil && tt.wantErr { 121 t.Errorf("PrintWorkloadSummary (%v) did not produce expected err", tt.name) 122 } else if err != nil && !tt.wantErr { 123 t.Errorf("PrintWorkloadSummary (%v) produced unexpected err: %v", tt.name, err) 124 } 125 util.CompareContent(t, gotOut.Bytes(), tt.wantOutputWorkload) 126 } 127 }) 128 } 129 }