github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/generators/value_interpolation_test.go (about)

     1  package generators
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestValueInterpolation(t *testing.T) {
    11  	testCases := []struct {
    12  		name     string
    13  		values   map[string]string
    14  		params   map[string]any
    15  		expected map[string]any
    16  	}{
    17  		{
    18  			name: "Simple interpolation",
    19  			values: map[string]string{
    20  				"hello": "{{ world }}",
    21  			},
    22  			params: map[string]any{
    23  				"world": "world!",
    24  			},
    25  			expected: map[string]any{
    26  				"world":        "world!",
    27  				"values.hello": "world!",
    28  			},
    29  		},
    30  		{
    31  			name: "Non-existent",
    32  			values: map[string]string{
    33  				"non-existent": "{{ non-existent }}",
    34  			},
    35  			params: map[string]any{},
    36  			expected: map[string]any{
    37  				"values.non-existent": "{{ non-existent }}",
    38  			},
    39  		},
    40  		{
    41  			name: "Billion laughs",
    42  			values: map[string]string{
    43  				"lol1": "lol",
    44  				"lol2": "{{values.lol1}}{{values.lol1}}",
    45  				"lol3": "{{values.lol2}}{{values.lol2}}{{values.lol2}}",
    46  			},
    47  			params: map[string]any{},
    48  			expected: map[string]any{
    49  				"values.lol1": "lol",
    50  				"values.lol2": "{{values.lol1}}{{values.lol1}}",
    51  				"values.lol3": "{{values.lol2}}{{values.lol2}}{{values.lol2}}",
    52  			},
    53  		},
    54  	}
    55  
    56  	for _, testCase := range testCases {
    57  		t.Run(testCase.name, func(t *testing.T) {
    58  			err := appendTemplatedValues(testCase.values, testCase.params, false, nil)
    59  			require.NoError(t, err)
    60  			assert.Equal(t, testCase.expected, testCase.params)
    61  		})
    62  	}
    63  }
    64  
    65  func TestValueInterpolationWithGoTemplating(t *testing.T) {
    66  	testCases := []struct {
    67  		name     string
    68  		values   map[string]string
    69  		params   map[string]any
    70  		expected map[string]any
    71  	}{
    72  		{
    73  			name: "Simple interpolation",
    74  			values: map[string]string{
    75  				"hello": "{{ .world }}",
    76  			},
    77  			params: map[string]any{
    78  				"world": "world!",
    79  			},
    80  			expected: map[string]any{
    81  				"world": "world!",
    82  				"values": map[string]string{
    83  					"hello": "world!",
    84  				},
    85  			},
    86  		},
    87  		{
    88  			name: "Non-existent to default",
    89  			values: map[string]string{
    90  				"non_existent": "{{ default \"bar\" .non_existent }}",
    91  			},
    92  			params: map[string]any{},
    93  			expected: map[string]any{
    94  				"values": map[string]string{
    95  					"non_existent": "bar",
    96  				},
    97  			},
    98  		},
    99  		{
   100  			name: "Billion laughs",
   101  			values: map[string]string{
   102  				"lol1": "lol",
   103  				"lol2": "{{.values.lol1}}{{.values.lol1}}",
   104  				"lol3": "{{.values.lol2}}{{.values.lol2}}{{.values.lol2}}",
   105  			},
   106  			params: map[string]any{},
   107  			expected: map[string]any{
   108  				"values": map[string]string{
   109  					"lol1": "lol",
   110  					"lol2": "<no value><no value>",
   111  					"lol3": "<no value><no value><no value>",
   112  				},
   113  			},
   114  		},
   115  	}
   116  
   117  	for _, testCase := range testCases {
   118  		t.Run(testCase.name, func(t *testing.T) {
   119  			err := appendTemplatedValues(testCase.values, testCase.params, true, nil)
   120  			require.NoError(t, err)
   121  			assert.Equal(t, testCase.expected, testCase.params)
   122  		})
   123  	}
   124  }