github.com/argoproj/argo-cd@v1.8.7/test/e2e/sync_options_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	. "github.com/argoproj/gitops-engine/pkg/sync/common"
     9  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/types"
    11  
    12  	. "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    13  	"github.com/argoproj/argo-cd/test/e2e/fixture"
    14  	. "github.com/argoproj/argo-cd/test/e2e/fixture/app"
    15  	"github.com/argoproj/argo-cd/util/errors"
    16  )
    17  
    18  // TestSyncOptionsValidateFalse verifies we can disable validation during kubectl apply, using the
    19  // 'argocd.argoproj.io/sync-options: Validate=false' sync option
    20  func TestSyncOptionsValidateFalse(t *testing.T) {
    21  	Given(t).
    22  		Path("sync-options-validate-false").
    23  		When().
    24  		Create().
    25  		Sync().
    26  		Then().
    27  		Expect(OperationPhaseIs(OperationSucceeded))
    28  	// NOTE: it is a bug that we do not detect this as OutOfSync. This is because we
    29  	// are dropping fields as part of remarshalling. See: https://github.com/argoproj/argo-cd/issues/1787
    30  	// Expect(SyncStatusIs(SyncStatusCodeOutOfSync))
    31  }
    32  
    33  // TestSyncOptionsValidateTrue verifies when 'argocd.argoproj.io/sync-options: Validate=false' is
    34  // not present, then validation is performed and we fail during the apply
    35  func TestSyncOptionsValidateTrue(t *testing.T) {
    36  	// k3s does not validate at all, so this test does not work
    37  	if os.Getenv("ARGOCD_E2E_K3S") == "true" {
    38  		t.SkipNow()
    39  	}
    40  	Given(t).
    41  		Path("sync-options-validate-false").
    42  		When().
    43  		IgnoreErrors().
    44  		Create().
    45  		PatchFile("invalid-cm.yaml", `[{"op": "remove", "path": "/metadata/annotations"}]`).
    46  		Sync().
    47  		Then().
    48  		Expect(OperationPhaseIs(OperationFailed))
    49  }
    50  
    51  func TestSyncWithStatusIgnored(t *testing.T) {
    52  	Given(t).
    53  		Path(guestbookPath).
    54  		When().
    55  		And(func() {
    56  			fixture.SetResourceOverrides(map[string]ResourceOverride{
    57  				"/": {
    58  					IgnoreDifferences: OverrideIgnoreDiff{JSONPointers: []string{"/status"}},
    59  				},
    60  			})
    61  		}).
    62  		CreateFromFile(func(app *Application) {
    63  			app.Spec.SyncPolicy = &SyncPolicy{Automated: &SyncPolicyAutomated{SelfHeal: true}}
    64  		}).
    65  		Then().
    66  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    67  		// app should remain synced if git change detected
    68  		When().
    69  		PatchFile("guestbook-ui-deployment.yaml", `[{ "op": "add", "path": "/status", "value": { "observedGeneration": 1 }}]`).
    70  		Refresh(RefreshTypeNormal).
    71  		Then().
    72  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    73  		// app should remain synced if k8s change detected
    74  		When().
    75  		And(func() {
    76  			errors.FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(context.Background(),
    77  				"guestbook-ui", types.JSONPatchType, []byte(`[{ "op": "replace", "path": "/status/observedGeneration", "value": 2 }]`), v1.PatchOptions{}))
    78  		}).
    79  		Then().
    80  		Expect(SyncStatusIs(SyncStatusCodeSynced))
    81  }
    82  
    83  func TestSyncWithSkipHook(t *testing.T) {
    84  	Given(t).
    85  		Path(guestbookPath).
    86  		When().
    87  		CreateFromFile(func(app *Application) {
    88  			app.Spec.SyncPolicy = &SyncPolicy{Automated: &SyncPolicyAutomated{SelfHeal: true}}
    89  		}).
    90  		Then().
    91  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    92  		// app should remain synced when app has skipped annotation even if git change detected
    93  		When().
    94  		PatchFile("guestbook-ui-deployment.yaml", `[{ "op": "add", "path": "/metadata/annotations", "value": { "argocd.argoproj.io/hook": "Skip" }}]`).
    95  		PatchFile("guestbook-ui-deployment.yaml", `[{ "op": "replace", "path": "/spec/replicas", "value": 1 }]`).
    96  		Refresh(RefreshTypeNormal).
    97  		Then().
    98  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    99  		// app should not remain synced if skipped annotation removed
   100  		When().
   101  		PatchFile("guestbook-ui-deployment.yaml", `[{ "op": "remove", "path": "/metadata/annotations" }]`).
   102  		Refresh(RefreshTypeNormal).
   103  		Then().
   104  		Expect(SyncStatusIs(SyncStatusCodeOutOfSync))
   105  }