-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathupgrade_test.go
98 lines (81 loc) · 2.21 KB
/
upgrade_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package kv
import (
"context"
"fmt"
"reflect"
"sync/atomic"
"testing"
"time"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/logical"
)
func TestVersionedKV_Upgrade(t *testing.T) {
b, storage := testPassthroughBackendWithStorage()
for i := 0; i < 1024*1024; i++ {
data := map[string]interface{}{
"bar": i,
}
req := &logical.Request{
Operation: logical.CreateOperation,
Path: fmt.Sprintf("%d/foo", i),
Storage: storage,
Data: data,
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
}
config := &logical.BackendConfig{
Logger: logging.NewVaultLogger(log.Trace),
System: &logical.StaticSystemView{},
StorageView: storage,
BackendUUID: "test",
Config: map[string]string{
"version": "2",
"upgrade": "true",
},
}
var err error
b, err = Factory(context.Background(), config)
if err != nil {
t.Fatal(err)
}
// verify requests are rejected during upgrade
req := &logical.Request{
Operation: logical.ReadOperation,
Path: fmt.Sprintf("data/%d/foo", 1),
Storage: storage,
}
resp, err := b.HandleRequest(context.Background(), req)
if resp == nil || resp.Error().Error() != "Upgrading from non-versioned to versioned data. This backend will be unavailable for a brief period and will resume service shortly." {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
// wait for upgrade to finish
for {
if atomic.LoadUint32(b.(*versionedKVBackend).upgrading) == 0 {
break
}
time.Sleep(time.Second)
}
for i := 0; i < 1024*1024; i++ {
data := map[string]interface{}{
"bar": float64(i),
}
req := &logical.Request{
Operation: logical.ReadOperation,
Path: fmt.Sprintf("data/%d/foo", i),
Storage: storage,
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
if !reflect.DeepEqual(resp.Data["data"].(map[string]interface{}), data) {
t.Fatalf("bad response %#v", resp)
}
}
}