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

     1  package generators
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestValueInterpolation(t *testing.T) {
    10  	testCases := []struct {
    11  		name     string
    12  		values   map[string]string
    13  		params   map[string]interface{}
    14  		expected map[string]interface{}
    15  	}{
    16  		{
    17  			name: "Simple interpolation",
    18  			values: map[string]string{
    19  				"hello": "{{ world }}",
    20  			},
    21  			params: map[string]interface{}{
    22  				"world": "world!",
    23  			},
    24  			expected: map[string]interface{}{
    25  				"world":        "world!",
    26  				"values.hello": "world!",
    27  			},
    28  		},
    29  		{
    30  			name: "Non-existent",
    31  			values: map[string]string{
    32  				"non-existent": "{{ non-existent }}",
    33  			},
    34  			params: map[string]interface{}{},
    35  			expected: map[string]interface{}{
    36  				"values.non-existent": "{{ non-existent }}",
    37  			},
    38  		},
    39  		{
    40  			name: "Billion laughs",
    41  			values: map[string]string{
    42  				"lol1": "lol",
    43  				"lol2": "{{values.lol1}}{{values.lol1}}",
    44  				"lol3": "{{values.lol2}}{{values.lol2}}{{values.lol2}}",
    45  			},
    46  			params: map[string]interface{}{},
    47  			expected: map[string]interface{}{
    48  				"values.lol1": "lol",
    49  				"values.lol2": "{{values.lol1}}{{values.lol1}}",
    50  				"values.lol3": "{{values.lol2}}{{values.lol2}}{{values.lol2}}",
    51  			},
    52  		},
    53  	}
    54  
    55  	for _, testCase := range testCases {
    56  
    57  		t.Run(testCase.name, func(t *testing.T) {
    58  			err := appendTemplatedValues(testCase.values, testCase.params, false, nil)
    59  			assert.NoError(t, err)
    60  			assert.EqualValues(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]interface{}
    70  		expected map[string]interface{}
    71  	}{
    72  		{
    73  			name: "Simple interpolation",
    74  			values: map[string]string{
    75  				"hello": "{{ .world }}",
    76  			},
    77  			params: map[string]interface{}{
    78  				"world": "world!",
    79  			},
    80  			expected: map[string]interface{}{
    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]interface{}{},
    93  			expected: map[string]interface{}{
    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]interface{}{},
   107  			expected: map[string]interface{}{
   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  
   119  		t.Run(testCase.name, func(t *testing.T) {
   120  			err := appendTemplatedValues(testCase.values, testCase.params, true, nil)
   121  			assert.NoError(t, err)
   122  			assert.EqualValues(t, testCase.expected, testCase.params)
   123  		})
   124  	}
   125  }