...

Source file src/sigs.k8s.io/kustomize/api/filters/filtersutil/setters_test.go

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

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package filtersutil_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"sigs.k8s.io/kustomize/api/filters/filtersutil"
    12  	"sigs.k8s.io/kustomize/kyaml/yaml"
    13  )
    14  
    15  func TestTrackableSetter_SetScalarIfEmpty(t *testing.T) {
    16  	tests := []struct {
    17  		name  string
    18  		input *yaml.RNode
    19  		value string
    20  		want  string
    21  	}{
    22  		{
    23  			name:  "sets null values",
    24  			input: yaml.MakeNullNode(),
    25  			value: "foo",
    26  			want:  "foo",
    27  		},
    28  		{
    29  			name:  "sets empty values",
    30  			input: yaml.NewScalarRNode(""),
    31  			value: "foo",
    32  			want:  "foo",
    33  		},
    34  		{
    35  			name:  "does not overwrite values",
    36  			input: yaml.NewStringRNode("a"),
    37  			value: "foo",
    38  			want:  "a",
    39  		},
    40  	}
    41  	for _, tt := range tests {
    42  		t.Run(tt.name, func(t *testing.T) {
    43  			wasSet := false
    44  			s := (&filtersutil.TrackableSetter{}).WithMutationTracker(func(_, _, _ string, _ *yaml.RNode) {
    45  				wasSet = true
    46  			})
    47  			wantSet := tt.value == tt.want
    48  			fn := s.SetScalarIfEmpty(tt.value)
    49  			require.NoError(t, fn(tt.input))
    50  			assert.Equal(t, tt.want, yaml.GetValue(tt.input))
    51  			assert.Equal(t, wantSet, wasSet, "tracker invoked even though value was not changed")
    52  		})
    53  	}
    54  }
    55  
    56  func TestTrackableSetter_SetEntryIfEmpty(t *testing.T) {
    57  	tests := []struct {
    58  		name  string
    59  		input *yaml.RNode
    60  		key   string
    61  		value string
    62  		want  string
    63  	}{
    64  		{
    65  			name:  "sets empty values",
    66  			input: yaml.NewMapRNode(&map[string]string{"setMe": ""}),
    67  			key:   "setMe",
    68  			value: "foo",
    69  			want:  "foo",
    70  		},
    71  		{
    72  			name:  "sets missing keys",
    73  			input: yaml.NewMapRNode(&map[string]string{}),
    74  			key:   "setMe",
    75  			value: "foo",
    76  			want:  "foo",
    77  		},
    78  		{
    79  			name:  "does not overwrite values",
    80  			input: yaml.NewMapRNode(&map[string]string{"existing": "original"}),
    81  			key:   "existing",
    82  			value: "foo",
    83  			want:  "original",
    84  		},
    85  	}
    86  	for _, tt := range tests {
    87  		t.Run(tt.name, func(t *testing.T) {
    88  			wasSet := false
    89  			s := (&filtersutil.TrackableSetter{}).WithMutationTracker(func(_, _, _ string, _ *yaml.RNode) {
    90  				wasSet = true
    91  			})
    92  			wantSet := tt.value == tt.want
    93  			fn := s.SetEntryIfEmpty(tt.key, tt.value, "")
    94  			require.NoError(t, fn(tt.input))
    95  			assert.Equal(t, tt.want, yaml.GetValue(tt.input.Field(tt.key).Value))
    96  			assert.Equal(t, wantSet, wasSet, "tracker invoked even though value was not changed")
    97  		})
    98  	}
    99  }
   100  
   101  func TestTrackableSetter_SetEntryIfEmpty_BadInputNodeKind(t *testing.T) {
   102  	fn := filtersutil.TrackableSetter{}.SetEntryIfEmpty("foo", "false", yaml.NodeTagBool)
   103  	rn := yaml.NewListRNode("nope")
   104  	rn.AppendToFieldPath("dummy", "path")
   105  	assert.EqualError(t, fn(rn), `wrong node kind: expected MappingNode but got SequenceNode: node contents:
   106  - nope
   107  `)
   108  }
   109  

View as plain text