-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
261 lines (224 loc) · 5.72 KB
/
storage_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
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
package certmagicsqlite3_test
import (
"context"
"io/fs"
"testing"
"time"
"github.com/crmejia/certmagic-sqlite3"
"github.com/google/go-cmp/cmp"
)
func TestStorage_RoundtripStoreLoadExistsDelete(t *testing.T) {
t.Parallel()
tempDB := t.TempDir() + "roundtrip.db"
storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
key := "certkey"
want := []byte("certificate")
exists := storage.Exists(ctx, key)
if exists {
t.Error("key should not exist")
}
err = storage.Store(ctx, key, want)
if err != nil {
t.Fatal(err)
}
got, err := storage.Load(ctx, key)
if !cmp.Equal(want, got) {
t.Errorf("want value to be %s, got %s", want, got)
}
exists = storage.Exists(ctx, key)
if !exists {
t.Error("want key to exist")
}
err = storage.Delete(ctx, key)
if err != nil {
t.Error("want no error on delete existing key")
}
exists = storage.Exists(ctx, key)
if exists {
t.Error("key should not exist")
}
}
func TestInsertExistingKeyUpdates(t *testing.T) {
t.Parallel()
tempDB := t.TempDir() + "insert.db"
storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
key := "certkey"
want := []byte("certificate")
err = storage.Store(ctx, key, want)
if err != nil {
t.Fatal(err)
}
oldKeyInfo, err := storage.Stat(ctx, key)
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
newValue := "this is a new value"
err = storage.Store(ctx, key, []byte(newValue))
if err != nil {
t.Fatal(err)
}
newKeyInfo, err := storage.Stat(ctx, key)
if err != nil {
t.Fatal(err)
}
if oldKeyInfo.Key != newKeyInfo.Key {
t.Errorf("want new key and old key to be the same, got old %s and new %s", oldKeyInfo.Key, newKeyInfo.Key)
}
if oldKeyInfo.Modified == newKeyInfo.Modified {
t.Errorf("want new modified and old modified to be different,\n newModified: %v, oldModified: %v", newKeyInfo.Modified, oldKeyInfo.Modified)
}
if oldKeyInfo.Size == newKeyInfo.Size {
t.Error("want new size and old size to be different")
}
}
func TestList(t *testing.T) {
t.Parallel()
tempDB := t.TempDir() + "list.db"
storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
keys := []string{
"key1/key1",
"key1/key2",
"key2/key1/",
"key3/key1/",
"key1/key3",
"key1/",
"key1",
}
value := []byte("certificate")
for _, k := range keys {
err = storage.Store(ctx, k, value)
if err != nil {
t.Fatal(err)
}
}
key1Slice, err := storage.List(ctx, "key1", false)
if err != nil {
t.Fatal(err)
}
wantKey1 := 5
if len(key1Slice) != wantKey1 {
t.Errorf("want %d keys to be on the list, got %d", wantKey1, len(key1Slice))
}
}
func TestListErrorsOnRecursiveTrue(t *testing.T) {
t.Parallel()
tempDB := t.TempDir() + "recursive.db"
storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
if err != nil {
t.Fatal(err)
}
_, err = storage.List(context.Background(), "key", true)
if err == nil {
t.Error("want error on recursive = true")
}
}
func TestLoadReturnsFsErrNotExistOnNoKey(t *testing.T) {
t.Parallel()
tempDB := t.TempDir() + "fsError.db"
storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
const key = "key"
v, err := storage.Load(ctx, key)
// if err != fs.ErrNotExist {
if err == nil {
t.Errorf("want %s, got %s, value: %s --", fs.ErrNotExist, err, v)
}
}
func TestDeleteReturnsFsErrNotExistOnNoKey(t *testing.T) {
t.Parallel()
tempDB := t.TempDir() + "fsError.db"
storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
const key = "key"
err = storage.Delete(ctx, key)
if err != fs.ErrNotExist {
t.Errorf("want %s, got %s", fs.ErrNotExist, err)
}
}
func TestListReturnsFsErrNotExistOnNoKey(t *testing.T) {
t.Parallel()
tempDB := t.TempDir() + "fsError.db"
storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
const key = "key"
_, err = storage.List(ctx, key, false)
if err != fs.ErrNotExist {
t.Errorf("want %s, got %s", fs.ErrNotExist, err)
}
}
func TestStatReturnsFsErrNotExistOnNoKey(t *testing.T) {
t.Parallel()
tempDB := t.TempDir() + "fsError.db"
storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
const key = "key"
_, err = storage.Stat(ctx, key)
if err != fs.ErrNotExist {
t.Errorf("want %s, got %s", fs.ErrNotExist, err)
}
}
func TestLockUnlockErrors(t *testing.T) {
t.Parallel()
tempDB := t.TempDir() + "lockUnlockErrors.db"
storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
const testLock = "testLock"
storage.SetLockTimeOut(100 * time.Millisecond)
err = storage.Lock(ctx, testLock)
if err == nil {
t.Errorf("this is unimplemented. So want to get unimplemented error:")
}
err = storage.Unlock(ctx, testLock)
if err == nil {
t.Errorf("this is unimplemented. So want to get unimplemented error:")
}
}
// func TestLockTimesOut(t *testing.T) {
// t.Parallel()
// tempDB := t.TempDir() + "fsError.db"
// storage, err := certmagicsqlite3.OpenSQLiteStorage(tempDB)
// if err != nil {
// t.Fatal(err)
// }
// ctx := context.Background()
// const testLock = "testLock"
// storage.SetLockTimeOut(100 * time.Millisecond)
//
// err = storage.Lock(ctx, testLock)
// if err != nil{
// t.Fatalf("want to get Lock. Got an error instead: %s", err.Error())
// }
// time.Sleep(200*time.Millisecond)
// err = storage.Lock(ctx, testLock)
// if err != nil{
// t.Fatalf("should be able to get Lock. Got an error instead: %s", err.Error())
// }
// }