istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/util/formatting/formatter_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 formatting 16 17 import ( 18 "testing" 19 20 . "github.com/onsi/gomega" 21 22 "istio.io/istio/pkg/config/analysis/diag" 23 "istio.io/istio/pkg/url" 24 ) 25 26 func TestFormatter_PrintLog(t *testing.T) { 27 g := NewWithT(t) 28 29 firstMsg := diag.NewMessage( 30 diag.NewMessageType(diag.Error, "B1", "Explosion accident: %v"), 31 diag.MockResource("SoapBubble"), 32 "the bubble is too big", 33 ) 34 secondMsg := diag.NewMessage( 35 diag.NewMessageType(diag.Warning, "C1", "Collapse danger: %v"), 36 diag.MockResource("GrandCastle"), 37 "the castle is too old", 38 ) 39 40 msgs := diag.Messages{firstMsg, secondMsg} 41 output, _ := Print(msgs, LogFormat, false) 42 43 g.Expect(output).To(Equal( 44 "Error [B1] (SoapBubble) Explosion accident: the bubble is too big\n" + 45 "Warning [C1] (GrandCastle) Collapse danger: the castle is too old", 46 )) 47 } 48 49 func TestFormatter_PrintLogWithColor(t *testing.T) { 50 g := NewWithT(t) 51 52 firstMsg := diag.NewMessage( 53 diag.NewMessageType(diag.Error, "B1", "Explosion accident: %v"), 54 diag.MockResource("SoapBubble"), 55 "the bubble is too big", 56 ) 57 secondMsg := diag.NewMessage( 58 diag.NewMessageType(diag.Warning, "C1", "Collapse danger: %v"), 59 diag.MockResource("GrandCastle"), 60 "the castle is too old", 61 ) 62 63 msgs := diag.Messages{firstMsg, secondMsg} 64 output, _ := Print(msgs, LogFormat, true) 65 66 g.Expect(output).To(Equal( 67 "\033[1;31mError\033[0m [B1] (SoapBubble) Explosion accident: the bubble is too big\n" + 68 "\033[33mWarning\033[0m [C1] (GrandCastle) Collapse danger: the castle is too old", 69 )) 70 } 71 72 func TestFormatter_PrintJSON(t *testing.T) { 73 g := NewWithT(t) 74 75 firstMsg := diag.NewMessage( 76 diag.NewMessageType(diag.Error, "B1", "Explosion accident: %v"), 77 diag.MockResource("SoapBubble"), 78 "the bubble is too big", 79 ) 80 secondMsg := diag.NewMessage( 81 diag.NewMessageType(diag.Warning, "C1", "Collapse danger: %v"), 82 diag.MockResource("GrandCastle"), 83 "the castle is too old", 84 ) 85 86 msgs := diag.Messages{firstMsg, secondMsg} 87 output, _ := Print(msgs, JSONFormat, false) 88 89 expectedOutput := `[ 90 { 91 "code": "B1", 92 "documentationUrl": "` + url.ConfigAnalysis + `/b1/", 93 "level": "Error", 94 "message": "Explosion accident: the bubble is too big", 95 "origin": "SoapBubble" 96 }, 97 { 98 "code": "C1", 99 "documentationUrl": "` + url.ConfigAnalysis + `/c1/", 100 "level": "Warning", 101 "message": "Collapse danger: the castle is too old", 102 "origin": "GrandCastle" 103 } 104 ]` 105 106 g.Expect(output).To(Equal(expectedOutput)) 107 } 108 109 func TestFormatter_PrintYAML(t *testing.T) { 110 g := NewWithT(t) 111 112 firstMsg := diag.NewMessage( 113 diag.NewMessageType(diag.Error, "B1", "Explosion accident: %v"), 114 diag.MockResource("SoapBubble"), 115 "the bubble is too big", 116 ) 117 secondMsg := diag.NewMessage( 118 diag.NewMessageType(diag.Warning, "C1", "Collapse danger: %v"), 119 diag.MockResource("GrandCastle"), 120 "the castle is too old", 121 ) 122 123 msgs := diag.Messages{firstMsg, secondMsg} 124 output, _ := Print(msgs, YAMLFormat, false) 125 126 expectedOutput := `- code: B1 127 documentationUrl: ` + url.ConfigAnalysis + `/b1/ 128 level: Error 129 message: 'Explosion accident: the bubble is too big' 130 origin: SoapBubble 131 - code: C1 132 documentationUrl: ` + url.ConfigAnalysis + `/c1/ 133 level: Warning 134 message: 'Collapse danger: the castle is too old' 135 origin: GrandCastle 136 ` 137 138 g.Expect(output).To(Equal(expectedOutput)) 139 } 140 141 func TestFormatter_PrintEmpty(t *testing.T) { 142 g := NewWithT(t) 143 144 msgs := diag.Messages{} 145 146 logOutput, _ := Print(msgs, LogFormat, false) 147 g.Expect(logOutput).To(Equal("")) 148 149 jsonOutput, _ := Print(msgs, JSONFormat, false) 150 g.Expect(jsonOutput).To(Equal("[]")) 151 152 yamlOutput, _ := Print(msgs, YAMLFormat, false) 153 g.Expect(yamlOutput).To(Equal("[]\n")) 154 }