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

     1  package controller
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  
    13  	"github.com/argoproj/argo-cd/v3/common"
    14  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    15  	"github.com/argoproj/argo-cd/v3/reposerver/apiclient"
    16  	"github.com/argoproj/argo-cd/v3/test"
    17  	"github.com/argoproj/argo-cd/v3/util/settings"
    18  )
    19  
    20  func TestGetRepoObjs(t *testing.T) {
    21  	cm := test.NewConfigMap()
    22  	cm.SetAnnotations(map[string]string{
    23  		"custom-annotation":             "custom-value",
    24  		common.AnnotationInstallationID: "id",     // tracking annotation should be removed
    25  		common.AnnotationKeyAppInstance: "my-app", // tracking annotation should be removed
    26  	})
    27  	cmBytes, _ := json.Marshal(cm)
    28  
    29  	app := newFakeApp()
    30  	// Enable the manifest-generate-paths annotation and set a synced revision
    31  	app.SetAnnotations(map[string]string{v1alpha1.AnnotationKeyManifestGeneratePaths: "."})
    32  	app.Status.Sync = v1alpha1.SyncStatus{
    33  		Revision: "abc123",
    34  		Status:   v1alpha1.SyncStatusCodeSynced,
    35  	}
    36  
    37  	data := fakeData{
    38  		manifestResponse: &apiclient.ManifestResponse{
    39  			Manifests: []string{string(cmBytes)},
    40  			Namespace: test.FakeDestNamespace,
    41  			Server:    test.FakeClusterURL,
    42  			Revision:  "abc123",
    43  		},
    44  	}
    45  
    46  	ctrl := newFakeControllerWithResync(&data, time.Minute, nil, errors.New("this should not be called"))
    47  	source := app.Spec.GetSource()
    48  	source.RepoURL = "oci://example.com/argo/argo-cd"
    49  
    50  	objs, resp, err := ctrl.GetRepoObjs(t.Context(), app, source, "abc123", &v1alpha1.AppProject{
    51  		ObjectMeta: metav1.ObjectMeta{
    52  			Name:      "default",
    53  			Namespace: test.FakeArgoCDNamespace,
    54  		},
    55  		Spec: v1alpha1.AppProjectSpec{
    56  			SourceRepos: []string{"*"},
    57  			Destinations: []v1alpha1.ApplicationDestination{
    58  				{
    59  					Server:    "*",
    60  					Namespace: "*",
    61  				},
    62  			},
    63  		},
    64  	})
    65  	require.NoError(t, err)
    66  	assert.NotNil(t, resp)
    67  	assert.Equal(t, "abc123", resp.Revision)
    68  	assert.Len(t, objs, 1)
    69  
    70  	annotations := objs[0].GetAnnotations()
    71  
    72  	// only the tracking annotations set by Argo CD should be removed
    73  	// and not the custom annotations set by user
    74  	require.NotNil(t, annotations)
    75  	assert.Equal(t, "custom-value", annotations["custom-annotation"])
    76  	assert.NotContains(t, annotations, common.AnnotationInstallationID)
    77  	assert.NotContains(t, annotations, common.AnnotationKeyAppInstance)
    78  
    79  	assert.Equal(t, "ConfigMap", objs[0].GetKind())
    80  }
    81  
    82  func TestGetHydratorCommitMessageTemplate_WhenTemplateisNotDefined_FallbackToDefault(t *testing.T) {
    83  	cm := test.NewConfigMap()
    84  	cmBytes, _ := json.Marshal(cm)
    85  
    86  	data := fakeData{
    87  		manifestResponse: &apiclient.ManifestResponse{
    88  			Manifests: []string{string(cmBytes)},
    89  			Namespace: test.FakeDestNamespace,
    90  			Server:    test.FakeClusterURL,
    91  			Revision:  "abc123",
    92  		},
    93  	}
    94  
    95  	ctrl := newFakeControllerWithResync(&data, time.Minute, nil, errors.New("this should not be called"))
    96  
    97  	tmpl, err := ctrl.GetHydratorCommitMessageTemplate()
    98  	require.NoError(t, err)
    99  	assert.NotEmpty(t, tmpl) // should fallback to default
   100  	assert.Equal(t, settings.CommitMessageTemplate, tmpl)
   101  }
   102  
   103  func TestGetHydratorCommitMessageTemplate(t *testing.T) {
   104  	cm := test.NewFakeConfigMap()
   105  	cm.Data["sourceHydrator.commitMessageTemplate"] = settings.CommitMessageTemplate
   106  	cmBytes, _ := json.Marshal(cm)
   107  
   108  	data := fakeData{
   109  		manifestResponse: &apiclient.ManifestResponse{
   110  			Manifests: []string{string(cmBytes)},
   111  			Namespace: test.FakeDestNamespace,
   112  			Server:    test.FakeClusterURL,
   113  			Revision:  "abc123",
   114  		},
   115  		configMapData: cm.Data,
   116  	}
   117  
   118  	ctrl := newFakeControllerWithResync(&data, time.Minute, nil, errors.New("this should not be called"))
   119  
   120  	tmpl, err := ctrl.GetHydratorCommitMessageTemplate()
   121  	require.NoError(t, err)
   122  	assert.NotEmpty(t, tmpl)
   123  }