forked from GoCodeAlone/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_exposure_test.go
More file actions
368 lines (316 loc) · 11.4 KB
/
service_exposure_test.go
File metadata and controls
368 lines (316 loc) · 11.4 KB
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
package reverseproxy
import (
"context"
"log/slog"
"net/http"
"os"
"reflect"
"testing"
"github.com/CrisisTextLine/modular"
)
// TestFeatureFlagEvaluatorServiceExposure tests that the module exposes the feature flag evaluator as a service
func TestFeatureFlagEvaluatorServiceExposure(t *testing.T) {
tests := []struct {
name string
config *ReverseProxyConfig
expectService bool
expectFlags int
}{
{
name: "FeatureFlagsDisabled",
config: &ReverseProxyConfig{
BackendServices: map[string]string{
"test": "http://127.0.0.1:18080",
},
FeatureFlags: FeatureFlagsConfig{
Enabled: false,
},
},
expectService: false,
},
{
name: "FeatureFlagsEnabledNoDefaults",
config: &ReverseProxyConfig{
BackendServices: map[string]string{
"test": "http://127.0.0.1:18080",
},
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
},
},
expectService: true,
expectFlags: 0,
},
{
name: "FeatureFlagsEnabledWithDefaults",
config: &ReverseProxyConfig{
BackendServices: map[string]string{
"test": "http://127.0.0.1:18080",
},
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
Flags: map[string]bool{
"flag-1": true,
"flag-2": false,
},
},
},
expectService: true,
expectFlags: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create mock router
mockRouter := &testRouter{routes: make(map[string]http.HandlerFunc)}
// Create mock application
app := NewMockTenantApplication()
// Register the configuration with the application
app.RegisterConfigSection("reverseproxy", modular.NewStdConfigProvider(tt.config))
// Create module
module := NewModule()
// Set the configuration
module.config = tt.config
// Set router via constructor
services := map[string]any{
"router": mockRouter,
}
constructedModule, err := module.Constructor()(app, services)
if err != nil {
t.Fatalf("Failed to construct module: %v", err)
}
module = constructedModule.(*ReverseProxyModule)
// Set the app reference
module.app = app
// Start the module to trigger feature flag evaluator creation
if err := module.Start(context.Background()); err != nil {
t.Fatalf("Failed to start module: %v", err)
}
// Test service exposure
providedServices := module.ProvidesServices()
if tt.expectService {
// Should provide two services (reverseproxy.provider + featureFlagEvaluator)
if len(providedServices) != 2 {
t.Errorf("Expected 2 provided services, got %d", len(providedServices))
return
}
// Find the featureFlagEvaluator service
var flagService *modular.ServiceProvider
for i, service := range providedServices {
if service.Name == "featureFlagEvaluator" {
flagService = &providedServices[i]
break
}
}
if flagService == nil {
t.Error("Expected featureFlagEvaluator service to be provided")
return
}
// Verify the service implements FeatureFlagEvaluator
if _, ok := flagService.Instance.(FeatureFlagEvaluator); !ok {
t.Errorf("Expected service to implement FeatureFlagEvaluator, got %T", flagService.Instance)
}
// Test that it's now the FeatureFlagAggregator (new design)
evaluator, ok := flagService.Instance.(*FeatureFlagAggregator)
if !ok {
t.Errorf("Expected service to be *FeatureFlagAggregator, got %T", flagService.Instance)
return
}
// Test configuration was applied correctly through the aggregator
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/test", nil)
// Test flags
if tt.expectFlags > 0 {
for flagID, expectedValue := range tt.config.FeatureFlags.Flags {
actualValue, err := evaluator.EvaluateFlag(context.Background(), flagID, "", req)
if err != nil {
t.Errorf("Error evaluating flag %s: %v", flagID, err)
}
if actualValue != expectedValue {
t.Errorf("Flag %s: expected %v, got %v", flagID, expectedValue, actualValue)
}
}
}
} else {
// Should provide only one service (reverseproxy.provider)
if len(providedServices) != 1 {
t.Errorf("Expected 1 provided service, got %d", len(providedServices))
return
}
// Should be the reverseproxy.provider service
service := providedServices[0]
if service.Name != "reverseproxy.provider" {
t.Errorf("Expected service name 'reverseproxy.provider', got '%s'", service.Name)
}
}
})
}
}
// TestFeatureFlagEvaluatorServiceDependencyResolution tests that external evaluators are integrated into the aggregator
// for proper fallback behavior instead of bypassing the aggregation system entirely.
func TestFeatureFlagEvaluatorServiceDependencyResolution(t *testing.T) {
// Create mock router
mockRouter := &testRouter{routes: make(map[string]http.HandlerFunc)}
// Create external feature flag evaluator
app := NewMockTenantApplication()
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
// Configure the external evaluator with flags
externalConfig := &ReverseProxyConfig{
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
Flags: map[string]bool{
"external-flag": true,
},
},
}
app.RegisterConfigSection("reverseproxy", modular.NewStdConfigProvider(externalConfig))
externalEvaluator, err := NewFileBasedFeatureFlagEvaluator(context.Background(), app, logger)
if err != nil {
t.Fatalf("Failed to create feature flag evaluator: %v", err)
}
// Create mock application - already created above
// Create a separate application for the module
moduleApp := NewMockTenantApplication()
// Register the module configuration with the module app
moduleApp.RegisterConfigSection("reverseproxy", modular.NewStdConfigProvider(&ReverseProxyConfig{
BackendServices: map[string]string{
"test": "http://127.0.0.1:18080",
},
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
Flags: map[string]bool{
"internal-flag": true,
},
},
}))
// Create module
module := NewModule()
// Set configuration with feature flags enabled
module.config = &ReverseProxyConfig{
BackendServices: map[string]string{
"test": "http://127.0.0.1:18080",
},
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
Flags: map[string]bool{
"internal-flag": true,
},
},
}
// Set router and external evaluator via constructor
services := map[string]any{
"router": mockRouter,
"featureFlagEvaluator": externalEvaluator,
}
constructedModule, err := module.Constructor()(moduleApp, services)
if err != nil {
t.Fatalf("Failed to construct module: %v", err)
}
module = constructedModule.(*ReverseProxyModule)
// Set the app reference
module.app = moduleApp
// Start the module
if err := module.Start(context.Background()); err != nil {
t.Fatalf("Failed to start module: %v", err)
}
// Test that the external evaluator is used, not the internal one
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/test", nil)
// The external flag should exist and work
externalValue, err := module.featureFlagEvaluator.EvaluateFlag(context.Background(), "external-flag", "", req)
if err != nil {
t.Errorf("Error evaluating external flag: %v", err)
}
if !externalValue {
t.Error("Expected external flag to be true")
}
// The internal flag should now ALSO work (via aggregator fallback when external evaluator abstains)
// This is the fix: aggregator provides fallback behavior instead of only using external evaluator
internalValue, err := module.featureFlagEvaluator.EvaluateFlag(context.Background(), "internal-flag", "", req)
if err != nil {
t.Logf("Internal flag evaluation error (expected with current external evaluator): %v", err)
// This is okay - the external evaluator doesn't have internal-flag, so it should fallback to file evaluator
// Let's test with EvaluateFlagWithDefault to see fallback behavior
internalValueWithDefault := module.featureFlagEvaluator.EvaluateFlagWithDefault(context.Background(), "internal-flag", "", req, false)
if !internalValueWithDefault {
t.Error("Expected internal flag to be true via aggregator fallback, but got false")
}
} else if !internalValue {
t.Error("Expected internal flag to be true via aggregator fallback")
}
// The module should still provide both services (reverseproxy.provider + external evaluator)
providedServices := module.ProvidesServices()
if len(providedServices) != 2 {
t.Errorf("Expected 2 provided services, got %d", len(providedServices))
return
}
// Find the featureFlagEvaluator service
var flagService *modular.ServiceProvider
for i, service := range providedServices {
if service.Name == "featureFlagEvaluator" {
flagService = &providedServices[i]
break
}
}
if flagService == nil {
t.Error("Expected featureFlagEvaluator service to be provided")
return
}
// Verify the provided service is now the aggregator (which incorporates the external evaluator)
if _, isAggregator := flagService.Instance.(*FeatureFlagAggregator); !isAggregator {
t.Errorf("Expected provided service to be aggregator, got: %T", flagService.Instance)
}
// Verify that the external evaluator was registered and can be discovered by the aggregator
var registeredExternalEvaluator FeatureFlagEvaluator
if err := module.app.GetService("featureFlagEvaluator.external", ®isteredExternalEvaluator); err != nil {
t.Errorf("Expected external evaluator to be registered for aggregation: %v", err)
} else {
// The registered external evaluator should be the same instance we provided
if registeredExternalEvaluator != externalEvaluator {
t.Error("Expected registered external evaluator to be the same instance we provided")
}
}
}
// TestFeatureFlagEvaluatorConfigValidation tests configuration validation
func TestFeatureFlagEvaluatorConfigValidation(t *testing.T) {
// Create mock router
mockRouter := &testRouter{routes: make(map[string]http.HandlerFunc)}
// Create mock application
app := NewMockTenantApplication()
// Create module
module := NewModule()
// Test with nil config (should not crash)
module.config = nil
// Set router via constructor
services := map[string]any{
"router": mockRouter,
}
constructedModule, err := module.Constructor()(app, services)
if err != nil {
t.Fatalf("Failed to construct module: %v", err)
}
module = constructedModule.(*ReverseProxyModule)
// Set the app reference
module.app = app
// This should not crash even with nil config
providedServices := module.ProvidesServices()
if len(providedServices) != 0 {
t.Errorf("Expected 0 provided services with nil config, got %d", len(providedServices))
}
}
// TestServiceProviderInterface tests that the service properly implements the expected interface
func TestServiceProviderInterface(t *testing.T) {
// Create the evaluator
app := NewMockTenantApplication()
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
evaluator, err := NewFileBasedFeatureFlagEvaluator(context.Background(), app, logger)
if err != nil {
t.Fatalf("Failed to create feature flag evaluator: %v", err)
}
// Test that it implements FeatureFlagEvaluator
var _ FeatureFlagEvaluator = evaluator
// Test using reflection (as the framework would)
evaluatorType := reflect.TypeOf(evaluator)
featureFlagInterface := reflect.TypeOf((*FeatureFlagEvaluator)(nil)).Elem()
if !evaluatorType.Implements(featureFlagInterface) {
t.Error("FileBasedFeatureFlagEvaluator does not implement FeatureFlagEvaluator interface")
}
}