-
Notifications
You must be signed in to change notification settings - Fork 18
/
ldclient_end_to_end_test.go
346 lines (276 loc) · 13.6 KB
/
ldclient_end_to_end_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
package ldclient
import (
"crypto/x509"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-sdk-common/v3/ldlogtest"
"github.com/launchdarkly/go-sdk-common/v3/lduser"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
"github.com/launchdarkly/go-server-sdk-evaluation/v3/ldbuilders"
"github.com/launchdarkly/go-server-sdk/v7/interfaces"
"github.com/launchdarkly/go-server-sdk/v7/internal/sharedtest"
"github.com/launchdarkly/go-server-sdk/v7/ldcomponents"
"github.com/launchdarkly/go-server-sdk/v7/testhelpers/ldservices"
"github.com/launchdarkly/go-test-helpers/v3/httphelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
initializationFailedErrorMessage = "LaunchDarkly client initialization failed"
pollingModeWarningMessage = "You should only disable the streaming API if instructed to do so by LaunchDarkly support"
)
var (
alwaysTrueFlag = ldbuilders.NewFlagBuilder("always-true-flag").SingleVariation(ldvalue.Bool(true)).Build()
testUser = lduser.NewUser("test-user-key")
)
// This file contains smoke tests for a complete SDK instance running against embedded HTTP servers. We have many
// component-level tests elsewhere (including tests of the components' network behavior using an instrumented
// HTTPClient), but the end-to-end tests verify that the client is setting those components up correctly, with a
// configuration that's as close to the default configuration as possible (just changing the service URIs).
func assertNoMoreRequests(t *testing.T, requestsCh <-chan httphelpers.HTTPRequestInfo) {
assert.Equal(t, 0, len(requestsCh))
}
func TestDefaultDataSourceIsStreaming(t *testing.T) {
data := ldservices.NewServerSDKData().Flags(&alwaysTrueFlag)
streamHandler, _ := ldservices.ServerSideStreamingServiceHandler(data.ToPutEvent())
httphelpers.WithServer(streamHandler, func(streamServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
defer logCapture.DumpIfTestFailed(t)
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
assert.Equal(t, string(interfaces.DataSourceStateValid), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.True(t, value)
})
}
func TestClientStartsInStreamingMode(t *testing.T) {
data := ldservices.NewServerSDKData().Flags(&alwaysTrueFlag)
streamHandler, _ := ldservices.ServerSideStreamingServiceHandler(data.ToPutEvent())
handler, requestsCh := httphelpers.RecordingHandler(streamHandler)
httphelpers.WithServer(handler, func(streamServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
defer logCapture.DumpIfTestFailed(t)
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
assert.Equal(t, string(interfaces.DataSourceStateValid), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.True(t, value)
r := <-requestsCh
assert.Equal(t, testSdkKey, r.Request.Header.Get("Authorization"))
assertNoMoreRequests(t, requestsCh)
assert.Len(t, logCapture.GetOutput(ldlog.Error), 0)
assert.Len(t, logCapture.GetOutput(ldlog.Warn), 0)
})
}
func TestClientFailsToStartInStreamingModeWith401Error(t *testing.T) {
handler, requestsCh := httphelpers.RecordingHandler(httphelpers.HandlerWithStatus(401))
httphelpers.WithServer(handler, func(streamServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.Error(t, err)
require.NotNil(t, client)
defer client.Close()
assert.Equal(t, initializationFailedErrorMessage, err.Error())
assert.Equal(t, string(interfaces.DataSourceStateOff), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.False(t, value)
r := <-requestsCh
assert.Equal(t, testSdkKey, r.Request.Header.Get("Authorization"))
assertNoMoreRequests(t, requestsCh)
expectedError := "Error in stream connection (giving up permanently): HTTP error 401 (invalid SDK key)"
assert.Equal(t, []string{expectedError}, logCapture.GetOutput(ldlog.Error))
assert.Equal(t, []string{initializationFailedErrorMessage}, logCapture.GetOutput(ldlog.Warn))
})
}
func TestClientRetriesConnectionInStreamingModeWithNonFatalError(t *testing.T) {
data := ldservices.NewServerSDKData().Flags(&alwaysTrueFlag)
streamHandler, _ := ldservices.ServerSideStreamingServiceHandler(data.ToPutEvent())
failThenSucceedHandler := httphelpers.SequentialHandler(httphelpers.HandlerWithStatus(503), streamHandler)
handler, requestsCh := httphelpers.RecordingHandler(failThenSucceedHandler)
httphelpers.WithServer(handler, func(streamServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
assert.Equal(t, string(interfaces.DataSourceStateValid), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.True(t, value)
r0 := <-requestsCh
assert.Equal(t, testSdkKey, r0.Request.Header.Get("Authorization"))
r1 := <-requestsCh
assert.Equal(t, testSdkKey, r1.Request.Header.Get("Authorization"))
assertNoMoreRequests(t, requestsCh)
expectedWarning := "Error in stream connection (will retry): HTTP error 503"
assert.Equal(t, []string{expectedWarning}, logCapture.GetOutput(ldlog.Warn))
assert.Len(t, logCapture.GetOutput(ldlog.Error), 0)
})
}
func TestClientStartsInPollingMode(t *testing.T) {
data := ldservices.NewServerSDKData().Flags(&alwaysTrueFlag)
pollHandler, requestsCh := httphelpers.RecordingHandler(ldservices.ServerSidePollingServiceHandler(data))
httphelpers.WithServer(pollHandler, func(pollServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
DataSource: ldcomponents.PollingDataSource(),
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Polling: pollServer.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
assert.Equal(t, string(interfaces.DataSourceStateValid), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.True(t, value)
r := <-requestsCh
assert.Equal(t, testSdkKey, r.Request.Header.Get("Authorization"))
assertNoMoreRequests(t, requestsCh)
assert.Len(t, logCapture.GetOutput(ldlog.Error), 0)
assert.Equal(t, []string{pollingModeWarningMessage}, logCapture.GetOutput(ldlog.Warn))
})
}
func TestClientFailsToStartInPollingModeWith401Error(t *testing.T) {
handler, requestsCh := httphelpers.RecordingHandler(httphelpers.HandlerWithStatus(401))
httphelpers.WithServer(handler, func(pollServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
DataSource: ldcomponents.PollingDataSource(),
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Polling: pollServer.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.Error(t, err)
require.NotNil(t, client)
defer client.Close()
assert.Equal(t, initializationFailedErrorMessage, err.Error())
assert.Equal(t, string(interfaces.DataSourceStateOff), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.False(t, value)
r := <-requestsCh
assert.Equal(t, testSdkKey, r.Request.Header.Get("Authorization"))
assertNoMoreRequests(t, requestsCh)
expectedError := "Error on polling request (giving up permanently): HTTP error 401 (invalid SDK key)"
assert.Equal(t, []string{expectedError}, logCapture.GetOutput(ldlog.Error))
assert.Equal(t, []string{pollingModeWarningMessage, initializationFailedErrorMessage}, logCapture.GetOutput(ldlog.Warn))
})
}
func TestClientSendsEventWithoutDiagnostics(t *testing.T) {
eventsHandler, eventRequestsCh := httphelpers.RecordingHandler(ldservices.ServerSideEventsServiceHandler())
httphelpers.WithServer(eventsHandler, func(eventsServer *httptest.Server) {
data := ldservices.NewServerSDKData().Flags(&alwaysTrueFlag)
streamHandler, _ := ldservices.ServerSideStreamingServiceHandler(data.ToPutEvent())
httphelpers.WithServer(streamHandler, func(streamServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
DiagnosticOptOut: true,
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL, Events: eventsServer.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
client.Identify(testUser)
client.Flush()
r := <-eventRequestsCh
assert.Equal(t, testSdkKey, r.Request.Header.Get("Authorization"))
assert.Equal(t, "/bulk", r.Request.URL.Path)
assertNoMoreRequests(t, eventRequestsCh)
var jsonValue ldvalue.Value
err = json.Unmarshal(r.Body, &jsonValue)
assert.NoError(t, err)
assert.Equal(t, ldvalue.String("identify"), jsonValue.GetByIndex(0).GetByKey("kind"))
})
})
}
func TestClientSendsDiagnostics(t *testing.T) {
eventsHandler, eventRequestsCh := httphelpers.RecordingHandler(ldservices.ServerSideEventsServiceHandler())
httphelpers.WithServer(eventsHandler, func(eventsServer *httptest.Server) {
data := ldservices.NewServerSDKData().Flags(&alwaysTrueFlag)
streamHandler, _ := ldservices.ServerSideStreamingServiceHandler(data.ToPutEvent())
httphelpers.WithServer(streamHandler, func(streamServer *httptest.Server) {
config := Config{
Logging: ldcomponents.Logging().Loggers(sharedtest.NewTestLoggers()),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL, Events: eventsServer.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
r := <-eventRequestsCh
assert.Equal(t, testSdkKey, r.Request.Header.Get("Authorization"))
assert.Equal(t, "/diagnostic", r.Request.URL.Path)
var jsonValue ldvalue.Value
err = json.Unmarshal(r.Body, &jsonValue)
assert.NoError(t, err)
assert.Equal(t, ldvalue.String("diagnostic-init"), jsonValue.GetByKey("kind"))
})
})
}
func TestClientUsesCustomTLSConfiguration(t *testing.T) {
data := ldservices.NewServerSDKData().Flags(&alwaysTrueFlag)
streamHandler, _ := ldservices.ServerSideStreamingServiceHandler(data.ToPutEvent())
httphelpers.WithSelfSignedServer(streamHandler, func(server *httptest.Server, certData []byte, certs *x509.CertPool) {
config := Config{
Events: ldcomponents.NoEvents(),
HTTP: ldcomponents.HTTPConfiguration().CACert(certData),
Logging: ldcomponents.Logging().Loggers(sharedtest.NewTestLoggers()),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: server.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.True(t, value)
})
}
func TestClientStartupTimesOut(t *testing.T) {
data := ldservices.NewServerSDKData().Flags(&alwaysTrueFlag)
streamHandler, _ := ldservices.ServerSideStreamingServiceHandler(data.ToPutEvent())
slowHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(300 * time.Millisecond)
streamHandler.ServeHTTP(w, r)
})
httphelpers.WithServer(slowHandler, func(streamServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL},
}
client, err := MakeCustomClient(testSdkKey, config, time.Millisecond*100)
require.Error(t, err)
require.NotNil(t, client)
defer client.Close()
assert.Equal(t, "timeout encountered waiting for LaunchDarkly client initialization", err.Error())
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.False(t, value)
assert.Equal(t, []string{"Timeout encountered waiting for LaunchDarkly client initialization"}, logCapture.GetOutput(ldlog.Warn))
assert.Len(t, logCapture.GetOutput(ldlog.Error), 0)
})
}