2024-06-17 10:13:18 +00:00
package syslog
import (
"bytes"
"reflect"
"testing"
"time"
2024-06-17 20:28:15 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlinsert/insertutils"
2024-06-17 10:13:18 +00:00
)
2024-06-17 20:28:15 +00:00
func TestSyslogLineReader_Success ( t * testing . T ) {
f := func ( data string , linesExpected [ ] string ) {
2024-06-17 10:13:18 +00:00
t . Helper ( )
2024-06-17 20:28:15 +00:00
r := bytes . NewBufferString ( data )
slr := getSyslogLineReader ( r )
defer putSyslogLineReader ( slr )
2024-06-17 10:13:18 +00:00
2024-06-17 20:28:15 +00:00
var lines [ ] string
for slr . nextLine ( ) {
lines = append ( lines , string ( slr . line ) )
}
if err := slr . Error ( ) ; err != nil {
t . Fatalf ( "unexpected error: %s" , err )
2024-06-17 10:13:18 +00:00
}
2024-06-17 20:28:15 +00:00
if ! reflect . DeepEqual ( lines , linesExpected ) {
t . Fatalf ( "unexpected lines read;\ngot\n%q\nwant\n%q" , lines , linesExpected )
}
}
f ( "" , nil )
2024-06-28 12:09:26 +00:00
f ( "\n" , nil )
f ( "\n\n\n" , nil )
2024-06-17 20:28:15 +00:00
f ( "foobar" , [ ] string { "foobar" } )
2024-06-28 12:09:26 +00:00
f ( "foobar\n" , [ ] string { "foobar\n" } )
f ( "\n\nfoo\n\nbar\n\n" , [ ] string { "foo\n\nbar\n\n" } )
f ( ` Jun 3 12:08:33 abcd systemd: Starting Update the local ESM caches... ` , [ ] string { "Jun 3 12:08:33 abcd systemd: Starting Update the local ESM caches..." } )
2024-06-17 20:28:15 +00:00
f ( ` Jun 3 12 : 0 8 : 33 abcd systemd : Starting Update the local ESM caches ...
2024-06-28 12:09:26 +00:00
2024-06-17 20:28:15 +00:00
48 < 165 > Jun 4 12 : 0 8 : 33 abcd systemd [ 345 ] : abc defg < 123 > 1 2023 - 06 - 03 T17 : 42 : 12.345 Z mymachine . example . com appname 12345 ID47 [ exampleSDID @ 32473 iut = "3" eventSource = "Application 123 = ] 56" eventID = "11211" ] This is a test message with structured data .
2024-06-28 12:09:26 +00:00
2024-06-17 20:28:15 +00:00
` , [ ] string {
"Jun 3 12:08:33 abcd systemd: Starting Update the local ESM caches..." ,
"<165>Jun 4 12:08:33 abcd systemd[345]: abc defg" ,
` <123>1 2023-06-03T17:42:12.345Z mymachine.example.com appname 12345 ID47 [exampleSDID@32473 iut="3" eventSource="Application 123 = ] 56" eventID="11211"] This is a test message with structured data. ` ,
} )
}
func TestSyslogLineReader_Failure ( t * testing . T ) {
f := func ( data string ) {
t . Helper ( )
2024-06-17 10:13:18 +00:00
r := bytes . NewBufferString ( data )
2024-06-17 20:28:15 +00:00
slr := getSyslogLineReader ( r )
defer putSyslogLineReader ( slr )
if slr . nextLine ( ) {
t . Fatalf ( "expecting failure to read the first line" )
2024-06-17 10:13:18 +00:00
}
2024-06-17 20:28:15 +00:00
if err := slr . Error ( ) ; err == nil {
t . Fatalf ( "expecting non-nil error" )
2024-06-17 10:13:18 +00:00
}
2024-06-17 20:28:15 +00:00
}
// invalid format for message size
f ( "12foo bar" )
// too big message size
f ( "123 aa" )
f ( "1233423432 abc" )
}
func TestProcessStreamInternal_Success ( t * testing . T ) {
f := func ( data string , currentYear , rowsExpected int , timestampsExpected [ ] int64 , resultExpected string ) {
t . Helper ( )
2024-06-17 10:13:18 +00:00
2024-06-17 20:28:15 +00:00
MustInit ( )
defer MustStop ( )
globalTimezone = time . UTC
globalCurrentYear . Store ( int64 ( currentYear ) )
tlp := & insertutils . TestLogMessageProcessor { }
r := bytes . NewBufferString ( data )
2024-07-01 22:23:54 +00:00
if err := processStreamInternal ( r , "" , false , tlp ) ; err != nil {
2024-06-17 20:28:15 +00:00
t . Fatalf ( "unexpected error: %s" , err )
2024-06-17 10:13:18 +00:00
}
2024-06-17 20:28:15 +00:00
if err := tlp . Verify ( rowsExpected , timestampsExpected , resultExpected ) ; err != nil {
t . Fatal ( err )
2024-06-17 10:13:18 +00:00
}
}
data := ` Jun 3 12 : 0 8 : 33 abcd systemd : Starting Update the local ESM caches ...
2024-06-17 20:28:15 +00:00
48 < 165 > Jun 4 12 : 0 8 : 33 abcd systemd [ 345 ] : abc defg < 123 > 1 2023 - 06 - 03 T17 : 42 : 12.345 Z mymachine . example . com appname 12345 ID47 [ exampleSDID @ 32473 iut = "3" eventSource = "Application 123 = ] 56" eventID = "11211" ] This is a test message with structured data .
2024-06-17 10:13:18 +00:00
`
currentYear := 2023
rowsExpected := 3
timestampsExpected := [ ] int64 { 1685794113000000000 , 1685880513000000000 , 1685814132345000000 }
resultExpected := ` { "format" : "rfc3164" , "timestamp" : "" , "hostname" : "abcd" , "app_name" : "systemd" , "_msg" : "Starting Update the local ESM caches..." }
{ "priority" : "165" , "facility" : "20" , "severity" : "5" , "format" : "rfc3164" , "timestamp" : "" , "hostname" : "abcd" , "app_name" : "systemd" , "proc_id" : "345" , "_msg" : "abc defg" }
2024-06-25 12:52:43 +00:00
{ "priority" : "123" , "facility" : "15" , "severity" : "3" , "format" : "rfc5424" , "timestamp" : "" , "hostname" : "mymachine.example.com" , "app_name" : "appname" , "proc_id" : "12345" , "msg_id" : "ID47" , "exampleSDID@32473.iut" : "3" , "exampleSDID@32473.eventSource" : "Application 123 = ] 56" , "exampleSDID@32473.eventID" : "11211" , "_msg" : "This is a test message with structured data." } `
2024-06-17 10:13:18 +00:00
f ( data , currentYear , rowsExpected , timestampsExpected , resultExpected )
}
2024-06-17 20:28:15 +00:00
func TestProcessStreamInternal_Failure ( t * testing . T ) {
f := func ( data string ) {
t . Helper ( )
MustInit ( )
defer MustStop ( )
tlp := & insertutils . TestLogMessageProcessor { }
r := bytes . NewBufferString ( data )
2024-07-01 22:23:54 +00:00
if err := processStreamInternal ( r , "" , false , tlp ) ; err == nil {
2024-06-17 20:28:15 +00:00
t . Fatalf ( "expecting non-nil error" )
}
}
// invalid format for message size
f ( "12foo bar" )
// too big message size
f ( "123 foo" )
f ( "123456789 bar" )
}