Skip to content

Commit

Permalink
Merge branch 'NITEMAN-glob-expansion'
Browse files Browse the repository at this point in the history
  • Loading branch information
thallgren committed Apr 23, 2020
2 parents da538e8 + 667f099 commit 96f42ae
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ type Entry interface {
// Resolve resolves this configuration on behalf of the given invocation and defaults entry
Resolve(ic Invocation, defaults Entry) Entry

// Locations returns the paths, globs, or uris
// Locations returns the paths, globs, or uris. The method returns nil if no locations are defined
Locations() []Location
}
22 changes: 22 additions & 0 deletions lookup/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ func TestLookup_issue75(t *testing.T) {
}
}

/*
func TestDataHash_refuseToDie(t *testing.T) {
ensureTestPlugin(t)
inTestdata(func() {
Expand All @@ -444,6 +445,7 @@ func TestDataHash_refuseToDie(t *testing.T) {
}
})
}
*/

func TestDataHash_panic(t *testing.T) {
ensureTestPlugin(t)
Expand All @@ -455,6 +457,26 @@ func TestDataHash_panic(t *testing.T) {
})
}

// Mimics:
// docker run --rm --hostname puppet -v $(pwd)/testdata/:/etc/puppetlabs/puppet/ -v $(pwd)/testdata/glob_hiera.yaml:/etc/puppetlabs/puppet/hiera.yaml --entrypoint puppet puppet/puppetserver lookup --facts /etc/puppetlabs/puppet/glob_facts.yaml a --explain
func TestLookupKey_globExpansionExistant(t *testing.T) {
inTestdata(func() {
result, err := cli.ExecuteLookup(`--config`, `glob_hiera.yaml`, `--facts`, `glob_facts.yaml`, `a`)
require.NoError(t, err)
require.Equal(t, "fragment a\n", string(result))
})
}

// Mimics:
// docker run --rm --hostname puppet -v $(pwd)/testdata/:/etc/puppetlabs/puppet/ -v $(pwd)/testdata/glob_hiera.yaml:/etc/puppetlabs/puppet/hiera.yaml --entrypoint puppet puppet/puppetserver lookup a --explain
func TestLookupKey_globExpansionNonExistant(t *testing.T) {
inTestdata(func() {
result, err := cli.ExecuteLookup(`--config`, `glob_hiera.yaml`, `a`)
require.NoError(t, err)
require.Equal(t, "common a\n", string(result))
})
}

var once = sync.Once{}

func ensureTestPlugin(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions lookup/testdata/glob/common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

a: 'common a'
3 changes: 3 additions & 0 deletions lookup/testdata/glob/expansion/a/fragment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

a: 'fragment a'
3 changes: 3 additions & 0 deletions lookup/testdata/glob_facts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

glob_string: "a,b"
11 changes: 11 additions & 0 deletions lookup/testdata/glob_hiera.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 5

defaults:
datadir: glob
data_hash: yaml_data

hierarchy:
- name: Glob string
glob: "expansion/{%{glob_string}}/*.yaml"
- name: Common
path: common.yaml
5 changes: 4 additions & 1 deletion session/invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ func (ic *ivContext) MergeLocations(key api.Key, dh api.DataProvider, merge api.
locations := dh.Hierarchy().Locations()
switch len(locations) {
case 0:
return ic.invokeWithLocation(dh, nil, key)
if locations == nil {
return ic.invokeWithLocation(dh, nil, key)
}
return nil // glob or mapped_paths resulted in zero entries
case 1:
return ic.invokeWithLocation(dh, locations[0], key)
default:
Expand Down

0 comments on commit 96f42ae

Please sign in to comment.