github.com/argoproj/argo-cd@v1.8.7/controller/sync_test.go (about)

     1  package controller
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/argoproj/gitops-engine/pkg/utils/kube"
     9  	"github.com/stretchr/testify/assert"
    10  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    12  	"k8s.io/apimachinery/pkg/runtime"
    13  
    14  	"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    15  	"github.com/argoproj/argo-cd/reposerver/apiclient"
    16  	"github.com/argoproj/argo-cd/test"
    17  )
    18  
    19  func TestPersistRevisionHistory(t *testing.T) {
    20  	app := newFakeApp()
    21  	app.Status.OperationState = nil
    22  	app.Status.History = nil
    23  
    24  	defaultProject := &v1alpha1.AppProject{
    25  		ObjectMeta: v1.ObjectMeta{
    26  			Namespace: test.FakeArgoCDNamespace,
    27  			Name:      "default",
    28  		},
    29  	}
    30  	data := fakeData{
    31  		apps: []runtime.Object{app, defaultProject},
    32  		manifestResponse: &apiclient.ManifestResponse{
    33  			Manifests: []string{},
    34  			Namespace: test.FakeDestNamespace,
    35  			Server:    test.FakeClusterURL,
    36  			Revision:  "abc123",
    37  		},
    38  		managedLiveObjs: make(map[kube.ResourceKey]*unstructured.Unstructured),
    39  	}
    40  	ctrl := newFakeController(&data)
    41  
    42  	// Sync with source unspecified
    43  	opState := &v1alpha1.OperationState{Operation: v1alpha1.Operation{
    44  		Sync: &v1alpha1.SyncOperation{},
    45  	}}
    46  	ctrl.appStateManager.SyncAppState(app, opState)
    47  	// Ensure we record spec.source into sync result
    48  	assert.Equal(t, app.Spec.Source, opState.SyncResult.Source)
    49  
    50  	updatedApp, err := ctrl.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(context.Background(), app.Name, v1.GetOptions{})
    51  	assert.Nil(t, err)
    52  	assert.Equal(t, 1, len(updatedApp.Status.History))
    53  	assert.Equal(t, app.Spec.Source, updatedApp.Status.History[0].Source)
    54  	assert.Equal(t, "abc123", updatedApp.Status.History[0].Revision)
    55  }
    56  
    57  func TestPersistRevisionHistoryRollback(t *testing.T) {
    58  	app := newFakeApp()
    59  	app.Status.OperationState = nil
    60  	app.Status.History = nil
    61  	defaultProject := &v1alpha1.AppProject{
    62  		ObjectMeta: v1.ObjectMeta{
    63  			Namespace: test.FakeArgoCDNamespace,
    64  			Name:      "default",
    65  		},
    66  	}
    67  	data := fakeData{
    68  		apps: []runtime.Object{app, defaultProject},
    69  		manifestResponse: &apiclient.ManifestResponse{
    70  			Manifests: []string{},
    71  			Namespace: test.FakeDestNamespace,
    72  			Server:    test.FakeClusterURL,
    73  			Revision:  "abc123",
    74  		},
    75  		managedLiveObjs: make(map[kube.ResourceKey]*unstructured.Unstructured),
    76  	}
    77  	ctrl := newFakeController(&data)
    78  
    79  	// Sync with source specified
    80  	source := v1alpha1.ApplicationSource{
    81  		Helm: &v1alpha1.ApplicationSourceHelm{
    82  			Parameters: []v1alpha1.HelmParameter{
    83  				{
    84  					Name:  "test",
    85  					Value: "123",
    86  				},
    87  			},
    88  		},
    89  	}
    90  	opState := &v1alpha1.OperationState{Operation: v1alpha1.Operation{
    91  		Sync: &v1alpha1.SyncOperation{
    92  			Source: &source,
    93  		},
    94  	}}
    95  	ctrl.appStateManager.SyncAppState(app, opState)
    96  	// Ensure we record opState's source into sync result
    97  	assert.Equal(t, source, opState.SyncResult.Source)
    98  
    99  	updatedApp, err := ctrl.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(context.Background(), app.Name, v1.GetOptions{})
   100  	assert.Nil(t, err)
   101  	assert.Equal(t, 1, len(updatedApp.Status.History))
   102  	assert.Equal(t, source, updatedApp.Status.History[0].Source)
   103  	assert.Equal(t, "abc123", updatedApp.Status.History[0].Revision)
   104  }
   105  
   106  func TestSyncComparisonError(t *testing.T) {
   107  	app := newFakeApp()
   108  	app.Status.OperationState = nil
   109  	app.Status.History = nil
   110  
   111  	defaultProject := &v1alpha1.AppProject{
   112  		ObjectMeta: v1.ObjectMeta{
   113  			Namespace: test.FakeArgoCDNamespace,
   114  			Name:      "default",
   115  		},
   116  		Spec: v1alpha1.AppProjectSpec{
   117  			SignatureKeys: []v1alpha1.SignatureKey{{KeyID: "test"}},
   118  		},
   119  	}
   120  	data := fakeData{
   121  		apps: []runtime.Object{app, defaultProject},
   122  		manifestResponse: &apiclient.ManifestResponse{
   123  			Manifests:    []string{},
   124  			Namespace:    test.FakeDestNamespace,
   125  			Server:       test.FakeClusterURL,
   126  			Revision:     "abc123",
   127  			VerifyResult: "something went wrong",
   128  		},
   129  		managedLiveObjs: make(map[kube.ResourceKey]*unstructured.Unstructured),
   130  	}
   131  	ctrl := newFakeController(&data)
   132  
   133  	// Sync with source unspecified
   134  	opState := &v1alpha1.OperationState{Operation: v1alpha1.Operation{
   135  		Sync: &v1alpha1.SyncOperation{},
   136  	}}
   137  	os.Setenv("ARGOCD_GPG_ENABLED", "true")
   138  	defer os.Setenv("ARGOCD_GPG_ENABLED", "false")
   139  	ctrl.appStateManager.SyncAppState(app, opState)
   140  
   141  	conditions := app.Status.GetConditions(map[v1alpha1.ApplicationConditionType]bool{v1alpha1.ApplicationConditionComparisonError: true})
   142  	assert.NotEmpty(t, conditions)
   143  	assert.Equal(t, "abc123", opState.SyncResult.Revision)
   144  }