-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend_test.go
81 lines (67 loc) · 2.18 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
package exoscale
import (
"context"
"testing"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/suite"
)
type testSuite struct {
suite.Suite
backend logical.Backend
storage logical.Storage
}
func (ts *testSuite) storeEntry(k string, v interface{}) {
entry, err := logical.StorageEntryJSON(k, v)
if err != nil {
ts.FailNow("unable to JSON-encode entry", err)
}
if err := ts.storage.Put(context.Background(), entry); err != nil {
ts.FailNow("unable to store entry", err)
}
}
func (ts *testSuite) SetupTest() {
config := logical.TestBackendConfig()
config.StorageView = new(logical.InmemStorage)
// putting raw data from a previous version of the plugin
// in storage to see if it is able to work with it
ts.Require().NoError(config.StorageView.Put(context.Background(), &logical.StorageEntry{
Key: "config/lease",
Value: []byte(`{"ttl":46800000000000,"max_ttl":72000000000000}`),
}))
ts.Require().NoError(config.StorageView.Put(context.Background(), &logical.StorageEntry{
Key: "config/root",
Value: []byte(`{"api_environment":"api","root_api_key":"EXO0000","root_api_secret":"xxxxxxxx","zone":"ch-gva-2"}`),
}))
ts.Require().NoError(config.StorageView.Put(context.Background(), &logical.StorageEntry{
Key: "role/mylegacyrole",
Value: []byte(`{"operations":["list-instance-types","list-templates","list-zones"],"resources":["sos/bucket:test"],"tags":["read"],"lease_config":{"ttl":600000000000,"max_ttl":3000000000000},"renewable":false}`),
}))
backend, err := Factory(context.Background(), config)
if err != nil {
ts.T().Fatal(err)
}
err = backend.Initialize(context.Background(), &logical.InitializationRequest{
Storage: config.StorageView,
})
if err != nil {
ts.T().Fatal(err)
}
backend.(*exoscaleBackend).exo.egoscaleClient = new(mockEgoscaleClient)
ts.backend = backend
ts.storage = config.StorageView
}
func (ts *testSuite) TearDownTest() {
ts.backend = nil
ts.storage = nil
}
func (ts *testSuite) randomID() string {
id, err := uuid.GenerateUUID()
if err != nil {
ts.T().Fatalf("unable to generate a new UUID: %s", err)
}
return id
}
func TestSuite(t *testing.T) {
suite.Run(t, new(testSuite))
}