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>
  • Loading branch information
crenshaw-dev committed May 24, 2022
1 parent 9346e19 commit 7faae31
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
Expand Up @@ -1099,7 +1099,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
Expand Up @@ -1716,6 +1716,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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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, nil)
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
@@ -0,0 +1,2 @@
apiVersion: v1
kind: ServiceAccount
1 change: 1 addition & 0 deletions reposerver/repository/testdata/invalid-json/invalid.json
@@ -0,0 +1 @@
[
@@ -0,0 +1 @@
some: [irrelevant, yaml]
2 changes: 2 additions & 0 deletions reposerver/repository/testdata/irrelevant-yaml/relevant.yaml
@@ -0,0 +1,2 @@
apiVersion: v1
kind: ConfigMap
2 changes: 2 additions & 0 deletions reposerver/repository/testdata/json-list/list.json
@@ -0,0 +1,2 @@
{"apiVersion": "v1", "kind": "ConfigMap"}
{"apiVersion": "v1", "kind": "ConfigMap"}
Empty file.
Empty file.
@@ -0,0 +1,4 @@
apiVersion: v1
kind: ConfigMap
---
invalid:
1 change: 1 addition & 0 deletions reposerver/repository/testdata/valid-json/valid.json
@@ -0,0 +1 @@
{"apiVersion": "v1", "kind": "ConfigMap"}
@@ -0,0 +1,4 @@
apiVersion: v1
kind: ConfigMap
---
---

0 comments on commit 7faae31

Please sign in to comment.