Skip to content

Commit

Permalink
test: directory app manifest generation (#9503)
Browse files Browse the repository at this point in the history
* test: directory app manifest generation

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

* git doesn't support empty dirs

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

fix bad cherry-pick

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
  • Loading branch information
crenshaw-dev committed Jun 14, 2022
1 parent 26ac321 commit 5e6b788
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ func findManifests(logCtx *log.Entry, appPath string, repoRoot string, env *v1al
return fmt.Errorf("failed to evaluate symlink at %q: %w", relPath, err)
}
}
if !files.Inbound(realPath, appPath) {
if !files.Inbound(realPath, repoRoot) {
logCtx.Warnf("illegal filepath in symlink: %s", realPath)
return fmt.Errorf("illegal filepath in symlink at %q", relPath)
}
Expand Down
121 changes: 121 additions & 0 deletions reposerver/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,127 @@ func TestFindManifests_Exclude_NothingMatches(t *testing.T) {
[]string{"nginx-deployment", "nginx-deployment-sub"}, []string{objs[0].GetName(), objs[1].GetName()})
}



func Test_findManifests(t *testing.T) {
logCtx := log.WithField("test", "test")
noRecurse := argoappv1.ApplicationSourceDirectory{Recurse: false}

t.Run("unreadable file throws error", func(t *testing.T) {
appDir := t.TempDir()
unreadablePath := filepath.Join(appDir, "unreadable.json")
err := os.WriteFile(unreadablePath, []byte{}, 0666)
require.NoError(t, err)
err = os.Chmod(appDir, 0000)
require.NoError(t, err)

manifests, err := findManifests(logCtx, appDir, appDir, nil, noRecurse)
assert.Empty(t, manifests)
assert.Error(t, err)

// allow cleanup
err = os.Chmod(appDir, 0777)
if err != nil {
panic(err)
}
})

t.Run("no recursion when recursion is disabled", func(t *testing.T) {
manifests, err := findManifests(logCtx, "./testdata/recurse", "./testdata/recurse", nil, noRecurse)
assert.Len(t, manifests, 2)
assert.NoError(t, err)
})

t.Run("recursion when recursion is enabled", func(t *testing.T) {
recurse := argoappv1.ApplicationSourceDirectory{Recurse: true}
manifests, err := findManifests(logCtx, "./testdata/recurse", "./testdata/recurse", nil, recurse)
assert.Len(t, manifests, 4)
assert.NoError(t, err)
})

t.Run("non-JSON/YAML is skipped", func(t *testing.T) {
manifests, err := findManifests(logCtx, "./testdata/non-manifest-file", "./testdata/non-manifest-file", nil, noRecurse)
assert.Empty(t, manifests)
assert.NoError(t, err)
})

t.Run("circular link should throw an error", func(t *testing.T) {
require.DirExists(t, "./testdata/circular-link")
manifests, err := findManifests(logCtx, "./testdata/circular-link", "./testdata/circular-link", nil, noRecurse)
assert.Empty(t, manifests)
assert.Error(t, err)
})

t.Run("out-of-bounds symlink should throw an error", func(t *testing.T) {
require.DirExists(t, "./testdata/out-of-bounds-link")
manifests, err := findManifests(logCtx, "./testdata/out-of-bounds-link", "./testdata/out-of-bounds-link", nil, noRecurse)
assert.Empty(t, manifests)
assert.Error(t, err)
})

t.Run("symlink to a regular file works", func(t *testing.T) {
repoRoot, err := filepath.Abs("./testdata/in-bounds-link")
require.NoError(t, err)
appPath, err := filepath.Abs("./testdata/in-bounds-link/app")
require.NoError(t, err)
manifests, err := findManifests(logCtx, appPath, repoRoot, nil, noRecurse)
assert.Len(t, manifests, 1)
assert.NoError(t, err)
})

t.Run("symlink to nowhere should be ignored", func(t *testing.T) {
manifests, err := findManifests(logCtx, "./testdata/link-to-nowhere", "./testdata/link-to-nowhere", nil, noRecurse)
assert.Empty(t, manifests)
assert.NoError(t, err)
})

t.Run("partially valid YAML file throws an error", func(t *testing.T) {
require.DirExists(t, "./testdata/partially-valid-yaml")
manifests, err := findManifests(logCtx, "./testdata/partially-valid-yaml", "./testdata/partially-valid-yaml", nil, noRecurse)
assert.Empty(t, manifests)
assert.Error(t, err)
})

t.Run("invalid manifest throws an error", func(t *testing.T) {
require.DirExists(t, "./testdata/invalid-manifests")
manifests, err := findManifests(logCtx, "./testdata/invalid-manifests", "./testdata/invalid-manifests", nil, noRecurse)
assert.Empty(t, manifests)
assert.Error(t, err)
})

t.Run("irrelevant YAML gets skipped, relevant YAML gets parsed", func(t *testing.T) {
manifests, err := findManifests(logCtx, "./testdata/irrelevant-yaml", "./testdata/irrelevant-yaml", nil, noRecurse)
assert.Len(t, manifests, 1)
assert.NoError(t, err)
})

t.Run("multiple JSON objects in one file throws an error", func(t *testing.T) {
require.DirExists(t, "./testdata/json-list")
manifests, err := findManifests(logCtx, "./testdata/json-list", "./testdata/json-list", nil, noRecurse)
assert.Empty(t, manifests)
assert.Error(t, err)
})

t.Run("invalid JSON throws an error", func(t *testing.T) {
require.DirExists(t, "./testdata/invalid-json")
manifests, err := findManifests(logCtx, "./testdata/invalid-json", "./testdata/invalid-json", nil, noRecurse)
assert.Empty(t, manifests)
assert.Error(t, err)
})

t.Run("valid JSON returns manifest and no error", func(t *testing.T) {
manifests, err := findManifests(logCtx, "./testdata/valid-json", "./testdata/valid-json", nil, noRecurse)
assert.Len(t, manifests, 1)
assert.NoError(t, err)
})

t.Run("YAML with an empty document doesn't throw an error", func(t *testing.T) {
manifests, err := findManifests(logCtx, "./testdata/yaml-with-empty-document", "./testdata/yaml-with-empty-document", nil, noRecurse)
assert.Len(t, manifests, 1)
assert.NoError(t, err)
})
}

func TestTestRepoOCI(t *testing.T) {
service := newService(".")
_, err := service.TestRepository(context.Background(), &apiclient.TestRepositoryRequest{
Expand Down
1 change: 1 addition & 0 deletions reposerver/repository/testdata/circular-link/a.json
1 change: 1 addition & 0 deletions reposerver/repository/testdata/circular-link/b.json
2 changes: 2 additions & 0 deletions reposerver/repository/testdata/in-bounds-link/cm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: v1
kind: ServiceAccount
1 change: 1 addition & 0 deletions reposerver/repository/testdata/invalid-json/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some: [irrelevant, yaml]
2 changes: 2 additions & 0 deletions reposerver/repository/testdata/irrelevant-yaml/relevant.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: v1
kind: ConfigMap
2 changes: 2 additions & 0 deletions reposerver/repository/testdata/json-list/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"apiVersion": "v1", "kind": "ConfigMap"}
{"apiVersion": "v1", "kind": "ConfigMap"}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: ConfigMap
---
invalid:
1 change: 1 addition & 0 deletions reposerver/repository/testdata/valid-json/valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"apiVersion": "v1", "kind": "ConfigMap"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: ConfigMap
---
---

0 comments on commit 5e6b788

Please sign in to comment.