...

Source file src/sigs.k8s.io/kustomize/api/filters/refvar/refvar_test.go

Documentation: sigs.k8s.io/kustomize/api/filters/refvar

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package refvar_test
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	. "sigs.k8s.io/kustomize/api/filters/refvar"
    12  	filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest"
    13  	"sigs.k8s.io/kustomize/api/types"
    14  	"sigs.k8s.io/kustomize/kyaml/yaml"
    15  )
    16  
    17  var makeMf = func(theMap map[string]interface{}) MappingFunc {
    18  	ignored := make(map[string]int)
    19  	return MakePrimitiveReplacer(ignored, theMap)
    20  }
    21  
    22  func TestFilter(t *testing.T) {
    23  	testCases := map[string]struct {
    24  		input    string
    25  		expected string
    26  		filter   Filter
    27  	}{
    28  		"simple scalar": {
    29  			input: `
    30  apiVersion: apps/v1
    31  kind: Deployment
    32  metadata:
    33    name: dep
    34  spec:
    35    replicas: $(VAR)`,
    36  			expected: `
    37  apiVersion: apps/v1
    38  kind: Deployment
    39  metadata:
    40    name: dep
    41  spec:
    42    replicas: 5`,
    43  			filter: Filter{
    44  				MappingFunc: makeMf(map[string]interface{}{
    45  					"VAR": int64(5),
    46  				}),
    47  				FieldSpec: types.FieldSpec{Path: "spec/replicas"},
    48  			},
    49  		},
    50  		"non-string scalar": {
    51  			input: `
    52  apiVersion: apps/v1
    53  kind: Deployment
    54  metadata:
    55    name: dep
    56  spec:
    57    replicas: 1`,
    58  			expected: `
    59  apiVersion: apps/v1
    60  kind: Deployment
    61  metadata:
    62    name: dep
    63  spec:
    64    replicas: 1`,
    65  			filter: Filter{
    66  				MappingFunc: makeMf(map[string]interface{}{
    67  					"VAR": int64(5),
    68  				}),
    69  				FieldSpec: types.FieldSpec{Path: "spec/replicas"},
    70  			},
    71  		},
    72  		"wrong path": {
    73  			input: `
    74  apiVersion: apps/v1
    75  kind: Deployment
    76  metadata:
    77    name: dep
    78  spec:
    79    replicas: 1`,
    80  			expected: `
    81  apiVersion: apps/v1
    82  kind: Deployment
    83  metadata:
    84    name: dep
    85  spec:
    86    replicas: 1`,
    87  			filter: Filter{
    88  				MappingFunc: makeMf(map[string]interface{}{
    89  					"VAR": int64(5),
    90  				}),
    91  				FieldSpec: types.FieldSpec{Path: "a/b/c"},
    92  			},
    93  		},
    94  		"sequence": {
    95  			input: `
    96  apiVersion: apps/v1
    97  kind: Deployment
    98  metadata:
    99    name: dep
   100  data:
   101  - $(FOO)
   102  - $(BAR)
   103  - $(BAZ)
   104  - $(FOO)+$(BAR)
   105  - $(BOOL)
   106  - $(FLOAT)`,
   107  			expected: `
   108  apiVersion: apps/v1
   109  kind: Deployment
   110  metadata:
   111    name: dep
   112  data:
   113  - foo
   114  - bar
   115  - $(BAZ)
   116  - foo+bar
   117  - false
   118  - 1.23`,
   119  			filter: Filter{
   120  				MappingFunc: makeMf(map[string]interface{}{
   121  					"FOO":   "foo",
   122  					"BAR":   "bar",
   123  					"BOOL":  false,
   124  					"FLOAT": 1.23,
   125  				}),
   126  				FieldSpec: types.FieldSpec{Path: "data"},
   127  			},
   128  		},
   129  		"maps": {
   130  			input: `
   131  apiVersion: apps/v1
   132  kind: Deployment
   133  metadata:
   134    name: dep
   135  data:
   136    FOO: $(FOO)
   137    BAR: $(BAR)
   138    BAZ: $(BAZ)
   139    PLUS: $(FOO)+$(BAR)`,
   140  			expected: `
   141  apiVersion: apps/v1
   142  kind: Deployment
   143  metadata:
   144    name: dep
   145  data:
   146    FOO: foo
   147    BAR: bar
   148    BAZ: $(BAZ)
   149    PLUS: foo+bar`,
   150  			filter: Filter{
   151  				MappingFunc: makeMf(map[string]interface{}{
   152  					"FOO": "foo",
   153  					"BAR": "bar",
   154  				}),
   155  				FieldSpec: types.FieldSpec{Path: "data"},
   156  			},
   157  		},
   158  		"complicated case": {
   159  			input: `
   160  apiVersion: apps/v1
   161  kind: Deployment
   162  metadata:
   163    name: dep
   164  data:
   165    slice1:
   166    - $(FOO)
   167    slice2:
   168      FOO: $(FOO)
   169      BAR: $(BAR)
   170      BOOL: false
   171      INT: 0
   172      SLICE:
   173      - $(FOO)`,
   174  			expected: `
   175  apiVersion: apps/v1
   176  kind: Deployment
   177  metadata:
   178    name: dep
   179  data:
   180    slice1:
   181    - $(FOO)
   182    slice2:
   183      FOO: foo
   184      BAR: bar
   185      BOOL: false
   186      INT: 0
   187      SLICE:
   188      - $(FOO)`,
   189  			filter: Filter{
   190  				MappingFunc: makeMf(map[string]interface{}{
   191  					"FOO": "foo",
   192  					"BAR": "bar",
   193  				}),
   194  				FieldSpec: types.FieldSpec{Path: "data/slice2"},
   195  			},
   196  		},
   197  		"null value": {
   198  			input: `
   199  apiVersion: apps/v1
   200  kind: Deployment
   201  metadata:
   202    name: dep
   203  data:
   204    FOO: null`,
   205  			expected: `
   206  apiVersion: apps/v1
   207  kind: Deployment
   208  metadata:
   209    name: dep
   210  data:
   211    FOO: null`,
   212  			filter: Filter{
   213  				MappingFunc: makeMf(map[string]interface{}{
   214  					// no replacements!
   215  				}),
   216  				FieldSpec: types.FieldSpec{Path: "data/FOO"},
   217  			},
   218  		},
   219  	}
   220  
   221  	for tn, tc := range testCases {
   222  		t.Run(tn, func(t *testing.T) {
   223  			if !assert.Equal(t,
   224  				strings.TrimSpace(tc.expected),
   225  				strings.TrimSpace(
   226  					filtertest_test.RunFilter(t, tc.input, tc.filter))) {
   227  				t.FailNow()
   228  			}
   229  		})
   230  	}
   231  }
   232  
   233  func TestFilterUnhappy(t *testing.T) {
   234  	testCases := map[string]struct {
   235  		input         string
   236  		expectedError string
   237  		filter        Filter
   238  	}{
   239  		"non-string in sequence": {
   240  			input: `
   241  apiVersion: apps/v1
   242  kind: Deployment
   243  metadata:
   244    name: dep
   245  data:
   246    slice:
   247    - false`,
   248  			expectedError: `considering field 'data/slice' of object Deployment.v1.apps/dep.[noNs]: invalid value type expect a string`,
   249  			filter: Filter{
   250  				MappingFunc: makeMf(map[string]interface{}{
   251  					"VAR": int64(5),
   252  				}),
   253  				FieldSpec: types.FieldSpec{Path: "data/slice"},
   254  			},
   255  		},
   256  		"invalid key in map": {
   257  			input: `
   258  apiVersion: apps/v1
   259  kind: Deployment
   260  metadata:
   261    name: dep
   262  data:
   263    1: str`,
   264  			expectedError: `considering field 'data' of object Deployment.v1.apps/dep.[noNs]: invalid map key: value='1', tag='` + yaml.NodeTagInt + `'`,
   265  			filter: Filter{
   266  				MappingFunc: makeMf(map[string]interface{}{
   267  					"VAR": int64(5),
   268  				}),
   269  				FieldSpec: types.FieldSpec{Path: "data"},
   270  			},
   271  		},
   272  	}
   273  
   274  	for tn, tc := range testCases {
   275  		t.Run(tn, func(t *testing.T) {
   276  			_, err := filtertest_test.RunFilterE(t, tc.input, tc.filter)
   277  			if !assert.EqualError(t, err, tc.expectedError) {
   278  				t.FailNow()
   279  			}
   280  		})
   281  	}
   282  }
   283  

View as plain text