Update directory loading convention

Previously, the loader would use directory names as top-level keys when
paths referred to directories. This meant that identical queries against
policies/data in differently named directories would return different
answers.

Now, the loader ignores the first directory name when recursing on
paths. Unfortunately this is not backwards compatible. Scripts and
workflows can be adapted as follows:

Before: opa test *
After:  opa test .

Before: opa test /some/path/to/dir/*
After:  opa test /some/path/to/dir

The same goes for opa run.
This commit is contained in:
Torin Sandall
2018-03-11 21:49:45 -07:00
parent 0c02943b9b
commit 39f7b45fb7
3 changed files with 12 additions and 14 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ func all(paths []string, f func(*Result, string) error) (*Result, error) {
}
if info.IsDir() {
loadDirRecursive(&errors, path, loaded.withParent(info.Name()))
loadDirRecursive(&errors, path, loaded)
} else {
err := f(loaded, path)
if err != nil {
+10 -12
View File
@@ -108,7 +108,7 @@ func TestLoadGuessYAML(t *testing.T) {
func TestLoadDirRecursive(t *testing.T) {
files := map[string]string{
"/a/data1.json": `[1,2,3]`,
"/a/data1.json": `{"a": [1,2,3]}`,
"/a/e.rego": `package q`,
"/b/data2.yaml": `{"aaa": {"bbb": 1}}`,
"/b/data3.yaml": `{"aaa": {"ccc": 2}}`,
@@ -127,13 +127,11 @@ func TestLoadDirRecursive(t *testing.T) {
{
"zzz": "b",
"a": [1,2,3],
"b": {
"aaa": {
"bbb": 1,
"ccc": 2
},
"d": null
}
"aaa": {
"bbb": 1,
"ccc": 2
},
"d": null
}
`)
if !reflect.DeepEqual(loaded.Documents, expectedDocuments) {
@@ -154,9 +152,9 @@ func TestLoadDirRecursive(t *testing.T) {
func TestLoadRooted(t *testing.T) {
files := map[string]string{
"/foo.json": "[1,2,3]",
"/bar.yaml": "abc",
"/baz/qux.json": "null",
"/foo.json": "[1,2,3]",
"/bar/bar.yaml": "abc",
"/baz/qux/qux.json": "null",
}
test.WithTempFS(files, func(rootDir string) {
@@ -170,7 +168,7 @@ func TestLoadRooted(t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}
expected := parseJSON(`
{"four": [1,2,3], "one": {"two": "abc"}, "three": {"baz": null}}
{"four": [1,2,3], "one": {"two": "abc"}, "three": {"qux": null}}
`)
if !reflect.DeepEqual(loaded.Documents, expected) {
t.Fatalf("Expected %v but got: %v", expected, loaded.Documents)
+1 -1
View File
@@ -159,7 +159,7 @@ func TestRuntimeProcessWatchEvents(t *testing.T) {
}
t0 := time.Now()
path := storage.MustParsePath("/" + path.Base(rootDir) + "/some")
path := storage.MustParsePath("/some")
// In practice, reload takes ~100us on development machine.
maxWaitTime := time.Second * 1