-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
211 lines (208 loc) · 6.54 KB
/
options_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
package local_bucketing_proxy
import (
"os"
"testing"
"time"
"github.com/devcyclehq/go-server-sdk/v2/api"
"github.com/kr/pretty"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseConfig(t *testing.T) {
defaultSDKConfig := SDKConfig{}
defaultSDKConfig.Default()
tests := []struct {
name string
flag string
env map[string]string
expected *ProxyConfig
expectedErr string
}{
{
name: "no config",
env: map[string]string{},
expected: nil,
expectedErr: "required key SDK_KEY missing value",
},
{
name: "minimum env config",
env: map[string]string{
"SDK_KEY": "dvc-test-key",
},
expected: &ProxyConfig{
Instances: []*ProxyInstance{
{
UnixSocketPath: "",
UnixSocketPermissions: "0755",
HTTPPort: 8080,
UnixSocketEnabled: false,
HTTPEnabled: true,
SDKKey: "dvc-test-key",
LogFile: "",
PlatformData: api.PlatformData{},
SDKConfig: SDKConfig{},
},
},
},
},
{
name: "all env config",
env: map[string]string{
"SDK_KEY": "dvc-test-key",
"DEBUG": "True",
"UNIX_SOCKET_PATH": "/tmp/dvc2.sock",
"HTTP_PORT": "1234",
"UNIX_SOCKET_ENABLED": "True",
"HTTP_ENABLED": "False",
"PLATFORMDATA_SDKTYPE": "sdk type",
"PLATFORMDATA_SDKVERSION": "v1.2.3",
"PLATFORMDATA_PLATFORMVERSION": "v2.3.4",
"PLATFORMDATA_DEVICEMODEL": "device model",
"PLATFORMDATA_PLATFORM": "platform",
"PLATFORMDATA_HOSTNAME": "hostname",
"SDKCONFIG_EVENT_FLUSH_INTERVAL_MS": "60000",
"SDKCONFIG_CONFIG_POLLING_INTERVAL_MS": "120000",
"SDKCONFIG_REQUEST_TIMEOUT": "3000",
"SDKCONFIG_DISABLE_AUTOMATIC_EVENT_LOGGING": "True",
"SDKCONFIG_DISABLE_CUSTOM_EVENT_LOGGING": "True",
"SDKCONFIG_MAX_EVENT_QUEUE_SIZE": "123",
"SDKCONFIG_FLUSH_EVENT_QUEUE_SIZE": "456",
"SDKCONFIG_CONFIG_CDN_URI": "https://example.com/config",
"SDKCONFIG_EVENTS_API_URI": "https://example.com/events",
},
expected: &ProxyConfig{
Instances: []*ProxyInstance{
{
UnixSocketPath: "/tmp/dvc2.sock",
HTTPPort: 1234,
UnixSocketEnabled: true,
UnixSocketPermissions: "0755",
HTTPEnabled: false,
SDKKey: "dvc-test-key",
LogFile: "",
PlatformData: api.PlatformData{
SdkType: "sdk type",
SdkVersion: "v1.2.3",
PlatformVersion: "v2.3.4",
DeviceModel: "device model",
Platform: "platform",
Hostname: "hostname",
},
SDKConfig: SDKConfig{
EventFlushIntervalMS: time.Minute.Milliseconds(),
ConfigPollingIntervalMS: (2 * time.Minute).Milliseconds(),
RequestTimeout: (3 * time.Second).Milliseconds(),
DisableAutomaticEventLogging: true,
DisableCustomEventLogging: true,
MaxEventQueueSize: 123,
FlushEventQueueSize: 456,
ConfigCDNURI: "https://example.com/config",
EventsAPIURI: "https://example.com/events",
},
},
},
},
},
{
name: "bad JSON config from flag",
flag: "./testdata/invalid.config.json",
env: map[string]string{},
expectedErr: "failed to parse config from JSON: invalid character ',' looking for beginning of object key string",
},
{
name: "minimum config from flag",
flag: "./testdata/minimum.config.json",
env: map[string]string{},
expected: &ProxyConfig{
Instances: []*ProxyInstance{
{
UnixSocketPath: "",
HTTPPort: 0,
UnixSocketEnabled: false,
HTTPEnabled: false,
SDKKey: "dvc-sample-key",
LogFile: "",
PlatformData: api.PlatformData{},
SDKConfig: defaultSDKConfig,
},
},
},
},
{
name: "minimum config from file env var",
env: map[string]string{
"CONFIG": "./testdata/minimum.config.json",
},
expected: &ProxyConfig{
Instances: []*ProxyInstance{
{
UnixSocketPath: "",
HTTPPort: 0,
UnixSocketEnabled: false,
HTTPEnabled: false,
SDKKey: "dvc-sample-key",
LogFile: "",
PlatformData: api.PlatformData{},
SDKConfig: defaultSDKConfig,
},
},
},
},
{
name: "all config from flag",
flag: "./config.json.example",
env: map[string]string{},
expected: &ProxyConfig{
Instances: []*ProxyInstance{
{
UnixSocketPath: "/tmp/devcycle.sock",
HTTPPort: 8080,
UnixSocketEnabled: false,
HTTPEnabled: true,
SDKKey: "dvc_YOUR_KEY_HERE",
LogFile: "",
SSEEnabled: false,
PlatformData: api.PlatformData{
SdkType: "server",
SdkVersion: "2.10.2",
PlatformVersion: "go1.20.3",
DeviceModel: "",
Platform: "Go",
Hostname: "localhost",
},
SDKConfig: SDKConfig{
EventFlushIntervalMS: (time.Second * 3).Milliseconds(),
ConfigPollingIntervalMS: (time.Second * 30).Milliseconds(),
RequestTimeout: (time.Second * 60).Milliseconds(),
DisableAutomaticEventLogging: false,
DisableCustomEventLogging: false,
MaxEventQueueSize: 10000,
FlushEventQueueSize: 100,
ConfigCDNURI: "https://config-cdn.devcycle.com",
EventsAPIURI: "https://events.devcycle.com",
},
},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
for key, value := range test.env {
_ = os.Setenv(EnvVarPrefix+"_"+key, value)
defer func(key string) {
_ = os.Unsetenv(EnvVarPrefix + "_" + key)
}(key)
}
actual, err := ParseConfig(test.flag)
if test.expectedErr != "" {
require.EqualError(t, err, test.expectedErr)
} else {
require.NoError(t, err)
if !assert.Equal(t, test.expected, actual) {
pretty.Println(test.name, actual)
}
}
})
}
}