-
Notifications
You must be signed in to change notification settings - Fork 19
/
backend_test.go
126 lines (106 loc) · 3.32 KB
/
backend_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azuresecrets
import (
"context"
"testing"
"time"
"github.com/hashicorp/go-hclog"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/helper/pluginutil"
"github.com/hashicorp/vault/sdk/logical"
)
const (
defaultLeaseTTLHr = 1 * time.Hour
maxLeaseTTLHr = 12 * time.Hour
defaultTestTTL = 300
defaultTestMaxTTL = 3600
defaultTestExplicitMaxTTL = 7200
)
var (
testClientID = "testClientId"
testClientSecret = "testClientSecret"
)
type testSystemViewEnt struct {
logical.StaticSystemView
}
func (d testSystemViewEnt) GenerateIdentityToken(_ context.Context, _ *pluginutil.IdentityTokenRequest) (*pluginutil.IdentityTokenResponse, error) {
return &pluginutil.IdentityTokenResponse{}, nil
}
func getTestBackendMocked(t *testing.T, initConfig bool) (*azureSecretBackend, logical.Storage) {
b := backend()
sysView := testSystemViewEnt{}
sysView.DefaultLeaseTTLVal = defaultLeaseTTLHr
sysView.MaxLeaseTTLVal = maxLeaseTTLHr
config := &logical.BackendConfig{
Logger: logging.NewVaultLogger(log.Trace),
System: &sysView,
StorageView: &logical.InmemStorage{},
}
err := b.Setup(context.Background(), config)
if err != nil {
t.Fatalf("unable to create backend: %v", err)
}
b.settings = new(clientSettings)
mockProvider := newMockProvider()
b.getProvider = func(context.Context, hclog.Logger, logical.SystemView, *clientSettings) (AzureProvider, error) {
return mockProvider, nil
}
if initConfig {
cfg := map[string]interface{}{
"subscription_id": generateUUID(),
"tenant_id": generateUUID(),
"client_id": testClientID,
"client_secret": testClientSecret,
"environment": "AZURECHINACLOUD",
"ttl": defaultTestTTL,
"max_ttl": defaultTestMaxTTL,
"explicit_max_ttl": defaultTestExplicitMaxTTL,
}
testConfigCreate(t, b, config.StorageView, cfg, false)
}
return b, config.StorageView
}
func getTestBackend(t *testing.T) (*azureSecretBackend, logical.Storage) {
b := backend()
config := &logical.BackendConfig{
Logger: logging.NewVaultLogger(log.Trace),
System: &logical.StaticSystemView{
DefaultLeaseTTLVal: defaultLeaseTTLHr,
MaxLeaseTTLVal: maxLeaseTTLHr,
},
StorageView: &logical.InmemStorage{},
}
err := b.Setup(context.Background(), config)
if err != nil {
t.Fatalf("unable to create backend: %v", err)
}
return b, config.StorageView
}
func TestPeriodicFuncNilConfig(t *testing.T) {
b := backend()
config := &logical.BackendConfig{
Logger: logging.NewVaultLogger(log.Trace),
System: &logical.StaticSystemView{
DefaultLeaseTTLVal: defaultLeaseTTLHr,
MaxLeaseTTLVal: maxLeaseTTLHr,
},
StorageView: &logical.InmemStorage{},
}
err := b.Setup(context.Background(), config)
if err != nil {
t.Fatalf("unable to create backend: %v", err)
}
b.settings = new(clientSettings)
mockProvider := newMockProvider()
b.getProvider = func(context.Context, hclog.Logger, logical.SystemView, *clientSettings) (AzureProvider, error) {
return mockProvider, nil
}
err = b.periodicFunc(context.Background(), &logical.Request{
Storage: config.StorageView,
})
if err != nil {
t.Fatalf("periodicFunc error not nil, it should have been: %v", err)
}
}