forked from cleanshavenalex/pvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructfill_test.go
More file actions
167 lines (158 loc) · 3.31 KB
/
structfill_test.go
File metadata and controls
167 lines (158 loc) · 3.31 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package pvc
import (
"bytes"
"fmt"
"testing"
)
type fakeBackend struct {
GetFunc func(id string) ([]byte, error)
}
func (fb *fakeBackend) Get(id string) ([]byte, error) {
if fb.GetFunc != nil {
return fb.GetFunc(id)
}
return []byte{}, nil
}
var _ secretBackend = &fakeBackend{}
type secrets struct {
Name string `secret:"name"`
Key []byte `secret:"key"`
IgnoredKey string `secret:"-"`
Unused string
}
type badsecrets struct {
Age uint `secret:"age"`
}
type badsecrets2 struct {
RandomThing []rune `secret:"random"`
}
var somestr = "asdf"
var nilptr *int = nil
var ifacewithnil interface{} = nilptr
func TestSecretsClient_Fill(t *testing.T) {
tests := []struct {
name string
backend secretBackend
s interface{}
wantErr bool
validatef func(s interface{}) error
}{
{
name: "success",
backend: &fakeBackend{
GetFunc: func(id string) ([]byte, error) {
switch id {
case "name":
return []byte("Frank"), nil
case "key":
return []byte("asdf"), nil
default:
return nil, fmt.Errorf("unknown id: %v", id)
}
},
},
s: &secrets{},
validatef: func(s interface{}) error {
v, ok := s.(*secrets)
if !ok {
return fmt.Errorf("bad type: %T", s)
}
if v == nil {
return fmt.Errorf("s is nil")
}
if v.Name != "Frank" {
return fmt.Errorf("bad name: %v", v.Name)
}
if !bytes.Equal(v.Key, []byte("asdf")) {
return fmt.Errorf("bad key: %v", string(v.Key))
}
if v.IgnoredKey != "" {
return fmt.Errorf("ignored field should be an empty string: %v", v.IgnoredKey)
}
if v.Unused != "" {
return fmt.Errorf("unused should be an empty string: %v", v.Unused)
}
return nil
},
},
{
name: "bad type",
backend: &fakeBackend{
GetFunc: func(id string) ([]byte, error) {
return []byte("anything"), nil
},
},
s: &badsecrets{},
wantErr: true,
},
{
name: "bad slice type",
backend: &fakeBackend{
GetFunc: func(id string) ([]byte, error) {
return []byte("anything"), nil
},
},
s: &badsecrets2{},
wantErr: true,
},
{
name: "not a struct",
backend: &fakeBackend{
GetFunc: func(id string) ([]byte, error) {
return []byte("anything"), nil
},
},
s: &somestr,
wantErr: true,
},
{
name: "not a pointer",
backend: &fakeBackend{
GetFunc: func(id string) ([]byte, error) {
return []byte("anything"), nil
},
},
s: somestr,
wantErr: true,
},
{
name: "nil",
backend: &fakeBackend{
GetFunc: func(id string) ([]byte, error) {
return []byte("anything"), nil
},
},
wantErr: true,
},
{
name: "interface with nil",
backend: &fakeBackend{
GetFunc: func(id string) ([]byte, error) {
return []byte("anything"), nil
},
},
s: ifacewithnil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sc := &SecretsClient{
backend: tt.backend,
}
err := sc.Fill(tt.s)
if (err != nil) != tt.wantErr {
t.Errorf("Fill() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
t.Logf("err: %v", err)
return
}
if tt.validatef != nil {
if err := tt.validatef(tt.s); err != nil {
t.Errorf("validate failed: %v", err)
}
}
})
}
}