istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/config/param/template_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 param_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  
    22  	"istio.io/istio/pkg/test/framework/components/echo/config/param"
    23  	"istio.io/istio/pkg/test/util/tmpl"
    24  )
    25  
    26  func TestContains(t *testing.T) {
    27  	cases := []struct {
    28  		name        string
    29  		template    string
    30  		expectFound bool
    31  	}{
    32  		{
    33  			name:        "empty",
    34  			template:    "",
    35  			expectFound: false,
    36  		},
    37  		{
    38  			name:        "wrong param",
    39  			template:    "{{ .Other.some.thing }}",
    40  			expectFound: false,
    41  		},
    42  		{
    43  			name:        "basic",
    44  			template:    "{{ .To }}",
    45  			expectFound: true,
    46  		},
    47  		{
    48  			name:        "with method calls",
    49  			template:    "{{ .To.some.thing }}",
    50  			expectFound: true,
    51  		},
    52  		{
    53  			name:        "if",
    54  			template:    "{{ if .To.some.thing }}{{ end }}",
    55  			expectFound: true,
    56  		},
    57  		{
    58  			name:        "range",
    59  			template:    "{{- range $key, $val := .To.some.thing }}{{ $key }}{{ $val }}{{- end }}",
    60  			expectFound: true,
    61  		},
    62  	}
    63  
    64  	for _, c := range cases {
    65  		t.Run(c.name, func(t *testing.T) {
    66  			tpl := param.Parse(tmpl.ParseOrFail(t, c.template))
    67  			actual := tpl.ContainsWellKnown(param.To)
    68  
    69  			g := NewWithT(t)
    70  			g.Expect(actual).To(Equal(c.expectFound))
    71  		})
    72  	}
    73  }