This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathstorageredis_test.go
254 lines (197 loc) · 7.18 KB
/
storageredis_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
package storageredis
import (
"context"
"encoding/json"
"errors"
"io/fs"
"os"
"path"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
const TestPrefix = "redistlstest"
// these tests needs a running Redis server
func setupRedisEnv(t *testing.T) *RedisStorage {
os.Setenv(EnvNameKeyPrefix, TestPrefix)
os.Setenv(EnvNameRedisDB, "9")
rd := new(RedisStorage)
rd.GetConfigValue()
err := rd.BuildRedisClient(context.TODO())
// skip test if no redis storage
if err != nil {
t.Skip()
return nil
}
assert.NoError(t, err)
assert.Equal(t, TestPrefix, rd.KeyPrefix)
assert.Equal(t, 9, rd.DB)
_, err = rd.Client.FlushAll(rd.ctx).Result()
assert.NoError(t, err)
return rd
}
func TestRedisStorage_Store(t *testing.T) {
rd := setupRedisEnv(t)
err := rd.Store(context.TODO(), path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"), []byte("crt data"))
assert.NoError(t, err)
}
func TestRedisStorage_Exists(t *testing.T) {
rd := setupRedisEnv(t)
key := path.Join("acme", "example.com", "sites", "example.com", "example.com.crt")
err := rd.Store(context.TODO(), key, []byte("crt data"))
assert.NoError(t, err)
exists := rd.Exists(context.TODO(), key)
assert.True(t, exists)
}
func TestRedisStorage_Load(t *testing.T) {
rd := setupRedisEnv(t)
key := path.Join("acme", "example.com", "sites", "example.com", "example.com.crt")
content := []byte("crt data")
err := rd.Store(context.TODO(), key, content)
assert.NoError(t, err)
contentLoded, err := rd.Load(context.TODO(), key)
assert.NoError(t, err)
assert.Equal(t, content, contentLoded)
}
func TestRedisStorage_Delete(t *testing.T) {
rd := setupRedisEnv(t)
key := path.Join("acme", "example.com", "sites", "example.com", "example.com.crt")
content := []byte("crt data")
err := rd.Store(context.TODO(), key, content)
assert.NoError(t, err)
err = rd.Delete(context.TODO(), key)
assert.NoError(t, err)
exists := rd.Exists(context.TODO(), key)
assert.False(t, exists)
contentLoaded, err := rd.Load(context.TODO(), key)
assert.Nil(t, contentLoaded)
notExist := errors.Is(err, fs.ErrNotExist)
assert.True(t, notExist)
}
func TestRedisStorage_Stat(t *testing.T) {
rd := setupRedisEnv(t)
key := path.Join("acme", "example.com", "sites", "example.com", "example.com.crt")
content := []byte("crt data")
err := rd.Store(context.TODO(), key, content)
assert.NoError(t, err)
info, err := rd.Stat(context.TODO(), key)
assert.NoError(t, err)
assert.Equal(t, key, info.Key)
}
func TestRedisStorage_List(t *testing.T) {
rd := setupRedisEnv(t)
err := rd.Store(context.TODO(), path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"), []byte("crt"))
assert.NoError(t, err)
err = rd.Store(context.TODO(), path.Join("acme", "example.com", "sites", "example.com", "example.com.key"), []byte("key"))
assert.NoError(t, err)
err = rd.Store(context.TODO(), path.Join("acme", "example.com", "sites", "example.com", "example.com.json"), []byte("meta"))
assert.NoError(t, err)
keys, err := rd.List(context.TODO(), path.Join("acme", "example.com", "sites", "example.com"), true)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"))
keys, err = rd.List(context.TODO(), "*", true)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"))
keys, err = rd.List(context.TODO(), "", true)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"))
keys, err = rd.List(context.TODO(), " ", true)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"))
}
func TestRedisStorage_ListNonRecursive(t *testing.T) {
rd := setupRedisEnv(t)
err := rd.Store(context.TODO(), path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"), []byte("crt"))
assert.NoError(t, err)
err = rd.Store(context.TODO(), path.Join("acme", "example.com", "sites", "example.com", "example.com.key"), []byte("key"))
assert.NoError(t, err)
err = rd.Store(context.TODO(), path.Join("acme", "example.com", "sites", "example.com", "example.com.json"), []byte("meta"))
assert.NoError(t, err)
keys, err := rd.List(context.TODO(), path.Join("acme", "example.com", "sites"), false)
assert.NoError(t, err)
assert.Len(t, keys, 1)
assert.Contains(t, keys, path.Join("acme", "example.com", "sites", "example.com"))
keys, err = rd.List(context.TODO(), "*", false)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"))
keys, err = rd.List(context.TODO(), "", false)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"))
keys, err = rd.List(context.TODO(), " ", false)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, path.Join("acme", "example.com", "sites", "example.com", "example.com.crt"))
}
func TestRedisStorage_LockUnlock(t *testing.T) {
rd := setupRedisEnv(t)
lockKey := path.Join("acme", "example.com", "sites", "example.com", "lock")
err := rd.Lock(context.TODO(), lockKey)
assert.NoError(t, err)
err = rd.Unlock(context.TODO(), lockKey)
assert.NoError(t, err)
}
func lockAndUnlock(wg *sync.WaitGroup, t *testing.T, rd *RedisStorage, lockKey string) {
defer wg.Done()
err := rd.Lock(context.TODO(), lockKey)
assert.NoError(t, err)
err = rd.Unlock(context.TODO(), lockKey)
assert.NoError(t, err)
}
func TestRedisStorage_MultipleLocks(t *testing.T) {
lockKey := path.Join("acme", "example.com", "sites", "example.com", "lock")
var wg sync.WaitGroup
rds := make([]*RedisStorage, 100)
for i := 0; i < 100; i++ {
rd := setupRedisEnv(t)
wg.Add(1)
rds[i] = rd
}
for i := 0; i < len(rds); i++ {
go lockAndUnlock(&wg, t, rds[i], lockKey)
}
wg.Wait()
}
func TestRedisStorage_String(t *testing.T) {
rd := new(RedisStorage)
redacted := `REDACTED`
t.Run("validate password", func(t *testing.T) {
t.Run("is redacted when set", func(t *testing.T) {
testrd := new(RedisStorage)
password := "iAmASuperSecurePassword"
rd.Password = password
err := json.Unmarshal([]byte(rd.String()), &testrd)
assert.NoError(t, err)
assert.Equal(t, redacted, testrd.Password)
assert.Equal(t, password, rd.Password)
})
rd.Password = ""
t.Run("is empty if not set", func(t *testing.T) {
err := json.Unmarshal([]byte(rd.String()), &rd)
assert.NoError(t, err)
assert.Empty(t, rd.Password)
})
})
t.Run("validate AES key", func(t *testing.T) {
t.Run("is redacted when set", func(t *testing.T) {
testrd := new(RedisStorage)
aeskey := "abcdefghijklmnopqrstuvwxyz123456"
rd.AesKey = aeskey
err := json.Unmarshal([]byte(rd.String()), &testrd)
assert.NoError(t, err)
assert.Equal(t, redacted, testrd.AesKey)
assert.Equal(t, aeskey, rd.AesKey)
})
rd.AesKey = ""
t.Run("is empty if not set", func(t *testing.T) {
err := json.Unmarshal([]byte(rd.String()), &rd)
assert.NoError(t, err)
assert.Empty(t, rd.AesKey)
})
})
}