github.com/argoproj/argo-cd/v2@v2.10.5/test/e2e/app_autosync_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/argoproj/gitops-engine/pkg/sync/common"
     8  	"github.com/stretchr/testify/assert"
     9  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/types"
    11  
    12  	. "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    13  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
    14  	. "github.com/argoproj/argo-cd/v2/test/e2e/fixture/app"
    15  	"github.com/argoproj/argo-cd/v2/util/errors"
    16  )
    17  
    18  func TestAutoSyncSelfHealDisabled(t *testing.T) {
    19  	Given(t).
    20  		Path(guestbookPath).
    21  		When().
    22  		// app should be auto-synced once created
    23  		CreateFromFile(func(app *Application) {
    24  			app.Spec.SyncPolicy = &SyncPolicy{Automated: &SyncPolicyAutomated{SelfHeal: false}}
    25  		}).
    26  		Then().
    27  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    28  		// app should be auto-synced if git change detected
    29  		When().
    30  		PatchFile("guestbook-ui-deployment.yaml", `[{"op": "replace", "path": "/spec/revisionHistoryLimit", "value": 1}]`).
    31  		Refresh(RefreshTypeNormal).
    32  		Then().
    33  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    34  		// app should not be auto-synced if k8s change detected
    35  		When().
    36  		And(func() {
    37  			errors.FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
    38  				"guestbook-ui", types.MergePatchType, []byte(`{"spec": {"revisionHistoryLimit": 0}}`), v1.PatchOptions{}))
    39  		}).
    40  		Refresh(RefreshTypeNormal).
    41  		Then().
    42  		Expect(SyncStatusIs(SyncStatusCodeOutOfSync))
    43  }
    44  
    45  func TestAutoSyncSelfHealEnabled(t *testing.T) {
    46  	Given(t).
    47  		Path(guestbookPath).
    48  		When().
    49  		// app should be auto-synced once created
    50  		CreateFromFile(func(app *Application) {
    51  			app.Spec.SyncPolicy = &SyncPolicy{
    52  				Automated: &SyncPolicyAutomated{SelfHeal: true},
    53  				Retry:     &RetryStrategy{Limit: 0},
    54  			}
    55  		}).
    56  		Then().
    57  		Expect(OperationPhaseIs(OperationSucceeded)).
    58  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    59  		When().
    60  		// app should be auto-synced once k8s change detected
    61  		And(func() {
    62  			errors.FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
    63  				"guestbook-ui", types.MergePatchType, []byte(`{"spec": {"revisionHistoryLimit": 0}}`), v1.PatchOptions{}))
    64  		}).
    65  		Refresh(RefreshTypeNormal).
    66  		Then().
    67  		Expect(OperationPhaseIs(OperationSucceeded)).
    68  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    69  		When().
    70  		// app should be attempted to auto-synced once and marked with error after failed attempt detected
    71  		PatchFile("guestbook-ui-deployment.yaml", `[{"op": "replace", "path": "/spec/revisionHistoryLimit", "value": "badValue"}]`).
    72  		Refresh(RefreshTypeNormal).
    73  		Then().
    74  		Expect(OperationPhaseIs(OperationFailed)).
    75  		When().
    76  		// Trigger refresh again to make sure controller notices previously failed sync attempt before expectation timeout expires
    77  		Refresh(RefreshTypeNormal).
    78  		Then().
    79  		Expect(SyncStatusIs(SyncStatusCodeOutOfSync)).
    80  		Expect(Condition(ApplicationConditionSyncError, "Failed sync attempt")).
    81  		When().
    82  		// SyncError condition should be removed after successful sync
    83  		PatchFile("guestbook-ui-deployment.yaml", `[{"op": "replace", "path": "/spec/revisionHistoryLimit", "value": 1}]`).
    84  		Refresh(RefreshTypeNormal).
    85  		Then().
    86  		Expect(OperationPhaseIs(OperationSucceeded)).
    87  		When().
    88  		// Trigger refresh twice to make sure controller notices successful attempt and removes condition
    89  		Refresh(RefreshTypeNormal).
    90  		Then().
    91  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    92  		And(func(app *Application) {
    93  			assert.Len(t, app.Status.Conditions, 0)
    94  		})
    95  
    96  }