forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation_test.go
246 lines (217 loc) · 8.27 KB
/
evaluation_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
package statsig
import (
"fmt"
"os"
"path/filepath"
"reflect"
"sync"
"testing"
)
type data struct {
Entries []entry `json:"data"`
}
type entry struct {
User User `json:"user"`
Gates map[string]bool `json:"feature_gates"`
GatesV2 map[string]gateTestData `json:"feature_gates_v2"`
Configs map[string]configTestData `json:"dynamic_configs"`
Layers map[string]layerTestData `json:"layer_configs"`
}
type gateTestData struct {
Name string `json:"name"`
Value bool `json:"value"`
RuleID string `json:"rule_id"`
SecondaryExposures []SecondaryExposure `json:"secondary_exposures"`
}
type configTestData struct {
Name string `json:"name"`
Value map[string]interface{} `json:"value"`
RuleID string `json:"rule_id"`
GroupName string `json:"group_name"`
SecondaryExposures []SecondaryExposure `json:"secondary_exposures"`
}
type layerTestData struct {
Name string `json:"name"`
Value map[string]interface{} `json:"value"`
RuleID string `json:"rule_id"`
GroupName string `json:"group_name"`
SecondaryExposures []SecondaryExposure `json:"secondary_exposures"`
UndelegatedSecondaryExposures []SecondaryExposure `json:"undelegated_secondary_exposures"`
}
var secret string
var testAPIs = []string{
"https://statsigapi.net/v1",
"https://staging.statsigapi.net/v1",
}
var debugLogFile = "tmp/tests.log"
func getOutputLoggerOptionsForTest(t *testing.T) OutputLoggerOptions {
return OutputLoggerOptions{
LogCallback: func(message string, err error) {
var mu sync.RWMutex
mu.RLock()
_ = os.MkdirAll(filepath.Dir(debugLogFile), 0770)
f, e := os.OpenFile(debugLogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
mu.RUnlock()
if e != nil {
fmt.Println(e.Error())
}
defer f.Close()
mu.Lock()
_, e = f.WriteString(fmt.Sprintf("(%s) %s", t.Name(), message))
fmt.Fprint(os.Stderr, err)
mu.Unlock()
if e != nil {
fmt.Println(e.Error())
}
},
DisableInitDiagnostics: false,
DisableSyncDiagnostics: true,
}
}
func getStatsigLoggerOptionsForTest(t *testing.T) StatsigLoggerOptions {
return StatsigLoggerOptions{
DisableInitDiagnostics: true,
DisableSyncDiagnostics: true,
DisableApiDiagnostics: true,
}
}
func TestMain(m *testing.M) {
secret = os.Getenv("test_api_key")
if secret == "" {
absPath, _ := filepath.Abs("../ops/secrets/prod_keys/statsig-rulesets-eval-consistency-test-secret.key")
bytes, err := os.ReadFile(absPath)
if err != nil {
panic("THIS TEST IS EXPECTED TO FAIL FOR NON-STATSIG EMPLOYEES! If this is the only test failing, please proceed to submit a pull request. If you are a Statsig employee, chat with jkw.")
}
secret = string(bytes)
}
os.Remove(debugLogFile)
swallow_stderr(func() {
os.Exit(m.Run())
})
ShutdownAndDangerouslyClearInstance()
}
func TestEvaluation(t *testing.T) {
for _, api := range testAPIs {
test_helper(api, t)
}
}
func test_helper(apiOverride string, t *testing.T) {
t.Logf("Testing for " + apiOverride)
InitializeGlobalOutputLogger(getOutputLoggerOptionsForTest(t))
c := NewClientWithOptions(secret, &Options{API: apiOverride})
var d data
_, err := c.transport.post("/rulesets_e2e_test", nil, &d, RequestOptions{})
if err != nil || len(d.Entries) == 0 {
t.Errorf("Could not download test data")
}
gateChecks := len(d.Entries[0].GatesV2) * 3
configChecks := len(d.Entries[0].Configs) * 3
layerChecks := len(d.Entries[0].Layers) * 4
var totalChecks = (gateChecks + configChecks + layerChecks) * len(d.Entries)
var checks = 0
for _, entry := range d.Entries {
u := entry.User
for gate, serverResult := range entry.GatesV2 {
sdkResult := c.evaluator.evalGate(u, gate, StatsigContext{Hash: "none"})
if sdkResult.Value != serverResult.Value {
t.Errorf("Values are different for gate %s. SDK got %t but server is %t. User is %+v",
gate, sdkResult.Value, serverResult.Value, u)
}
if sdkResult.RuleID != serverResult.RuleID {
t.Errorf("Rule IDs are different for gate %s. SDK got %s but server is %s. User is %+v",
gate, sdkResult.RuleID, serverResult.RuleID, u)
}
if !compare_secondary_exp(t, sdkResult.SecondaryExposures, serverResult.SecondaryExposures) {
t.Errorf("Secondary exposures are different for gate %s. SDK got %s but server is %s",
gate, sdkResult.SecondaryExposures, serverResult.SecondaryExposures)
}
checks += 3
}
for config, serverResult := range entry.Configs {
sdkResult := c.evaluator.evalConfig(u, config, nil, StatsigContext{Hash: "none"})
if !reflect.DeepEqual(sdkResult.JsonValue, serverResult.Value) {
t.Errorf("Values are different for config %s. SDK got %s but server is %s. User is %+v",
config, sdkResult.JsonValue, serverResult.Value, u)
}
if sdkResult.RuleID != serverResult.RuleID {
t.Errorf("Rule IDs are different for config %s. SDK got %s but server is %s",
config, sdkResult.RuleID, serverResult.RuleID)
}
if sdkResult.GroupName != serverResult.GroupName {
t.Errorf("Group Names are different for config %s. SDK got %s but server is %s. User is %+v",
config, sdkResult.GroupName, serverResult.GroupName, u)
}
if !compare_secondary_exp(t, sdkResult.SecondaryExposures, serverResult.SecondaryExposures) {
t.Errorf("Secondary exposures are different for config %s. SDK got %s but server is %s",
config, sdkResult.SecondaryExposures, serverResult.SecondaryExposures)
}
checks += 3
}
for layer, serverResult := range entry.Layers {
sdkResult := c.evaluator.evalLayer(u, layer, nil, StatsigContext{Hash: "none"})
if !reflect.DeepEqual(sdkResult.JsonValue, serverResult.Value) {
t.Errorf("Values are different for layer %s. SDK got %s but server is %s. User is %+v",
layer, sdkResult.JsonValue, serverResult.Value, u)
}
if sdkResult.RuleID != serverResult.RuleID {
t.Errorf("Rule IDs are different for layer %s. SDK got %s but server is %s",
layer, sdkResult.RuleID, serverResult.RuleID)
}
if sdkResult.GroupName != serverResult.GroupName {
t.Errorf("Group Names are different for layer %s. SDK got %s but server is %s. User is %+v",
layer, sdkResult.GroupName, serverResult.GroupName, u)
}
if !compare_secondary_exp(t, sdkResult.SecondaryExposures, serverResult.SecondaryExposures) {
t.Errorf("Secondary exposures are different for layer %s. SDK got %s but server is %s",
layer, sdkResult.SecondaryExposures, serverResult.SecondaryExposures)
}
if !compare_secondary_exp(t, sdkResult.UndelegatedSecondaryExposures, serverResult.UndelegatedSecondaryExposures) {
t.Errorf("Undelegated Secondary exposures are different for layer %s. SDK got %s but server is %s",
layer, sdkResult.UndelegatedSecondaryExposures, serverResult.UndelegatedSecondaryExposures)
}
checks += 4
}
}
if totalChecks != checks {
t.Errorf("Expected to perform %d but only checked %d times for %s.", totalChecks, checks, apiOverride)
}
}
func TestStatsigLocalMode(t *testing.T) {
user := &User{UserID: "test"}
local := &Options{
LocalMode: true,
}
InitializeGlobalOutputLogger(getOutputLoggerOptionsForTest(t))
local_c := NewClientWithOptions("", local)
network := &Options{}
net_c := NewClientWithOptions(secret, network)
local_gate := local_c.CheckGate(*user, "test_public")
if local_gate {
t.Errorf("Local mode should always return false for gate checks")
}
net_gate := net_c.CheckGate(*user, "test_public")
if !net_gate {
t.Errorf("Network mode should work")
}
local_config := local_c.GetConfig(*user, "test_custom_config")
net_config := net_c.GetConfig(*user, "test_custom_config")
if len(local_config.Value) != 0 {
t.Errorf("Local mode should always return false for gate checks")
}
if len(net_config.Value) == 0 {
t.Errorf("Network mode should fetch configs")
}
}
func compare_secondary_exp(t *testing.T, v1 []SecondaryExposure, v2 []SecondaryExposure) bool {
if (v1 == nil && v2 != nil) || (v2 == nil && v1 != nil) {
return false
}
if v1 == nil {
v1 = []SecondaryExposure{}
}
if v2 == nil {
v2 = []SecondaryExposure{}
}
return reflect.DeepEqual(v1, v2)
}