Skip to content

Commit 52aa10c

Browse files
mjcheethamclaude
andcommitted
config: update tests for inline settings
Update config_test.go and summary_test.go to use inline structs instead of writing temporary YAML files to disk. This reflects the switch from file-path-based to inline configuration for pii, filter, and summary settings. Tests that validated loading from file paths (e.g., nonexistent file errors) are removed since that responsibility now belongs to the collector's ${file:PATH} resolver. The remaining tests verify that Config.Validate() correctly handles inline structs, including validation of summary field names and detection of duplicates. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
1 parent ebb73c4 commit 52aa10c

2 files changed

Lines changed: 77 additions & 164 deletions

File tree

config_test.go

Lines changed: 68 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package trace2receiver
22

33
import (
4-
"os"
5-
"path/filepath"
64
"runtime"
75
"testing"
86

97
"github.com/stretchr/testify/assert"
10-
"github.com/stretchr/testify/require"
118
)
129

1310
// Test Validate with minimal valid config on Windows
@@ -156,195 +153,111 @@ func Test_Config_Validate_RejectDgramUnix(t *testing.T) {
156153
assert.Contains(t, err.Error(), "SOCK_DGRAM sockets are not supported")
157154
}
158155

159-
// Test Validate with valid PII settings file
156+
// Test Validate with valid PII settings (inline)
160157
func Test_Config_Validate_WithValidPiiSettings(t *testing.T) {
161-
// Create a temporary PII settings file
162-
tmpDir := t.TempDir()
163-
piiPath := filepath.Join(tmpDir, "pii.yml")
164-
piiContent := `
165-
pii_filter:
166-
domains:
167-
- pattern: "example.com"
168-
replace: "<domain>"
169-
`
170-
err := os.WriteFile(piiPath, []byte(piiContent), 0644)
171-
require.NoError(t, err)
172-
173-
cfg := createMinimalValidConfig()
174-
cfg.PiiSettingsPath = piiPath
175-
176-
err = cfg.Validate()
177-
assert.NoError(t, err)
178-
assert.NotNil(t, cfg.piiSettings)
179-
}
180-
181-
// Test Validate with invalid PII settings file
182-
func Test_Config_Validate_WithInvalidPiiSettings(t *testing.T) {
183158
cfg := createMinimalValidConfig()
184-
cfg.PiiSettingsPath = "/nonexistent/pii.yml"
159+
cfg.Pii = &PiiSettings{
160+
Include: PiiInclude{
161+
Hostname: true,
162+
Username: false,
163+
},
164+
}
185165

186166
err := cfg.Validate()
187-
assert.Error(t, err)
188-
}
189-
190-
// Test Validate with valid filter settings file
191-
func Test_Config_Validate_WithValidFilterSettings(t *testing.T) {
192-
// Create a temporary filter settings file
193-
tmpDir := t.TempDir()
194-
filterPath := filepath.Join(tmpDir, "filter.yml")
195-
filterContent := `
196-
default_action: accept
197-
`
198-
err := os.WriteFile(filterPath, []byte(filterContent), 0644)
199-
require.NoError(t, err)
200-
201-
cfg := createMinimalValidConfig()
202-
cfg.FilterSettingsPath = filterPath
203-
204-
err = cfg.Validate()
205167
assert.NoError(t, err)
206-
assert.NotNil(t, cfg.filterSettings)
168+
assert.NotNil(t, cfg.Pii)
207169
}
208170

209-
// Test Validate with invalid filter settings file
210-
func Test_Config_Validate_WithInvalidFilterSettings(t *testing.T) {
171+
// Test Validate with valid filter settings (inline)
172+
func Test_Config_Validate_WithValidFilterSettings(t *testing.T) {
211173
cfg := createMinimalValidConfig()
212-
cfg.FilterSettingsPath = "/nonexistent/filter.yml"
174+
cfg.Filter = &FilterSettings{
175+
Defaults: FilterDefaults{
176+
RulesetName: "dl:verbose",
177+
},
178+
}
213179

214180
err := cfg.Validate()
215-
assert.Error(t, err)
216-
}
217-
218-
// Test Validate with valid summary settings file
219-
func Test_Config_Validate_WithValidSummary(t *testing.T) {
220-
// Create a temporary summary settings file
221-
tmpDir := t.TempDir()
222-
summaryPath := filepath.Join(tmpDir, "summary.yml")
223-
summaryContent := `
224-
message_patterns:
225-
- prefix: "error:"
226-
field_name: "error_count"
227-
- prefix: "warning:"
228-
field_name: "warning_count"
229-
230-
region_timers:
231-
- category: "index"
232-
label: "do_read_index"
233-
count_field: "index_read_count"
234-
time_field: "index_read_time"
235-
`
236-
err := os.WriteFile(summaryPath, []byte(summaryContent), 0644)
237-
require.NoError(t, err)
238-
239-
cfg := createMinimalValidConfig()
240-
cfg.SummaryPath = summaryPath
241-
242-
err = cfg.Validate()
243181
assert.NoError(t, err)
244-
assert.NotNil(t, cfg.summary)
245-
assert.Equal(t, 2, len(cfg.summary.MessagePatterns))
246-
assert.Equal(t, 1, len(cfg.summary.RegionTimers))
182+
assert.NotNil(t, cfg.Filter)
247183
}
248184

249-
// Test Validate with invalid summary settings file (nonexistent)
250-
func Test_Config_Validate_WithNonexistentSummary(t *testing.T) {
185+
// Test Validate with valid summary settings (inline)
186+
func Test_Config_Validate_WithValidSummary(t *testing.T) {
251187
cfg := createMinimalValidConfig()
252-
cfg.SummaryPath = "/nonexistent/summary.yml"
188+
cfg.Summary = &SummarySettings{
189+
MessagePatterns: []MessagePatternRule{
190+
{Prefix: "error:", FieldName: "error_count"},
191+
{Prefix: "warning:", FieldName: "warning_count"},
192+
},
193+
RegionTimers: []RegionTimerRule{
194+
{Category: "index", Label: "do_read_index", CountField: "index_read_count", TimeField: "index_read_time"},
195+
},
196+
}
253197

254198
err := cfg.Validate()
255-
assert.Error(t, err)
199+
assert.NoError(t, err)
200+
assert.NotNil(t, cfg.Summary)
201+
assert.Equal(t, 2, len(cfg.Summary.MessagePatterns))
202+
assert.Equal(t, 1, len(cfg.Summary.RegionTimers))
256203
}
257204

258-
// Test Validate with invalid summary settings file (malformed YAML)
205+
// Test Validate with invalid summary settings (empty field_name)
259206
func Test_Config_Validate_WithMalformedSummary(t *testing.T) {
260-
// Create a temporary malformed summary settings file
261-
tmpDir := t.TempDir()
262-
summaryPath := filepath.Join(tmpDir, "summary.yml")
263-
summaryContent := `
264-
message_patterns:
265-
- prefix: "error:"
266-
field_name: ""
267-
`
268-
err := os.WriteFile(summaryPath, []byte(summaryContent), 0644)
269-
require.NoError(t, err)
270-
271207
cfg := createMinimalValidConfig()
272-
cfg.SummaryPath = summaryPath
208+
cfg.Summary = &SummarySettings{
209+
MessagePatterns: []MessagePatternRule{
210+
{Prefix: "error:", FieldName: ""},
211+
},
212+
}
273213

274-
err = cfg.Validate()
214+
err := cfg.Validate()
275215
assert.Error(t, err)
276216
assert.Contains(t, err.Error(), "field_name cannot be empty")
277217
}
278218

279219
// Test Validate with summary settings with duplicate field names
280220
func Test_Config_Validate_WithDuplicateSummaryFields(t *testing.T) {
281-
// Create a temporary summary settings file with duplicate fields
282-
tmpDir := t.TempDir()
283-
summaryPath := filepath.Join(tmpDir, "summary.yml")
284-
summaryContent := `
285-
message_patterns:
286-
- prefix: "error:"
287-
field_name: "count"
288-
- prefix: "warning:"
289-
field_name: "count"
290-
`
291-
err := os.WriteFile(summaryPath, []byte(summaryContent), 0644)
292-
require.NoError(t, err)
293-
294221
cfg := createMinimalValidConfig()
295-
cfg.SummaryPath = summaryPath
222+
cfg.Summary = &SummarySettings{
223+
MessagePatterns: []MessagePatternRule{
224+
{Prefix: "error:", FieldName: "count"},
225+
{Prefix: "warning:", FieldName: "count"},
226+
},
227+
}
296228

297-
err = cfg.Validate()
229+
err := cfg.Validate()
298230
assert.Error(t, err)
299231
assert.Contains(t, err.Error(), "duplicate field_name")
300232
}
301233

302-
// Test Validate with all optional settings valid
234+
// Test Validate with all optional settings valid (inline)
303235
func Test_Config_Validate_WithAllOptionalSettings(t *testing.T) {
304-
// Create temporary files for all settings
305-
tmpDir := t.TempDir()
306-
307-
piiPath := filepath.Join(tmpDir, "pii.yml")
308-
piiContent := `
309-
pii_filter:
310-
domains:
311-
- pattern: "example.com"
312-
replace: "<domain>"
313-
`
314-
err := os.WriteFile(piiPath, []byte(piiContent), 0644)
315-
require.NoError(t, err)
316-
317-
filterPath := filepath.Join(tmpDir, "filter.yml")
318-
filterContent := `
319-
default_action: accept
320-
`
321-
err = os.WriteFile(filterPath, []byte(filterContent), 0644)
322-
require.NoError(t, err)
323-
324-
summaryPath := filepath.Join(tmpDir, "summary.yml")
325-
summaryContent := `
326-
message_patterns:
327-
- prefix: "error:"
328-
field_name: "error_count"
329-
330-
region_timers:
331-
- category: "index"
332-
label: "do_read_index"
333-
time_field: "index_read_time"
334-
`
335-
err = os.WriteFile(summaryPath, []byte(summaryContent), 0644)
336-
require.NoError(t, err)
337-
338236
cfg := createMinimalValidConfig()
339-
cfg.PiiSettingsPath = piiPath
340-
cfg.FilterSettingsPath = filterPath
341-
cfg.SummaryPath = summaryPath
237+
cfg.Pii = &PiiSettings{
238+
Include: PiiInclude{
239+
Hostname: true,
240+
},
241+
}
242+
cfg.Filter = &FilterSettings{
243+
Defaults: FilterDefaults{
244+
RulesetName: "dl:summary",
245+
},
246+
}
247+
cfg.Summary = &SummarySettings{
248+
MessagePatterns: []MessagePatternRule{
249+
{Prefix: "error:", FieldName: "error_count"},
250+
},
251+
RegionTimers: []RegionTimerRule{
252+
{Category: "index", Label: "do_read_index", TimeField: "index_read_time"},
253+
},
254+
}
342255

343-
err = cfg.Validate()
256+
err := cfg.Validate()
344257
assert.NoError(t, err)
345-
assert.NotNil(t, cfg.piiSettings)
346-
assert.NotNil(t, cfg.filterSettings)
347-
assert.NotNil(t, cfg.summary)
258+
assert.NotNil(t, cfg.Pii)
259+
assert.NotNil(t, cfg.Filter)
260+
assert.NotNil(t, cfg.Summary)
348261
}
349262

350263
// Test Validate with command control enabled

summary_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func Test_MessagePatternMatching_Basic(t *testing.T) {
347347
}
348348
tr2.rcvr_base = &Rcvr_Base{
349349
RcvrConfig: &Config{
350-
summary: css,
350+
Summary: css,
351351
},
352352
}
353353

@@ -380,7 +380,7 @@ func Test_MessagePatternMatching_MultipleMatches(t *testing.T) {
380380
}
381381
tr2.rcvr_base = &Rcvr_Base{
382382
RcvrConfig: &Config{
383-
summary: css,
383+
Summary: css,
384384
},
385385
}
386386

@@ -403,7 +403,7 @@ func Test_MessagePatternMatching_NoConfig(t *testing.T) {
403403
}
404404
tr2.rcvr_base = &Rcvr_Base{
405405
RcvrConfig: &Config{
406-
summary: nil,
406+
Summary: nil,
407407
},
408408
}
409409

@@ -432,7 +432,7 @@ func Test_RegionTimerAggregation_Basic(t *testing.T) {
432432
}
433433
tr2.rcvr_base = &Rcvr_Base{
434434
RcvrConfig: &Config{
435-
summary: css,
435+
Summary: css,
436436
},
437437
}
438438

@@ -482,7 +482,7 @@ func Test_RegionTimerAggregation_CountOnly(t *testing.T) {
482482
}
483483
tr2.rcvr_base = &Rcvr_Base{
484484
RcvrConfig: &Config{
485-
summary: css,
485+
Summary: css,
486486
},
487487
}
488488

@@ -522,7 +522,7 @@ func Test_RegionTimerAggregation_TimeOnly(t *testing.T) {
522522
}
523523
tr2.rcvr_base = &Rcvr_Base{
524524
RcvrConfig: &Config{
525-
summary: css,
525+
Summary: css,
526526
},
527527
}
528528

@@ -545,12 +545,12 @@ func Test_RegionTimerAggregation_TimeOnly(t *testing.T) {
545545
func Test_Summary_EmittedAtSummaryLevel(t *testing.T) {
546546
// Create a minimal config with summary
547547
cfg := &Config{
548-
summary: &SummarySettings{
548+
Summary: &SummarySettings{
549549
MessagePatterns: []MessagePatternRule{
550550
{Prefix: "test_msg:", FieldName: "msgCount"},
551551
},
552552
},
553-
filterSettings: &FilterSettings{},
553+
Filter: &FilterSettings{},
554554
}
555555

556556
rcvr := &Rcvr_Base{
@@ -615,7 +615,7 @@ func Test_RegionTimerAggregation_NoMatch(t *testing.T) {
615615
}
616616
tr2.rcvr_base = &Rcvr_Base{
617617
RcvrConfig: &Config{
618-
summary: css,
618+
Summary: css,
619619
},
620620
}
621621

0 commit comments

Comments
 (0)