istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/cmd/pilot-agent/config/config_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 config
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"google.golang.org/protobuf/testing/protocmp"
    23  	"google.golang.org/protobuf/types/known/durationpb"
    24  
    25  	meshconfig "istio.io/api/mesh/v1alpha1"
    26  	"istio.io/istio/pkg/config/mesh"
    27  )
    28  
    29  func TestGetMeshConfig(t *testing.T) {
    30  	meshOverride := `
    31  defaultConfig:
    32    discoveryAddress: foo:123
    33    controlPlaneAuthPolicy: NONE
    34    proxyMetadata:
    35      SOME: setting
    36    drainDuration: 1s`
    37  	proxyOverride := `discoveryAddress: foo:123
    38  proxyMetadata:
    39    SOME: setting
    40  drainDuration: 1s
    41  controlPlaneAuthPolicy: NONE`
    42  	overridesExpected := func() *meshconfig.ProxyConfig {
    43  		m := mesh.DefaultProxyConfig()
    44  		m.DiscoveryAddress = "foo:123"
    45  		m.ProxyMetadata = map[string]string{"SOME": "setting"}
    46  		m.DrainDuration = durationpb.New(time.Second)
    47  		m.ControlPlaneAuthPolicy = meshconfig.AuthenticationPolicy_NONE
    48  		return m
    49  	}()
    50  	cases := []struct {
    51  		name        string
    52  		annotation  string
    53  		environment string
    54  		file        string
    55  		expect      *meshconfig.ProxyConfig
    56  	}{
    57  		{
    58  			name: "Defaults",
    59  			expect: func() *meshconfig.ProxyConfig {
    60  				m := mesh.DefaultProxyConfig()
    61  				return m
    62  			}(),
    63  		},
    64  		{
    65  			name:       "Annotation Override",
    66  			annotation: proxyOverride,
    67  			expect:     overridesExpected,
    68  		},
    69  		{
    70  			name:   "File Override",
    71  			file:   meshOverride,
    72  			expect: overridesExpected,
    73  		},
    74  		{
    75  			name:        "Environment Override",
    76  			environment: proxyOverride,
    77  			expect:      overridesExpected,
    78  		},
    79  		{
    80  			// Hopefully no one actually has all three of these set in a real system, but we will still
    81  			// test them all together.
    82  			name: "Multiple Override",
    83  			// Order is file < env < annotation
    84  			file: `
    85  defaultConfig:
    86    discoveryAddress: file:123
    87    proxyMetadata:
    88      SOME: setting
    89    drainDuration: 1s
    90    extraStatTags: ["a"]
    91    proxyStatsMatcher:
    92      inclusionPrefixes: ["a"]
    93      inclusionSuffixes: ["b"]
    94      inclusionRegexps: ["c"]
    95    controlPlaneAuthPolicy: NONE`,
    96  			environment: `
    97  discoveryAddress: environment:123
    98  proxyMetadata:
    99  OTHER: option`,
   100  			annotation: `
   101  discoveryAddress: annotation:123
   102  proxyMetadata:
   103    ANNOTATION: something
   104  drainDuration: 5s
   105  extraStatTags: ["b"]
   106  proxyStatsMatcher:
   107    inclusionPrefixes: ["a"]
   108    inclusionSuffixes: ["e"]
   109    inclusionRegexps: ["f"]
   110  `,
   111  			expect: func() *meshconfig.ProxyConfig {
   112  				m := mesh.DefaultProxyConfig()
   113  				m.DiscoveryAddress = "annotation:123"
   114  				m.ProxyMetadata = map[string]string{"ANNOTATION": "something", "SOME": "setting"}
   115  				m.DrainDuration = durationpb.New(5 * time.Second)
   116  				m.ExtraStatTags = []string{"b"}
   117  				m.ProxyStatsMatcher = &meshconfig.ProxyConfig_ProxyStatsMatcher{}
   118  				m.ProxyStatsMatcher.InclusionPrefixes = []string{"a"}
   119  				m.ProxyStatsMatcher.InclusionSuffixes = []string{"e"}
   120  				m.ProxyStatsMatcher.InclusionRegexps = []string{"f"}
   121  				m.ControlPlaneAuthPolicy = meshconfig.AuthenticationPolicy_NONE
   122  				return m
   123  			}(),
   124  		},
   125  	}
   126  	for _, tt := range cases {
   127  		t.Run(tt.name, func(t *testing.T) {
   128  			proxyConfigEnv := tt.environment
   129  			got, err := getMeshConfig(tt.file, tt.annotation, proxyConfigEnv)
   130  			if err != nil {
   131  				t.Fatal(err)
   132  			}
   133  			if !cmp.Equal(got.DefaultConfig, tt.expect, protocmp.Transform()) {
   134  				t.Fatalf("got \n%v expected \n%v", got.DefaultConfig, tt.expect)
   135  			}
   136  		})
   137  	}
   138  }