-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtarget_repo_test.go
279 lines (248 loc) · 6.97 KB
/
target_repo_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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package repository
import (
"context"
"errors"
"testing"
"github.com/harness/ff-proxy/v2/cache"
"github.com/harness/ff-proxy/v2/domain"
clientgen "github.com/harness/ff-proxy/v2/gen/client"
"github.com/harness/ff-proxy/v2/log"
"github.com/stretchr/testify/assert"
)
type mockCache struct {
set func() error
get func() error
delete func() error
}
func (m *mockCache) HealthCheck(ctx context.Context) error {
return nil
}
// Set sets a value in the cache for a given key and field
func (m *mockCache) Set(ctx context.Context, key string, value interface{}) error {
return m.set()
}
// Get gets the value of a field for a given key
func (m *mockCache) Get(ctx context.Context, key string, v interface{}) error {
return m.get()
}
// Remove removes a field for a given key
func (m *mockCache) Delete(ctx context.Context, key string) error {
return m.delete()
}
func (m *mockCache) Keys(ctx context.Context, key string) ([]string, error) {
return []string{}, nil
}
func strPtr(s string) *string { return &s }
func int64Ptr(i int64) *int64 { return &i }
func boolPtr(b bool) *bool { return &b }
var (
targetFoo = domain.Target{
Target: clientgen.Target{
Account: "foo",
Anonymous: boolPtr(false),
CreatedAt: int64Ptr(1634222520273),
Environment: "featureflagsqa",
Identifier: "foo",
Name: "foo",
Org: "foo",
Project: "FeatureFlagsQADemo",
Segments: &[]clientgen.Segment{},
Attributes: &map[string]interface{}{
"age": float64(55),
"ages": []interface{}{
float64(1),
float64(2),
float64(3),
},
"happy": true,
"host": "foo.com",
"userGroups": []interface{}{"Foo", "Volvo", "BMW"},
},
},
}
targetBar = domain.Target{
Target: clientgen.Target{
Account: "bar",
Anonymous: boolPtr(false),
CreatedAt: int64Ptr(1634222520273),
Environment: "featureflagsqa",
Identifier: "bar",
Name: "bar",
Org: "bar",
Project: "FeatureFlagsQADemo",
Segments: &[]clientgen.Segment{},
Attributes: &map[string]interface{}{
"age": float64(55),
"ages": []interface{}{
float64(1),
float64(2),
float64(3),
},
"happy": true,
"host": "foo.com",
"userGroups": []interface{}{"Foo", "Volvo", "BMW"},
},
},
}
)
func TestTargetRepo_GetByIdentifer(t *testing.T) {
emptyConfig := []domain.Target{}
populatedConfig := []domain.Target{targetFoo, targetBar}
testCases := map[string]struct {
cache cache.Cache
repoConfig []domain.Target
envID string
identifier string
shouldErr bool
expected domain.Target
expectedErr error
}{
"Given I have an empty cache": {
cache: cache.NewMemCache(),
repoConfig: emptyConfig,
envID: "123",
identifier: "foo",
shouldErr: true,
expected: domain.Target{},
expectedErr: domain.ErrCacheNotFound,
},
"Given I have a populated cache and I get an identifier that's in the cache": {
cache: cache.NewMemCache(),
repoConfig: populatedConfig,
envID: "123",
identifier: "foo",
shouldErr: false,
expected: targetFoo,
expectedErr: nil,
},
"Given I have a populated cache and I try to get an identifier that isn't in the cache": {
cache: cache.NewMemCache(),
repoConfig: emptyConfig,
envID: "123",
identifier: "bar",
shouldErr: true,
expected: domain.Target{},
expectedErr: domain.ErrCacheNotFound,
},
}
for desc, tc := range testCases {
tc := tc
t.Run(desc, func(t *testing.T) {
ctx := context.Background()
repo := NewTargetRepo(tc.cache, log.NewNoOpLogger())
if len(tc.repoConfig) > 0 {
assert.Nil(t, repo.DeltaAdd(ctx, tc.envID, tc.repoConfig...))
}
actual, err := repo.GetByIdentifier(ctx, tc.envID, tc.identifier)
if (err != nil) != tc.shouldErr {
t.Errorf("(%s): error = %v, shouldErr = %v", desc, err, tc.shouldErr)
ok := errors.Is(err, tc.expectedErr)
assert.True(t, ok)
}
assert.Equal(t, tc.expected, actual)
})
}
}
func TestTargetRepo_DeltaAdd(t *testing.T) {
target1 := domain.Target{
Target: clientgen.Target{
Identifier: "target1",
Name: "target1",
Environment: "123",
Project: "foo",
},
}
target2 := domain.Target{
Target: clientgen.Target{
Identifier: "target2",
Name: "target2",
Environment: "123",
Project: "foo",
},
}
target3 := domain.Target{
Target: clientgen.Target{
Identifier: "target3",
Name: "target3",
Environment: "123",
Project: "foo",
},
}
target1ProjectBar := domain.Target{
Target: clientgen.Target{
Identifier: "target1",
Name: "target1",
Environment: "123",
Project: "bar",
},
}
target2ProjectBar := domain.Target{
Target: clientgen.Target{
Identifier: "target2",
Name: "target2",
Environment: "123",
Project: "bar",
},
}
testCases := map[string]struct {
cache cache.Cache
repoConfig []domain.Target
env string
targets []domain.Target
expected []domain.Target
shouldErr bool
}{
"Given I have an empty TargetRepo and I add two Targets": {
cache: cache.NewMemCache(),
repoConfig: []domain.Target{},
env: "123",
targets: []domain.Target{target1, target2},
expected: []domain.Target{target1, target2},
shouldErr: false,
},
"Given I have a TargetRepo with Target3 and I add Target1 and Target1": {
cache: cache.NewMemCache(),
repoConfig: []domain.Target{target3},
env: "123",
targets: []domain.Target{target1, target2},
expected: []domain.Target{target1, target2},
shouldErr: false,
},
"Given I have a TargetRepo with two Targets and I add the same Targets with a different Project value ": {
cache: cache.NewMemCache(),
repoConfig: []domain.Target{target1, target2},
env: "123",
targets: []domain.Target{target1ProjectBar, target2ProjectBar},
expected: []domain.Target{target1ProjectBar, target2ProjectBar},
shouldErr: false,
},
"Given I have a TargetRepo with two Targets and I try to add no Targets": {
cache: cache.NewMemCache(),
repoConfig: []domain.Target{target1, target2},
env: "123",
targets: []domain.Target{},
expected: []domain.Target{target1, target2},
shouldErr: true,
},
}
for desc, tc := range testCases {
tc := tc
t.Run(desc, func(t *testing.T) {
ctx := context.Background()
repo := NewTargetRepo(tc.cache, log.NewNoOpLogger())
if len(tc.repoConfig) > 0 {
assert.Nil(t, repo.DeltaAdd(ctx, tc.env, tc.repoConfig...))
}
err := repo.DeltaAdd(ctx, tc.env, tc.targets...)
if (err != nil) != tc.shouldErr {
t.Errorf("(%s): error = %v, shouldErr = %v", desc, err, tc.shouldErr)
}
t.Log("And the values in the repo should match the expected values")
actual, err := repo.Get(ctx, tc.env)
if err != nil {
t.Errorf("(%s): unexpected error getting targets: %s", desc, err)
}
assert.ElementsMatch(t, tc.expected, actual)
})
}
}