github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/admin/admin_test.go (about)

     1  package admin
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
     8  	"k8s.io/apimachinery/pkg/runtime"
     9  	dynfake "k8s.io/client-go/dynamic/fake"
    10  )
    11  
    12  func TestGetAdditionalNamespaces(t *testing.T) {
    13  	createArgoCDCmdCMWithKeys := func(data map[string]any) *unstructured.Unstructured {
    14  		return &unstructured.Unstructured{
    15  			Object: map[string]any{
    16  				"apiVersion": "v1",
    17  				"kind":       "ConfigMap",
    18  				"metadata": map[string]any{
    19  					"name":      "argocd-cmd-params-cm",
    20  					"namespace": "argocd",
    21  				},
    22  				"data": data,
    23  			},
    24  		}
    25  	}
    26  
    27  	testCases := []struct {
    28  		CmdParamsKeys map[string]any
    29  		expected      argocdAdditionalNamespaces
    30  		description   string
    31  	}{
    32  		{
    33  			description:   "empty configmap should return no additional namespaces",
    34  			CmdParamsKeys: map[string]any{},
    35  			expected:      argocdAdditionalNamespaces{applicationNamespaces: []string{}, applicationsetNamespaces: []string{}},
    36  		},
    37  		{
    38  			description:   "empty strings in respective keys in cm shoud return empty namespace list",
    39  			CmdParamsKeys: map[string]any{applicationsetNamespacesCmdParamsKey: "", applicationNamespacesCmdParamsKey: ""},
    40  			expected:      argocdAdditionalNamespaces{applicationNamespaces: []string{}, applicationsetNamespaces: []string{}},
    41  		},
    42  		{
    43  			description:   "when only one of the keys in the cm is set only correct respective list of namespaces should be returned",
    44  			CmdParamsKeys: map[string]any{applicationNamespacesCmdParamsKey: "foo, bar*"},
    45  			expected:      argocdAdditionalNamespaces{applicationsetNamespaces: []string{}, applicationNamespaces: []string{"foo", "bar*"}},
    46  		},
    47  		{
    48  			description:   "when only one of the keys in the cm is set only correct respective list of namespaces should be returned",
    49  			CmdParamsKeys: map[string]any{applicationsetNamespacesCmdParamsKey: "foo, bar*"},
    50  			expected:      argocdAdditionalNamespaces{applicationNamespaces: []string{}, applicationsetNamespaces: []string{"foo", "bar*"}},
    51  		},
    52  		{
    53  			description:   "whitespaces are removed for both multiple and single namespace",
    54  			CmdParamsKeys: map[string]any{applicationNamespacesCmdParamsKey: "  bar    ", applicationsetNamespacesCmdParamsKey: " foo , bar*  "},
    55  			expected:      argocdAdditionalNamespaces{applicationNamespaces: []string{"bar"}, applicationsetNamespaces: []string{"foo", "bar*"}},
    56  		},
    57  	}
    58  
    59  	for _, c := range testCases {
    60  		fakeDynClient := dynfake.NewSimpleDynamicClient(runtime.NewScheme(), createArgoCDCmdCMWithKeys(c.CmdParamsKeys))
    61  
    62  		argoCDClientsets := &argoCDClientsets{
    63  			configMaps: fakeDynClient.Resource(configMapResource).Namespace("argocd"),
    64  		}
    65  
    66  		result := getAdditionalNamespaces(t.Context(), argoCDClientsets.configMaps)
    67  		assert.Equal(t, c.expected, *result)
    68  	}
    69  }