@@ -557,6 +557,120 @@ func TestNginxConfigParser_sslCert(t *testing.T) {
557557 assert .Equal (t , certFile , sslCert .GetFileMeta ().GetName ())
558558}
559559
560+ func TestNginxConfigParser_checkLog (t * testing.T ) {
561+ logBuf := & bytes.Buffer {}
562+ stub .StubLoggerWith (logBuf )
563+ tests := []struct {
564+ name string
565+ expectedLog string
566+ accessLog * model.AccessLog
567+ currentAccessLogs []* model.AccessLog
568+ expectedAccessLogs []* model.AccessLog
569+ }{
570+ {
571+ name : "Test 1: valid access log" ,
572+ accessLog : & model.AccessLog {
573+ Name : "/var/log/nginx/access2.log" ,
574+ Format : "$remote_addr - $remote_user [$time_local] \" $request\" \" $http_user_agent\" " +
575+ "\" $http_x_forwarded_for\" $status $body_bytes_sent \" $http_referer\" " ,
576+ Permissions : "" ,
577+ Readable : true ,
578+ },
579+ currentAccessLogs : []* model.AccessLog {
580+ {
581+ Name : "/var/log/nginx/access.log" ,
582+ Format : "$remote_addr - $remote_user [$time_local] \" $request\" \" $http_user_agent\" " +
583+ "\" $http_x_forwarded_for\" $status $body_bytes_sent \" $http_referer\" " ,
584+ Permissions : "" ,
585+ Readable : true ,
586+ },
587+ },
588+ expectedAccessLogs : []* model.AccessLog {
589+ {
590+ Name : "/var/log/nginx/access.log" ,
591+ Format : "$remote_addr - $remote_user [$time_local] \" $request\" \" $http_user_agent\" " +
592+ "\" $http_x_forwarded_for\" $status $body_bytes_sent \" $http_referer\" " ,
593+ Permissions : "" ,
594+ Readable : true ,
595+ },
596+ {
597+ Name : "/var/log/nginx/access2.log" ,
598+ Format : "$remote_addr - $remote_user [$time_local] \" $request\" \" $http_user_agent\" " +
599+ "\" $http_x_forwarded_for\" $status $body_bytes_sent \" $http_referer\" " ,
600+ Permissions : "" ,
601+ Readable : true ,
602+ },
603+ },
604+ expectedLog : "Found valid access log" ,
605+ },
606+ {
607+ name : "Test 2: Duplicate access log, with same format" ,
608+ accessLog : & model.AccessLog {
609+ Name : "/var/log/nginx/access.log" ,
610+ Format : "$remote_addr - $remote_user [$time_local] \" $request\" \" $http_user_agent\" " +
611+ "\" $http_x_forwarded_for\" $status $body_bytes_sent \" $http_referer\" " ,
612+ Permissions : "" ,
613+ Readable : true ,
614+ },
615+ currentAccessLogs : []* model.AccessLog {
616+ {
617+ Name : "/var/log/nginx/access.log" ,
618+ Format : "$remote_addr - $remote_user [$time_local] \" $request\" \" $http_user_agent\" " +
619+ "\" $http_x_forwarded_for\" $status $body_bytes_sent \" $http_referer\" " ,
620+ Permissions : "" ,
621+ Readable : true ,
622+ },
623+ },
624+ expectedAccessLogs : []* model.AccessLog {
625+ {
626+ Name : "/var/log/nginx/access.log" ,
627+ Format : "$remote_addr - $remote_user [$time_local] \" $request\" \" $http_user_agent\" " +
628+ "\" $http_x_forwarded_for\" $status $body_bytes_sent \" $http_referer\" " ,
629+ Permissions : "" ,
630+ Readable : true ,
631+ },
632+ },
633+ expectedLog : "Found duplicate access log, skipping" ,
634+ },
635+
636+ {
637+ name : "Test 3: invalid access log, duplicate access log with different format" ,
638+ accessLog : & model.AccessLog {
639+ Name : "/var/log/nginx/access.log" ,
640+ Format : "$remote_addr - $remote_user [$time_local] \" $request\" \" $http_user_agent\" " +
641+ "\" $http_x_forwarded_for\" $status $body_bytes_sent" ,
642+ Permissions : "" ,
643+ Readable : true ,
644+ },
645+ currentAccessLogs : []* model.AccessLog {
646+ {
647+ Name : "/var/log/nginx/access.log" ,
648+ Format : "$remote_addr - $remote_user [$time_local] \" $request\" \" $http_user_agent\" " +
649+ "\" $http_x_forwarded_for\" $status $body_bytes_sent \" $http_referer\" " ,
650+ Permissions : "" ,
651+ Readable : true ,
652+ },
653+ },
654+ expectedAccessLogs : []* model.AccessLog {},
655+ expectedLog : "Found multiple log_format directives for the same access log. " +
656+ "Multiple log formats are not supported in the same access log, metrics from this access log " +
657+ "will not be collected" ,
658+ },
659+ }
660+
661+ for _ , test := range tests {
662+ t .Run (test .name , func (t * testing.T ) {
663+ ncp := NewNginxConfigParser (types .AgentConfig ())
664+ logs := ncp .addAccessLog (test .accessLog , test .currentAccessLogs )
665+ assert .Equal (t , test .expectedAccessLogs , logs )
666+
667+ helpers .ValidateLog (t , test .expectedLog , logBuf )
668+
669+ logBuf .Reset ()
670+ })
671+ }
672+ }
673+
560674// nolint: maintidx
561675func TestNginxConfigParser_urlsForLocationDirective (t * testing.T ) {
562676 tmpDir := t .TempDir ()
0 commit comments