Skip to content

Commit

Permalink
Update mock-plugin (#3107)
Browse files Browse the repository at this point in the history
  • Loading branch information
calvn authored Aug 4, 2017
1 parent bc09549 commit 663185c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 70 deletions.
18 changes: 10 additions & 8 deletions logical/plugin/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ func TestBackendPlugin_HandleRequest(t *testing.T) {
defer cleanup()

resp, err := b.HandleRequest(&logical.Request{
Operation: logical.ReadOperation,
Path: "test/ing",
Data: map[string]interface{}{"value": "foo"},
Operation: logical.CreateOperation,
Path: "kv/foo",
Data: map[string]interface{}{
"value": "bar",
},
})
if err != nil {
t.Fatal(err)
}
if resp.Data["value"] != "foo" {
if resp.Data["value"] != "bar" {
t.Fatalf("bad: %#v", resp)
}
}
Expand Down Expand Up @@ -76,17 +78,17 @@ func TestBackendPlugin_HandleExistenceCheck(t *testing.T) {

checkFound, exists, err := b.HandleExistenceCheck(&logical.Request{
Operation: logical.CreateOperation,
Path: "test/ing",
Data: map[string]interface{}{"value": "foo"},
Path: "kv/foo",
Data: map[string]interface{}{"value": "bar"},
})
if err != nil {
t.Fatal(err)
}
if !checkFound {
t.Fatal("existence check not found for path 'test/ing'")
t.Fatal("existence check not found for path 'kv/foo")
}
if exists {
t.Fatal("existence check should have returned 'false' for 'testing/read'")
t.Fatal("existence check should have returned 'false' for 'kv/foo'")
}
}

Expand Down
2 changes: 1 addition & 1 deletion logical/plugin/mock/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Backend() *backend {
b.Backend = &framework.Backend{
Help: "",
Paths: []*framework.Path{
pathTesting(&b),
pathKV(&b),
pathInternal(&b),
},
PathsSpecial: &logical.Paths{
Expand Down
2 changes: 1 addition & 1 deletion logical/plugin/mock/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (
"github.com/hashicorp/vault/logical"
)

func TestMockBackend_impl(t *testing.T) {
func TestBackend_impl(t *testing.T) {
var _ logical.Backend = new(backend)
}
5 changes: 4 additions & 1 deletion logical/plugin/mock/mock-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/hashicorp/vault/helper/pluginutil"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/plugin"
"github.com/hashicorp/vault/logical/plugin/mock"
)
Expand All @@ -17,8 +18,10 @@ func main() {
tlsConfig := apiClientMeta.GetTLSConfig()
tlsProviderFunc := pluginutil.VaultPluginTLSProvider(tlsConfig)

factoryFunc := mock.FactoryType(logical.TypeLogical)

err := plugin.Serve(&plugin.ServeOpts{
BackendFactoryFunc: mock.Factory,
BackendFactoryFunc: factoryFunc,
TLSProviderFunc: tlsProviderFunc,
})
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions logical/plugin/mock/path_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"github.com/hashicorp/vault/logical/framework"
)

// pathInternal is used to test viewing internal backend values. In this case,
// it is used to test the invalidate func.
func pathInternal(b *backend) *framework.Path {
return &framework.Path{
Pattern: "internal",
Fields: map[string]*framework.FieldSchema{},
ExistenceCheck: b.pathTestingExistenceCheck,
ExistenceCheck: b.pathExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathTestingReadInternal,
logical.ReadOperation: b.pathInternalRead,
},
}
}

func (b *backend) pathTestingReadInternal(
func (b *backend) pathInternalRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// Return the secret
return &logical.Response{
Expand Down
96 changes: 96 additions & 0 deletions logical/plugin/mock/path_kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package mock

import (
"fmt"

"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)

// pathKV is used to test CRUD and List operations. It is a simplified
// version of the passthrough backend that only accepts string values.
func pathKV(b *backend) *framework.Path {
return &framework.Path{
Pattern: "kv/" + framework.GenericNameRegex("key"),
Fields: map[string]*framework.FieldSchema{
"key": &framework.FieldSchema{Type: framework.TypeString},
"value": &framework.FieldSchema{Type: framework.TypeString},
},
ExistenceCheck: b.pathExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathKVRead,
logical.CreateOperation: b.pathKVCreateUpdate,
logical.UpdateOperation: b.pathKVCreateUpdate,
logical.DeleteOperation: b.pathKVDelete,
logical.ListOperation: b.pathKVList,
},
}
}

func (b *backend) pathExistenceCheck(req *logical.Request, data *framework.FieldData) (bool, error) {
out, err := req.Storage.Get(req.Path)
if err != nil {
return false, fmt.Errorf("existence check failed: %v", err)
}

return out != nil, nil
}

func (b *backend) pathKVRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entry, err := req.Storage.Get(req.Path)
if err != nil {
return nil, err
}

if entry == nil {
return nil, nil
}

value := string(entry.Value)

// Return the secret
return &logical.Response{
Data: map[string]interface{}{
"value": value,
},
}, nil
}

func (b *backend) pathKVCreateUpdate(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
value := data.Get("value").(string)

entry := &logical.StorageEntry{
Key: req.Path,
Value: []byte(value),
}

s := req.Storage
err := s.Put(entry)
if err != nil {
return nil, err
}

return &logical.Response{
Data: map[string]interface{}{
"value": value,
},
}, nil
}

func (b *backend) pathKVDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
if err := req.Storage.Delete(req.Path); err != nil {
return nil, err
}

return nil, nil
}

func (b *backend) pathKVList(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
vals, err := req.Storage.List("kv/")
if err != nil {
return nil, err
}
return logical.ListResponse(vals), nil
}
56 changes: 0 additions & 56 deletions logical/plugin/mock/path_testing.go

This file was deleted.

0 comments on commit 663185c

Please sign in to comment.