-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_test.go
451 lines (404 loc) · 16.5 KB
/
provider_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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package split_openfeature_provider_go
import (
"github.com/open-feature/go-sdk/pkg/openfeature"
"github.com/splitio/go-client/splitio/client"
"github.com/splitio/go-client/splitio/conf"
"github.com/splitio/go-toolkit/logging"
"reflect"
"strings"
"testing"
)
func create(t *testing.T) *openfeature.Client {
cfg := conf.Default()
cfg.SplitFile = "./split.yaml"
cfg.LoggerConfig.LogLevel = logging.LevelNone
factory, err := client.NewSplitFactory("localhost", cfg)
if err != nil {
// error
t.Error("Error creating split factory")
}
splitClient := factory.Client()
err = splitClient.BlockUntilReady(10)
if err != nil {
// error timeout
t.Error("Split sdk timeout error")
}
provider, err := NewProvider(*splitClient)
if err != nil {
t.Error(err)
}
if provider == nil {
t.Error("Error creating Split Provider")
}
openfeature.SetProvider(provider)
return openfeature.NewClient("test_client")
}
func evaluationContext() openfeature.EvaluationContext {
return openfeature.NewEvaluationContext("key", nil)
}
func TestCreateSimple(t *testing.T) {
provider, err := NewProviderSimple("localhost")
if err != nil {
t.Error(err)
}
if provider == nil {
t.Error("Error creating Split Provider")
}
}
func TestUseDefault(t *testing.T) {
ofClient := create(t)
flagName := "random-non-existent-feature"
evalCtx := evaluationContext()
result, err := ofClient.BooleanValue(nil, flagName, false, evalCtx)
if err == nil {
t.Error("Should have returned flag not found error")
} else if !strings.Contains(err.Error(), string(openfeature.FlagNotFoundCode)) {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result == true {
t.Error("Result was true, but should have been default value of false")
}
result, err = ofClient.BooleanValue(nil, flagName, true, evalCtx)
if err == nil {
t.Error("Should have returned flag not found error")
} else if !strings.Contains(err.Error(), string(openfeature.FlagNotFoundCode)) {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result == false {
t.Error("Result was false, but should have been default value of true")
}
}
func TestMissingTargetingKey(t *testing.T) {
ofClient := create(t)
flagName := "random-non-existent-feature"
result, err := ofClient.BooleanValue(nil, flagName, false, openfeature.EvaluationContext{})
if err == nil {
t.Error("Should have returned targeting key missing error")
} else if !strings.Contains(err.Error(), string(openfeature.TargetingKeyMissingCode)) {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result == true {
t.Error("Result was true, but should have been default value of false")
}
}
func TestGetControlVariantNonExistentSplit(t *testing.T) {
ofClient := create(t)
flagName := "random-non-existent-feature"
evalCtx := evaluationContext()
result, err := ofClient.BooleanValueDetails(nil, flagName, false, evalCtx)
if err == nil {
t.Error("Should have returned flag not found error")
} else if !strings.Contains(err.Error(), string(openfeature.FlagNotFoundCode)) {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result.Value == true {
t.Error("Result was true, but should have been default value of false")
} else if result.Variant != "control" {
t.Error("Variant should be control due to Split Go SDK functionality")
}
}
func TestGetBooleanSplit(t *testing.T) {
ofClient := create(t)
flagName := "some_other_feature"
evalCtx := evaluationContext()
result, err := ofClient.BooleanValue(nil, flagName, true, evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result == true {
t.Error("Result was true, but should have been false as set in split.yaml")
}
}
func TestGetBooleanWithKeySplit(t *testing.T) {
ofClient := create(t)
flagName := "my_feature"
evalCtx := evaluationContext()
result, err := ofClient.BooleanValue(nil, flagName, false, evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result == false {
t.Error("Result was false, but should have been true as set in split.yaml")
}
evalCtx = openfeature.NewEvaluationContext("randomKey", nil)
result, err = ofClient.BooleanValue(nil, flagName, true, evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result == true {
t.Error("Result was true, but should have been false as set in split.yaml")
}
}
func TestGetStringSplit(t *testing.T) {
ofClient := create(t)
flagName := "some_other_feature"
evalCtx := evaluationContext()
result, err := ofClient.StringValue(nil, flagName, "on", evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result != "off" {
t.Errorf("Result was %s, not off as set in split.yaml", result)
}
}
func TestGetIntegerSplit(t *testing.T) {
ofClient := create(t)
flagName := "int_feature"
evalCtx := evaluationContext()
result, err := ofClient.IntValue(nil, flagName, 0, evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result != 32 {
t.Errorf("Result was %d, not 32 as set in split.yaml", result)
}
}
func TestGetObjectSplit(t *testing.T) {
ofClient := create(t)
flagName := "obj_feature"
evalCtx := evaluationContext()
result, err := ofClient.ObjectValue(nil, flagName, 0, evalCtx)
expectedResult := map[string]interface{}{
"key": "value",
}
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if !reflect.DeepEqual(result, expectedResult) {
t.Error("Result was not map from key to value as set in split.yaml")
}
}
func TestGetFloatSplit(t *testing.T) {
ofClient := create(t)
flagName := "int_feature"
evalCtx := evaluationContext()
result, err := ofClient.FloatValue(nil, flagName, 0, evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result != float64(32) {
t.Errorf("Result was %f, not 32 as set in split.yaml", result)
}
}
func TestMetadataName(t *testing.T) {
ofClient := create(t)
if ofClient.Metadata().Name() != "test_client" {
t.Error("Client name was not set properly")
}
if openfeature.ProviderMetadata().Name != "Split" {
t.Errorf("Provider metadata name was %s, not Split", openfeature.ProviderMetadata().Name)
}
}
func TestBooleanDetails(t *testing.T) {
ofClient := create(t)
flagName := "some_other_feature"
evalCtx := evaluationContext()
result, err := ofClient.BooleanValueDetails(nil, flagName, true, evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result.FlagKey != flagName {
t.Errorf("Flag name is %s, not %s", result.FlagKey, flagName)
} else if !strings.Contains(string(result.Reason), string(openfeature.TargetingMatchReason)) {
t.Errorf("reason is %s, not targeting match", result.Reason)
} else if result.Value == true {
t.Error("Result was true, but should have been false as in split.yaml")
} else if result.Variant != "off" {
t.Errorf("Variant should be off as in split.yaml, but was %s", result.Variant)
} else if result.ErrorCode != "" {
t.Errorf("Unexpected error in result %s", result.ErrorCode)
}
}
func TestIntegerDetails(t *testing.T) {
ofClient := create(t)
flagName := "int_feature"
evalCtx := evaluationContext()
result, err := ofClient.IntValueDetails(nil, flagName, 0, evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result.FlagKey != flagName {
t.Errorf("Flag name is %s, not %s", result.FlagKey, flagName)
} else if !strings.Contains(string(result.Reason), string(openfeature.TargetingMatchReason)) {
t.Errorf("reason is %s, not targeting match", result.Reason)
} else if result.Value != int64(32) {
t.Errorf("Result was %d, but should have been 32 as in split.yaml", result.Value)
} else if result.Variant != "32" {
t.Errorf("Variant should be 32 as in split.yaml, but was %s", result.Variant)
} else if result.ErrorCode != "" {
t.Errorf("Unexpected error in result %s", result.ErrorCode)
}
}
func TestStringDetails(t *testing.T) {
ofClient := create(t)
flagName := "some_other_feature"
evalCtx := evaluationContext()
result, err := ofClient.StringValueDetails(nil, flagName, "blah", evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result.FlagKey != flagName {
t.Errorf("Flag name is %s, not %s", result.FlagKey, flagName)
} else if !strings.Contains(string(result.Reason), string(openfeature.TargetingMatchReason)) {
t.Errorf("reason is %s, not targeting match", result.Reason)
} else if result.Value != "off" {
t.Errorf("Result was %s, but should have been off as in split.yaml", result.Value)
} else if result.Variant != "off" {
t.Errorf("Variant should be off as in split.yaml, but was %s", result.Variant)
} else if result.ErrorCode != "" {
t.Errorf("Unexpected error in result %s", result.ErrorCode)
}
}
func TestObjectDetails(t *testing.T) {
ofClient := create(t)
flagName := "obj_feature"
evalCtx := evaluationContext()
result, err := ofClient.ObjectValueDetails(nil, flagName, map[string]interface{}{}, evalCtx)
expectedResult := map[string]interface{}{
"key": "value",
}
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result.FlagKey != flagName {
t.Errorf("Flag name is %s, not %s", result.FlagKey, flagName)
} else if !strings.Contains(string(result.Reason), string(openfeature.TargetingMatchReason)) {
t.Errorf("reason is %s, not targeting match", result.Reason)
} else if !reflect.DeepEqual(result.Value, expectedResult) {
t.Error("Result was not map of key->value as in split.yaml")
} else if result.Variant != "{\"key\": \"value\"}" {
t.Errorf("Variant should be {\"key\": \"value\"} as in split.yaml, but was %s", result.Variant)
} else if result.ErrorCode != "" {
t.Errorf("Unexpected error in result %s", result.ErrorCode)
}
}
func TestFloatDetails(t *testing.T) {
ofClient := create(t)
flagName := "int_feature"
evalCtx := evaluationContext()
result, err := ofClient.FloatValueDetails(nil, flagName, 0, evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result.FlagKey != flagName {
t.Errorf("Flag name is %s, not %s", result.FlagKey, flagName)
} else if !strings.Contains(string(result.Reason), string(openfeature.TargetingMatchReason)) {
t.Errorf("reason is %s, not targeting match", result.Reason)
} else if result.Value != float64(32) {
t.Errorf("Result was %f, but should have been 32 as in split.yaml", result.Value)
} else if result.Variant != "32" {
t.Errorf("Variant should be 32 as in split.yaml, but was %s", result.Variant)
} else if result.ErrorCode != "" {
t.Errorf("Unexpected error in result %s", result.ErrorCode)
}
flagName = "float_feature"
result, err = ofClient.FloatValueDetails(nil, flagName, 0, evalCtx)
if err != nil {
t.Errorf("Unexpected error occurred %s", err.Error())
} else if result.Value != 32.5 {
t.Errorf("Result was %f, but should have been 32.5 as in split.yaml", result.Value)
} else if result.Variant != "32.5" {
t.Errorf("Variant should be 32 as in split.yaml, but was %s", result.Variant)
} else if result.ErrorCode != "" {
t.Errorf("Unexpected error in result %s", result.ErrorCode)
}
}
func TestBooleanFail(t *testing.T) {
// attempt to fetch an object treatment as a boolean. Should result in the default
ofClient := create(t)
flagName := "obj_feature"
evalCtx := evaluationContext()
result, err := ofClient.BooleanValue(nil, flagName, false, evalCtx)
if err == nil {
t.Error("Expected exception to occur")
} else if !strings.Contains(err.Error(), string(openfeature.ParseErrorCode)) {
t.Errorf("Expected parse error, got %s", err.Error())
} else if result != false {
t.Error("Result was true, but should have been default of false")
}
resultDetails, err := ofClient.BooleanValueDetails(nil, flagName, false, evalCtx)
if err == nil {
t.Error("Expected exception to occur")
} else if !strings.Contains(err.Error(), string(openfeature.ParseErrorCode)) {
t.Errorf("Expected parse error, got %s", err.Error())
} else if resultDetails.Value != false {
t.Error("Result was true, but should have been default of false")
} else if resultDetails.ErrorCode != openfeature.ParseErrorCode {
t.Errorf("Expected parse error code, got %s", resultDetails.ErrorCode)
} else if resultDetails.Reason != openfeature.ErrorReason {
t.Errorf("Expected error reason code, got %s", resultDetails.Reason)
} else if resultDetails.Variant != "{\"key\": \"value\"}" {
t.Errorf("Expected variant to be string of map, got %s", resultDetails.Variant)
}
}
func TestIntegerFail(t *testing.T) {
// attempt to fetch an object treatment as an integer. Should result in the default
ofClient := create(t)
flagName := "obj_feature"
evalCtx := evaluationContext()
result, err := ofClient.IntValue(nil, flagName, 10, evalCtx)
if err == nil {
t.Error("Expected exception to occur")
} else if !strings.Contains(err.Error(), string(openfeature.ParseErrorCode)) {
t.Errorf("Expected parse error, got %s", err.Error())
} else if result != int64(10) {
t.Errorf("Result was %d, but should have been default of 10", result)
}
resultDetails, err := ofClient.IntValueDetails(nil, flagName, 10, evalCtx)
if err == nil {
t.Error("Expected exception to occur")
} else if !strings.Contains(err.Error(), string(openfeature.ParseErrorCode)) {
t.Errorf("Expected parse error, got %s", err.Error())
} else if resultDetails.Value != int64(10) {
t.Errorf("Result was %d, but should have been default of 10", resultDetails.Value)
} else if resultDetails.ErrorCode != openfeature.ParseErrorCode {
t.Errorf("Expected parse error code, got %s", resultDetails.ErrorCode)
} else if resultDetails.Reason != openfeature.ErrorReason {
t.Errorf("Expected error reason code, got %s", resultDetails.Reason)
} else if resultDetails.Variant != "{\"key\": \"value\"}" {
t.Errorf("Expected variant to be string of map, got %s", resultDetails.Variant)
}
}
func TestFloatFail(t *testing.T) {
// attempt to fetch an object treatment as a float. Should result in the default
ofClient := create(t)
flagName := "obj_feature"
evalCtx := evaluationContext()
result, err := ofClient.FloatValue(nil, flagName, 10, evalCtx)
if err == nil {
t.Error("Expected exception to occur")
} else if !strings.Contains(err.Error(), string(openfeature.ParseErrorCode)) {
t.Errorf("Expected parse error, got %s", err.Error())
} else if result != float64(10) {
t.Errorf("Result was %f, but should have been default of 10", result)
}
resultDetails, err := ofClient.FloatValueDetails(nil, flagName, 10, evalCtx)
if err == nil {
t.Error("Expected exception to occur")
} else if !strings.Contains(err.Error(), string(openfeature.ParseErrorCode)) {
t.Errorf("Expected parse error, got %s", err.Error())
} else if resultDetails.Value != float64(10) {
t.Errorf("Result was %f, but should have been default of 10", resultDetails.Value)
} else if resultDetails.ErrorCode != openfeature.ParseErrorCode {
t.Errorf("Expected parse error code, got %s", resultDetails.ErrorCode)
} else if resultDetails.Reason != openfeature.ErrorReason {
t.Errorf("Expected error reason code, got %s", resultDetails.Reason)
} else if resultDetails.Variant != "{\"key\": \"value\"}" {
t.Errorf("Expected variant to be string of map, got %s", resultDetails.Variant)
}
}
func TestObjectFail(t *testing.T) {
// attempt to fetch an int as an object. Should result in the default
ofClient := create(t)
flagName := "int_feature"
evalCtx := evaluationContext()
defaultTreatment := map[string]interface{}{
"key": "value",
}
result, err := ofClient.ObjectValue(nil, flagName, defaultTreatment, evalCtx)
if err == nil {
t.Error("Expected exception to occur")
} else if !strings.Contains(err.Error(), string(openfeature.ParseErrorCode)) {
t.Errorf("Expected parse error, got %s", err.Error())
} else if !reflect.DeepEqual(result, defaultTreatment) {
t.Error("Result was not default treatment")
}
resultDetails, err := ofClient.ObjectValueDetails(nil, flagName, defaultTreatment, evalCtx)
if err == nil {
t.Error("Expected exception to occur")
} else if !strings.Contains(err.Error(), string(openfeature.ParseErrorCode)) {
t.Errorf("Expected parse error, got %s", err.Error())
} else if !reflect.DeepEqual(resultDetails.Value, defaultTreatment) {
t.Errorf("Result was %f, but should have been default of 10", resultDetails.Value)
} else if resultDetails.ErrorCode != openfeature.ParseErrorCode {
t.Errorf("Expected parse error code, got %s", resultDetails.ErrorCode)
} else if resultDetails.Reason != openfeature.ErrorReason {
t.Errorf("Expected error reason code, got %s", resultDetails.Reason)
} else if resultDetails.Variant != "32" {
t.Errorf("Expected variant to be string of integer, got %s", resultDetails.Variant)
}
}