istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/compare/comparator_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 compare 16 17 import ( 18 "bytes" 19 "os" 20 "testing" 21 ) 22 23 // TestComparatorMatchingConfigs tests the scenario where Istiod and Envoy configurations match 24 func TestComparatorMatchingSameConfigs(t *testing.T) { 25 cfg, err := os.ReadFile("testdata/configdump.json") 26 if err != nil { 27 t.Fatalf("Failed to read test data: %v", err) 28 } 29 30 var outputBuffer bytes.Buffer 31 comparator, err := NewComparator(&outputBuffer, map[string][]byte{"default": cfg}, cfg) 32 if err != nil { 33 t.Fatalf("Failed to create Comparator: %v", err) 34 } 35 err = comparator.Diff() 36 if err != nil { 37 t.Errorf("Unexpected error during diff: %v", err) 38 } 39 40 expected := []string{"Clusters Match", "Listeners Match", "Routes Match"} 41 for _, exp := range expected { 42 if !bytes.Contains(outputBuffer.Bytes(), []byte(exp)) { 43 t.Errorf("Expected %s, but it was not found", exp) 44 } 45 } 46 } 47 48 // TestComparatorMismatchedConfigs tests the scenario where Istiod and Envoy configurations do not match 49 func TestComparatorMismatchedConfigs(t *testing.T) { 50 cfg, err := os.ReadFile("testdata/configdump.json") 51 if err != nil { 52 t.Fatalf("Failed to read test data: %v", err) 53 } 54 diffCfg, err := os.ReadFile("testdata/configdump_diff.json") 55 if err != nil { 56 t.Fatalf("Failed to read test data: %v", err) 57 } 58 59 var outputBuffer bytes.Buffer 60 comparator, err := NewComparator(&outputBuffer, map[string][]byte{"default": cfg}, diffCfg) 61 if err != nil { 62 t.Fatalf("Failed to create Comparator: %v", err) 63 } 64 err = comparator.Diff() 65 if err != nil { 66 t.Errorf("Unexpected error during diff: %v", err) 67 } 68 69 expectedNotMatch := []string{"Clusters Don't Match", "Listeners Don't Match", "Routes Don't Match"} 70 for _, exp := range expectedNotMatch { 71 if !bytes.Contains(outputBuffer.Bytes(), []byte(exp)) { 72 t.Errorf("Expected %s, but it was not found", exp) 73 } 74 } 75 }