istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/diag/message_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 diag
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  
    24  	"istio.io/istio/pkg/config/resource"
    25  	"istio.io/istio/pkg/url"
    26  )
    27  
    28  func TestMessage_String(t *testing.T) {
    29  	g := NewWithT(t)
    30  	mt := NewMessageType(Error, "IST-0042", "Cheese type not found: %q")
    31  	m := NewMessage(mt, nil, "Feta")
    32  
    33  	g.Expect(m.String()).To(Equal(`Error [IST-0042] Cheese type not found: "Feta"`))
    34  }
    35  
    36  func TestMessageWithResource_String(t *testing.T) {
    37  	g := NewWithT(t)
    38  	mt := NewMessageType(Error, "IST-0042", "Cheese type not found: %q")
    39  	m := NewMessage(mt, &resource.Instance{Origin: testOrigin{name: "toppings/cheese", ref: testReference{"path/to/file"}}}, "Feta")
    40  
    41  	g.Expect(m.String()).To(Equal(`Error [IST-0042] (toppings/cheese path/to/file) Cheese type not found: "Feta"`))
    42  }
    43  
    44  func TestMessage_Unstructured(t *testing.T) {
    45  	g := NewWithT(t)
    46  	mt := NewMessageType(Error, "IST-0042", "Cheese type not found: %q")
    47  	m := NewMessage(mt, nil, "Feta")
    48  
    49  	g.Expect(m.Unstructured(true)).To(Not(HaveKey("origin")))
    50  	g.Expect(m.Unstructured(false)).To(Not(HaveKey("origin")))
    51  
    52  	m = NewMessage(mt, &resource.Instance{Origin: testOrigin{name: "toppings/cheese"}}, "Feta")
    53  
    54  	g.Expect(m.Unstructured(true)).To((HaveKey("origin")))
    55  	g.Expect(m.Unstructured(false)).To(Not(HaveKey("origin")))
    56  }
    57  
    58  func TestMessageWithDocRef(t *testing.T) {
    59  	g := NewWithT(t)
    60  	mt := NewMessageType(Error, "IST0042", "Cheese type not found: %q")
    61  	m := NewMessage(mt, nil, "Feta")
    62  	m.DocRef = "test-ref"
    63  	g.Expect(m.Unstructured(false)["documentationUrl"]).To(Equal(url.ConfigAnalysis + "/ist0042/?ref=test-ref"))
    64  }
    65  
    66  func TestMessage_JSON(t *testing.T) {
    67  	g := NewWithT(t)
    68  	mt := NewMessageType(Error, "IST0042", "Cheese type not found: %q")
    69  	m := NewMessage(mt, &resource.Instance{Origin: testOrigin{name: "toppings/cheese", ref: testReference{"path/to/file"}}}, "Feta")
    70  
    71  	j, _ := json.Marshal(&m)
    72  	g.Expect(string(j)).To(Equal(`{"code":"IST0042","documentationUrl":"` + url.ConfigAnalysis + `/ist0042/"` +
    73  		`,"level":"Error","message":"Cheese type not found: \"Feta\"","origin":"toppings/cheese","reference":"path/to/file"}`))
    74  }
    75  
    76  func TestMessage_ReplaceLine(t *testing.T) {
    77  	testCases := []string{"test.yaml", "test.yaml:1", "test.yaml:10", "test.yaml: 10", "test", "test:10", "123:10", "123"}
    78  	result := make([]string, 0)
    79  	g := NewGomegaWithT(t)
    80  	m := &Message{Line: 321}
    81  	for _, v := range testCases {
    82  		result = append(result, m.ReplaceLine(v))
    83  	}
    84  	g.Expect(result).To(Equal([]string{"test.yaml", "test.yaml:321", "test.yaml:321", "test.yaml:321", "test", "test:321", "123:321", "123"}))
    85  }
    86  
    87  func TestMessage_UnstructuredAnalysisMessageBase(t *testing.T) {
    88  	g := NewWithT(t)
    89  	mt := NewMessageType(Error, "IST0042", "Cheese type not found: %q")
    90  	m := NewMessage(mt, &resource.Instance{Origin: testOrigin{name: "toppings/cheese", ref: testReference{"path/to/file"}}}, "Feta")
    91  	m.DocRef = "test-ref"
    92  
    93  	mb := m.UnstructuredAnalysisMessageBase()
    94  	g.Expect(mb["documentationUrl"]).To(Equal(fmt.Sprintf("%s/%s/%s", url.ConfigAnalysis, "ist0042", "?ref=test-ref")))
    95  	g.Expect(mb["level"]).To(Equal("ERROR"))
    96  	g.Expect(mb["type"]).To(Equal(
    97  		map[string]any{
    98  			"code": "IST0042",
    99  		},
   100  	))
   101  }