Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: directory app manifest generation #9503

Merged
merged 2 commits into from May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
124 changes: 123 additions & 1 deletion reposerver/repository/repository_test.go
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
goio "io"
"io/ioutil"
"os"
Expand All @@ -17,6 +16,8 @@ import (
"testing"
"time"

log "github.com/sirupsen/logrus"

"github.com/ghodss/yaml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -1714,6 +1715,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]
@@ -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
---
---