github.com/argoproj/argo-cd/v3@v3.2.1/test/e2e/selective_sync_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/argoproj/gitops-engine/pkg/health"
     9  	. "github.com/argoproj/gitops-engine/pkg/sync/common"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	. "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    13  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
    14  	. "github.com/argoproj/argo-cd/v3/test/e2e/fixture/app"
    15  	"github.com/argoproj/argo-cd/v3/util/errors"
    16  	"github.com/argoproj/argo-cd/v3/util/rand"
    17  )
    18  
    19  // when you selectively sync, only selected resources should be synced, but the app will be out of sync
    20  func TestSelectiveSync(t *testing.T) {
    21  	Given(t).
    22  		Path("guestbook").
    23  		SelectedResource(":Service:guestbook-ui").
    24  		When().
    25  		CreateApp().
    26  		Sync().
    27  		Then().
    28  		Expect(Success("")).
    29  		Expect(OperationPhaseIs(OperationSucceeded)).
    30  		Expect(SyncStatusIs(SyncStatusCodeOutOfSync)).
    31  		Expect(ResourceHealthIs("Service", "guestbook-ui", health.HealthStatusHealthy)).
    32  		Expect(ResourceHealthIs("Deployment", "guestbook-ui", health.HealthStatusMissing))
    33  }
    34  
    35  // when running selective sync, hooks do not run
    36  // hooks don't run even if all resources are selected
    37  func TestSelectiveSyncDoesNotRunHooks(t *testing.T) {
    38  	Given(t).
    39  		Path("hook").
    40  		SelectedResource(":Pod:pod").
    41  		When().
    42  		CreateApp().
    43  		Sync().
    44  		Then().
    45  		Expect(Success("")).
    46  		Expect(OperationPhaseIs(OperationSucceeded)).
    47  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    48  		Expect(HealthIs(health.HealthStatusHealthy)).
    49  		Expect(ResourceHealthIs("Pod", "pod", health.HealthStatusHealthy)).
    50  		Expect(ResourceResultNumbering(1))
    51  }
    52  
    53  func TestSelectiveSyncWithoutNamespace(t *testing.T) {
    54  	selectedResourceNamespace := getNewNamespace(t)
    55  	defer func() {
    56  		if !t.Skipped() {
    57  			errors.NewHandler(t).FailOnErr(fixture.Run("", "kubectl", "delete", "namespace", selectedResourceNamespace))
    58  		}
    59  	}()
    60  	Given(t).
    61  		Prune(true).
    62  		Path("guestbook-with-namespace").
    63  		And(func() {
    64  			errors.NewHandler(t).FailOnErr(fixture.Run("", "kubectl", "create", "namespace", selectedResourceNamespace))
    65  		}).
    66  		SelectedResource("apps:Deployment:guestbook-ui").
    67  		When().
    68  		PatchFile("guestbook-ui-deployment-ns.yaml", fmt.Sprintf(`[{"op": "replace", "path": "/metadata/namespace", "value": %q}]`, selectedResourceNamespace)).
    69  		PatchFile("guestbook-ui-svc-ns.yaml", fmt.Sprintf(`[{"op": "replace", "path": "/metadata/namespace", "value": %q}]`, selectedResourceNamespace)).
    70  		CreateApp().
    71  		Sync().
    72  		Then().
    73  		Expect(Success("")).
    74  		Expect(OperationPhaseIs(OperationSucceeded)).
    75  		Expect(SyncStatusIs(SyncStatusCodeOutOfSync)).
    76  		Expect(ResourceHealthWithNamespaceIs("Deployment", "guestbook-ui", selectedResourceNamespace, health.HealthStatusHealthy)).
    77  		Expect(ResourceHealthWithNamespaceIs("Deployment", "guestbook-ui", fixture.DeploymentNamespace(), health.HealthStatusHealthy)).
    78  		Expect(ResourceSyncStatusWithNamespaceIs("Deployment", "guestbook-ui", selectedResourceNamespace, SyncStatusCodeSynced)).
    79  		Expect(ResourceSyncStatusWithNamespaceIs("Deployment", "guestbook-ui", fixture.DeploymentNamespace(), SyncStatusCodeSynced))
    80  }
    81  
    82  // In selectedResource to sync, namespace is provided
    83  func TestSelectiveSyncWithNamespace(t *testing.T) {
    84  	selectedResourceNamespace := getNewNamespace(t)
    85  	defer func() {
    86  		if !t.Skipped() {
    87  			errors.NewHandler(t).FailOnErr(fixture.Run("", "kubectl", "delete", "namespace", selectedResourceNamespace))
    88  		}
    89  	}()
    90  	Given(t).
    91  		Prune(true).
    92  		Path("guestbook-with-namespace").
    93  		And(func() {
    94  			errors.NewHandler(t).FailOnErr(fixture.Run("", "kubectl", "create", "namespace", selectedResourceNamespace))
    95  		}).
    96  		SelectedResource(fmt.Sprintf("apps:Deployment:%s/guestbook-ui", selectedResourceNamespace)).
    97  		When().
    98  		PatchFile("guestbook-ui-deployment-ns.yaml", fmt.Sprintf(`[{"op": "replace", "path": "/metadata/namespace", "value": %q}]`, selectedResourceNamespace)).
    99  		PatchFile("guestbook-ui-svc-ns.yaml", fmt.Sprintf(`[{"op": "replace", "path": "/metadata/namespace", "value": %q}]`, selectedResourceNamespace)).
   100  		CreateApp().
   101  		Sync().
   102  		Then().
   103  		Expect(Success("")).
   104  		Expect(OperationPhaseIs(OperationSucceeded)).
   105  		Expect(SyncStatusIs(SyncStatusCodeOutOfSync)).
   106  		Expect(ResourceHealthWithNamespaceIs("Deployment", "guestbook-ui", selectedResourceNamespace, health.HealthStatusHealthy)).
   107  		Expect(ResourceHealthWithNamespaceIs("Deployment", "guestbook-ui", fixture.DeploymentNamespace(), health.HealthStatusMissing)).
   108  		Expect(ResourceSyncStatusWithNamespaceIs("Deployment", "guestbook-ui", selectedResourceNamespace, SyncStatusCodeSynced)).
   109  		Expect(ResourceSyncStatusWithNamespaceIs("Deployment", "guestbook-ui", fixture.DeploymentNamespace(), SyncStatusCodeOutOfSync))
   110  }
   111  
   112  func getNewNamespace(t *testing.T) string {
   113  	t.Helper()
   114  	randStr, err := rand.String(5)
   115  	require.NoError(t, err)
   116  	postFix := "-" + strings.ToLower(randStr)
   117  	name := fixture.DnsFriendly(t.Name(), "")
   118  	return fixture.DnsFriendly("argocd-e2e-"+name, postFix)
   119  }