/
test_utils.go
238 lines (199 loc) · 6.45 KB
/
test_utils.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
package backends
import (
"context"
"errors"
as "github.com/aerospike/aerospike-client-go/v6"
as_types "github.com/aerospike/aerospike-client-go/v6/types"
"github.com/google/gomemcache/memcache"
"github.com/prebid/prebid-cache/utils"
)
// ------------------------------------------
// Aerospike client mocks
// ------------------------------------------
func NewMockAerospikeBackend(mockClient AerospikeDB) *AerospikeBackend {
return &AerospikeBackend{client: mockClient}
}
type ErrorProneAerospikeClient struct {
ServerError string
}
func (c *ErrorProneAerospikeClient) NewUUIDKey(namespace string, key string) (*as.Key, error) {
if c.ServerError == "TEST_KEY_GEN_ERROR" {
return nil, &as.AerospikeError{ResultCode: as_types.NOT_AUTHENTICATED}
}
return nil, nil
}
func (c *ErrorProneAerospikeClient) Get(key *as.Key) (*as.Record, error) {
if c.ServerError == "TEST_GET_ERROR" {
return nil, &as.AerospikeError{ResultCode: as_types.KEY_NOT_FOUND_ERROR}
} else if c.ServerError == "TEST_NO_BUCKET_ERROR" {
return &as.Record{Bins: as.BinMap{"AnyKey": "any_value"}}, nil
} else if c.ServerError == "TEST_NON_STRING_VALUE_ERROR" {
return &as.Record{Bins: as.BinMap{binValue: 0.0}}, nil
}
return nil, nil
}
func (c *ErrorProneAerospikeClient) Put(policy *as.WritePolicy, key *as.Key, binMap as.BinMap) error {
if c.ServerError == "TEST_PUT_ERROR" {
return &as.AerospikeError{ResultCode: as_types.KEY_EXISTS_ERROR}
}
return nil
}
// Aerospike client that does not throw errors
type GoodAerospikeClient struct {
StoredData map[string]string
}
func (c *GoodAerospikeClient) Get(aeKey *as.Key) (*as.Record, error) {
if aeKey != nil && aeKey.Value() != nil {
key := aeKey.Value().String()
if value, found := c.StoredData[key]; found {
rec := &as.Record{
Bins: as.BinMap{binValue: value},
}
return rec, nil
}
}
return nil, &as.AerospikeError{ResultCode: as_types.KEY_NOT_FOUND_ERROR}
}
func (c *GoodAerospikeClient) Put(policy *as.WritePolicy, aeKey *as.Key, binMap as.BinMap) error {
if aeKey != nil && aeKey.Value() != nil {
key := aeKey.Value().String()
if interfaceValue, found := binMap[binValue]; found {
if str, asserted := interfaceValue.(string); asserted {
c.StoredData[key] = str
}
}
return nil
}
return &as.AerospikeError{ResultCode: as_types.KEY_MISMATCH}
}
func (c *GoodAerospikeClient) NewUUIDKey(namespace string, key string) (*as.Key, error) {
return as.NewKey(namespace, setName, key)
}
// ------------------------------------------
// Cassandra client mocks
// ------------------------------------------
func NewMockCassandraBackend(ttl int, mockClient CassandraDB) *CassandraBackend {
return &CassandraBackend{
defaultTTL: ttl,
client: mockClient,
}
}
type ErrorProneCassandraClient struct {
Applied bool
ServerError error
}
func (ec *ErrorProneCassandraClient) Init() error {
return errors.New("init error")
}
func (ec *ErrorProneCassandraClient) Get(ctx context.Context, key string) (string, error) {
return "", ec.ServerError
}
func (ec *ErrorProneCassandraClient) Put(ctx context.Context, key string, value string, ttlSeconds int) (bool, error) {
return ec.Applied, ec.ServerError
}
// Cassandra client client that does not throw errors
type GoodCassandraClient struct {
StoredData map[string]string
}
func (gc *GoodCassandraClient) Init() error {
return nil
}
func (gc *GoodCassandraClient) Get(ctx context.Context, key string) (string, error) {
if value, found := gc.StoredData[key]; found {
return value, nil
}
return "", utils.NewPBCError(utils.KEY_NOT_FOUND)
}
func (gc *GoodCassandraClient) Put(ctx context.Context, key string, value string, ttlSeconds int) (bool, error) {
if _, found := gc.StoredData[key]; !found {
gc.StoredData[key] = value
}
return true, nil
}
// ------------------------------------------
// Memcache client mocks
// ------------------------------------------
func NewMockMemcacheBackend(mockClient MemcacheDataStore) *MemcacheBackend {
return &MemcacheBackend{
memcache: mockClient,
}
}
type ErrorProneMemcache struct {
ServerError error
}
func (ec *ErrorProneMemcache) Get(key string) (*memcache.Item, error) {
return nil, ec.ServerError
}
func (ec *ErrorProneMemcache) Put(key string, value string, ttlSeconds int) error {
return ec.ServerError
}
// Memcache client that does not throw errors
type GoodMemcache struct {
StoredData map[string]string
}
func (gm *GoodMemcache) Get(key string) (*memcache.Item, error) {
if value, found := gm.StoredData[key]; found {
return &memcache.Item{Key: key, Value: []byte(value)}, nil
}
return nil, utils.NewPBCError(utils.KEY_NOT_FOUND)
}
func (gm *GoodMemcache) Put(key string, value string, ttlSeconds int) error {
if _, found := gm.StoredData[key]; !found {
gm.StoredData[key] = value
}
return nil
}
// ------------------------------------------
// Redis client mocks
// ------------------------------------------
func NewFakeRedisBackend(mockClient RedisDB) *RedisBackend {
return &RedisBackend{
client: mockClient,
}
}
type FakeRedisClient struct {
StoredData map[string]string
ServerError error
Success bool
}
// Get returns an error if the FakeRedisClient has a non-nil ServerError field.
func (r FakeRedisClient) Get(ctx context.Context, key string) (string, error) {
if r.ServerError != nil {
return "", r.ServerError
}
if value, found := r.StoredData[key]; found {
return value, nil
}
return "", utils.NewPBCError(utils.KEY_NOT_FOUND)
}
func (r FakeRedisClient) Put(ctx context.Context, key string, value string, ttlSeconds int) (bool, error) {
if _, found := r.StoredData[key]; !found {
r.StoredData[key] = value
}
return r.Success, r.ServerError
}
// ------------------------------------------
// Memory client mocks
// ------------------------------------------
func NewErrorResponseMemoryBackend() *ErrorProneMemoryClient {
return &ErrorProneMemoryClient{}
}
type ErrorProneMemoryClient struct{}
func (ec *ErrorProneMemoryClient) Get(ctx context.Context, key string) (string, error) {
return "", errors.New("Bakend error")
}
func (ec *ErrorProneMemoryClient) Put(ctx context.Context, key string, value string, ttlSeconds int) error {
return errors.New("Bakend error")
}
// Good memory client does not throw errors
func NewMemoryBackendWithValues(customData map[string]string) (*MemoryBackend, error) {
backend := NewMemoryBackend()
if len(customData) > 0 {
for k, v := range customData {
if err := backend.Put(context.Background(), k, v, 1); err != nil {
return backend, err
}
}
}
return backend, nil
}