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

     1  package e2e
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/hex"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/redis/go-redis/v9"
    11  	"github.com/stretchr/testify/require"
    12  	corev1 "k8s.io/api/core/v1"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  
    15  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    16  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
    17  
    18  	"github.com/argoproj/argo-cd/v3/pkg/apis/application"
    19  	. "github.com/argoproj/argo-cd/v3/test/e2e/fixture/applicationsets"
    20  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture/applicationsets/utils"
    21  )
    22  
    23  func randStr(t *testing.T) string {
    24  	t.Helper()
    25  	bytes := make([]byte, 16)
    26  	if _, err := rand.Read(bytes); err != nil {
    27  		require.NoError(t, err)
    28  		return ""
    29  	}
    30  	return hex.EncodeToString(bytes)
    31  }
    32  
    33  func TestSimpleGitDirectoryGenerator(t *testing.T) {
    34  	generateExpectedApp := func(name string) v1alpha1.Application {
    35  		return v1alpha1.Application{
    36  			TypeMeta: metav1.TypeMeta{
    37  				Kind:       application.ApplicationKind,
    38  				APIVersion: "argoproj.io/v1alpha1",
    39  			},
    40  			ObjectMeta: metav1.ObjectMeta{
    41  				Name:       name,
    42  				Namespace:  fixture.TestNamespace(),
    43  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
    44  			},
    45  			Spec: v1alpha1.ApplicationSpec{
    46  				Project: "default",
    47  				Source: &v1alpha1.ApplicationSource{
    48  					RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
    49  					TargetRevision: "HEAD",
    50  					Path:           name,
    51  				},
    52  				Destination: v1alpha1.ApplicationDestination{
    53  					Server:    "https://kubernetes.default.svc",
    54  					Namespace: name,
    55  				},
    56  			},
    57  		}
    58  	}
    59  
    60  	expectedApps := []v1alpha1.Application{
    61  		generateExpectedApp("kustomize-guestbook"),
    62  		generateExpectedApp("helm-guestbook"),
    63  	}
    64  
    65  	var expectedAppsNewNamespace []v1alpha1.Application
    66  	var expectedAppsNewMetadata []v1alpha1.Application
    67  
    68  	Given(t).
    69  		When().
    70  		// Create a GitGenerator-based ApplicationSet
    71  		Create(v1alpha1.ApplicationSet{
    72  			ObjectMeta: metav1.ObjectMeta{
    73  				Name: "simple-git-generator",
    74  			},
    75  			Spec: v1alpha1.ApplicationSetSpec{
    76  				Template: v1alpha1.ApplicationSetTemplate{
    77  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{path.basename}}"},
    78  					Spec: v1alpha1.ApplicationSpec{
    79  						Project: "default",
    80  						Source: &v1alpha1.ApplicationSource{
    81  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
    82  							TargetRevision: "HEAD",
    83  							Path:           "{{path}}",
    84  						},
    85  						Destination: v1alpha1.ApplicationDestination{
    86  							Server:    "https://kubernetes.default.svc",
    87  							Namespace: "{{path.basename}}",
    88  						},
    89  					},
    90  				},
    91  				Generators: []v1alpha1.ApplicationSetGenerator{
    92  					{
    93  						Git: &v1alpha1.GitGenerator{
    94  							RepoURL: "https://github.com/argoproj/argocd-example-apps.git",
    95  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
    96  								{
    97  									Path: "*guestbook*",
    98  								},
    99  							},
   100  						},
   101  					},
   102  				},
   103  			},
   104  		}).Then().Expect(ApplicationsExist(expectedApps)).
   105  
   106  		// Update the ApplicationSet template namespace, and verify it updates the Applications
   107  		When().
   108  		And(func() {
   109  			for _, expectedApp := range expectedApps {
   110  				newExpectedApp := expectedApp.DeepCopy()
   111  				newExpectedApp.Spec.Destination.Namespace = "guestbook2"
   112  				expectedAppsNewNamespace = append(expectedAppsNewNamespace, *newExpectedApp)
   113  			}
   114  		}).
   115  		Update(func(appset *v1alpha1.ApplicationSet) {
   116  			appset.Spec.Template.Spec.Destination.Namespace = "guestbook2"
   117  		}).Then().Expect(ApplicationsExist(expectedAppsNewNamespace)).
   118  
   119  		// Update the metadata fields in the appset template, and make sure it propagates to the apps
   120  		When().
   121  		And(func() {
   122  			for _, expectedApp := range expectedAppsNewNamespace {
   123  				expectedAppNewMetadata := expectedApp.DeepCopy()
   124  				expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
   125  				expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
   126  				expectedAppsNewMetadata = append(expectedAppsNewMetadata, *expectedAppNewMetadata)
   127  			}
   128  		}).
   129  		Update(func(appset *v1alpha1.ApplicationSet) {
   130  			appset.Spec.Template.Annotations = map[string]string{"annotation-key": "annotation-value"}
   131  			appset.Spec.Template.Labels = map[string]string{"label-key": "label-value"}
   132  		}).Then().Expect(ApplicationsExist(expectedAppsNewMetadata)).
   133  
   134  		// verify the ApplicationSet status conditions were set correctly
   135  		Expect(ApplicationSetHasConditions("simple-git-generator", ExpectedConditions)).
   136  
   137  		// Delete the ApplicationSet, and verify it deletes the Applications
   138  		When().
   139  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
   140  }
   141  
   142  func TestSimpleGitDirectoryGeneratorGoTemplate(t *testing.T) {
   143  	generateExpectedApp := func(name string) v1alpha1.Application {
   144  		return v1alpha1.Application{
   145  			TypeMeta: metav1.TypeMeta{
   146  				Kind:       application.ApplicationKind,
   147  				APIVersion: "argoproj.io/v1alpha1",
   148  			},
   149  			ObjectMeta: metav1.ObjectMeta{
   150  				Name:       name,
   151  				Namespace:  fixture.TestNamespace(),
   152  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
   153  			},
   154  			Spec: v1alpha1.ApplicationSpec{
   155  				Project: "default",
   156  				Source: &v1alpha1.ApplicationSource{
   157  					RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   158  					TargetRevision: "HEAD",
   159  					Path:           name,
   160  				},
   161  				Destination: v1alpha1.ApplicationDestination{
   162  					Server:    "https://kubernetes.default.svc",
   163  					Namespace: name,
   164  				},
   165  			},
   166  		}
   167  	}
   168  
   169  	expectedApps := []v1alpha1.Application{
   170  		generateExpectedApp("kustomize-guestbook"),
   171  		generateExpectedApp("helm-guestbook"),
   172  	}
   173  
   174  	var expectedAppsNewNamespace []v1alpha1.Application
   175  	var expectedAppsNewMetadata []v1alpha1.Application
   176  
   177  	Given(t).
   178  		When().
   179  		// Create a GitGenerator-based ApplicationSet
   180  		Create(v1alpha1.ApplicationSet{
   181  			ObjectMeta: metav1.ObjectMeta{
   182  				Name: "simple-git-generator",
   183  			},
   184  			Spec: v1alpha1.ApplicationSetSpec{
   185  				GoTemplate: true,
   186  				Template: v1alpha1.ApplicationSetTemplate{
   187  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{.path.basename}}"},
   188  					Spec: v1alpha1.ApplicationSpec{
   189  						Project: "default",
   190  						Source: &v1alpha1.ApplicationSource{
   191  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   192  							TargetRevision: "HEAD",
   193  							Path:           "{{.path.path}}",
   194  						},
   195  						Destination: v1alpha1.ApplicationDestination{
   196  							Server:    "https://kubernetes.default.svc",
   197  							Namespace: "{{.path.basename}}",
   198  						},
   199  					},
   200  				},
   201  				Generators: []v1alpha1.ApplicationSetGenerator{
   202  					{
   203  						Git: &v1alpha1.GitGenerator{
   204  							RepoURL: "https://github.com/argoproj/argocd-example-apps.git",
   205  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
   206  								{
   207  									Path: "*guestbook*",
   208  								},
   209  							},
   210  						},
   211  					},
   212  				},
   213  			},
   214  		}).Then().Expect(ApplicationsExist(expectedApps)).
   215  
   216  		// Update the ApplicationSet template namespace, and verify it updates the Applications
   217  		When().
   218  		And(func() {
   219  			for _, expectedApp := range expectedApps {
   220  				newExpectedApp := expectedApp.DeepCopy()
   221  				newExpectedApp.Spec.Destination.Namespace = "guestbook2"
   222  				expectedAppsNewNamespace = append(expectedAppsNewNamespace, *newExpectedApp)
   223  			}
   224  		}).
   225  		Update(func(appset *v1alpha1.ApplicationSet) {
   226  			appset.Spec.Template.Spec.Destination.Namespace = "guestbook2"
   227  		}).Then().Expect(ApplicationsExist(expectedAppsNewNamespace)).
   228  
   229  		// Update the metadata fields in the appset template, and make sure it propagates to the apps
   230  		When().
   231  		And(func() {
   232  			for _, expectedApp := range expectedAppsNewNamespace {
   233  				expectedAppNewMetadata := expectedApp.DeepCopy()
   234  				expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
   235  				expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
   236  				expectedAppsNewMetadata = append(expectedAppsNewMetadata, *expectedAppNewMetadata)
   237  			}
   238  		}).
   239  		Update(func(appset *v1alpha1.ApplicationSet) {
   240  			appset.Spec.Template.Annotations = map[string]string{"annotation-key": "annotation-value"}
   241  			appset.Spec.Template.Labels = map[string]string{"label-key": "label-value"}
   242  		}).Then().Expect(ApplicationsExist(expectedAppsNewMetadata)).
   243  
   244  		// verify the ApplicationSet status conditions were set correctly
   245  		Expect(ApplicationSetHasConditions("simple-git-generator", ExpectedConditions)).
   246  
   247  		// Delete the ApplicationSet, and verify it deletes the Applications
   248  		When().
   249  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
   250  }
   251  
   252  func TestSimpleGitDirectoryGeneratorGPGEnabledUnsignedCommits(t *testing.T) {
   253  	fixture.SkipOnEnv(t, "GPG")
   254  	expectedErrorMessage := `error generating params from git: error getting directories from repo: error retrieving Git Directories: rpc error: code = Unknown desc = permission denied`
   255  	expectedConditionsParamsError := []v1alpha1.ApplicationSetCondition{
   256  		{
   257  			Type:    v1alpha1.ApplicationSetConditionErrorOccurred,
   258  			Status:  v1alpha1.ApplicationSetConditionStatusTrue,
   259  			Message: expectedErrorMessage,
   260  			Reason:  v1alpha1.ApplicationSetReasonApplicationParamsGenerationError,
   261  		},
   262  		{
   263  			Type:    v1alpha1.ApplicationSetConditionParametersGenerated,
   264  			Status:  v1alpha1.ApplicationSetConditionStatusFalse,
   265  			Message: expectedErrorMessage,
   266  			Reason:  v1alpha1.ApplicationSetReasonErrorOccurred,
   267  		},
   268  		{
   269  			Type:    v1alpha1.ApplicationSetConditionResourcesUpToDate,
   270  			Status:  v1alpha1.ApplicationSetConditionStatusFalse,
   271  			Message: expectedErrorMessage,
   272  			Reason:  v1alpha1.ApplicationSetReasonErrorOccurred,
   273  		},
   274  	}
   275  	generateExpectedApp := func(name string) v1alpha1.Application {
   276  		return v1alpha1.Application{
   277  			TypeMeta: metav1.TypeMeta{
   278  				Kind:       application.ApplicationKind,
   279  				APIVersion: "argoproj.io/v1alpha1",
   280  			},
   281  			ObjectMeta: metav1.ObjectMeta{
   282  				Name:       name,
   283  				Namespace:  fixture.TestNamespace(),
   284  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
   285  			},
   286  			Spec: v1alpha1.ApplicationSpec{
   287  				Project: "default",
   288  				Source: &v1alpha1.ApplicationSource{
   289  					RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   290  					TargetRevision: "HEAD",
   291  					Path:           name,
   292  				},
   293  				Destination: v1alpha1.ApplicationDestination{
   294  					Server:    "https://kubernetes.default.svc",
   295  					Namespace: name,
   296  				},
   297  			},
   298  		}
   299  	}
   300  
   301  	expectedApps := []v1alpha1.Application{
   302  		generateExpectedApp("guestbook"),
   303  	}
   304  	project := "gpg"
   305  
   306  	Given(t).
   307  		When().
   308  		// Create a GitGenerator-based ApplicationSet
   309  		Create(v1alpha1.ApplicationSet{
   310  			ObjectMeta: metav1.ObjectMeta{
   311  				Name: "simple-git-generator",
   312  			},
   313  			Spec: v1alpha1.ApplicationSetSpec{
   314  				Template: v1alpha1.ApplicationSetTemplate{
   315  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{path.basename}}"},
   316  					Spec: v1alpha1.ApplicationSpec{
   317  						Project: project,
   318  						Source: &v1alpha1.ApplicationSource{
   319  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   320  							TargetRevision: "HEAD",
   321  							Path:           "{{path}}",
   322  						},
   323  						Destination: v1alpha1.ApplicationDestination{
   324  							Server:    "https://kubernetes.default.svc",
   325  							Namespace: "{{path.basename}}",
   326  						},
   327  					},
   328  				},
   329  				Generators: []v1alpha1.ApplicationSetGenerator{
   330  					{
   331  						Git: &v1alpha1.GitGenerator{
   332  							RepoURL: "https://github.com/argoproj/argocd-example-apps.git",
   333  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
   334  								{
   335  									Path: guestbookPath,
   336  								},
   337  							},
   338  						},
   339  					},
   340  				},
   341  			},
   342  		}).
   343  		Then().Expect(ApplicationsDoNotExist(expectedApps)).
   344  		// verify the ApplicationSet error status conditions were set correctly
   345  		Expect(ApplicationSetHasConditions("simple-git-generator", expectedConditionsParamsError)).
   346  		When().
   347  		Delete().Then().Expect(ApplicationsDoNotExist(expectedApps))
   348  }
   349  
   350  func TestSimpleGitDirectoryGeneratorGPGEnabledWithoutKnownKeys(t *testing.T) {
   351  	fixture.SkipOnEnv(t, "GPG")
   352  	expectedErrorMessage := `error generating params from git: error getting directories from repo: error retrieving Git Directories: rpc error: code = Unknown desc = permission denied`
   353  	expectedConditionsParamsError := []v1alpha1.ApplicationSetCondition{
   354  		{
   355  			Type:    v1alpha1.ApplicationSetConditionErrorOccurred,
   356  			Status:  v1alpha1.ApplicationSetConditionStatusTrue,
   357  			Message: expectedErrorMessage,
   358  			Reason:  v1alpha1.ApplicationSetReasonApplicationParamsGenerationError,
   359  		},
   360  		{
   361  			Type:    v1alpha1.ApplicationSetConditionParametersGenerated,
   362  			Status:  v1alpha1.ApplicationSetConditionStatusFalse,
   363  			Message: expectedErrorMessage,
   364  			Reason:  v1alpha1.ApplicationSetReasonErrorOccurred,
   365  		},
   366  		{
   367  			Type:    v1alpha1.ApplicationSetConditionResourcesUpToDate,
   368  			Status:  v1alpha1.ApplicationSetConditionStatusFalse,
   369  			Message: expectedErrorMessage,
   370  			Reason:  v1alpha1.ApplicationSetReasonErrorOccurred,
   371  		},
   372  	}
   373  	generateExpectedApp := func(name string) v1alpha1.Application {
   374  		return v1alpha1.Application{
   375  			TypeMeta: metav1.TypeMeta{
   376  				Kind:       application.ApplicationKind,
   377  				APIVersion: "argoproj.io/v1alpha1",
   378  			},
   379  			ObjectMeta: metav1.ObjectMeta{
   380  				Name:       name,
   381  				Namespace:  fixture.TestNamespace(),
   382  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
   383  			},
   384  			Spec: v1alpha1.ApplicationSpec{
   385  				Project: "default",
   386  				Source: &v1alpha1.ApplicationSource{
   387  					RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   388  					TargetRevision: "HEAD",
   389  					Path:           name,
   390  				},
   391  				Destination: v1alpha1.ApplicationDestination{
   392  					Server:    "https://kubernetes.default.svc",
   393  					Namespace: name,
   394  				},
   395  			},
   396  		}
   397  	}
   398  
   399  	expectedApps := []v1alpha1.Application{
   400  		generateExpectedApp("guestbook"),
   401  	}
   402  
   403  	project := "gpg"
   404  
   405  	Given(t).
   406  		Path(guestbookPath).
   407  		When().
   408  		AddSignedFile("test.yaml", randStr(t)).IgnoreErrors().
   409  		IgnoreErrors().
   410  		// Create a GitGenerator-based ApplicationSet
   411  		Create(v1alpha1.ApplicationSet{
   412  			ObjectMeta: metav1.ObjectMeta{
   413  				Name: "simple-git-generator",
   414  			},
   415  			Spec: v1alpha1.ApplicationSetSpec{
   416  				Template: v1alpha1.ApplicationSetTemplate{
   417  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{path.basename}}"},
   418  					Spec: v1alpha1.ApplicationSpec{
   419  						Project: project,
   420  						Source: &v1alpha1.ApplicationSource{
   421  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   422  							TargetRevision: "HEAD",
   423  							Path:           "{{path}}",
   424  						},
   425  						Destination: v1alpha1.ApplicationDestination{
   426  							Server:    "https://kubernetes.default.svc",
   427  							Namespace: "{{path.basename}}",
   428  						},
   429  						// Automatically create resources
   430  						SyncPolicy: &v1alpha1.SyncPolicy{
   431  							Automated: &v1alpha1.SyncPolicyAutomated{},
   432  						},
   433  					},
   434  				},
   435  				Generators: []v1alpha1.ApplicationSetGenerator{
   436  					{
   437  						Git: &v1alpha1.GitGenerator{
   438  							RepoURL: "https://github.com/argoproj/argocd-example-apps.git",
   439  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
   440  								{
   441  									Path: guestbookPath,
   442  								},
   443  							},
   444  						},
   445  					},
   446  				},
   447  			},
   448  		}).Then().
   449  		// verify the ApplicationSet error status conditions were set correctly
   450  		Expect(ApplicationSetHasConditions("simple-git-generator", expectedConditionsParamsError)).
   451  		Expect(ApplicationsDoNotExist(expectedApps)).
   452  		When().
   453  		Delete().Then().Expect(ApplicationsDoNotExist(expectedApps))
   454  }
   455  
   456  func TestSimpleGitFilesGenerator(t *testing.T) {
   457  	generateExpectedApp := func(name string) v1alpha1.Application {
   458  		return v1alpha1.Application{
   459  			TypeMeta: metav1.TypeMeta{
   460  				Kind:       application.ApplicationKind,
   461  				APIVersion: "argoproj.io/v1alpha1",
   462  			},
   463  			ObjectMeta: metav1.ObjectMeta{
   464  				Name:       name,
   465  				Namespace:  fixture.TestNamespace(),
   466  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
   467  			},
   468  			Spec: v1alpha1.ApplicationSpec{
   469  				Project: "default",
   470  				Source: &v1alpha1.ApplicationSource{
   471  					RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   472  					TargetRevision: "HEAD",
   473  					Path:           "guestbook",
   474  				},
   475  				Destination: v1alpha1.ApplicationDestination{
   476  					Server:    "https://kubernetes.default.svc",
   477  					Namespace: "guestbook",
   478  				},
   479  			},
   480  		}
   481  	}
   482  
   483  	expectedApps := []v1alpha1.Application{
   484  		generateExpectedApp("engineering-dev-guestbook"),
   485  		generateExpectedApp("engineering-prod-guestbook"),
   486  	}
   487  
   488  	var expectedAppsNewNamespace []v1alpha1.Application
   489  	var expectedAppsNewMetadata []v1alpha1.Application
   490  
   491  	Given(t).
   492  		When().
   493  		// Create a GitGenerator-based ApplicationSet
   494  		Create(v1alpha1.ApplicationSet{
   495  			ObjectMeta: metav1.ObjectMeta{
   496  				Name: "simple-git-generator",
   497  			},
   498  			Spec: v1alpha1.ApplicationSetSpec{
   499  				Template: v1alpha1.ApplicationSetTemplate{
   500  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{cluster.name}}-guestbook"},
   501  					Spec: v1alpha1.ApplicationSpec{
   502  						Project: "default",
   503  						Source: &v1alpha1.ApplicationSource{
   504  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   505  							TargetRevision: "HEAD",
   506  							Path:           "guestbook",
   507  						},
   508  						Destination: v1alpha1.ApplicationDestination{
   509  							Server:    "https://kubernetes.default.svc",
   510  							Namespace: "guestbook",
   511  						},
   512  					},
   513  				},
   514  				Generators: []v1alpha1.ApplicationSetGenerator{
   515  					{
   516  						Git: &v1alpha1.GitGenerator{
   517  							RepoURL: "https://github.com/argoproj/applicationset.git",
   518  							Files: []v1alpha1.GitFileGeneratorItem{
   519  								{
   520  									Path: "examples/git-generator-files-discovery/cluster-config/**/config.json",
   521  								},
   522  							},
   523  						},
   524  					},
   525  				},
   526  			},
   527  		}).Then().Expect(ApplicationsExist(expectedApps)).
   528  
   529  		// Update the ApplicationSet template namespace, and verify it updates the Applications
   530  		When().
   531  		And(func() {
   532  			for _, expectedApp := range expectedApps {
   533  				newExpectedApp := expectedApp.DeepCopy()
   534  				newExpectedApp.Spec.Destination.Namespace = "guestbook2"
   535  				expectedAppsNewNamespace = append(expectedAppsNewNamespace, *newExpectedApp)
   536  			}
   537  		}).
   538  		Update(func(appset *v1alpha1.ApplicationSet) {
   539  			appset.Spec.Template.Spec.Destination.Namespace = "guestbook2"
   540  		}).Then().Expect(ApplicationsExist(expectedAppsNewNamespace)).
   541  
   542  		// Update the metadata fields in the appset template, and make sure it propagates to the apps
   543  		When().
   544  		And(func() {
   545  			for _, expectedApp := range expectedAppsNewNamespace {
   546  				expectedAppNewMetadata := expectedApp.DeepCopy()
   547  				expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
   548  				expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
   549  				expectedAppsNewMetadata = append(expectedAppsNewMetadata, *expectedAppNewMetadata)
   550  			}
   551  		}).
   552  		Update(func(appset *v1alpha1.ApplicationSet) {
   553  			appset.Spec.Template.Annotations = map[string]string{"annotation-key": "annotation-value"}
   554  			appset.Spec.Template.Labels = map[string]string{"label-key": "label-value"}
   555  		}).Then().Expect(ApplicationsExist(expectedAppsNewMetadata)).
   556  
   557  		// verify the ApplicationSet status conditions were set correctly
   558  		Expect(ApplicationSetHasConditions("simple-git-generator", ExpectedConditions)).
   559  
   560  		// Delete the ApplicationSet, and verify it deletes the Applications
   561  		When().
   562  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
   563  }
   564  
   565  func TestSimpleGitFilesGeneratorGPGEnabledUnsignedCommits(t *testing.T) {
   566  	fixture.SkipOnEnv(t, "GPG")
   567  	expectedErrorMessage := `error generating params from git: error retrieving Git files: rpc error: code = Unknown desc = permission denied`
   568  	expectedConditionsParamsError := []v1alpha1.ApplicationSetCondition{
   569  		{
   570  			Type:    v1alpha1.ApplicationSetConditionErrorOccurred,
   571  			Status:  v1alpha1.ApplicationSetConditionStatusTrue,
   572  			Message: expectedErrorMessage,
   573  			Reason:  v1alpha1.ApplicationSetReasonApplicationParamsGenerationError,
   574  		},
   575  		{
   576  			Type:    v1alpha1.ApplicationSetConditionParametersGenerated,
   577  			Status:  v1alpha1.ApplicationSetConditionStatusFalse,
   578  			Message: expectedErrorMessage,
   579  			Reason:  v1alpha1.ApplicationSetReasonErrorOccurred,
   580  		},
   581  		{
   582  			Type:    v1alpha1.ApplicationSetConditionResourcesUpToDate,
   583  			Status:  v1alpha1.ApplicationSetConditionStatusFalse,
   584  			Message: expectedErrorMessage,
   585  			Reason:  v1alpha1.ApplicationSetReasonErrorOccurred,
   586  		},
   587  	}
   588  	project := "gpg"
   589  	generateExpectedApp := func(name string) v1alpha1.Application {
   590  		return v1alpha1.Application{
   591  			TypeMeta: metav1.TypeMeta{
   592  				Kind:       application.ApplicationKind,
   593  				APIVersion: "argoproj.io/v1alpha1",
   594  			},
   595  			ObjectMeta: metav1.ObjectMeta{
   596  				Name:       name,
   597  				Namespace:  fixture.TestNamespace(),
   598  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
   599  			},
   600  			Spec: v1alpha1.ApplicationSpec{
   601  				Project: project,
   602  				Source: &v1alpha1.ApplicationSource{
   603  					RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   604  					TargetRevision: "HEAD",
   605  					Path:           "guestbook",
   606  				},
   607  				Destination: v1alpha1.ApplicationDestination{
   608  					Server:    "https://kubernetes.default.svc",
   609  					Namespace: "guestbook",
   610  				},
   611  			},
   612  		}
   613  	}
   614  
   615  	expectedApps := []v1alpha1.Application{
   616  		generateExpectedApp("engineering-dev-guestbook"),
   617  		generateExpectedApp("engineering-prod-guestbook"),
   618  	}
   619  
   620  	Given(t).
   621  		When().
   622  		// Create a GitGenerator-based ApplicationSet
   623  		Create(v1alpha1.ApplicationSet{
   624  			ObjectMeta: metav1.ObjectMeta{
   625  				Name: "simple-git-generator",
   626  			},
   627  			Spec: v1alpha1.ApplicationSetSpec{
   628  				Template: v1alpha1.ApplicationSetTemplate{
   629  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{cluster.name}}-guestbook"},
   630  					Spec: v1alpha1.ApplicationSpec{
   631  						Project: project,
   632  						Source: &v1alpha1.ApplicationSource{
   633  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   634  							TargetRevision: "HEAD",
   635  							Path:           "guestbook",
   636  						},
   637  						Destination: v1alpha1.ApplicationDestination{
   638  							Server:    "https://kubernetes.default.svc",
   639  							Namespace: "guestbook",
   640  						},
   641  					},
   642  				},
   643  				Generators: []v1alpha1.ApplicationSetGenerator{
   644  					{
   645  						Git: &v1alpha1.GitGenerator{
   646  							RepoURL: "https://github.com/argoproj/applicationset.git",
   647  							Files: []v1alpha1.GitFileGeneratorItem{
   648  								{
   649  									Path: "examples/git-generator-files-discovery/cluster-config/**/config.json",
   650  								},
   651  							},
   652  						},
   653  					},
   654  				},
   655  			},
   656  		}).Then().Expect(ApplicationsDoNotExist(expectedApps)).
   657  		// verify the ApplicationSet error status conditions were set correctly
   658  		Expect(ApplicationSetHasConditions("simple-git-generator", expectedConditionsParamsError)).
   659  		When().
   660  		Delete().Then().Expect(ApplicationsDoNotExist(expectedApps))
   661  }
   662  
   663  func TestSimpleGitFilesGeneratorGPGEnabledWithoutKnownKeys(t *testing.T) {
   664  	fixture.SkipOnEnv(t, "GPG")
   665  	expectedErrorMessage := `error generating params from git: error retrieving Git files: rpc error: code = Unknown desc = permission denied`
   666  	expectedConditionsParamsError := []v1alpha1.ApplicationSetCondition{
   667  		{
   668  			Type:    v1alpha1.ApplicationSetConditionErrorOccurred,
   669  			Status:  v1alpha1.ApplicationSetConditionStatusTrue,
   670  			Message: expectedErrorMessage,
   671  			Reason:  v1alpha1.ApplicationSetReasonApplicationParamsGenerationError,
   672  		},
   673  		{
   674  			Type:    v1alpha1.ApplicationSetConditionParametersGenerated,
   675  			Status:  v1alpha1.ApplicationSetConditionStatusFalse,
   676  			Message: expectedErrorMessage,
   677  			Reason:  v1alpha1.ApplicationSetReasonErrorOccurred,
   678  		},
   679  		{
   680  			Type:    v1alpha1.ApplicationSetConditionResourcesUpToDate,
   681  			Status:  v1alpha1.ApplicationSetConditionStatusFalse,
   682  			Message: expectedErrorMessage,
   683  			Reason:  v1alpha1.ApplicationSetReasonErrorOccurred,
   684  		},
   685  	}
   686  	project := "gpg"
   687  	generateExpectedApp := func(name string) v1alpha1.Application {
   688  		return v1alpha1.Application{
   689  			TypeMeta: metav1.TypeMeta{
   690  				Kind:       application.ApplicationKind,
   691  				APIVersion: "argoproj.io/v1alpha1",
   692  			},
   693  			ObjectMeta: metav1.ObjectMeta{
   694  				Name:       name,
   695  				Namespace:  fixture.TestNamespace(),
   696  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
   697  			},
   698  			Spec: v1alpha1.ApplicationSpec{
   699  				Project: project,
   700  				Source: &v1alpha1.ApplicationSource{
   701  					RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   702  					TargetRevision: "HEAD",
   703  					Path:           "guestbook",
   704  				},
   705  				Destination: v1alpha1.ApplicationDestination{
   706  					Server:    "https://kubernetes.default.svc",
   707  					Namespace: "guestbook",
   708  				},
   709  			},
   710  		}
   711  	}
   712  
   713  	expectedApps := []v1alpha1.Application{
   714  		generateExpectedApp("engineering-dev-guestbook"),
   715  		generateExpectedApp("engineering-prod-guestbook"),
   716  	}
   717  
   718  	Given(t).
   719  		Path(guestbookPath).
   720  		When().
   721  		AddSignedFile("test.yaml", randStr(t)).IgnoreErrors().
   722  		IgnoreErrors().
   723  		// Create a GitGenerator-based ApplicationSet
   724  		Create(v1alpha1.ApplicationSet{
   725  			ObjectMeta: metav1.ObjectMeta{
   726  				Name: "simple-git-generator",
   727  			},
   728  			Spec: v1alpha1.ApplicationSetSpec{
   729  				Template: v1alpha1.ApplicationSetTemplate{
   730  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{cluster.name}}-guestbook"},
   731  					Spec: v1alpha1.ApplicationSpec{
   732  						Project: project,
   733  						Source: &v1alpha1.ApplicationSource{
   734  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   735  							TargetRevision: "HEAD",
   736  							Path:           "guestbook",
   737  						},
   738  						Destination: v1alpha1.ApplicationDestination{
   739  							Server:    "https://kubernetes.default.svc",
   740  							Namespace: "guestbook",
   741  						},
   742  					},
   743  				},
   744  				Generators: []v1alpha1.ApplicationSetGenerator{
   745  					{
   746  						Git: &v1alpha1.GitGenerator{
   747  							RepoURL: "https://github.com/argoproj/applicationset.git",
   748  							Files: []v1alpha1.GitFileGeneratorItem{
   749  								{
   750  									Path: "examples/git-generator-files-discovery/cluster-config/**/config.json",
   751  								},
   752  							},
   753  						},
   754  					},
   755  				},
   756  			},
   757  		}).Then().
   758  		// verify the ApplicationSet error status conditions were set correctly
   759  		Expect(ApplicationSetHasConditions("simple-git-generator", expectedConditionsParamsError)).
   760  		Expect(ApplicationsDoNotExist(expectedApps)).
   761  		When().
   762  		Delete().Then().Expect(ApplicationsDoNotExist(expectedApps))
   763  }
   764  
   765  func TestSimpleGitFilesGeneratorGoTemplate(t *testing.T) {
   766  	generateExpectedApp := func(name string) v1alpha1.Application {
   767  		return v1alpha1.Application{
   768  			TypeMeta: metav1.TypeMeta{
   769  				Kind:       application.ApplicationKind,
   770  				APIVersion: "argoproj.io/v1alpha1",
   771  			},
   772  			ObjectMeta: metav1.ObjectMeta{
   773  				Name:       name,
   774  				Namespace:  fixture.TestNamespace(),
   775  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
   776  			},
   777  			Spec: v1alpha1.ApplicationSpec{
   778  				Project: "default",
   779  				Source: &v1alpha1.ApplicationSource{
   780  					RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   781  					TargetRevision: "HEAD",
   782  					Path:           "guestbook",
   783  				},
   784  				Destination: v1alpha1.ApplicationDestination{
   785  					Server:    "https://kubernetes.default.svc",
   786  					Namespace: "guestbook",
   787  				},
   788  			},
   789  		}
   790  	}
   791  
   792  	expectedApps := []v1alpha1.Application{
   793  		generateExpectedApp("engineering-dev-guestbook"),
   794  		generateExpectedApp("engineering-prod-guestbook"),
   795  	}
   796  
   797  	var expectedAppsNewNamespace []v1alpha1.Application
   798  	var expectedAppsNewMetadata []v1alpha1.Application
   799  
   800  	Given(t).
   801  		When().
   802  		// Create a GitGenerator-based ApplicationSet
   803  		Create(v1alpha1.ApplicationSet{
   804  			ObjectMeta: metav1.ObjectMeta{
   805  				Name: "simple-git-generator",
   806  			},
   807  			Spec: v1alpha1.ApplicationSetSpec{
   808  				GoTemplate: true,
   809  				Template: v1alpha1.ApplicationSetTemplate{
   810  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{.cluster.name}}-guestbook"},
   811  					Spec: v1alpha1.ApplicationSpec{
   812  						Project: "default",
   813  						Source: &v1alpha1.ApplicationSource{
   814  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   815  							TargetRevision: "HEAD",
   816  							Path:           "guestbook",
   817  						},
   818  						Destination: v1alpha1.ApplicationDestination{
   819  							Server:    "https://kubernetes.default.svc",
   820  							Namespace: "guestbook",
   821  						},
   822  					},
   823  				},
   824  				Generators: []v1alpha1.ApplicationSetGenerator{
   825  					{
   826  						Git: &v1alpha1.GitGenerator{
   827  							RepoURL: "https://github.com/argoproj/applicationset.git",
   828  							Files: []v1alpha1.GitFileGeneratorItem{
   829  								{
   830  									Path: "examples/git-generator-files-discovery/cluster-config/**/config.json",
   831  								},
   832  							},
   833  						},
   834  					},
   835  				},
   836  			},
   837  		}).Then().Expect(ApplicationsExist(expectedApps)).
   838  
   839  		// Update the ApplicationSet template namespace, and verify it updates the Applications
   840  		When().
   841  		And(func() {
   842  			for _, expectedApp := range expectedApps {
   843  				newExpectedApp := expectedApp.DeepCopy()
   844  				newExpectedApp.Spec.Destination.Namespace = "guestbook2"
   845  				expectedAppsNewNamespace = append(expectedAppsNewNamespace, *newExpectedApp)
   846  			}
   847  		}).
   848  		Update(func(appset *v1alpha1.ApplicationSet) {
   849  			appset.Spec.Template.Spec.Destination.Namespace = "guestbook2"
   850  		}).Then().Expect(ApplicationsExist(expectedAppsNewNamespace)).
   851  
   852  		// Update the metadata fields in the appset template, and make sure it propagates to the apps
   853  		When().
   854  		And(func() {
   855  			for _, expectedApp := range expectedAppsNewNamespace {
   856  				expectedAppNewMetadata := expectedApp.DeepCopy()
   857  				expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
   858  				expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
   859  				expectedAppsNewMetadata = append(expectedAppsNewMetadata, *expectedAppNewMetadata)
   860  			}
   861  		}).
   862  		Update(func(appset *v1alpha1.ApplicationSet) {
   863  			appset.Spec.Template.Annotations = map[string]string{"annotation-key": "annotation-value"}
   864  			appset.Spec.Template.Labels = map[string]string{"label-key": "label-value"}
   865  		}).Then().Expect(ApplicationsExist(expectedAppsNewMetadata)).
   866  
   867  		// verify the ApplicationSet status conditions were set correctly
   868  		Expect(ApplicationSetHasConditions("simple-git-generator", ExpectedConditions)).
   869  
   870  		// Delete the ApplicationSet, and verify it deletes the Applications
   871  		When().
   872  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
   873  }
   874  
   875  func TestSimpleGitFilesPreserveResourcesOnDeletion(t *testing.T) {
   876  	Given(t).
   877  		When().
   878  		CreateNamespace(utils.ApplicationsResourcesNamespace).
   879  		// Create a GitGenerator-based ApplicationSet
   880  		Create(v1alpha1.ApplicationSet{
   881  			ObjectMeta: metav1.ObjectMeta{
   882  				Name: "simple-git-generator",
   883  			},
   884  			Spec: v1alpha1.ApplicationSetSpec{
   885  				Template: v1alpha1.ApplicationSetTemplate{
   886  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{cluster.name}}-guestbook"},
   887  					Spec: v1alpha1.ApplicationSpec{
   888  						Project: "default",
   889  						Source: &v1alpha1.ApplicationSource{
   890  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   891  							TargetRevision: "HEAD",
   892  							Path:           "guestbook",
   893  						},
   894  						Destination: v1alpha1.ApplicationDestination{
   895  							Server:    "https://kubernetes.default.svc",
   896  							Namespace: utils.ApplicationsResourcesNamespace,
   897  						},
   898  
   899  						// Automatically create resources
   900  						SyncPolicy: &v1alpha1.SyncPolicy{
   901  							Automated: &v1alpha1.SyncPolicyAutomated{},
   902  						},
   903  					},
   904  				},
   905  				SyncPolicy: &v1alpha1.ApplicationSetSyncPolicy{
   906  					PreserveResourcesOnDeletion: true,
   907  				},
   908  				Generators: []v1alpha1.ApplicationSetGenerator{
   909  					{
   910  						Git: &v1alpha1.GitGenerator{
   911  							RepoURL: "https://github.com/argoproj/applicationset.git",
   912  							Files: []v1alpha1.GitFileGeneratorItem{
   913  								{
   914  									Path: "examples/git-generator-files-discovery/cluster-config/**/config.json",
   915  								},
   916  							},
   917  						},
   918  					},
   919  				},
   920  			},
   921  			// We use an extra-long duration here, as we might need to wait for image pull.
   922  		}).Then().ExpectWithDuration(Pod(t, func(p corev1.Pod) bool { return strings.Contains(p.Name, "guestbook-ui") }), 6*time.Minute).
   923  		When().
   924  		Delete().
   925  		And(func() {
   926  			t.Log("Waiting 15 seconds to give the cluster a chance to delete the pods.")
   927  			// Wait 15 seconds to give the cluster a chance to deletes the pods, if it is going to do so.
   928  			// It should NOT delete the pods; to do so would be an ApplicationSet bug, and
   929  			// that is what we are testing here.
   930  			time.Sleep(15 * time.Second)
   931  			// The pod should continue to exist after 15 seconds.
   932  		}).Then().Expect(Pod(t, func(p corev1.Pod) bool { return strings.Contains(p.Name, "guestbook-ui") }))
   933  }
   934  
   935  func TestSimpleGitFilesPreserveResourcesOnDeletionGoTemplate(t *testing.T) {
   936  	Given(t).
   937  		When().
   938  		CreateNamespace(utils.ApplicationsResourcesNamespace).
   939  		// Create a GitGenerator-based ApplicationSet
   940  		Create(v1alpha1.ApplicationSet{
   941  			ObjectMeta: metav1.ObjectMeta{
   942  				Name: "simple-git-generator",
   943  			},
   944  			Spec: v1alpha1.ApplicationSetSpec{
   945  				GoTemplate: true,
   946  				Template: v1alpha1.ApplicationSetTemplate{
   947  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{.cluster.name}}-guestbook"},
   948  					Spec: v1alpha1.ApplicationSpec{
   949  						Project: "default",
   950  						Source: &v1alpha1.ApplicationSource{
   951  							RepoURL:        "https://github.com/argoproj/argocd-example-apps.git",
   952  							TargetRevision: "HEAD",
   953  							Path:           "guestbook",
   954  						},
   955  						Destination: v1alpha1.ApplicationDestination{
   956  							Server:    "https://kubernetes.default.svc",
   957  							Namespace: utils.ApplicationsResourcesNamespace,
   958  						},
   959  
   960  						// Automatically create resources
   961  						SyncPolicy: &v1alpha1.SyncPolicy{
   962  							Automated: &v1alpha1.SyncPolicyAutomated{},
   963  						},
   964  					},
   965  				},
   966  				SyncPolicy: &v1alpha1.ApplicationSetSyncPolicy{
   967  					PreserveResourcesOnDeletion: true,
   968  				},
   969  				Generators: []v1alpha1.ApplicationSetGenerator{
   970  					{
   971  						Git: &v1alpha1.GitGenerator{
   972  							RepoURL: "https://github.com/argoproj/applicationset.git",
   973  							Files: []v1alpha1.GitFileGeneratorItem{
   974  								{
   975  									Path: "examples/git-generator-files-discovery/cluster-config/**/config.json",
   976  								},
   977  							},
   978  						},
   979  					},
   980  				},
   981  			},
   982  			// We use an extra-long duration here, as we might need to wait for image pull.
   983  		}).Then().ExpectWithDuration(Pod(t, func(p corev1.Pod) bool { return strings.Contains(p.Name, "guestbook-ui") }), 6*time.Minute).
   984  		When().
   985  		Delete().
   986  		And(func() {
   987  			t.Log("Waiting 15 seconds to give the cluster a chance to delete the pods.")
   988  			// Wait 15 seconds to give the cluster a chance to deletes the pods, if it is going to do so.
   989  			// It should NOT delete the pods; to do so would be an ApplicationSet bug, and
   990  			// that is what we are testing here.
   991  			time.Sleep(15 * time.Second)
   992  			// The pod should continue to exist after 15 seconds.
   993  		}).Then().Expect(Pod(t, func(p corev1.Pod) bool { return strings.Contains(p.Name, "guestbook-ui") }))
   994  }
   995  
   996  func TestGitGeneratorPrivateRepo(t *testing.T) {
   997  	generateExpectedApp := func(name string) v1alpha1.Application {
   998  		return v1alpha1.Application{
   999  			TypeMeta: metav1.TypeMeta{
  1000  				Kind:       application.ApplicationKind,
  1001  				APIVersion: "argoproj.io/v1alpha1",
  1002  			},
  1003  			ObjectMeta: metav1.ObjectMeta{
  1004  				Name:       name,
  1005  				Namespace:  fixture.TestNamespace(),
  1006  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
  1007  			},
  1008  			Spec: v1alpha1.ApplicationSpec{
  1009  				Project: "default",
  1010  				Source: &v1alpha1.ApplicationSource{
  1011  					RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1012  					TargetRevision: "HEAD",
  1013  					Path:           name,
  1014  				},
  1015  				Destination: v1alpha1.ApplicationDestination{
  1016  					Server:    "https://kubernetes.default.svc",
  1017  					Namespace: name,
  1018  				},
  1019  			},
  1020  		}
  1021  	}
  1022  
  1023  	expectedApps := []v1alpha1.Application{
  1024  		generateExpectedApp("https-kustomize-base"),
  1025  	}
  1026  
  1027  	var expectedAppsNewNamespace []v1alpha1.Application
  1028  
  1029  	Given(t).
  1030  		HTTPSInsecureRepoURLAdded("").
  1031  		When().
  1032  		// Create a GitGenerator-based ApplicationSet
  1033  
  1034  		Create(v1alpha1.ApplicationSet{
  1035  			ObjectMeta: metav1.ObjectMeta{
  1036  				Name: "simple-git-generator-private",
  1037  			},
  1038  			Spec: v1alpha1.ApplicationSetSpec{
  1039  				Template: v1alpha1.ApplicationSetTemplate{
  1040  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{path.basename}}"},
  1041  					Spec: v1alpha1.ApplicationSpec{
  1042  						Project: "default",
  1043  						Source: &v1alpha1.ApplicationSource{
  1044  							RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1045  							TargetRevision: "HEAD",
  1046  							Path:           "{{path}}",
  1047  						},
  1048  						Destination: v1alpha1.ApplicationDestination{
  1049  							Server:    "https://kubernetes.default.svc",
  1050  							Namespace: "{{path.basename}}",
  1051  						},
  1052  					},
  1053  				},
  1054  				Generators: []v1alpha1.ApplicationSetGenerator{
  1055  					{
  1056  						Git: &v1alpha1.GitGenerator{
  1057  							RepoURL: fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1058  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
  1059  								{
  1060  									Path: "*kustomize*",
  1061  								},
  1062  							},
  1063  						},
  1064  					},
  1065  				},
  1066  			},
  1067  		}).Then().Expect(ApplicationsExist(expectedApps)).
  1068  		// Delete the ApplicationSet, and verify it deletes the Applications
  1069  		When().
  1070  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
  1071  }
  1072  
  1073  func TestGitGeneratorPrivateRepoGoTemplate(t *testing.T) {
  1074  	generateExpectedApp := func(name string) v1alpha1.Application {
  1075  		return v1alpha1.Application{
  1076  			TypeMeta: metav1.TypeMeta{
  1077  				Kind:       application.ApplicationKind,
  1078  				APIVersion: "argoproj.io/v1alpha1",
  1079  			},
  1080  			ObjectMeta: metav1.ObjectMeta{
  1081  				Name:       name,
  1082  				Namespace:  fixture.TestNamespace(),
  1083  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
  1084  			},
  1085  			Spec: v1alpha1.ApplicationSpec{
  1086  				Project: "default",
  1087  				Source: &v1alpha1.ApplicationSource{
  1088  					RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1089  					TargetRevision: "HEAD",
  1090  					Path:           name,
  1091  				},
  1092  				Destination: v1alpha1.ApplicationDestination{
  1093  					Server:    "https://kubernetes.default.svc",
  1094  					Namespace: name,
  1095  				},
  1096  			},
  1097  		}
  1098  	}
  1099  
  1100  	expectedApps := []v1alpha1.Application{
  1101  		generateExpectedApp("https-kustomize-base"),
  1102  	}
  1103  
  1104  	var expectedAppsNewNamespace []v1alpha1.Application
  1105  
  1106  	Given(t).
  1107  		HTTPSInsecureRepoURLAdded("").
  1108  		When().
  1109  		// Create a GitGenerator-based ApplicationSet
  1110  		Create(v1alpha1.ApplicationSet{
  1111  			ObjectMeta: metav1.ObjectMeta{
  1112  				Name: "simple-git-generator-private",
  1113  			},
  1114  			Spec: v1alpha1.ApplicationSetSpec{
  1115  				GoTemplate: true,
  1116  				Template: v1alpha1.ApplicationSetTemplate{
  1117  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{.path.basename}}"},
  1118  					Spec: v1alpha1.ApplicationSpec{
  1119  						Project: "default",
  1120  						Source: &v1alpha1.ApplicationSource{
  1121  							RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1122  							TargetRevision: "HEAD",
  1123  							Path:           "{{.path.path}}",
  1124  						},
  1125  						Destination: v1alpha1.ApplicationDestination{
  1126  							Server:    "https://kubernetes.default.svc",
  1127  							Namespace: "{{.path.basename}}",
  1128  						},
  1129  					},
  1130  				},
  1131  				Generators: []v1alpha1.ApplicationSetGenerator{
  1132  					{
  1133  						Git: &v1alpha1.GitGenerator{
  1134  							RepoURL: fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1135  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
  1136  								{
  1137  									Path: "*kustomize*",
  1138  								},
  1139  							},
  1140  						},
  1141  					},
  1142  				},
  1143  			},
  1144  		}).Then().Expect(ApplicationsExist(expectedApps)).
  1145  		// Delete the ApplicationSet, and verify it deletes the Applications
  1146  		When().
  1147  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
  1148  }
  1149  
  1150  func TestSimpleGitGeneratorPrivateRepoWithNoRepo(t *testing.T) {
  1151  	generateExpectedApp := func(name string) v1alpha1.Application {
  1152  		return v1alpha1.Application{
  1153  			TypeMeta: metav1.TypeMeta{
  1154  				Kind:       application.ApplicationKind,
  1155  				APIVersion: "argoproj.io/v1alpha1",
  1156  			},
  1157  			ObjectMeta: metav1.ObjectMeta{
  1158  				Name:       name,
  1159  				Namespace:  fixture.TestNamespace(),
  1160  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
  1161  			},
  1162  			Spec: v1alpha1.ApplicationSpec{
  1163  				Project: "default",
  1164  				Source: &v1alpha1.ApplicationSource{
  1165  					RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1166  					TargetRevision: "HEAD",
  1167  					Path:           name,
  1168  				},
  1169  				Destination: v1alpha1.ApplicationDestination{
  1170  					Server:    "https://kubernetes.default.svc",
  1171  					Namespace: name,
  1172  				},
  1173  			},
  1174  		}
  1175  	}
  1176  
  1177  	expectedApps := []v1alpha1.Application{
  1178  		generateExpectedApp("https-kustomize-base"),
  1179  	}
  1180  
  1181  	var expectedAppsNewNamespace []v1alpha1.Application
  1182  
  1183  	Given(t).
  1184  		When().
  1185  		// Create a GitGenerator-based ApplicationSet
  1186  		Create(v1alpha1.ApplicationSet{
  1187  			ObjectMeta: metav1.ObjectMeta{
  1188  				Name: "simple-git-generator-private",
  1189  			},
  1190  			Spec: v1alpha1.ApplicationSetSpec{
  1191  				Template: v1alpha1.ApplicationSetTemplate{
  1192  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{path.basename}}"},
  1193  					Spec: v1alpha1.ApplicationSpec{
  1194  						Project: "default",
  1195  						Source: &v1alpha1.ApplicationSource{
  1196  							RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1197  							TargetRevision: "HEAD",
  1198  							Path:           "{{path}}",
  1199  						},
  1200  						Destination: v1alpha1.ApplicationDestination{
  1201  							Server:    "https://kubernetes.default.svc",
  1202  							Namespace: "{{path.basename}}",
  1203  						},
  1204  					},
  1205  				},
  1206  				Generators: []v1alpha1.ApplicationSetGenerator{
  1207  					{
  1208  						Git: &v1alpha1.GitGenerator{
  1209  							RepoURL: fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1210  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
  1211  								{
  1212  									Path: "*kustomize*",
  1213  								},
  1214  							},
  1215  						},
  1216  					},
  1217  				},
  1218  			},
  1219  		}).Then().Expect(ApplicationsDoNotExist(expectedApps)).
  1220  		// Delete the ApplicationSet, and verify it deletes the Applications
  1221  		When().
  1222  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
  1223  }
  1224  
  1225  func TestSimpleGitGeneratorPrivateRepoWithMatchingProject(t *testing.T) {
  1226  	generateExpectedApp := func(name string) v1alpha1.Application {
  1227  		return v1alpha1.Application{
  1228  			TypeMeta: metav1.TypeMeta{
  1229  				Kind:       application.ApplicationKind,
  1230  				APIVersion: "argoproj.io/v1alpha1",
  1231  			},
  1232  			ObjectMeta: metav1.ObjectMeta{
  1233  				Name:       name,
  1234  				Namespace:  fixture.TestNamespace(),
  1235  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
  1236  			},
  1237  			Spec: v1alpha1.ApplicationSpec{
  1238  				Project: "default",
  1239  				Source: &v1alpha1.ApplicationSource{
  1240  					RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1241  					TargetRevision: "HEAD",
  1242  					Path:           name,
  1243  				},
  1244  				Destination: v1alpha1.ApplicationDestination{
  1245  					Server:    "https://kubernetes.default.svc",
  1246  					Namespace: name,
  1247  				},
  1248  			},
  1249  		}
  1250  	}
  1251  
  1252  	expectedApps := []v1alpha1.Application{
  1253  		generateExpectedApp("https-kustomize-base"),
  1254  	}
  1255  
  1256  	var expectedAppsNewNamespace []v1alpha1.Application
  1257  
  1258  	Given(t).
  1259  		HTTPSInsecureRepoURLAdded("default").
  1260  		When().
  1261  		// Create a GitGenerator-based ApplicationSet
  1262  		Create(v1alpha1.ApplicationSet{
  1263  			ObjectMeta: metav1.ObjectMeta{
  1264  				Name: "simple-git-generator-private",
  1265  			},
  1266  			Spec: v1alpha1.ApplicationSetSpec{
  1267  				Template: v1alpha1.ApplicationSetTemplate{
  1268  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{path.basename}}"},
  1269  					Spec: v1alpha1.ApplicationSpec{
  1270  						Project: "default",
  1271  						Source: &v1alpha1.ApplicationSource{
  1272  							RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1273  							TargetRevision: "HEAD",
  1274  							Path:           "{{path}}",
  1275  						},
  1276  						Destination: v1alpha1.ApplicationDestination{
  1277  							Server:    "https://kubernetes.default.svc",
  1278  							Namespace: "{{path.basename}}",
  1279  						},
  1280  					},
  1281  				},
  1282  				Generators: []v1alpha1.ApplicationSetGenerator{
  1283  					{
  1284  						Git: &v1alpha1.GitGenerator{
  1285  							RepoURL: fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1286  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
  1287  								{
  1288  									Path: "*kustomize*",
  1289  								},
  1290  							},
  1291  						},
  1292  					},
  1293  				},
  1294  			},
  1295  		}).Then().Expect(ApplicationsExist(expectedApps)).
  1296  		// Delete the ApplicationSet, and verify it deletes the Applications
  1297  		When().
  1298  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
  1299  }
  1300  
  1301  func TestSimpleGitGeneratorPrivateRepoWithMismatchingProject(t *testing.T) {
  1302  	generateExpectedApp := func(name string) v1alpha1.Application {
  1303  		return v1alpha1.Application{
  1304  			TypeMeta: metav1.TypeMeta{
  1305  				Kind:       application.ApplicationKind,
  1306  				APIVersion: "argoproj.io/v1alpha1",
  1307  			},
  1308  			ObjectMeta: metav1.ObjectMeta{
  1309  				Name:       name,
  1310  				Namespace:  fixture.TestNamespace(),
  1311  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
  1312  			},
  1313  			Spec: v1alpha1.ApplicationSpec{
  1314  				Project: "default",
  1315  				Source: &v1alpha1.ApplicationSource{
  1316  					RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1317  					TargetRevision: "HEAD",
  1318  					Path:           name,
  1319  				},
  1320  				Destination: v1alpha1.ApplicationDestination{
  1321  					Server:    "https://kubernetes.default.svc",
  1322  					Namespace: name,
  1323  				},
  1324  			},
  1325  		}
  1326  	}
  1327  
  1328  	expectedApps := []v1alpha1.Application{
  1329  		generateExpectedApp("https-kustomize-base"),
  1330  	}
  1331  
  1332  	var expectedAppsNewNamespace []v1alpha1.Application
  1333  
  1334  	Given(t).
  1335  		HTTPSInsecureRepoURLAdded("some-other-project").
  1336  		When().
  1337  		// Create a GitGenerator-based ApplicationSet
  1338  		Create(v1alpha1.ApplicationSet{
  1339  			ObjectMeta: metav1.ObjectMeta{
  1340  				Name: "simple-git-generator-private",
  1341  			},
  1342  			Spec: v1alpha1.ApplicationSetSpec{
  1343  				Template: v1alpha1.ApplicationSetTemplate{
  1344  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{path.basename}}"},
  1345  					Spec: v1alpha1.ApplicationSpec{
  1346  						Project: "default",
  1347  						Source: &v1alpha1.ApplicationSource{
  1348  							RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1349  							TargetRevision: "HEAD",
  1350  							Path:           "{{path}}",
  1351  						},
  1352  						Destination: v1alpha1.ApplicationDestination{
  1353  							Server:    "https://kubernetes.default.svc",
  1354  							Namespace: "{{path.basename}}",
  1355  						},
  1356  					},
  1357  				},
  1358  				Generators: []v1alpha1.ApplicationSetGenerator{
  1359  					{
  1360  						Git: &v1alpha1.GitGenerator{
  1361  							RepoURL: fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1362  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
  1363  								{
  1364  									Path: "*kustomize*",
  1365  								},
  1366  							},
  1367  						},
  1368  					},
  1369  				},
  1370  			},
  1371  		}).Then().Expect(ApplicationsDoNotExist(expectedApps)).
  1372  		// Delete the ApplicationSet, and verify it deletes the Applications
  1373  		When().
  1374  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
  1375  }
  1376  
  1377  func TestGitGeneratorPrivateRepoWithTemplatedProject(t *testing.T) {
  1378  	generateExpectedApp := func(name string) v1alpha1.Application {
  1379  		return v1alpha1.Application{
  1380  			TypeMeta: metav1.TypeMeta{
  1381  				Kind:       application.ApplicationKind,
  1382  				APIVersion: "argoproj.io/v1alpha1",
  1383  			},
  1384  			ObjectMeta: metav1.ObjectMeta{
  1385  				Name:       name,
  1386  				Namespace:  fixture.TestNamespace(),
  1387  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
  1388  			},
  1389  			Spec: v1alpha1.ApplicationSpec{
  1390  				Project: "default",
  1391  				Source: &v1alpha1.ApplicationSource{
  1392  					RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1393  					TargetRevision: "HEAD",
  1394  					Path:           name,
  1395  				},
  1396  				Destination: v1alpha1.ApplicationDestination{
  1397  					Server:    "https://kubernetes.default.svc",
  1398  					Namespace: name,
  1399  				},
  1400  			},
  1401  		}
  1402  	}
  1403  
  1404  	expectedApps := []v1alpha1.Application{
  1405  		generateExpectedApp("https-kustomize-base"),
  1406  	}
  1407  
  1408  	var expectedAppsNewNamespace []v1alpha1.Application
  1409  
  1410  	Given(t).
  1411  		HTTPSInsecureRepoURLAdded("").
  1412  		When().
  1413  		// Create a GitGenerator-based ApplicationSet
  1414  		Create(v1alpha1.ApplicationSet{
  1415  			ObjectMeta: metav1.ObjectMeta{
  1416  				Name: "simple-git-generator-private",
  1417  			},
  1418  			Spec: v1alpha1.ApplicationSetSpec{
  1419  				Template: v1alpha1.ApplicationSetTemplate{
  1420  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{path.basename}}"},
  1421  					Spec: v1alpha1.ApplicationSpec{
  1422  						Project: "{{values.project}}",
  1423  						Source: &v1alpha1.ApplicationSource{
  1424  							RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1425  							TargetRevision: "HEAD",
  1426  							Path:           "{{path}}",
  1427  						},
  1428  						Destination: v1alpha1.ApplicationDestination{
  1429  							Server:    "https://kubernetes.default.svc",
  1430  							Namespace: "{{path.basename}}",
  1431  						},
  1432  					},
  1433  				},
  1434  				Generators: []v1alpha1.ApplicationSetGenerator{
  1435  					{
  1436  						Git: &v1alpha1.GitGenerator{
  1437  							RepoURL: fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1438  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
  1439  								{
  1440  									Path: "*kustomize*",
  1441  								},
  1442  							},
  1443  							Values: map[string]string{
  1444  								"project": "default",
  1445  							},
  1446  						},
  1447  					},
  1448  				},
  1449  			},
  1450  		}).Then().Expect(ApplicationsExist(expectedApps)).
  1451  		// Delete the ApplicationSet, and verify it deletes the Applications
  1452  		When().
  1453  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
  1454  }
  1455  
  1456  func TestGitGeneratorPrivateRepoWithTemplatedProjectAndProjectScopedRepo(t *testing.T) {
  1457  	// Flush repo-server cache. Why? We want to ensure that the previous test has not already populated the repo-server
  1458  	// cache.
  1459  	r := redis.NewClient(&redis.Options{
  1460  		Addr: "localhost:6379",
  1461  	})
  1462  	all := r.FlushAll(t.Context())
  1463  	require.NoError(t, all.Err())
  1464  
  1465  	generateExpectedApp := func(name string) v1alpha1.Application {
  1466  		return v1alpha1.Application{
  1467  			TypeMeta: metav1.TypeMeta{
  1468  				Kind:       application.ApplicationKind,
  1469  				APIVersion: "argoproj.io/v1alpha1",
  1470  			},
  1471  			ObjectMeta: metav1.ObjectMeta{
  1472  				Name:       name,
  1473  				Namespace:  fixture.TestNamespace(),
  1474  				Finalizers: []string{v1alpha1.ResourcesFinalizerName},
  1475  			},
  1476  			Spec: v1alpha1.ApplicationSpec{
  1477  				Project: "default",
  1478  				Source: &v1alpha1.ApplicationSource{
  1479  					RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1480  					TargetRevision: "HEAD",
  1481  					Path:           name,
  1482  				},
  1483  				Destination: v1alpha1.ApplicationDestination{
  1484  					Server:    "https://kubernetes.default.svc",
  1485  					Namespace: name,
  1486  				},
  1487  			},
  1488  		}
  1489  	}
  1490  
  1491  	expectedApps := []v1alpha1.Application{
  1492  		generateExpectedApp("https-kustomize-base"),
  1493  	}
  1494  
  1495  	var expectedAppsNewNamespace []v1alpha1.Application
  1496  
  1497  	Given(t).
  1498  		HTTPSInsecureRepoURLAdded("default").
  1499  		When().
  1500  		// Create a GitGenerator-based ApplicationSet
  1501  		Create(v1alpha1.ApplicationSet{
  1502  			ObjectMeta: metav1.ObjectMeta{
  1503  				Name: "simple-git-generator-private",
  1504  			},
  1505  			Spec: v1alpha1.ApplicationSetSpec{
  1506  				Template: v1alpha1.ApplicationSetTemplate{
  1507  					ApplicationSetTemplateMeta: v1alpha1.ApplicationSetTemplateMeta{Name: "{{path.basename}}"},
  1508  					Spec: v1alpha1.ApplicationSpec{
  1509  						Project: "{{values.project}}",
  1510  						Source: &v1alpha1.ApplicationSource{
  1511  							RepoURL:        fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1512  							TargetRevision: "HEAD",
  1513  							Path:           "{{path}}",
  1514  						},
  1515  						Destination: v1alpha1.ApplicationDestination{
  1516  							Server:    "https://kubernetes.default.svc",
  1517  							Namespace: "{{path.basename}}",
  1518  						},
  1519  					},
  1520  				},
  1521  				Generators: []v1alpha1.ApplicationSetGenerator{
  1522  					{
  1523  						Git: &v1alpha1.GitGenerator{
  1524  							RepoURL: fixture.RepoURL(fixture.RepoURLTypeHTTPS),
  1525  							Directories: []v1alpha1.GitDirectoryGeneratorItem{
  1526  								{
  1527  									Path: "*kustomize*",
  1528  								},
  1529  							},
  1530  							Values: map[string]string{
  1531  								"project": "default",
  1532  							},
  1533  						},
  1534  					},
  1535  				},
  1536  			},
  1537  		}).Then().Expect(ApplicationsDoNotExist(expectedApps)).
  1538  		// Delete the ApplicationSet, and verify it deletes the Applications
  1539  		When().
  1540  		Delete().Then().Expect(ApplicationsDoNotExist(expectedAppsNewNamespace))
  1541  }