istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/cmd/mesh/profile-dump_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 mesh 16 17 import ( 18 "os" 19 "path/filepath" 20 "regexp" 21 "testing" 22 23 "github.com/kylelemons/godebug/diff" 24 25 "istio.io/istio/operator/pkg/util" 26 ) 27 28 func TestProfileDump(t *testing.T) { 29 testDataDir := filepath.Join(operatorRootDir, "cmd/mesh/testdata/profile-dump") 30 tests := []struct { 31 desc string 32 configPath string 33 }{ 34 { 35 desc: "all_off", 36 }, 37 { 38 desc: "config_path", 39 configPath: "components", 40 }, 41 { 42 desc: "list_path", 43 configPath: "values.gateways.istio-egressgateway.secretVolumes", 44 }, 45 } 46 installPackagePathRegex := regexp.MustCompile(" installPackagePath: .*") 47 for _, tt := range tests { 48 t.Run(tt.desc, func(t *testing.T) { 49 inPath := filepath.Join(testDataDir, "input", tt.desc+".yaml") 50 outPath := filepath.Join(testDataDir, "output", tt.desc+".yaml") 51 52 got, err := runProfileDump(inPath, tt.configPath, snapshotCharts, "") 53 if err != nil { 54 t.Fatal(err) 55 } 56 // installPackagePath may change, we will remove it for consistent output 57 got = installPackagePathRegex.ReplaceAllString(got, "") 58 59 if refreshGoldenFiles() { 60 t.Logf("Refreshing golden file for %s", outPath) 61 if err := os.WriteFile(outPath, []byte(got), 0o644); err != nil { 62 t.Error(err) 63 } 64 } 65 66 want, err := readFile(outPath) 67 if err != nil { 68 t.Fatal(err) 69 } 70 if !util.IsYAMLEqual(got, want) { 71 t.Errorf("profile-dump command(%s): got:\n%s\n\nwant:\n%s\nDiff:\n%s\n", tt.desc, got, want, util.YAMLDiff(got, want)) 72 } 73 }) 74 } 75 } 76 77 func runProfileDump(profilePath, configPath string, chartSource chartSourceType, outfmt string) (string, error) { 78 cmd := "profile dump -f " + profilePath 79 if configPath != "" { 80 cmd += " --config-path " + configPath 81 } 82 if len(chartSource) > 0 { 83 cmd += " --manifests=" + string(chartSource) 84 } 85 if outfmt != "" { 86 cmd += " --output=" + outfmt 87 } 88 return runCommand(cmd) 89 } 90 91 func TestProfileDumpFlags(t *testing.T) { 92 testDataDir := filepath.Join(operatorRootDir, "cmd/mesh/testdata/profile-dump") 93 tests := []struct { 94 desc string 95 configPath string 96 }{ 97 { 98 desc: "all_off", 99 }, 100 { 101 desc: "config_path", 102 configPath: "components", 103 }, 104 { 105 desc: "list_path", 106 configPath: "values.gateways.istio-egressgateway.secretVolumes", 107 }, 108 } 109 installPackagePathRegex := regexp.MustCompile("(?m)^installPackagePath=\".*\"\n") 110 for _, tt := range tests { 111 t.Run(tt.desc, func(t *testing.T) { 112 inPath := filepath.Join(testDataDir, "input", tt.desc+".yaml") 113 outPath := filepath.Join(testDataDir, "output", tt.desc+".txt") 114 115 got, err := runProfileDump(inPath, tt.configPath, snapshotCharts, "flags") 116 if err != nil { 117 t.Fatal(err) 118 } 119 // installPackagePath may change, we will remove it for consistent output 120 got = installPackagePathRegex.ReplaceAllString(got, "") 121 122 if refreshGoldenFiles() { 123 t.Logf("Refreshing golden file for %s", outPath) 124 if err := os.WriteFile(outPath, []byte(got), 0o644); err != nil { 125 t.Error(err) 126 } 127 } 128 129 want, err := readFile(outPath) 130 if err != nil { 131 t.Fatal(err) 132 } 133 if got != want { 134 t.Errorf("profile-dump command(%s): got:\n%s\n\nwant:\n%s\nDiff:\n%s\n", tt.desc, got, want, diff.Diff(got, want)) 135 } 136 }) 137 } 138 }