2015-04-17 16:18:21 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
2018-01-08 20:26:13 +00:00
|
|
|
"context"
|
2015-04-17 16:18:21 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-04-17 16:18:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPathStruct(t *testing.T) {
|
|
|
|
p := &PathStruct{
|
|
|
|
Name: "foo",
|
|
|
|
Path: "bar",
|
|
|
|
Schema: map[string]*FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"value": {Type: TypeString},
|
2015-04-17 16:18:21 +00:00
|
|
|
},
|
|
|
|
Read: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := new(logical.InmemStorage)
|
|
|
|
var b logical.Backend = &Backend{Paths: p.Paths()}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2015-04-17 16:18:21 +00:00
|
|
|
// Write via HTTP
|
2018-01-19 06:44:44 +00:00
|
|
|
_, err := b.HandleRequest(ctx, &logical.Request{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-17 16:18:21 +00:00
|
|
|
Path: "bar",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"value": "baz",
|
|
|
|
},
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read via HTTP
|
2018-01-19 06:44:44 +00:00
|
|
|
resp, err := b.HandleRequest(ctx, &logical.Request{
|
2015-04-17 16:18:21 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "bar",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["value"] != "baz" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read via API
|
2018-01-19 06:44:44 +00:00
|
|
|
v, err := p.Get(ctx, storage)
|
2015-04-17 16:18:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if v["value"] != "baz" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
2015-05-14 12:16:55 +00:00
|
|
|
|
|
|
|
// Delete via HTTP
|
2018-01-19 06:44:44 +00:00
|
|
|
resp, err = b.HandleRequest(ctx, &logical.Request{
|
2015-05-14 12:16:55 +00:00
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "bar",
|
|
|
|
Data: nil,
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-read via HTTP
|
2018-01-19 06:44:44 +00:00
|
|
|
resp, err = b.HandleRequest(ctx, &logical.Request{
|
2015-05-14 12:16:55 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "bar",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if _, ok := resp.Data["value"]; ok {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-read via API
|
2018-01-19 06:44:44 +00:00
|
|
|
v, err = p.Get(ctx, storage)
|
2015-05-14 12:16:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
2015-04-17 16:18:21 +00:00
|
|
|
}
|