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

     1  package e2e
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/argoproj/gitops-engine/pkg/health"
     7  	. "github.com/argoproj/gitops-engine/pkg/sync/common"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	. "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    11  	"github.com/argoproj/argo-cd/test/e2e/fixture"
    12  	. "github.com/argoproj/argo-cd/test/e2e/fixture/app"
    13  	"github.com/argoproj/argo-cd/util/errors"
    14  )
    15  
    16  func TestKustomize2AppSource(t *testing.T) {
    17  
    18  	patchLabelMatchesFor := func(kind string) func(app *Application) {
    19  		return func(app *Application) {
    20  			name := "k2-patched-guestbook-ui-deploy1"
    21  			labelValue, err := fixture.Run(
    22  				"", "kubectl", "-n="+fixture.DeploymentNamespace(),
    23  				"get", kind, name,
    24  				"-ojsonpath={.metadata.labels.patched-by}")
    25  			assert.NoError(t, err)
    26  			assert.Equal(t, "argo-cd", labelValue, "wrong value of 'patched-by' label of %s %s", kind, name)
    27  		}
    28  	}
    29  
    30  	Given(t).
    31  		Path(guestbookPath).
    32  		NamePrefix("k2-").
    33  		NameSuffix("-deploy1").
    34  		When().
    35  		Create().
    36  		Then().
    37  		Expect(SyncStatusIs(SyncStatusCodeOutOfSync)).
    38  		When().
    39  		PatchApp(`[
    40  			{
    41  				"op": "replace",
    42  				"path": "/spec/source/kustomize/namePrefix",
    43  				"value": "k2-patched-"
    44  			},
    45  			{
    46  				"op": "add",
    47  				"path": "/spec/source/kustomize/commonLabels",
    48  				"value": {
    49  					"patched-by": "argo-cd"
    50  				}
    51  			}
    52  		]`).
    53  		Then().
    54  		Expect(Success("")).
    55  		When().
    56  		Sync().
    57  		Then().
    58  		Expect(Success("")).
    59  		Expect(OperationPhaseIs(OperationSucceeded)).
    60  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    61  		Expect(HealthIs(health.HealthStatusHealthy)).
    62  		And(patchLabelMatchesFor("Service")).
    63  		And(patchLabelMatchesFor("Deployment"))
    64  }
    65  
    66  // when we have a config map generator, AND the ignore annotation, it is ignored in the app's sync status
    67  func TestSyncStatusOptionIgnore(t *testing.T) {
    68  	var mapName string
    69  	Given(t).
    70  		Path("kustomize-cm-gen").
    71  		When().
    72  		Create().
    73  		Sync().
    74  		Then().
    75  		Expect(OperationPhaseIs(OperationSucceeded)).
    76  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    77  		Expect(HealthIs(health.HealthStatusHealthy)).
    78  		And(func(app *Application) {
    79  			resourceStatus := app.Status.Resources[0]
    80  			assert.Contains(t, resourceStatus.Name, "my-map-")
    81  			assert.Equal(t, SyncStatusCodeSynced, resourceStatus.Status)
    82  
    83  			mapName = resourceStatus.Name
    84  		}).
    85  		When().
    86  		// we now force generation of a second CM
    87  		PatchFile("kustomization.yaml", `[{"op": "replace", "path": "/configMapGenerator/0/literals/0", "value": "foo=baz"}]`).
    88  		Refresh(RefreshTypeHard).
    89  		Then().
    90  		// this is standard logging from the command - tough one - true statement
    91  		When().
    92  		Sync().
    93  		Then().
    94  		Expect(OperationPhaseIs(OperationSucceeded)).
    95  		// this is a key check - we expect the app to be healthy because, even though we have a resources that needs
    96  		// pruning, because it is annotated with IgnoreExtraneous it should not contribute to the sync status
    97  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    98  		Expect(HealthIs(health.HealthStatusHealthy)).
    99  		And(func(app *Application) {
   100  			assert.Equal(t, 2, len(app.Status.Resources))
   101  			// new map in-sync
   102  			{
   103  				resourceStatus := app.Status.Resources[0]
   104  				assert.Contains(t, resourceStatus.Name, "my-map-")
   105  				// make sure we've a new map with changed name
   106  				assert.NotEqual(t, mapName, resourceStatus.Name)
   107  				assert.Equal(t, SyncStatusCodeSynced, resourceStatus.Status)
   108  			}
   109  			// old map is out of sync
   110  			{
   111  				resourceStatus := app.Status.Resources[1]
   112  				assert.Equal(t, mapName, resourceStatus.Name)
   113  				assert.Equal(t, SyncStatusCodeOutOfSync, resourceStatus.Status)
   114  			}
   115  		})
   116  }
   117  
   118  // make sure we can create an app which has a SSH remote base
   119  func TestKustomizeSSHRemoteBase(t *testing.T) {
   120  	Given(t).
   121  		// not the best test, as we should have two remote repos both with the same SSH private key
   122  		SSHInsecureRepoURLAdded(true).
   123  		RepoURLType(fixture.RepoURLTypeSSH).
   124  		Path("ssh-kustomize-base").
   125  		When().
   126  		Create().
   127  		Sync().
   128  		Then().
   129  		Expect(OperationPhaseIs(OperationSucceeded)).
   130  		Expect(ResourceSyncStatusIs("ConfigMap", "my-map", SyncStatusCodeSynced))
   131  }
   132  
   133  // make sure we can create an app which has a SSH remote base
   134  func TestKustomizeDeclarativeInvalidApp(t *testing.T) {
   135  	Given(t).
   136  		Path("invalid-kustomize").
   137  		When().
   138  		Declarative("declarative-apps/app.yaml").
   139  		Then().
   140  		Expect(Success("")).
   141  		Expect(HealthIs(health.HealthStatusHealthy)).
   142  		Expect(SyncStatusIs(SyncStatusCodeUnknown)).
   143  		Expect(Condition(ApplicationConditionComparisonError, "invalid-kustomize/does-not-exist.yaml: no such file or directory"))
   144  }
   145  
   146  func TestKustomizeBuildOptionsLoadRestrictor(t *testing.T) {
   147  	Given(t).
   148  		Path(guestbookPath).
   149  		And(func() {
   150  			errors.FailOnErr(fixture.Run("", "kubectl", "patch", "cm", "argocd-cm",
   151  				"-n", fixture.ArgoCDNamespace,
   152  				"-p", `{ "data": { "kustomize.buildOptions": "--load_restrictor none" } }`))
   153  		}).
   154  		When().
   155  		PatchFile("kustomization.yaml", `[{"op": "replace", "path": "/resources/1", "value": "../guestbook_local/guestbook-ui-svc.yaml"}]`).
   156  		Create().
   157  		Sync().
   158  		Then().
   159  		Expect(OperationPhaseIs(OperationSucceeded)).
   160  		Expect(HealthIs(health.HealthStatusHealthy)).
   161  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
   162  		Given().
   163  		And(func() {
   164  			errors.FailOnErr(fixture.Run("", "kubectl", "patch", "cm", "argocd-cm",
   165  				"-n", fixture.ArgoCDNamespace,
   166  				"-p", `{ "data": { "kustomize.buildOptions": "" } }`))
   167  		})
   168  }
   169  
   170  // make sure we we can invoke the CLI to replace images
   171  func TestKustomizeImages(t *testing.T) {
   172  	Given(t).
   173  		Path("kustomize").
   174  		When().
   175  		Create().
   176  		// pass two flags to check the multi flag logic works
   177  		AppSet("--kustomize-image", "alpine:foo", "--kustomize-image", "alpine:bar").
   178  		Then().
   179  		And(func(app *Application) {
   180  			assert.Contains(t, app.Spec.Source.Kustomize.Images, KustomizeImage("alpine:bar"))
   181  		})
   182  }
   183  
   184  // make sure we we can invoke the CLI to set namesuffix
   185  func TestKustomizeNameSuffix(t *testing.T) {
   186  	Given(t).
   187  		Path("kustomize").
   188  		When().
   189  		Create().
   190  		AppSet("--namesuffix", "-suf").
   191  		Then().
   192  		And(func(app *Application) {
   193  			assert.Contains(t, app.Spec.Source.Kustomize.NameSuffix, "-suf")
   194  		})
   195  }
   196  
   197  // make sure we we can invoke the CLI to set and unset namesuffix and kustomize-image
   198  func TestKustomizeUnsetOverride(t *testing.T) {
   199  	Given(t).
   200  		Path("kustomize").
   201  		When().
   202  		Create().
   203  		AppSet("--namesuffix", "-suf").
   204  		Then().
   205  		And(func(app *Application) {
   206  			assert.Contains(t, app.Spec.Source.Kustomize.NameSuffix, "-suf")
   207  		}).
   208  		When().
   209  		AppUnSet("--namesuffix").
   210  		Then().
   211  		And(func(app *Application) {
   212  			assert.Nil(t, app.Spec.Source.Kustomize)
   213  		}).
   214  		When().
   215  		AppSet("--kustomize-image", "alpine:foo", "--kustomize-image", "alpine:bar").
   216  		Then().
   217  		And(func(app *Application) {
   218  			assert.Contains(t, app.Spec.Source.Kustomize.Images, KustomizeImage("alpine:bar"))
   219  		}).
   220  		When().
   221  		//AppUnSet("--kustomize-image=alpine").
   222  		AppUnSet("--kustomize-image", "alpine", "--kustomize-image", "alpine").
   223  		Then().
   224  		And(func(app *Application) {
   225  			assert.Nil(t, app.Spec.Source.Kustomize)
   226  		})
   227  }