Skip to content

Commit

Permalink
fix: directory source include/exclude should match relative file path (
Browse files Browse the repository at this point in the history
…#5277)

Signed-off-by: Alexander Matyushentsev <[email protected]>
  • Loading branch information
Alexander Matyushentsev authored Jan 20, 2021
1 parent 41fb0ac commit 9dd5dd2
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 156 deletions.
9 changes: 6 additions & 3 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
77 changes: 77 additions & 0 deletions reposerver/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
}
23 changes: 0 additions & 23 deletions test/e2e/app_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 0 additions & 14 deletions test/e2e/testdata/app-exclusions/.bot.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions test/e2e/testdata/app-exclusions/deploymentDoc.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions test/e2e/testdata/app-exclusions/subdir/.botSub.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions test/e2e/testdata/app-inclusions/.bot.yaml

This file was deleted.

21 changes: 0 additions & 21 deletions test/e2e/testdata/app-inclusions/deployment.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions test/e2e/testdata/app-inclusions/deploymentDoc.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions test/e2e/testdata/app-inclusions/subdir/.botSub.yaml

This file was deleted.

21 changes: 0 additions & 21 deletions test/e2e/testdata/app-inclusions/subdir/deploymentSub.yaml

This file was deleted.

0 comments on commit 9dd5dd2

Please sign in to comment.