forked from hashicorp/vault-plugin-secrets-azure
-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers_test.go
86 lines (73 loc) · 1.91 KB
/
helpers_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azuresecrets
import (
"bytes"
"encoding/json"
"strings"
"testing"
"github.com/hashicorp/vault/sdk/logical"
"github.com/go-test/deep"
uuid "github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
)
func init() {
deep.CompareUnexportedFields = true
}
// assertErrorIsNil tests for non-nil errors
func assertErrorIsNil(tb testing.TB, err error) {
tb.Helper()
if err != nil {
tb.Fatalf("\nunexpected error: %s", err.Error())
}
}
// assertKeyExists asserts that the provided key exists within the map
func assertKeyExists(tb testing.TB, m map[string]interface{}, key string) {
tb.Helper()
if _, exists := m[key]; !exists {
tb.Fatalf("key %s does not exist", key)
}
}
// equal tests for deep equality
func equal(tb testing.TB, exp, act interface{}) {
tb.Helper()
if diff := deep.Equal(exp, act); diff != nil {
tb.Fatal(diff)
}
}
// encodeJSON is an optimistic JSON encoder that will
// panic on error.
func encodeJSON(data interface{}) string {
d, err := jsonutil.EncodeJSON(data)
if err != nil {
panic(err)
}
return strings.TrimSpace(string(d))
}
// compactJSON is a JSON compactor that works on strings
// and panics on any error
func compactJSON(s string) string {
var b bytes.Buffer
if err := json.Compact(&b, []byte(s)); err != nil {
panic(err)
}
return b.String()
}
// generateUUID is an optimistic UUID source that will
// panic on error.
func generateUUID() string {
u, err := uuid.GenerateUUID()
if err != nil {
panic(err)
}
return u
}
// fakeSaveLoad will simulate the JSON encoding/decoding process that secrets will normally go
// through. If not done, some of the secret data will retain richer data types (e.g. pointers)
// than would normally be seen.
func fakeSaveLoad(s *logical.Secret) {
enc := encodeJSON(s)
if err := jsonutil.DecodeJSON([]byte(enc), s); err != nil {
panic(err)
}
}