istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/cmd/mesh/profile-diff_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 "bytes" 19 "fmt" 20 "os" 21 "strings" 22 "testing" 23 24 "istio.io/istio/operator/pkg/util/clog" 25 ) 26 27 type profileDiffTestcase struct { 28 args string 29 shouldFail bool 30 expectedString string // String output is expected to contain 31 notExpected string // String the output must NOT contain 32 } 33 34 func TestProfileDiff(t *testing.T) { 35 cases := []profileDiffTestcase{ 36 { 37 args: "profile diff demo default --unknown-flag", 38 shouldFail: true, 39 }, 40 { 41 args: "profile diff demo", 42 shouldFail: true, 43 }, 44 { 45 args: "profile diff", 46 shouldFail: true, 47 }, 48 { 49 args: fmt.Sprintf("profile diff default unknown-profile --manifests %s", snapshotCharts), 50 shouldFail: true, 51 }, 52 { 53 args: fmt.Sprintf("profile diff default default --manifests %s", snapshotCharts), 54 expectedString: "Profiles are identical", 55 }, 56 { 57 args: fmt.Sprintf("profile diff demo demo --manifests %s", snapshotCharts), 58 expectedString: "Profiles are identical", 59 }, 60 { 61 args: fmt.Sprintf("profile diff openshift openshift --manifests %s", snapshotCharts), 62 expectedString: "Profiles are identical", 63 }, 64 } 65 66 for i, c := range cases { 67 t.Run(fmt.Sprintf("case %d %q", i, c.args), func(t *testing.T) { 68 output, fErr := runCommand(c.args) 69 verifyProfileDiffCommandCaseOutput(t, c, output, fErr) 70 }) 71 } 72 } 73 74 func TestProfileDiffDirect(t *testing.T) { 75 cases := []profileDiffTestcase{ 76 { 77 // We won't be parsing with Cobra, but this is the command equivalent 78 args: "profile diff default openshift", 79 // This is just one of the many differences 80 expectedString: "+ profile: openshift", 81 // The profile doesn't change istiocoredns, so we shouldn't see this in the diff 82 notExpected: "- istiocoredns:", 83 // 'profile diff' "fails" so that the error level `$?` is 1, not 0, if there is a diff 84 shouldFail: false, 85 }, 86 } 87 88 l := clog.NewConsoleLogger(os.Stdout, os.Stderr, nil) 89 for i, c := range cases { 90 t.Run(fmt.Sprintf("case %d %q", i, c.args), func(t *testing.T) { 91 args := strings.Split(c.args, " ") 92 setFlag := fmt.Sprintf("installPackagePath=%s", snapshotCharts) 93 by := &bytes.Buffer{} 94 _, err := profileDiffInternal(args[len(args)-2], args[len(args)-1], []string{setFlag}, by, l) 95 verifyProfileDiffCommandCaseOutput(t, c, by.String(), err) 96 }) 97 } 98 } 99 100 func verifyProfileDiffCommandCaseOutput(t *testing.T, c profileDiffTestcase, output string, fErr error) { 101 t.Helper() 102 103 if c.expectedString != "" && !strings.Contains(output, c.expectedString) { 104 t.Fatalf("Output didn't match for 'istioctl %s'\n got %v\nwant: %v", c.args, output, c.expectedString) 105 } 106 107 if c.notExpected != "" && strings.Contains(output, c.notExpected) { 108 t.Fatalf("Output didn't match for 'istioctl %s'\n got %v\nDON'T want: %v", c.args, output, c.expectedString) 109 } 110 111 if c.shouldFail { 112 if fErr == nil { 113 t.Fatalf("Command should have failed for 'istioctl %s', didn't get one, output was %q", 114 c.args, output) 115 } 116 } else { 117 if fErr != nil { 118 t.Fatalf("Command should not have failed for 'istioctl %s': %v", c.args, fErr) 119 } 120 } 121 }