github.com/argoproj/argo-cd/v3@v3.2.1/controller/sync_namespace_test.go (about)

     1  package controller
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
     9  	"k8s.io/apimachinery/pkg/types"
    10  
    11  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    12  )
    13  
    14  func createFakeNamespace(uid string, resourceVersion string, labels map[string]string, annotations map[string]string) *unstructured.Unstructured {
    15  	un := unstructured.Unstructured{}
    16  	un.SetUID(types.UID(uid))
    17  	un.SetResourceVersion(resourceVersion)
    18  	un.SetLabels(labels)
    19  	un.SetAnnotations(annotations)
    20  	un.SetKind("Namespace")
    21  	un.SetName("some-namespace")
    22  	return &un
    23  }
    24  
    25  func Test_shouldNamespaceSync(t *testing.T) {
    26  	tests := []struct {
    27  		name                string
    28  		syncPolicy          *v1alpha1.SyncPolicy
    29  		managedNs           *unstructured.Unstructured
    30  		liveNs              *unstructured.Unstructured
    31  		expected            bool
    32  		expectedLabels      map[string]string
    33  		expectedAnnotations map[string]string
    34  	}{
    35  		{
    36  			name:       "liveNs is nil and syncPolicy is nil",
    37  			expected:   false,
    38  			managedNs:  nil,
    39  			liveNs:     nil,
    40  			syncPolicy: nil,
    41  		},
    42  		{
    43  			name:      "liveNs is nil and syncPolicy is not nil",
    44  			expected:  false,
    45  			managedNs: nil,
    46  			liveNs:    nil,
    47  			syncPolicy: &v1alpha1.SyncPolicy{
    48  				ManagedNamespaceMetadata: nil,
    49  			},
    50  		},
    51  		{
    52  			name:                "liveNs is nil and syncPolicy has labels and annotations",
    53  			expected:            false,
    54  			managedNs:           nil,
    55  			liveNs:              nil,
    56  			expectedLabels:      map[string]string{"my-cool-label": "some-value"},
    57  			expectedAnnotations: map[string]string{"my-cool-annotation": "some-value"},
    58  			syncPolicy: &v1alpha1.SyncPolicy{
    59  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
    60  					Labels:      map[string]string{"my-cool-label": "some-value"},
    61  					Annotations: map[string]string{"my-cool-annotation": "some-value"},
    62  				},
    63  			},
    64  		},
    65  		{
    66  			name:                "namespace does not yet exist and managedNamespaceMetadata nil",
    67  			expected:            true,
    68  			expectedLabels:      map[string]string{},
    69  			expectedAnnotations: map[string]string{},
    70  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
    71  			liveNs:              nil,
    72  			syncPolicy: &v1alpha1.SyncPolicy{
    73  				ManagedNamespaceMetadata: nil,
    74  			},
    75  		},
    76  		{
    77  			name:                "namespace does not yet exist and managedNamespaceMetadata not nil",
    78  			expected:            true,
    79  			expectedAnnotations: map[string]string{"argocd.argoproj.io/sync-options": "ServerSideApply=true"},
    80  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
    81  			liveNs:              nil,
    82  			syncPolicy: &v1alpha1.SyncPolicy{
    83  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{},
    84  			},
    85  		},
    86  		{
    87  			name:                "namespace does not yet exist and managedNamespaceMetadata has empty labels map",
    88  			expected:            true,
    89  			expectedLabels:      map[string]string{},
    90  			expectedAnnotations: map[string]string{"argocd.argoproj.io/sync-options": "ServerSideApply=true"},
    91  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
    92  			liveNs:              nil,
    93  			syncPolicy: &v1alpha1.SyncPolicy{
    94  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
    95  					Labels: map[string]string{},
    96  				},
    97  			},
    98  		},
    99  		{
   100  			name:                "namespace does not yet exist and managedNamespaceMetadata has empty annotations map",
   101  			expected:            true,
   102  			expectedAnnotations: map[string]string{"argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   103  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   104  			liveNs:              nil,
   105  			syncPolicy: &v1alpha1.SyncPolicy{
   106  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   107  					Annotations: map[string]string{},
   108  				},
   109  			},
   110  		},
   111  		{
   112  			name:                "namespace does not yet exist and managedNamespaceMetadata has empty annotations and labels map",
   113  			expected:            true,
   114  			expectedLabels:      map[string]string{},
   115  			expectedAnnotations: map[string]string{"argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   116  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   117  			liveNs:              nil,
   118  			syncPolicy: &v1alpha1.SyncPolicy{
   119  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   120  					Labels:      map[string]string{},
   121  					Annotations: map[string]string{},
   122  				},
   123  			},
   124  		},
   125  		{
   126  			name:                "namespace does not yet exist and managedNamespaceMetadata has labels",
   127  			expected:            true,
   128  			expectedLabels:      map[string]string{"my-cool-label": "some-value"},
   129  			expectedAnnotations: map[string]string{"argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   130  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   131  			liveNs:              nil,
   132  			syncPolicy: &v1alpha1.SyncPolicy{
   133  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   134  					Labels:      map[string]string{"my-cool-label": "some-value"},
   135  					Annotations: nil,
   136  				},
   137  			},
   138  		},
   139  		{
   140  			name:                "namespace does not yet exist and managedNamespaceMetadata has annotations",
   141  			expected:            true,
   142  			expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   143  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   144  			liveNs:              nil,
   145  			syncPolicy: &v1alpha1.SyncPolicy{
   146  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   147  					Labels:      nil,
   148  					Annotations: map[string]string{"my-cool-annotation": "some-value"},
   149  				},
   150  			},
   151  		},
   152  		{
   153  			name:                "namespace does not yet exist and managedNamespaceMetadata has annotations and labels",
   154  			expected:            true,
   155  			expectedLabels:      map[string]string{"my-cool-label": "some-value"},
   156  			expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   157  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   158  			liveNs:              nil,
   159  			syncPolicy: &v1alpha1.SyncPolicy{
   160  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   161  					Labels:      map[string]string{"my-cool-label": "some-value"},
   162  					Annotations: map[string]string{"my-cool-annotation": "some-value"},
   163  				},
   164  			},
   165  		},
   166  		{
   167  			name:                "namespace exists with no labels or annotations and managedNamespaceMetadata has labels",
   168  			expected:            true,
   169  			expectedLabels:      map[string]string{"my-cool-label": "some-value"},
   170  			expectedAnnotations: map[string]string{"argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   171  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   172  			liveNs:              createFakeNamespace("something", "1", map[string]string{}, map[string]string{}),
   173  			syncPolicy: &v1alpha1.SyncPolicy{
   174  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   175  					Labels: map[string]string{"my-cool-label": "some-value"},
   176  				},
   177  			},
   178  		},
   179  		{
   180  			name:                "namespace exists with no labels or annotations and managedNamespaceMetadata has annotations",
   181  			expected:            true,
   182  			expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   183  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   184  			liveNs:              createFakeNamespace("something", "1", map[string]string{}, map[string]string{}),
   185  			syncPolicy: &v1alpha1.SyncPolicy{
   186  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   187  					Annotations: map[string]string{"my-cool-annotation": "some-value"},
   188  				},
   189  			},
   190  		},
   191  		{
   192  			name:                "namespace exists with no labels or annotations and managedNamespaceMetadata has annotations and labels",
   193  			expected:            true,
   194  			expectedLabels:      map[string]string{"my-cool-label": "some-value"},
   195  			expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   196  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   197  			liveNs:              createFakeNamespace("something", "1", map[string]string{}, map[string]string{}),
   198  			syncPolicy: &v1alpha1.SyncPolicy{
   199  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   200  					Labels:      map[string]string{"my-cool-label": "some-value"},
   201  					Annotations: map[string]string{"my-cool-annotation": "some-value"},
   202  				},
   203  			},
   204  		},
   205  		{
   206  			name:                "namespace exists with labels and managedNamespaceMetadata has mismatching labels",
   207  			expected:            true,
   208  			expectedAnnotations: map[string]string{"argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   209  			expectedLabels:      map[string]string{"my-cool-label": "some-value", "my-other-label": "some-other-value"},
   210  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   211  			liveNs:              createFakeNamespace("something", "1", map[string]string{"my-cool-label": "some-value"}, map[string]string{}),
   212  			syncPolicy: &v1alpha1.SyncPolicy{
   213  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   214  					Labels:      map[string]string{"my-cool-label": "some-value", "my-other-label": "some-other-value"},
   215  					Annotations: map[string]string{},
   216  				},
   217  			},
   218  		},
   219  		{
   220  			name:                "namespace exists with annotations and managedNamespaceMetadata has mismatching annotations",
   221  			expected:            true,
   222  			expectedLabels:      map[string]string{},
   223  			expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   224  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   225  			liveNs:              createFakeNamespace("something", "1", map[string]string{}, map[string]string{"my-cool-annotation": "some-value", "my-other-annotation": "some-other-value"}),
   226  			syncPolicy: &v1alpha1.SyncPolicy{
   227  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   228  					Labels:      map[string]string{},
   229  					Annotations: map[string]string{"my-cool-annotation": "some-value"},
   230  				},
   231  			},
   232  		},
   233  		{
   234  			name:                "namespace exists with annotations and labels managedNamespaceMetadata has mismatching annotations and labels",
   235  			expected:            true,
   236  			expectedLabels:      map[string]string{"my-cool-label": "some-value", "my-other-label": "some-other-value"},
   237  			expectedAnnotations: map[string]string{"my-cool-annotation": "some-value", "my-other-annotation": "some-other-value", "argocd.argoproj.io/sync-options": "ServerSideApply=true"},
   238  			managedNs:           createFakeNamespace("", "", map[string]string{}, map[string]string{}),
   239  			liveNs:              createFakeNamespace("something", "1", map[string]string{"my-cool-label": "some-value"}, map[string]string{"my-cool-annotation": "some-value"}),
   240  			syncPolicy: &v1alpha1.SyncPolicy{
   241  				ManagedNamespaceMetadata: &v1alpha1.ManagedNamespaceMetadata{
   242  					Labels:      map[string]string{"my-cool-label": "some-value", "my-other-label": "some-other-value"},
   243  					Annotations: map[string]string{"my-cool-annotation": "some-value", "my-other-annotation": "some-other-value"},
   244  				},
   245  			},
   246  		},
   247  	}
   248  
   249  	for _, tt := range tests {
   250  		t.Run(tt.name, func(t *testing.T) {
   251  			actual, err := syncNamespace(tt.syncPolicy)(tt.managedNs, tt.liveNs)
   252  			require.NoError(t, err)
   253  
   254  			if tt.managedNs != nil {
   255  				assert.Equal(t, tt.expectedLabels, tt.managedNs.GetLabels())
   256  				assert.Equal(t, tt.expectedAnnotations, tt.managedNs.GetAnnotations())
   257  			}
   258  
   259  			assert.Equalf(t, tt.expected, actual, "syncNamespace(%v)", tt.syncPolicy)
   260  		})
   261  	}
   262  }