-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkv1_test.go
More file actions
355 lines (287 loc) · 8.44 KB
/
kv1_test.go
File metadata and controls
355 lines (287 loc) · 8.44 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package vaultkv_test
import (
"fmt"
"sort"
"strings"
"github.com/cloudfoundry-community/vaultkv"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("KVv1", func() {
//This is a hack because I don't want to refactor _everything_ in these tests
var getAssertionPath string
var AssertGetEquals = func(expected map[string]string) func() {
return func() {
output := make(map[string]string)
err = vault.Get(getAssertionPath, &output)
Expect(err).NotTo(HaveOccurred())
Expect(output).To(Equal(expected))
}
}
var AssertExists = func(exists bool) func() {
var fn func()
if exists {
fn = func() {
err = vault.Get(getAssertionPath, nil)
Expect(err).NotTo(HaveOccurred())
}
} else {
fn = func() {
err = vault.Get(getAssertionPath, nil)
AssertErrorOfType(&vaultkv.ErrNotFound{})
}
}
return fn
}
BeforeEach(func() {
InitAndUnsealVault()
})
Describe("Setting something in the Vault", func() {
var testPath string
var testValue map[string]string
BeforeEach(func() {
testPath = "secret/foo"
testValue = map[string]string{
"foo": "bar",
"beep": "boop",
}
})
JustBeforeEach(func() {
err = vault.Set(testPath, testValue)
})
When("the value is nil", func() {
BeforeEach(func() {
testValue = nil
getAssertionPath = testPath
})
It("should err properly", func() {
By("returning ErrBadRequest")
AssertErrorOfType(&vaultkv.ErrBadRequest{})()
By("having Get find nothing at this path after the call")
AssertExists(false)()
})
})
When("the path is doesn't correspond to a mounted backend", func() {
BeforeEach(func() {
testPath = "notabackend/foo"
})
It("should err properly", func() {
By("returning ErrNotFound")
AssertErrorOfType(&vaultkv.ErrNotFound{})()
By("having Get find nothing at this path after the call")
AssertExists(false)()
})
})
When("the path has a leading slash", func() {
BeforeEach(func() {
testPath = "/secret/foo"
})
It("should get inserted properly", func() {
By("not erroring")
Expect(err).NotTo(HaveOccurred())
By("having get find the key at the path without a slash")
getAssertionPath = strings.TrimPrefix(testPath, "/")
AssertExists(true)()
By("having get find the key at the path without a slash")
AssertGetEquals(map[string]string{"foo": "bar", "beep": "boop"})()
By("having get find the key at the path with a slash")
getAssertionPath = testPath
AssertExists(true)()
By("having get find the key at the path with a slash")
AssertGetEquals(map[string]string{"foo": "bar", "beep": "boop"})()
})
})
When("the path has a trailing slash", func() {
BeforeEach(func() {
testPath = "secret/foo/"
})
It("should get inserted properly", func() {
By("not erroring")
Expect(err).NotTo(HaveOccurred())
By("having get find the key at the path without a slash")
getAssertionPath = strings.TrimSuffix(testPath, "/")
AssertExists(true)()
By("having get find the key at the path without a slash")
AssertGetEquals(map[string]string{"foo": "bar", "beep": "boop"})()
By("having get find the key at the path with a slash")
getAssertionPath = testPath
AssertExists(true)()
By("having get find the key at the path with a slash")
AssertGetEquals(map[string]string{"foo": "bar", "beep": "boop"})()
})
})
When("setting an already set key", func() {
var secondTestValue map[string]string
BeforeEach(func() {
secondTestValue = map[string]string{
"thisisanotherkey": "thisisanothervalue",
}
getAssertionPath = testPath
})
JustBeforeEach(func() {
err = vault.Set(testPath, secondTestValue)
})
It("should overwrite the value", func() {
By("not erroring")
Expect(err).NotTo(HaveOccurred())
By("having Get find the value that was added second")
AssertGetEquals(map[string]string{"thisisanotherkey": "thisisanothervalue"})
})
})
Describe("Get", func() {
var getTestPath string
var getOutputValue map[string]string
BeforeEach(func() {
getOutputValue = make(map[string]string)
})
JustBeforeEach(func() {
err = vault.Get(getTestPath, &getOutputValue)
})
When("the key exists", func() {
BeforeEach(func() {
getTestPath = testPath
})
It("should retrieve the key", func() {
By("not erroring")
Expect(err).NotTo(HaveOccurred())
By("returning the value that was inserted")
Expect(getOutputValue).To(Equal(testValue))
})
})
When("the key doesn't exist", func() {
BeforeEach(func() {
getTestPath = fmt.Sprintf("%sabcd", testPath)
})
It("should return ErrNotFound", AssertErrorOfType(&vaultkv.ErrNotFound{}))
})
})
Describe("Delete", func() {
var deleteTestPath string
JustBeforeEach(func() {
err = vault.Delete(deleteTestPath)
})
When("the key exists", func() {
BeforeEach(func() {
deleteTestPath = testPath
getAssertionPath = testPath
})
It("should delete the key", func() {
By("not erroring")
Expect(err).NotTo(HaveOccurred())
By("get not finding the deleted key")
AssertExists(false)()
})
})
When("the key doesn't exist", func() {
BeforeEach(func() {
deleteTestPath = fmt.Sprintf("%sabcd", testPath)
})
It("should not return an error", func() { Expect(err).NotTo(HaveOccurred()) })
})
})
Describe("Adding another key with multiple parts", func() {
var secondTestPath string
var secondTestValue map[string]string
BeforeEach(func() {
secondTestPath = "secret/beep/bar"
secondTestValue = map[string]string{
"werealljustlittlebabybirds": "peckingourwayoutofourshells",
}
})
JustBeforeEach(func() {
err = vault.Set(secondTestPath, secondTestValue)
Expect(err).NotTo(HaveOccurred())
})
Describe("List", func() {
var listTestPath string
var listTestOutput []string
JustBeforeEach(func() {
listTestOutput, err = vault.List(listTestPath)
})
//Order doesn't matter
var AssertListEquals = func(expected []string) func() {
return func() {
Expect(listTestOutput).ToNot(BeNil())
Expect(expected).ToNot(BeNil())
sort.Strings(expected)
sort.Strings(listTestOutput)
Expect(listTestOutput).To(Equal(expected))
}
}
Context("on `secret'", func() {
BeforeEach(func() {
listTestPath = "secret"
})
It("should return the correct paths", func() {
By("not erroring")
Expect(err).NotTo(HaveOccurred())
By("returning the correct list of paths")
AssertListEquals([]string{"foo", "beep/"})
})
})
Context("on the dir of the nested key", func() {
BeforeEach(func() {
listTestPath = "secret/beep"
})
It("should return the correct paths", func() {
By("not erroring")
Expect(err).NotTo(HaveOccurred())
By("returning the correct list of paths")
AssertListEquals([]string{"bar"})
})
})
When("the path doesn't exist", func() {
BeforeEach(func() {
listTestPath = "secret/boo/hiss"
})
It("should return an ErrNotFound", AssertErrorOfType(&vaultkv.ErrNotFound{}))
})
When("the path is a secret, not a folder", func() {
BeforeEach(func() {
listTestPath = "secret/foo"
})
It("should return an ErrNotFound", AssertErrorOfType(&vaultkv.ErrNotFound{}))
})
})
Describe("Get", func() {
var getTestPath string
var getOutputValue map[string]string
BeforeEach(func() {
getOutputValue = make(map[string]string)
})
JustBeforeEach(func() {
err = vault.Get(getTestPath, &getOutputValue)
})
Context("on the nested key", func() {
BeforeEach(func() {
getTestPath = secondTestPath
})
It("should retrieve the value", func() {
By("not erroring")
Expect(err).NotTo(HaveOccurred())
By("returning the value that was inserted")
Expect(getOutputValue).To(Equal(secondTestValue))
})
})
})
Describe("Delete", func() {
var deleteTestPath string
JustBeforeEach(func() {
err = vault.Delete(deleteTestPath)
})
Context("on the nested key", func() {
BeforeEach(func() {
deleteTestPath = secondTestPath
getAssertionPath = deleteTestPath
})
It("should delete the key", func() {
By("not erroring")
Expect(err).NotTo(HaveOccurred())
By("having Get be unable to find the key")
AssertExists(false)()
})
})
})
})
})
})