istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/analyzers/util/find_errorline_utils_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 util
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  
    23  	legacykube "istio.io/istio/pkg/config/analysis/legacy/source/kube"
    24  	"istio.io/istio/pkg/config/resource"
    25  )
    26  
    27  var fieldMap = map[string]int{
    28  	"{.metadata.name}":                                              1,
    29  	"{.metadata.namespace}":                                         1,
    30  	"{.metadata.annotations.test}":                                  1,
    31  	"{.spec.test[0].route[0].destination.host}":                     1,
    32  	"{.spec.http[0].mirror.host}":                                   1,
    33  	"{.spec.gateways[0]}":                                           1,
    34  	"{.spec.http[0].match[0].test.regex}":                           1,
    35  	"{.spec.http[0].match[0].test.test.regex}":                      1,
    36  	"{.spec.http[0].corsPolicy.allowOrigins[0].regex}":              1,
    37  	"{.spec.workloadSelector.labels.test}":                          1,
    38  	"{.spec.ports[0].port}":                                         1,
    39  	"{.spec.containers[0].image}":                                   1,
    40  	"{.spec.rules[0].from[0].source.namespaces[0]}":                 1,
    41  	"{.spec.selector.test}":                                         1,
    42  	"{.spec.servers[0].tls.credentialName}":                         1,
    43  	"{.networks.test.endpoints[0]}":                                 1,
    44  	"{.spec.trafficPolicy.tls.caCertificates}":                      1,
    45  	"{.spec.trafficPolicy.portLevelSettings[0].tls.caCertificates}": 1,
    46  	"{.spec.configPatches[0].patch.value}":                          1,
    47  }
    48  
    49  func TestExtractLabelFromSelectorString(t *testing.T) {
    50  	g := NewWithT(t)
    51  	s := "label=test"
    52  	g.Expect(ExtractLabelFromSelectorString(s)).To(Equal("label"))
    53  }
    54  
    55  func TestErrorLine(t *testing.T) {
    56  	g := NewWithT(t)
    57  	r := &resource.Instance{Origin: &legacykube.Origin{FieldsMap: fieldMap}}
    58  	test1, err1 := ErrorLine(r, "{.metadata.name}")
    59  	test2, err2 := ErrorLine(r, "{.metadata.fake}")
    60  	g.Expect(test1).To(Equal(1))
    61  	g.Expect(err1).To(Equal(true))
    62  	g.Expect(test2).To(Equal(0))
    63  	g.Expect(err2).To(Equal(false))
    64  }
    65  
    66  func TestConstants(t *testing.T) {
    67  	g := NewWithT(t)
    68  
    69  	constantsPath := []string{
    70  		fmt.Sprintf(DestinationHost, "test", 0, 0),
    71  		fmt.Sprintf(MirrorHost, 0),
    72  		fmt.Sprintf(VSGateway, 0),
    73  		fmt.Sprintf(URISchemeMethodAuthorityRegexMatch, 0, 0, "test"),
    74  		fmt.Sprintf(HeaderAndQueryParamsRegexMatch, 0, 0, "test", "test"),
    75  		fmt.Sprintf(AllowOriginsRegexMatch, 0, 0),
    76  		fmt.Sprintf(WorkloadSelector, "test"),
    77  		fmt.Sprintf(PortInPorts, 0),
    78  		fmt.Sprintf(FromRegistry, "test", 0),
    79  		fmt.Sprintf(ImageInContainer, 0),
    80  		fmt.Sprintf(AuthorizationPolicyNameSpace, 0, 0, 0),
    81  		fmt.Sprintf(Annotation, "test"),
    82  		fmt.Sprintf(GatewaySelector, "test"),
    83  		fmt.Sprintf(CredentialName, 0),
    84  		fmt.Sprintf(DestinationRuleTLSPortLevelCert, 0),
    85  		MetadataNamespace,
    86  		MetadataName,
    87  		DestinationRuleTLSCert,
    88  		fmt.Sprintf(EnvoyFilterConfigPath, 0),
    89  	}
    90  
    91  	for _, v := range constantsPath {
    92  		g.Expect(fieldMap[v]).To(Equal(1))
    93  	}
    94  }