@@ -243,12 +243,54 @@ func TestResolveAllowedDirectories(t *testing.T) {
243243
244244func TestResolveLog (t * testing.T ) {
245245 viperInstance = viper .NewWithOptions (viper .KeyDelimiter (KeyDelimiter ))
246- viperInstance .Set (LogLevelKey , "error" )
247- viperInstance .Set (LogPathKey , "/var/log/test/test.log" )
248246
249- result := resolveLog ()
250- assert .Equal (t , "error" , result .Level )
251- assert .Equal (t , "/var/log/test/test.log" , result .Path )
247+ tests := []struct {
248+ name string
249+ logLevel string
250+ logPath string
251+ expectedLogPath string
252+ expectedLogLevel string
253+ }{
254+ {
255+ name : "Test 1: Log level set to info" ,
256+ logLevel : "info" ,
257+ logPath : "/var/log/test/test.log" ,
258+ expectedLogPath : "/var/log/test/test.log" ,
259+ expectedLogLevel : "info" ,
260+ },
261+ {
262+ name : "Test 2: Invalid log level set" ,
263+ logLevel : "trace" ,
264+ logPath : "/var/log/test/test.log" ,
265+ expectedLogPath : "/var/log/test/test.log" ,
266+ expectedLogLevel : "info" ,
267+ },
268+ {
269+ name : "Test 3: Log level set to debug" ,
270+ logLevel : "debug" ,
271+ logPath : "/var/log/test/test.log" ,
272+ expectedLogPath : "/var/log/test/test.log" ,
273+ expectedLogLevel : "debug" ,
274+ },
275+ {
276+ name : "Test 4: Log level set with capitalization" ,
277+ logLevel : "DEBUG" ,
278+ logPath : "./logs/nginx.log" ,
279+ expectedLogPath : "./logs/nginx.log" ,
280+ expectedLogLevel : "debug" ,
281+ },
282+ }
283+
284+ for _ , test := range tests {
285+ t .Run (test .name , func (t * testing.T ) {
286+ viperInstance .Set (LogLevelKey , test .logLevel )
287+ viperInstance .Set (LogPathKey , test .logPath )
288+
289+ result := resolveLog ()
290+ assert .Equal (t , test .expectedLogLevel , result .Level )
291+ assert .Equal (t , test .expectedLogPath , result .Path )
292+ })
293+ }
252294}
253295
254296func TestResolveClient (t * testing.T ) {
@@ -299,29 +341,57 @@ func TestResolveCollector(t *testing.T) {
299341}
300342
301343func TestResolveCollectorLog (t * testing.T ) {
302- t .Run ("Test 1: OTel Log Level Set In Config" , func (t * testing.T ) {
303- viperInstance = viper .NewWithOptions (viper .KeyDelimiter (KeyDelimiter ))
304-
305- viperInstance .Set (CollectorLogLevelKey , "info" )
306- viperInstance .Set (CollectorLogPathKey , "/tmp/collector.log" )
307- viperInstance .Set (LogLevelKey , "debug" )
344+ viperInstance = viper .NewWithOptions (viper .KeyDelimiter (KeyDelimiter ))
345+ viperInstance .SetDefault (CollectorLogLevelKey , DefCollectorLogLevel )
346+ tests := []struct {
347+ name string
348+ logLevel string
349+ logPath string
350+ agentLogLevel string
351+ expectedLogPath string
352+ expectedLogLevel string
353+ }{
354+ {
355+ name : "Test 1: OTel Log Level Set In Config" ,
356+ logLevel : "" ,
357+ logPath : "/tmp/collector.log" ,
358+ agentLogLevel : "debug" ,
359+ expectedLogPath : "/tmp/collector.log" ,
360+ expectedLogLevel : "debug" ,
361+ },
362+ {
363+ name : "Test 2: Agent Log Level is Warn" ,
364+ logLevel : "" ,
365+ logPath : "/tmp/collector.log" ,
366+ agentLogLevel : "warn" ,
367+ expectedLogPath : "/tmp/collector.log" ,
368+ expectedLogLevel : "INFO" ,
369+ },
370+ {
371+ name : "Test 3: OTel Log Level Set In Config" ,
372+ logLevel : "INFO" ,
373+ logPath : "/tmp/collector.log" ,
374+ agentLogLevel : "debug" ,
375+ expectedLogPath : "/tmp/collector.log" ,
376+ expectedLogLevel : "INFO" ,
377+ },
378+ }
308379
309- log := resolveCollectorLog ()
380+ for _ , test := range tests {
381+ t .Run (test .name , func (t * testing.T ) {
382+ viperInstance .Set (CollectorLogPathKey , test .logPath )
383+ viperInstance .Set (LogLevelKey , test .agentLogLevel )
310384
311- // Check
312- assert .Equal (t , "info" , log .Level )
313- assert .Equal (t , "/tmp/collector.log" , log .Path )
314- })
385+ if test .logLevel != "" {
386+ viperInstance .Set (CollectorLogLevelKey , test .logLevel )
387+ }
315388
316- t .Run ("Test 2: OTel Log Level Not Set In Config" , func (t * testing.T ) {
317- viperInstance = viper .NewWithOptions (viper .KeyDelimiter (KeyDelimiter ))
318- viperInstance .Set (LogLevelKey , "debug" )
319- viperInstance .Set (CollectorLogPathKey , "/tmp/collector.log" )
389+ log := resolveCollectorLog ()
320390
321- log := resolveCollectorLog ( )
322- assert .Equal (t , "debug" , log .Level )
323- assert . Equal ( t , "/tmp/collector.log" , log . Path )
324- })
391+ assert . Equal ( t , test . expectedLogLevel , log . Level )
392+ assert .Equal (t , test . expectedLogPath , log .Path )
393+ } )
394+ }
325395}
326396
327397func TestCommand (t * testing.T ) {
0 commit comments