istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/manifest/shared_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 manifest
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"istio.io/istio/operator/pkg/util"
    25  	"istio.io/istio/pkg/test/env"
    26  )
    27  
    28  func TestReadLayeredYAMLs(t *testing.T) {
    29  	testDataDir := filepath.Join(env.IstioSrc, "operator/pkg/util/testdata/yaml")
    30  	tests := []struct {
    31  		name     string
    32  		overlays []string
    33  		wantErr  bool
    34  		stdin    bool
    35  	}{
    36  		{
    37  			name:     "layer1",
    38  			overlays: []string{"yaml_layer1"},
    39  			wantErr:  false,
    40  		},
    41  		{
    42  			name:     "layer1_stdin",
    43  			overlays: []string{"yaml_layer1"},
    44  			wantErr:  false,
    45  			stdin:    true,
    46  		},
    47  		{
    48  			name:     "layer1_2",
    49  			overlays: []string{"yaml_layer1", "yaml_layer2"},
    50  			wantErr:  false,
    51  		},
    52  		{
    53  			name:     "layer1_2_3",
    54  			overlays: []string{"yaml_layer1", "yaml_layer2", "yaml_layer3"},
    55  			wantErr:  false,
    56  		},
    57  	}
    58  	for _, tt := range tests {
    59  		t.Run(fmt.Sprintf("%s stdin=%v", tt.name, tt.stdin), func(t *testing.T) {
    60  			inDir := filepath.Join(testDataDir, "input")
    61  			outPath := filepath.Join(testDataDir, "output", tt.name+".yaml")
    62  			wantBytes, err := os.ReadFile(outPath)
    63  			want := string(wantBytes)
    64  			if err != nil {
    65  				t.Errorf("os.ReadFile() error = %v, filename: %v", err, outPath)
    66  			}
    67  
    68  			stdinReader := &bytes.Buffer{}
    69  
    70  			var filenames []string
    71  			for _, ol := range tt.overlays {
    72  				filename := filepath.Join(inDir, ol+".yaml")
    73  				if tt.stdin {
    74  					b, err := os.ReadFile(filename)
    75  					if err != nil {
    76  						t.Fatalf("os.ReadFile() error = %v, filenaem: %v", err, filename)
    77  					}
    78  					if _, err := stdinReader.Write(b); err != nil {
    79  						t.Fatalf("failed to populate fake sdtin")
    80  					}
    81  					filenames = append(filenames, "-")
    82  				} else {
    83  					filenames = append(filenames, filename)
    84  				}
    85  			}
    86  			got, err := readLayeredYAMLs(filenames, stdinReader)
    87  			if (err != nil) != tt.wantErr {
    88  				t.Errorf("ReadLayeredYAMLs() error = %v, wantErr %v", err, tt.wantErr)
    89  				return
    90  			}
    91  
    92  			diff := util.YAMLDiff(got, want)
    93  			if diff != "" {
    94  				t.Errorf("ReadLayeredYAMLs() got:\n%s\nwant:\n%s\ndiff:\n%s", got, want, diff)
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  func TestConvertIOPMapValues(t *testing.T) {
   101  	testDataDir := filepath.Join(env.IstioSrc, "operator/pkg/util/testdata/yaml")
   102  	tests := []struct {
   103  		name         string
   104  		inputFlags   []string
   105  		convertPaths []string
   106  	}{
   107  		{
   108  			name:         "convention_boolean",
   109  			convertPaths: defaultSetFlagConvertPaths,
   110  			inputFlags: []string{
   111  				"meshConfig.defaultConfig.proxyMetadata.ISTIO_DUAL_STACK=false",
   112  				"meshConfig.defaultConfig.proxyMetadata.PROXY_XDS_VIA_AGENT=false",
   113  			},
   114  		}, {
   115  			name:         "convention_integer",
   116  			convertPaths: defaultSetFlagConvertPaths,
   117  			inputFlags: []string{
   118  				"meshConfig.defaultConfig.proxyMetadata.ISTIO_MULTI_CLUSTERS=10",
   119  				"meshConfig.defaultConfig.proxyMetadata.PROXY_XDS_LISTENERS=20",
   120  			},
   121  		}, {
   122  			name:         "convention_float",
   123  			convertPaths: defaultSetFlagConvertPaths,
   124  			inputFlags: []string{
   125  				"meshConfig.defaultConfig.proxyMetadata.PROXY_UPSTREAM_WEIGHT=0.85",
   126  				"meshConfig.defaultConfig.proxyMetadata.PROXY_DOWNSTREAM_WEIGHT=0.15",
   127  			},
   128  		},
   129  	}
   130  
   131  	for _, tt := range tests {
   132  		t.Run(tt.name, func(t *testing.T) {
   133  			inPath := filepath.Join(testDataDir, "input", tt.name+".yaml")
   134  			outPath := filepath.Join(testDataDir, "output", tt.name+".yaml")
   135  			input, err := os.ReadFile(inPath)
   136  			if err != nil {
   137  				t.Fatalf(err.Error())
   138  			}
   139  			actualOutput, err := convertIOPMapValues(string(input), tt.inputFlags, tt.convertPaths)
   140  			if err != nil {
   141  				t.Fatalf(err.Error())
   142  			}
   143  			expectOutput, err := os.ReadFile(outPath)
   144  			if err != nil {
   145  				t.Fatalf(err.Error())
   146  			}
   147  
   148  			diff := util.YAMLDiff(actualOutput, string(expectOutput))
   149  			if diff != "" {
   150  				t.Errorf("convertIOPMapValues() got:\n%s\nwant:\n%s\ndiff:\n%s", actualOutput, string(expectOutput), diff)
   151  			}
   152  		})
   153  	}
   154  }