|
1 | 1 | package trace2receiver |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "os" |
5 | | - "path/filepath" |
6 | 4 | "runtime" |
7 | 5 | "testing" |
8 | 6 |
|
9 | 7 | "github.com/stretchr/testify/assert" |
10 | | - "github.com/stretchr/testify/require" |
11 | 8 | ) |
12 | 9 |
|
13 | 10 | // Test Validate with minimal valid config on Windows |
@@ -156,195 +153,111 @@ func Test_Config_Validate_RejectDgramUnix(t *testing.T) { |
156 | 153 | assert.Contains(t, err.Error(), "SOCK_DGRAM sockets are not supported") |
157 | 154 | } |
158 | 155 |
|
159 | | -// Test Validate with valid PII settings file |
| 156 | +// Test Validate with valid PII settings (inline) |
160 | 157 | 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) { |
183 | 158 | cfg := createMinimalValidConfig() |
184 | | - cfg.PiiSettingsPath = "/nonexistent/pii.yml" |
| 159 | + cfg.Pii = &PiiSettings{ |
| 160 | + Include: PiiInclude{ |
| 161 | + Hostname: true, |
| 162 | + Username: false, |
| 163 | + }, |
| 164 | + } |
185 | 165 |
|
186 | 166 | 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() |
205 | 167 | assert.NoError(t, err) |
206 | | - assert.NotNil(t, cfg.filterSettings) |
| 168 | + assert.NotNil(t, cfg.Pii) |
207 | 169 | } |
208 | 170 |
|
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) { |
211 | 173 | cfg := createMinimalValidConfig() |
212 | | - cfg.FilterSettingsPath = "/nonexistent/filter.yml" |
| 174 | + cfg.Filter = &FilterSettings{ |
| 175 | + Defaults: FilterDefaults{ |
| 176 | + RulesetName: "dl:verbose", |
| 177 | + }, |
| 178 | + } |
213 | 179 |
|
214 | 180 | 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() |
243 | 181 | 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) |
247 | 183 | } |
248 | 184 |
|
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) { |
251 | 187 | 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 | + } |
253 | 197 |
|
254 | 198 | 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)) |
256 | 203 | } |
257 | 204 |
|
258 | | -// Test Validate with invalid summary settings file (malformed YAML) |
| 205 | +// Test Validate with invalid summary settings (empty field_name) |
259 | 206 | 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 | | - |
271 | 207 | cfg := createMinimalValidConfig() |
272 | | - cfg.SummaryPath = summaryPath |
| 208 | + cfg.Summary = &SummarySettings{ |
| 209 | + MessagePatterns: []MessagePatternRule{ |
| 210 | + {Prefix: "error:", FieldName: ""}, |
| 211 | + }, |
| 212 | + } |
273 | 213 |
|
274 | | - err = cfg.Validate() |
| 214 | + err := cfg.Validate() |
275 | 215 | assert.Error(t, err) |
276 | 216 | assert.Contains(t, err.Error(), "field_name cannot be empty") |
277 | 217 | } |
278 | 218 |
|
279 | 219 | // Test Validate with summary settings with duplicate field names |
280 | 220 | 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 | | - |
294 | 221 | 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 | + } |
296 | 228 |
|
297 | | - err = cfg.Validate() |
| 229 | + err := cfg.Validate() |
298 | 230 | assert.Error(t, err) |
299 | 231 | assert.Contains(t, err.Error(), "duplicate field_name") |
300 | 232 | } |
301 | 233 |
|
302 | | -// Test Validate with all optional settings valid |
| 234 | +// Test Validate with all optional settings valid (inline) |
303 | 235 | 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 | | - |
338 | 236 | 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 | + } |
342 | 255 |
|
343 | | - err = cfg.Validate() |
| 256 | + err := cfg.Validate() |
344 | 257 | 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) |
348 | 261 | } |
349 | 262 |
|
350 | 263 | // Test Validate with command control enabled |
|
0 commit comments