rfc3164_formatter_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package syslogexporter
  4. import (
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/pdata/pcommon"
  10. "go.opentelemetry.io/collector/pdata/plog"
  11. )
  12. func TestRFC3164Formatter(t *testing.T) {
  13. expected := "<34>Aug 24 05:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8\n"
  14. logRecord := plog.NewLogRecord()
  15. logRecord.Attributes().PutStr("appname", "su")
  16. logRecord.Attributes().PutStr("hostname", "mymachine")
  17. logRecord.Attributes().PutStr("message", "'su root' failed for lonvick on /dev/pts/8")
  18. logRecord.Attributes().PutInt("priority", 34)
  19. timestamp, err := time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003Z")
  20. require.NoError(t, err)
  21. logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
  22. actual := newRFC3164Formatter().format(logRecord)
  23. assert.NoError(t, err)
  24. assert.Equal(t, expected, actual)
  25. expected = "<165>Aug 24 05:14:15 - -\n"
  26. logRecord = plog.NewLogRecord()
  27. logRecord.Attributes().PutStr("message", "-")
  28. timestamp, err = time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003Z")
  29. require.NoError(t, err)
  30. logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
  31. actual = newRFC3164Formatter().format(logRecord)
  32. assert.NoError(t, err)
  33. assert.Equal(t, expected, actual)
  34. }