diff --git a/reposerver/repository/repository.go b/reposerver/repository/repository.go index 493136b1c6919..57a6838730da2 100644 --- a/reposerver/repository/repository.go +++ b/reposerver/repository/repository.go @@ -875,12 +875,15 @@ func findManifests(appPath string, repoRoot string, env *v1alpha1.Env, directory return nil } - fileNameWithPath := filepath.Join(appPath, f.Name()) - if directory.Exclude != "" && glob.Match(directory.Exclude, fileNameWithPath) { + relPath, err := filepath.Rel(appPath, path) + if err != nil { + return err + } + if directory.Exclude != "" && glob.Match(directory.Exclude, relPath) { return nil } - if directory.Include != "" && !glob.Match(directory.Include, fileNameWithPath) { + if directory.Include != "" && !glob.Match(directory.Include, relPath) { return nil } diff --git a/reposerver/repository/repository_test.go b/reposerver/repository/repository_test.go index 7e680c4288126..135ed492d567c 100644 --- a/reposerver/repository/repository_test.go +++ b/reposerver/repository/repository_test.go @@ -1393,5 +1393,82 @@ func TestGenerateManifestWithAnnotatedAndRegularGitTagHashes(t *testing.T) { }) } +} + +func TestFindResources(t *testing.T) { + testCases := []struct { + name string + include string + exclude string + expectedNames []string + }{{ + name: "Include One Match", + include: "subdir/deploymentSub.yaml", + expectedNames: []string{"nginx-deployment-sub"}, + }, { + name: "Include Everything", + include: "*.yaml", + expectedNames: []string{"nginx-deployment", "nginx-deployment-sub"}, + }, { + name: "Include Subdirectory", + include: "**/*.yaml", + expectedNames: []string{"nginx-deployment-sub"}, + }, { + name: "Include No Matches", + include: "nothing.yaml", + expectedNames: []string{}, + }, { + name: "Exclude - One Match", + exclude: "subdir/deploymentSub.yaml", + expectedNames: []string{"nginx-deployment"}, + }, { + name: "Exclude - Everything", + exclude: "*.yaml", + expectedNames: []string{}, + }} + for i := range testCases { + tc := testCases[i] + t.Run(tc.name, func(t *testing.T) { + objs, err := findManifests("testdata/app-include-exclude", ".", nil, argoappv1.ApplicationSourceDirectory{ + Recurse: true, + Include: tc.include, + Exclude: tc.exclude, + }) + if !assert.NoError(t, err) { + return + } + var names []string + for i := range objs { + names = append(names, objs[i].GetName()) + } + assert.ElementsMatch(t, tc.expectedNames, names) + }) + } +} + +func TestFindManifests_Exclude(t *testing.T) { + objs, err := findManifests("testdata/app-include-exclude", ".", nil, argoappv1.ApplicationSourceDirectory{ + Recurse: true, + Exclude: "subdir/deploymentSub.yaml", + }) + + if !assert.NoError(t, err) || !assert.Len(t, objs, 1) { + return + } + + assert.Equal(t, "nginx-deployment", objs[0].GetName()) +} + +func TestFindManifests_Exclude_NothingMatches(t *testing.T) { + objs, err := findManifests("testdata/app-include-exclude", ".", nil, argoappv1.ApplicationSourceDirectory{ + Recurse: true, + Exclude: "nothing.yaml", + }) + + if !assert.NoError(t, err) || !assert.Len(t, objs, 2) { + return + } + assert.ElementsMatch(t, + []string{"nginx-deployment", "nginx-deployment-sub"}, []string{objs[0].GetName(), objs[1].GetName()}) } diff --git a/test/e2e/testdata/app-exclusions/deployment.yaml b/reposerver/repository/testdata/app-include-exclude/deployment.yaml similarity index 100% rename from test/e2e/testdata/app-exclusions/deployment.yaml rename to reposerver/repository/testdata/app-include-exclude/deployment.yaml diff --git a/test/e2e/testdata/app-exclusions/subdir/deploymentSub.yaml b/reposerver/repository/testdata/app-include-exclude/subdir/deploymentSub.yaml similarity index 100% rename from test/e2e/testdata/app-exclusions/subdir/deploymentSub.yaml rename to reposerver/repository/testdata/app-include-exclude/subdir/deploymentSub.yaml diff --git a/test/e2e/app_management_test.go b/test/e2e/app_management_test.go index cf22213cca499..a0f1b42eef683 100644 --- a/test/e2e/app_management_test.go +++ b/test/e2e/app_management_test.go @@ -1432,29 +1432,6 @@ spec: assert.Equal(t, []HelmParameter{{Name: "foo", Value: "foo"}}, app.Spec.Source.Helm.Parameters) }) } -func TestAppCreationWithExclude(t *testing.T) { - Given(t). - Path("app-exclusions"). - When(). - Create("--directory-exclude", "**/.*", "--directory-recurse"). - Sync(). - Then(). - Expect(OperationPhaseIs(OperationSucceeded)). - Expect(SyncStatusIs(SyncStatusCodeSynced)). - Expect(HealthIs(health.HealthStatusHealthy)) -} - -func TestAppCreationWithInclude(t *testing.T) { - Given(t). - Path("app-inclusions"). - When(). - Create("--directory-include", "**/.*", "--directory-recurse"). - Sync(). - Then(). - Expect(OperationPhaseIs(OperationSucceeded)). - Expect(SyncStatusIs(SyncStatusCodeSynced)). - Expect(HealthIs(health.HealthStatusHealthy)) -} // Ensure actions work when using a resource action that modifies status and/or spec func TestCRDStatusSubresourceAction(t *testing.T) { diff --git a/test/e2e/testdata/app-exclusions/.bot.yaml b/test/e2e/testdata/app-exclusions/.bot.yaml deleted file mode 100644 index edac8fb9bbd13..0000000000000 --- a/test/e2e/testdata/app-exclusions/.bot.yaml +++ /dev/null @@ -1,14 +0,0 @@ -metadata: - labels: - app.kubernetes.io/instance: a-single-pod - foo: bar - name: helloBot -spec: - containers: - - name: hello - image: busybox - command: - - sh - args: - - -c - - "echo HI && tail -f /dev/null" diff --git a/test/e2e/testdata/app-exclusions/deploymentDoc.yaml b/test/e2e/testdata/app-exclusions/deploymentDoc.yaml deleted file mode 100644 index e8d058fb6330e..0000000000000 --- a/test/e2e/testdata/app-exclusions/deploymentDoc.yaml +++ /dev/null @@ -1,16 +0,0 @@ -##Purpose of this file is to test this kind of files should be ignored from marshalling since it is lacking of apiVersion, kind, and metadata -spec: - replicas: 0 - selector: - matchLabels: - app: nginx - template: - metadata: - labels: - app: nginx - spec: - containers: - - name: nginx - image: nginx:1.17.4-alpine - ports: - - containerPort: 80 diff --git a/test/e2e/testdata/app-exclusions/subdir/.botSub.yaml b/test/e2e/testdata/app-exclusions/subdir/.botSub.yaml deleted file mode 100644 index 6c4c1fbabc9c5..0000000000000 --- a/test/e2e/testdata/app-exclusions/subdir/.botSub.yaml +++ /dev/null @@ -1,14 +0,0 @@ -metadata: - labels: - app.kubernetes.io/instance: a-single-pod - foo: bar - name: helloBotSub -spec: - containers: - - name: hello - image: busybox - command: - - sh - args: - - -c - - "echo HI && tail -f /dev/null" diff --git a/test/e2e/testdata/app-inclusions/.bot.yaml b/test/e2e/testdata/app-inclusions/.bot.yaml deleted file mode 100644 index edac8fb9bbd13..0000000000000 --- a/test/e2e/testdata/app-inclusions/.bot.yaml +++ /dev/null @@ -1,14 +0,0 @@ -metadata: - labels: - app.kubernetes.io/instance: a-single-pod - foo: bar - name: helloBot -spec: - containers: - - name: hello - image: busybox - command: - - sh - args: - - -c - - "echo HI && tail -f /dev/null" diff --git a/test/e2e/testdata/app-inclusions/deployment.yaml b/test/e2e/testdata/app-inclusions/deployment.yaml deleted file mode 100644 index d84ca5e1f785f..0000000000000 --- a/test/e2e/testdata/app-inclusions/deployment.yaml +++ /dev/null @@ -1,21 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment - labels: - app: nginx -spec: - replicas: 0 - selector: - matchLabels: - app: nginx - template: - metadata: - labels: - app: nginx - spec: - containers: - - name: nginx - image: nginx:1.17.4-alpine - ports: - - containerPort: 80 diff --git a/test/e2e/testdata/app-inclusions/deploymentDoc.yaml b/test/e2e/testdata/app-inclusions/deploymentDoc.yaml deleted file mode 100644 index e8d058fb6330e..0000000000000 --- a/test/e2e/testdata/app-inclusions/deploymentDoc.yaml +++ /dev/null @@ -1,16 +0,0 @@ -##Purpose of this file is to test this kind of files should be ignored from marshalling since it is lacking of apiVersion, kind, and metadata -spec: - replicas: 0 - selector: - matchLabels: - app: nginx - template: - metadata: - labels: - app: nginx - spec: - containers: - - name: nginx - image: nginx:1.17.4-alpine - ports: - - containerPort: 80 diff --git a/test/e2e/testdata/app-inclusions/subdir/.botSub.yaml b/test/e2e/testdata/app-inclusions/subdir/.botSub.yaml deleted file mode 100644 index 6c4c1fbabc9c5..0000000000000 --- a/test/e2e/testdata/app-inclusions/subdir/.botSub.yaml +++ /dev/null @@ -1,14 +0,0 @@ -metadata: - labels: - app.kubernetes.io/instance: a-single-pod - foo: bar - name: helloBotSub -spec: - containers: - - name: hello - image: busybox - command: - - sh - args: - - -c - - "echo HI && tail -f /dev/null" diff --git a/test/e2e/testdata/app-inclusions/subdir/deploymentSub.yaml b/test/e2e/testdata/app-inclusions/subdir/deploymentSub.yaml deleted file mode 100644 index 29a6f9da215c5..0000000000000 --- a/test/e2e/testdata/app-inclusions/subdir/deploymentSub.yaml +++ /dev/null @@ -1,21 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment-sub - labels: - app: nginx -spec: - replicas: 0 - selector: - matchLabels: - app: nginx - template: - metadata: - labels: - app: nginx - spec: - containers: - - name: nginx - image: nginx:1.17.4-alpine - ports: - - containerPort: 80