forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move api.findEntry to plugin.FindEntry
Makes FindEntry a generally available utility for searching for a path in a plugin hierarchy. This is useful to both the FUSE and API modules. Signed-off-by: Michael Smith <[email protected]>
- Loading branch information
1 parent
ce774c4
commit 35773ea
Showing
4 changed files
with
150 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package plugin | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// FindEntry returns the child of start found by following the segments, or an error if it cannot be found. | ||
func FindEntry(ctx context.Context, start Entry, segments []string) (Entry, error) { | ||
visitedSegments := make([]string, 0, cap(segments)) | ||
for _, segment := range segments { | ||
switch curParent := start.(type) { | ||
case Parent: | ||
// Get the entries via. List() | ||
entries, err := CachedList(ctx, curParent) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Search for the specific entry | ||
entry, ok := entries[segment] | ||
if !ok { | ||
reason := fmt.Sprintf("The %v entry does not exist", segment) | ||
if len(visitedSegments) != 0 { | ||
reason += fmt.Sprintf(" in the %v parent", strings.Join(visitedSegments, "/")) | ||
} | ||
return nil, fmt.Errorf(reason) | ||
} | ||
|
||
start = entry | ||
visitedSegments = append(visitedSegments, segment) | ||
default: | ||
return nil, fmt.Errorf("The entry %v is not a parent", strings.Join(visitedSegments, "/")) | ||
} | ||
} | ||
|
||
return start, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package plugin | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/puppetlabs/wash/datastore" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockParent struct { | ||
EntryBase | ||
entries []Entry | ||
} | ||
|
||
func (g *mockParent) List(context.Context) ([]Entry, error) { | ||
return g.entries, nil | ||
} | ||
|
||
func (g *mockParent) ChildSchemas() []*EntrySchema { | ||
return nil | ||
} | ||
|
||
func (g *mockParent) Schema() *EntrySchema { | ||
return nil | ||
} | ||
|
||
type mockEntry struct { | ||
EntryBase | ||
} | ||
|
||
func newMockEntry(name string) *mockEntry { | ||
return &mockEntry{ | ||
EntryBase: NewEntry(name), | ||
} | ||
} | ||
|
||
func (e *mockEntry) Schema() *EntrySchema { | ||
return nil | ||
} | ||
|
||
func TestFindEntry(t *testing.T) { | ||
SetTestCache(datastore.NewMemCache()) | ||
defer UnsetTestCache() | ||
|
||
type testcase struct { | ||
segments []string | ||
expectedEntry string | ||
expectedErr error | ||
} | ||
runTestCase := func(parent Parent, c testcase) { | ||
got, err := FindEntry(context.Background(), parent, c.segments) | ||
if c.expectedEntry != "" && assert.NotNil(t, got) { | ||
assert.Equal(t, c.expectedEntry, CName(got)) | ||
} else { | ||
assert.Nil(t, got) | ||
} | ||
if c.expectedErr == nil { | ||
assert.Nil(t, err) | ||
} else { | ||
assert.Equal(t, c.expectedErr, err) | ||
} | ||
} | ||
|
||
foo := newMockEntry("foo/bar") | ||
parent := &mockParent{NewEntry("root"), []Entry{foo}} | ||
parent.SetTestID("/root") | ||
parent.DisableDefaultCaching() | ||
for _, c := range []testcase{ | ||
{[]string{"not found"}, "", fmt.Errorf("The not found entry does not exist")}, | ||
{[]string{"foo#bar"}, "foo#bar", nil}, | ||
{[]string{"foo#bar", "bar"}, "", fmt.Errorf("The entry foo#bar is not a parent")}, | ||
} { | ||
runTestCase(parent, c) | ||
} | ||
|
||
baz := newMockEntry("baz") | ||
nestedParent := &mockParent{NewEntry("bar"), []Entry{baz}} | ||
nestedParent.DisableDefaultCaching() | ||
parent.entries = append(parent.entries, nestedParent) | ||
for _, c := range []testcase{ | ||
{[]string{"bar"}, "bar", nil}, | ||
{[]string{"bar", "foo"}, "", fmt.Errorf("The foo entry does not exist in the bar parent")}, | ||
{[]string{"bar", "baz"}, "baz", nil}, | ||
} { | ||
runTestCase(parent, c) | ||
} | ||
|
||
// Finally, test the duplicate cname error response | ||
duplicateFoo := newMockEntry("foo#bar") | ||
parent.entries = append(parent.entries, duplicateFoo) | ||
expectedErr := DuplicateCNameErr{ | ||
ParentID: ID(parent), | ||
FirstChildName: foo.Name(), | ||
FirstChildSlashReplacer: '#', | ||
SecondChildName: duplicateFoo.Name(), | ||
SecondChildSlashReplacer: '#', | ||
CName: "foo#bar", | ||
} | ||
runTestCase( | ||
parent, | ||
testcase{[]string{"foo#bar"}, "", expectedErr}, | ||
) | ||
} |