github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/generators/list_test.go (about) 1 package generators 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 10 argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" 11 ) 12 13 func TestGenerateListParams(t *testing.T) { 14 testCases := []struct { 15 elements []apiextensionsv1.JSON 16 expected []map[string]interface{} 17 }{ 18 { 19 elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url"}`)}}, 20 expected: []map[string]interface{}{{"cluster": "cluster", "url": "url"}}, 21 }, { 22 elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url","values":{"foo":"bar"}}`)}}, 23 expected: []map[string]interface{}{{"cluster": "cluster", "url": "url", "values.foo": "bar"}}, 24 }, 25 } 26 27 for _, testCase := range testCases { 28 29 var listGenerator = NewListGenerator() 30 31 applicationSetInfo := argoprojiov1alpha1.ApplicationSet{ 32 ObjectMeta: metav1.ObjectMeta{ 33 Name: "set", 34 }, 35 Spec: argoprojiov1alpha1.ApplicationSetSpec{}, 36 } 37 38 got, err := listGenerator.GenerateParams(&argoprojiov1alpha1.ApplicationSetGenerator{ 39 List: &argoprojiov1alpha1.ListGenerator{ 40 Elements: testCase.elements, 41 }}, &applicationSetInfo) 42 43 assert.NoError(t, err) 44 assert.ElementsMatch(t, testCase.expected, got) 45 46 } 47 } 48 49 func TestGenerateListParamsGoTemplate(t *testing.T) { 50 testCases := []struct { 51 elements []apiextensionsv1.JSON 52 expected []map[string]interface{} 53 }{ 54 { 55 elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url"}`)}}, 56 expected: []map[string]interface{}{{"cluster": "cluster", "url": "url"}}, 57 }, { 58 elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url","values":{"foo":"bar"}}`)}}, 59 expected: []map[string]interface{}{{"cluster": "cluster", "url": "url", "values": map[string]interface{}{"foo": "bar"}}}, 60 }, 61 } 62 63 for _, testCase := range testCases { 64 65 var listGenerator = NewListGenerator() 66 67 applicationSetInfo := argoprojiov1alpha1.ApplicationSet{ 68 ObjectMeta: metav1.ObjectMeta{ 69 Name: "set", 70 }, 71 Spec: argoprojiov1alpha1.ApplicationSetSpec{ 72 GoTemplate: true, 73 }, 74 } 75 76 got, err := listGenerator.GenerateParams(&argoprojiov1alpha1.ApplicationSetGenerator{ 77 List: &argoprojiov1alpha1.ListGenerator{ 78 Elements: testCase.elements, 79 }}, &applicationSetInfo) 80 81 assert.NoError(t, err) 82 assert.ElementsMatch(t, testCase.expected, got) 83 } 84 }