Add context to storage backends and wire it through a lot of places (#3817)

This commit is contained in:
Brian Kassouf
2018-01-18 22:44:44 -08:00
committed by Jeff Mitchell
parent 2864fbd697
commit 8142b42d95
341 changed files with 3417 additions and 3083 deletions

View File

@@ -13,8 +13,10 @@ func TestPathMap(t *testing.T) {
storage := new(logical.InmemStorage)
var b logical.Backend = &Backend{Paths: p.Paths()}
ctx := context.Background()
// Write via HTTP
_, err := b.HandleRequest(context.Background(), &logical.Request{
_, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: "map/foo/a",
Data: map[string]interface{}{
@@ -27,7 +29,7 @@ func TestPathMap(t *testing.T) {
}
// Read via HTTP
resp, err := b.HandleRequest(context.Background(), &logical.Request{
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: "map/foo/a",
Storage: storage,
@@ -40,7 +42,7 @@ func TestPathMap(t *testing.T) {
}
// Read via API
v, err := p.Get(storage, "a")
v, err := p.Get(ctx, storage, "a")
if err != nil {
t.Fatalf("bad: %#v", err)
}
@@ -49,7 +51,7 @@ func TestPathMap(t *testing.T) {
}
// Read via API with other casing
v, err = p.Get(storage, "A")
v, err = p.Get(ctx, storage, "A")
if err != nil {
t.Fatalf("bad: %#v", err)
}
@@ -58,7 +60,7 @@ func TestPathMap(t *testing.T) {
}
// Verify List
keys, err := p.List(storage, "")
keys, err := p.List(ctx, storage, "")
if err != nil {
t.Fatalf("bad: %#v", err)
}
@@ -67,7 +69,7 @@ func TestPathMap(t *testing.T) {
}
// LIST via HTTP
resp, err = b.HandleRequest(context.Background(), &logical.Request{
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.ListOperation,
Path: "map/foo/",
Storage: storage,
@@ -81,7 +83,7 @@ func TestPathMap(t *testing.T) {
}
// Delete via HTTP
resp, err = b.HandleRequest(context.Background(), &logical.Request{
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.DeleteOperation,
Path: "map/foo/a",
Storage: storage,
@@ -94,7 +96,7 @@ func TestPathMap(t *testing.T) {
}
// Re-read via HTTP
resp, err = b.HandleRequest(context.Background(), &logical.Request{
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: "map/foo/a",
Storage: storage,
@@ -107,7 +109,7 @@ func TestPathMap(t *testing.T) {
}
// Re-read via API
v, err = p.Get(storage, "a")
v, err = p.Get(ctx, storage, "a")
if err != nil {
t.Fatalf("bad: %#v", err)
}
@@ -120,7 +122,7 @@ func TestPathMap_getInvalid(t *testing.T) {
p := &PathMap{Name: "foo"}
storage := new(logical.InmemStorage)
v, err := p.Get(storage, "nope")
v, err := p.Get(context.Background(), storage, "nope")
if err != nil {
t.Fatalf("bad: %#v", err)
}
@@ -148,15 +150,15 @@ func TestPathMap_Salted(t *testing.T) {
t.Fatalf("err: %v", err)
}
testSalting(t, storage, salt, &PathMap{Name: "foo", Salt: salt})
testSalting(t, context.Background(), storage, salt, &PathMap{Name: "foo", Salt: salt})
}
func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *PathMap) {
func testSalting(t *testing.T, ctx context.Context, storage logical.Storage, salt *saltpkg.Salt, p *PathMap) {
var b logical.Backend = &Backend{Paths: p.Paths()}
var err error
// Write via HTTP
_, err = b.HandleRequest(context.Background(), &logical.Request{
_, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: "map/foo/a",
Data: map[string]interface{}{
@@ -169,7 +171,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
}
// Non-salted version should not be there
out, err := storage.Get("struct/map/foo/a")
out, err := storage.Get(ctx, "struct/map/foo/a")
if err != nil {
t.Fatalf("err: %v", err)
}
@@ -179,7 +181,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
// Ensure the path is salted
expect := "s" + salt.SaltIDHashFunc("a", saltpkg.SHA256Hash)
out, err = storage.Get("struct/map/foo/" + expect)
out, err = storage.Get(ctx, "struct/map/foo/"+expect)
if err != nil {
t.Fatalf("err: %v", err)
}
@@ -188,7 +190,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
}
// Read via HTTP
resp, err := b.HandleRequest(context.Background(), &logical.Request{
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: "map/foo/a",
Storage: storage,
@@ -201,7 +203,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
}
// Read via API
v, err := p.Get(storage, "a")
v, err := p.Get(ctx, storage, "a")
if err != nil {
t.Fatalf("bad: %#v", err)
}
@@ -210,7 +212,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
}
// Read via API with other casing
v, err = p.Get(storage, "A")
v, err = p.Get(ctx, storage, "A")
if err != nil {
t.Fatalf("bad: %#v", err)
}
@@ -219,7 +221,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
}
// Verify List
keys, err := p.List(storage, "")
keys, err := p.List(ctx, storage, "")
if err != nil {
t.Fatalf("bad: %#v", err)
}
@@ -228,7 +230,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
}
// Delete via HTTP
resp, err = b.HandleRequest(context.Background(), &logical.Request{
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.DeleteOperation,
Path: "map/foo/a",
Storage: storage,
@@ -241,7 +243,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
}
// Re-read via HTTP
resp, err = b.HandleRequest(context.Background(), &logical.Request{
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: "map/foo/a",
Storage: storage,
@@ -254,7 +256,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
}
// Re-read via API
v, err = p.Get(storage, "a")
v, err = p.Get(ctx, storage, "a")
if err != nil {
t.Fatalf("bad: %#v", err)
}
@@ -264,7 +266,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
// Put in a non-salted version and make sure that after reading it's been
// upgraded
err = storage.Put(&logical.StorageEntry{
err = storage.Put(ctx, &logical.StorageEntry{
Key: "struct/map/foo/b",
Value: []byte(`{"foo": "bar"}`),
})
@@ -272,7 +274,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
t.Fatal("err: %v", err)
}
// A read should transparently upgrade
resp, err = b.HandleRequest(context.Background(), &logical.Request{
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: "map/foo/b",
Storage: storage,
@@ -280,7 +282,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
if err != nil {
t.Fatal(err)
}
list, _ := storage.List("struct/map/foo/")
list, _ := storage.List(ctx, "struct/map/foo/")
if len(list) != 1 {
t.Fatalf("unexpected number of entries left after upgrade; expected 1, got %d", len(list))
}
@@ -297,7 +299,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
// Put in a SHA1 salted version and make sure that after reading its been
// upgraded
err = storage.Put(&logical.StorageEntry{
err = storage.Put(ctx, &logical.StorageEntry{
Key: "struct/map/foo/" + salt.SaltID("b"),
Value: []byte(`{"foo": "bar"}`),
})
@@ -306,7 +308,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
}
// A read should transparently upgrade
resp, err = b.HandleRequest(context.Background(), &logical.Request{
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: "map/foo/b",
Storage: storage,
@@ -314,7 +316,7 @@ func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *P
if err != nil {
t.Fatal(err)
}
list, _ = storage.List("struct/map/foo/")
list, _ = storage.List(ctx, "struct/map/foo/")
if len(list) != 1 {
t.Fatalf("unexpected number of entries left after upgrade; expected 1, got %d", len(list))
}
@@ -344,5 +346,5 @@ func TestPathMap_SaltFunc(t *testing.T) {
return salt, nil
}
testSalting(t, storage, salt, &PathMap{Name: "foo", SaltFunc: saltFunc})
testSalting(t, context.Background(), storage, salt, &PathMap{Name: "foo", SaltFunc: saltFunc})
}