-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_field_tracking_implementation_test.go
More file actions
283 lines (243 loc) · 9.51 KB
/
config_field_tracking_implementation_test.go
File metadata and controls
283 lines (243 loc) · 9.51 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
package modular
import (
"strings"
"testing"
"github.com/GoCodeAlone/modular/feeders"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// FieldTrackingBridge bridges between the main package FieldTracker interface
// and the feeders package FieldTracker interface to avoid circular imports
type FieldTrackingBridge struct {
mainTracker FieldTracker
}
// NewFieldTrackingBridge creates a bridge between the interfaces
func NewFieldTrackingBridge(tracker FieldTracker) *FieldTrackingBridge {
return &FieldTrackingBridge{mainTracker: tracker}
}
// RecordFieldPopulation bridges field population records
func (b *FieldTrackingBridge) RecordFieldPopulation(fp feeders.FieldPopulation) {
// Convert from feeders.FieldPopulation to main.FieldPopulation
mainFP := FieldPopulation{
FieldPath: fp.FieldPath,
FieldName: fp.FieldName,
FieldType: fp.FieldType,
FeederType: fp.FeederType,
SourceType: fp.SourceType,
SourceKey: fp.SourceKey,
Value: fp.Value,
InstanceKey: fp.InstanceKey,
SearchKeys: fp.SearchKeys,
FoundKey: fp.FoundKey,
}
b.mainTracker.RecordFieldPopulation(mainFP)
}
// SetupFieldTrackingForFeeders sets up field tracking for feeders that support it
func SetupFieldTrackingForFeeders(cfgFeeders []Feeder, tracker FieldTracker) {
bridge := NewFieldTrackingBridge(tracker)
for _, feeder := range cfgFeeders {
// Use type assertion for the specific feeder types we know about
switch f := feeder.(type) {
case interface{ SetFieldTracker(feeders.FieldTracker) }:
f.SetFieldTracker(bridge)
}
}
}
// TestEnhancedFieldTracking tests the enhanced field tracking functionality
func TestEnhancedFieldTracking(t *testing.T) {
// NOTE: This test uses t.Setenv, so it must NOT call t.Parallel on the same *testing.T
// per Go 1.25 rules (tests using t.Setenv or t.Chdir cannot use t.Parallel). Keep it
// serial to avoid panic: "test using t.Setenv or t.Chdir can not use t.Parallel".
tests := []struct {
name string
envVars map[string]string
expected map[string]FieldPopulation
}{
{
name: "basic environment variable tracking",
envVars: map[string]string{
"APP_NAME": "Test App",
"APP_DEBUG": "true",
},
expected: map[string]FieldPopulation{
"AppName": {
FieldPath: "AppName",
FieldName: "AppName",
FieldType: "string",
FeederType: "*feeders.EnvFeeder",
SourceType: "env",
SourceKey: "APP_NAME",
Value: "Test App",
InstanceKey: "",
SearchKeys: []string{"APP_NAME"},
FoundKey: "APP_NAME",
},
"Debug": {
FieldPath: "Debug",
FieldName: "Debug",
FieldType: "bool",
FeederType: "*feeders.EnvFeeder",
SourceType: "env",
SourceKey: "APP_DEBUG",
Value: true,
InstanceKey: "",
SearchKeys: []string{"APP_DEBUG"},
FoundKey: "APP_DEBUG",
},
},
},
}
for _, tt := range tests {
// Set env for this test case
for key, value := range tt.envVars {
t.Setenv(key, value)
}
t.Run(tt.name, func(t *testing.T) {
// Subtest does not call t.Setenv, but the parent did so we also avoid t.Parallel here to
// keep semantics simple and consistent (can't parallelize parent anyway). If additional
// cases without env mutation are added we can split them into a separate parallel test.
// Create logger that captures debug output
mockLogger := new(MockLogger)
mockLogger.On("Debug", mock.Anything, mock.Anything).Return()
// Create field tracker
tracker := NewDefaultFieldTracker()
tracker.SetLogger(mockLogger)
// Create test configuration struct
type TestConfig struct {
AppName string `env:"APP_NAME"`
Debug bool `env:"APP_DEBUG"`
}
config := &TestConfig{}
// Create configuration builder with field tracking
cfgBuilder := NewConfig()
cfgBuilder.SetVerboseDebug(true, mockLogger)
cfgBuilder.SetFieldTracker(tracker)
// Add environment feeder - the AddFeeder method should set up field tracking automatically
envFeeder := feeders.NewEnvFeeder()
cfgBuilder.AddFeeder(envFeeder)
// Add the configuration structure
cfgBuilder.AddStructKey("test", config)
// Feed configuration
err := cfgBuilder.Feed()
require.NoError(t, err)
// Verify the config was populated correctly
assert.Equal(t, "Test App", config.AppName)
assert.True(t, config.Debug)
// Field tracking should now work with the bridge
// Verify field populations were tracked
assert.GreaterOrEqual(t, len(tracker.FieldPopulations), 2, "Expected at least 2 field populations, got %d", len(tracker.FieldPopulations))
// Check for specific field populations
appNamePop := tracker.GetFieldPopulation("AppName")
if assert.NotNil(t, appNamePop, "AppName field population should be tracked") {
assert.Equal(t, "Test App", appNamePop.Value)
assert.Equal(t, "env", appNamePop.SourceType)
assert.Equal(t, "APP_NAME", appNamePop.SourceKey)
}
debugPop := tracker.GetFieldPopulation("Debug")
if assert.NotNil(t, debugPop, "Debug field population should be tracked") {
assert.Equal(t, true, debugPop.Value)
assert.Equal(t, "env", debugPop.SourceType)
assert.Equal(t, "APP_DEBUG", debugPop.SourceKey)
}
})
}
}
// TestInstanceAwareFieldTracking tests instance-aware field tracking
func TestInstanceAwareFieldTracking(t *testing.T) {
// Uses t.Setenv; cannot call t.Parallel on this *testing.T (Go 1.25 restriction).
// Set up environment variables for instance-aware tracking
envVars := map[string]string{
"DB_PRIMARY_DRIVER": "postgres",
"DB_PRIMARY_DSN": "postgres://localhost/primary",
"DB_SECONDARY_DRIVER": "mysql",
"DB_SECONDARY_DSN": "mysql://localhost/secondary",
}
for key, value := range envVars {
t.Setenv(key, value)
}
// Create logger that captures debug output
mockLogger := new(MockLogger)
mockLogger.On("Debug", mock.Anything, mock.Anything).Return()
// Create field tracker
tracker := NewDefaultFieldTracker()
tracker.SetLogger(mockLogger)
// Create test configuration structures
type ConnectionConfig struct {
Driver string `env:"DRIVER"`
DSN string `env:"DSN"`
}
type DBConfig struct {
Connections map[string]ConnectionConfig
}
dbConfig := &DBConfig{
Connections: map[string]ConnectionConfig{
"primary": {},
"secondary": {},
},
}
// Create configuration builder with field tracking
cfgBuilder := NewConfig()
cfgBuilder.SetVerboseDebug(true, mockLogger)
cfgBuilder.SetFieldTracker(tracker)
// Add instance-aware environment feeder
instanceAwareFeeder := feeders.NewInstanceAwareEnvFeeder(func(instanceKey string) string {
return "DB_" + strings.ToUpper(instanceKey) + "_"
})
cfgBuilder.AddFeeder(instanceAwareFeeder)
// Add the configuration structure
cfgBuilder.AddStructKey("db", dbConfig)
// Feed configuration
err := cfgBuilder.Feed()
require.NoError(t, err)
// Now use FeedInstances specifically for the connections map
err = instanceAwareFeeder.FeedInstances(dbConfig.Connections)
require.NoError(t, err)
// Verify that config values were actually set
assert.Equal(t, "postgres", dbConfig.Connections["primary"].Driver)
assert.Equal(t, "postgres://localhost/primary", dbConfig.Connections["primary"].DSN)
assert.Equal(t, "mysql", dbConfig.Connections["secondary"].Driver)
assert.Equal(t, "mysql://localhost/secondary", dbConfig.Connections["secondary"].DSN)
// Field tracking should now work - verify we have tracked populations
assert.GreaterOrEqual(t, len(tracker.FieldPopulations), 4, "Should track at least 4 field populations (2 fields × 2 instances)")
// Check for specific field populations
var primaryDriverPop, primaryDSNPop, secondaryDriverPop, secondaryDSNPop *FieldPopulation
for i := range tracker.FieldPopulations {
pop := &tracker.FieldPopulations[i]
if pop.FieldName == "Driver" && pop.InstanceKey == "primary" {
primaryDriverPop = pop
} else if pop.FieldName == "DSN" && pop.InstanceKey == "primary" {
primaryDSNPop = pop
} else if pop.FieldName == "Driver" && pop.InstanceKey == "secondary" {
secondaryDriverPop = pop
} else if pop.FieldName == "DSN" && pop.InstanceKey == "secondary" {
secondaryDSNPop = pop
}
}
// Verify primary instance tracking
if assert.NotNil(t, primaryDriverPop, "Primary driver field population should be tracked") {
assert.Equal(t, "postgres", primaryDriverPop.Value)
assert.Equal(t, "env", primaryDriverPop.SourceType)
assert.Equal(t, "DB_PRIMARY_DRIVER", primaryDriverPop.SourceKey)
assert.Equal(t, "primary", primaryDriverPop.InstanceKey)
}
if assert.NotNil(t, primaryDSNPop, "Primary DSN field population should be tracked") {
assert.Equal(t, "postgres://localhost/primary", primaryDSNPop.Value)
assert.Equal(t, "env", primaryDSNPop.SourceType)
assert.Equal(t, "DB_PRIMARY_DSN", primaryDSNPop.SourceKey)
assert.Equal(t, "primary", primaryDSNPop.InstanceKey)
}
// Verify secondary instance tracking
if assert.NotNil(t, secondaryDriverPop, "Secondary driver field population should be tracked") {
assert.Equal(t, "mysql", secondaryDriverPop.Value)
assert.Equal(t, "env", secondaryDriverPop.SourceType)
assert.Equal(t, "DB_SECONDARY_DRIVER", secondaryDriverPop.SourceKey)
assert.Equal(t, "secondary", secondaryDriverPop.InstanceKey)
}
if assert.NotNil(t, secondaryDSNPop, "Secondary DSN field population should be tracked") {
assert.Equal(t, "mysql://localhost/secondary", secondaryDSNPop.Value)
assert.Equal(t, "env", secondaryDSNPop.SourceType)
assert.Equal(t, "DB_SECONDARY_DSN", secondaryDSNPop.SourceKey)
assert.Equal(t, "secondary", secondaryDSNPop.InstanceKey)
}
}