-
Notifications
You must be signed in to change notification settings - Fork 6
/
rollout_integration_test.go
329 lines (308 loc) · 8.5 KB
/
rollout_integration_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
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
package configcat
import (
"bufio"
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"sync"
"testing"
qt "github.com/frankban/quicktest"
)
type testKind int
const (
valueKind = testKind(0)
variationKind = testKind(1)
)
type integrationTestSuite struct {
sdkKey string
fileName string
kind testKind
}
var integrationTestSuites = []integrationTestSuite{
{
sdkKey: "PKDVCLf-Hq-h-kCzMp-L7Q/psuH7BGHoUmdONrzzUOY7A",
fileName: "testmatrix.csv",
kind: valueKind,
}, {
sdkKey: "PKDVCLf-Hq-h-kCzMp-L7Q/BAr3KgLTP0ObzKnBTo5nhA",
fileName: "testmatrix_semantic.csv",
kind: valueKind,
}, {
sdkKey: "PKDVCLf-Hq-h-kCzMp-L7Q/uGyK3q9_ckmdxRyI7vjwCw",
fileName: "testmatrix_number.csv",
kind: valueKind,
}, {
sdkKey: "PKDVCLf-Hq-h-kCzMp-L7Q/q6jMCFIp-EmuAfnmZhPY7w",
fileName: "testmatrix_semantic_2.csv",
kind: valueKind,
}, {
sdkKey: "PKDVCLf-Hq-h-kCzMp-L7Q/qX3TP2dTj06ZpCCT1h_SPA",
fileName: "testmatrix_sensitive.csv",
kind: valueKind,
}, {
sdkKey: "PKDVCLf-Hq-h-kCzMp-L7Q/LcYz135LE0qbcacz2mgXnA",
fileName: "testmatrix_segments_old.csv",
kind: valueKind,
}, {
sdkKey: "PKDVCLf-Hq-h-kCzMp-L7Q/nQ5qkhRAUEa6beEyyrVLBA",
fileName: "testmatrix_variationId.csv",
kind: variationKind,
},
}
var integrationTestSuitesV2 = []integrationTestSuite{
{
sdkKey: "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/AG6C1ngVb0CvM07un6JisQ",
fileName: "testmatrix.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/iV8vH2MBakKxkFZylxHmTg",
fileName: "testmatrix_semantic.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/FCWN-k1dV0iBf8QZrDgjdw",
fileName: "testmatrix_number.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/U8nt3zEhDEO5S2ulubCopA",
fileName: "testmatrix_semantic_2.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/-0YmVOUNgEGKkgRF-rU65g",
fileName: "testmatrix_sensitive.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/y_ZB7o-Xb0Swxth-ZlMSeA",
fileName: "testmatrix_segments_old.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/JcPbCGl_1E-K9M-fJOyKyQ/ByMO9yZNn02kXcm72lnY1A",
fileName: "testmatrix_and_or.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/JcPbCGl_1E-K9M-fJOyKyQ/OfQqcTjfFUGBwMKqtyEOrQ",
fileName: "testmatrix_comparators_v6.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/JcPbCGl_1E-K9M-fJOyKyQ/JoGwdqJZQ0K2xDy7LnbyOg",
fileName: "testmatrix_prerequisite_flag.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/JcPbCGl_1E-K9M-fJOyKyQ/h99HYXWWNE2bH8eWyLAVMA",
fileName: "testmatrix_segments.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/JcPbCGl_1E-K9M-fJOyKyQ/Da6w8dBbmUeMUBhh0iEeQQ",
fileName: "testmatrix_unicode.csv",
kind: valueKind,
}, {
sdkKey: "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/spQnkRTIPEWVivZkWM84lQ",
fileName: "testmatrix_variationId.csv",
kind: variationKind,
},
}
func TestRolloutIntegration(t *testing.T) {
t.Parallel()
integration := os.Getenv("CONFIGCAT_DISABLE_INTEGRATION_TESTS") == ""
testFunc := func(t *testing.T, test integrationTest) {
snap := test.client.Snapshot(test.user)
var val interface{}
switch test.kind {
case valueKind:
val = snap.GetValue(test.key)
case variationKind:
details := snap.GetValueDetails(test.key)
val = details.Data.VariationID
default:
t.Fatalf("unexpected kind %v", test.kind)
}
qt.Assert(t, val, qt.Equals, test.expect)
}
runIntegrationTests(t, integration, integrationTestSuites, testFunc)
runIntegrationTests(t, integration, integrationTestSuitesV2, testFunc)
}
func TestRolloutConcurrent(t *testing.T) {
t.Parallel()
testFunc := func(t *testing.T, test integrationTest) {
// Note: we can't call t.Parallel here because the outer client logger
// has been bound to this test.
snap := test.client.Snapshot(test.user)
// Run the test three times concurrently on the same snapshot so we get
// to test the cached case and concurrent case as well as the uncached case.
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go func() {
defer wg.Done()
var val interface{}
switch test.kind {
case valueKind:
val = snap.GetValue(test.key)
case variationKind:
details := snap.GetValueDetails(test.key)
val = details.Data.VariationID
}
qt.Check(t, val, qt.Equals, test.expect)
}()
}
wg.Wait()
}
// Test that the caching works OK by running all the tests
runIntegrationTests(t, false, integrationTestSuites, testFunc)
runIntegrationTests(t, false, integrationTestSuitesV2, testFunc)
}
func TestGenerateFiles(t *testing.T) {
t.Skip("this test only generates the content of 'resources/content-by-key.json'")
generateJsonContentFile("resources/content-by-key.json", append(integrationTestSuites, integrationTestSuitesV2...))
}
func runIntegrationTests(t *testing.T, integration bool, suite []integrationTestSuite, runTest func(t *testing.T, test integrationTest)) {
for _, test := range suite {
test := test
t.Run(test.fileName, func(t *testing.T) {
t.Parallel()
test.runTests(t, integration, runTest)
})
}
}
type integrationTest struct {
// client holds the client to use when querying.
client *Client
// user holds the user context for querying the value.
user User
// key holds a key to query
key string
// kind specifies what type of query to make.
kind testKind
// expect holds the expected result
expect interface{}
}
func (test integrationTestSuite) runTests(t *testing.T, integration bool, runTest func(t *testing.T, test integrationTest)) {
var cfg Config
if !integration {
srv := newConfigServerWithKey(t, test.sdkKey)
srv.setResponse(configResponse{body: contentForIntegrationTestKey(test.sdkKey)})
cfg = srv.config()
}
tlogger := newTestLogger(t).(*testLogger)
cfg.Logger = tlogger
cfg.SDKKey = test.sdkKey
client := NewCustomClient(cfg)
defer client.Close()
err := client.Refresh(context.Background())
if err != nil {
t.Fatalf("cannot refresh: %v", err)
}
file, fileErr := os.Open(filepath.Join("resources", test.fileName))
if fileErr != nil {
log.Fatal(fileErr)
}
defer file.Close()
reader := csv.NewReader(bufio.NewReader(file))
reader.Comma = ';'
reader.LazyQuotes = true
header, _ := reader.Read()
settingKeys := header[4:]
customKey := header[3]
for lineNumber := 2; ; lineNumber++ {
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
t.Run(fmt.Sprintf("line-%d", lineNumber), func(t *testing.T) {
var user User
if line[0] != "##null##" {
userVal := &UserData{
Identifier: nullStr(line[0]),
Email: nullStr(line[1]),
Country: nullStr(line[2]),
}
if s := nullStr(line[3]); s != "" {
userVal.Custom = map[string]interface{}{
customKey: s,
}
}
user = userVal
}
if cfg.LogLevel <= LogLevelInfo {
t.Logf("user %#v", user)
}
for i, settingKey := range settingKeys {
t.Run(fmt.Sprintf("key-%s", settingKey), func(t *testing.T) {
tlogger.t = t
expected := line[i+4]
var expectedVal interface{}
if test.kind == valueKind {
expectedVal = parseString(expected)
} else {
expectedVal = expected
}
runTest(t, integrationTest{
client: client,
user: user,
key: settingKey,
kind: test.kind,
expect: expectedVal,
})
})
}
})
}
}
func parseString(s string) interface{} {
if v, err := strconv.Atoi(s); err == nil {
return v
}
if v, err := strconv.ParseFloat(s, 64); err == nil {
return v
}
switch s {
case "True":
return true
case "False":
return false
}
return s
}
func nullStr(s string) string {
if s == "##null##" {
return ""
}
return s
}
func generateJsonContentFile(fileName string, suite []integrationTestSuite) {
cont := make(map[string]interface{}, len(suite))
for _, test := range suite {
resp, err := http.DefaultClient.Get("https://cdn-global.configcat.com/configuration-files/" + test.sdkKey + "/config_v6.json")
if err != nil {
panic(err)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var a map[string]interface{}
err = json.Unmarshal(b, &a)
if err != nil {
panic(err)
}
cont[test.sdkKey] = a
resp.Body.Close()
}
d, err := json.Marshal(cont)
if err != nil {
panic(err)
}
err = os.WriteFile(fileName, d, fs.ModeExclusive)
if err != nil {
panic(err)
}
}