github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/generators/list_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  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	argoprojiov1alpha1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    12  )
    13  
    14  func TestGenerateListParams(t *testing.T) {
    15  	testCases := []struct {
    16  		elements []apiextensionsv1.JSON
    17  		expected []map[string]any
    18  	}{
    19  		{
    20  			elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url"}`)}},
    21  			expected: []map[string]any{{"cluster": "cluster", "url": "url"}},
    22  		}, {
    23  			elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url","values":{"foo":"bar"}}`)}},
    24  			expected: []map[string]any{{"cluster": "cluster", "url": "url", "values.foo": "bar"}},
    25  		},
    26  	}
    27  
    28  	for _, testCase := range testCases {
    29  		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  			},
    42  		}, &applicationSetInfo, nil)
    43  
    44  		require.NoError(t, err)
    45  		assert.ElementsMatch(t, testCase.expected, got)
    46  	}
    47  }
    48  
    49  func TestGenerateListParamsGoTemplate(t *testing.T) {
    50  	testCases := []struct {
    51  		elements []apiextensionsv1.JSON
    52  		expected []map[string]any
    53  	}{
    54  		{
    55  			elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url"}`)}},
    56  			expected: []map[string]any{{"cluster": "cluster", "url": "url"}},
    57  		}, {
    58  			elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url","values":{"foo":"bar"}}`)}},
    59  			expected: []map[string]any{{"cluster": "cluster", "url": "url", "values": map[string]any{"foo": "bar"}}},
    60  		},
    61  	}
    62  
    63  	for _, testCase := range testCases {
    64  		listGenerator := NewListGenerator()
    65  
    66  		applicationSetInfo := argoprojiov1alpha1.ApplicationSet{
    67  			ObjectMeta: metav1.ObjectMeta{
    68  				Name: "set",
    69  			},
    70  			Spec: argoprojiov1alpha1.ApplicationSetSpec{
    71  				GoTemplate: true,
    72  			},
    73  		}
    74  
    75  		got, err := listGenerator.GenerateParams(&argoprojiov1alpha1.ApplicationSetGenerator{
    76  			List: &argoprojiov1alpha1.ListGenerator{
    77  				Elements: testCase.elements,
    78  			},
    79  		}, &applicationSetInfo, nil)
    80  
    81  		require.NoError(t, err)
    82  		assert.ElementsMatch(t, testCase.expected, got)
    83  	}
    84  }