raw_marshaller_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package kafkaexporter
  4. import (
  5. "testing"
  6. "github.com/IBM/sarama"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/pdata/plog"
  10. )
  11. func ptr(i int) *int {
  12. return &i
  13. }
  14. func Test_RawMarshaler(t *testing.T) {
  15. tests := []struct {
  16. name string
  17. countExpected *int
  18. logRecord func() plog.LogRecord
  19. marshaled sarama.ByteEncoder
  20. errorExpected bool
  21. }{
  22. {
  23. name: "string",
  24. logRecord: func() plog.LogRecord {
  25. lr := plog.NewLogRecord()
  26. lr.Body().SetStr("foo")
  27. return lr
  28. },
  29. errorExpected: false,
  30. marshaled: []byte("\"foo\""),
  31. },
  32. {
  33. name: "[]byte",
  34. logRecord: func() plog.LogRecord {
  35. lr := plog.NewLogRecord()
  36. lr.Body().SetEmptyBytes().FromRaw([]byte("foo"))
  37. return lr
  38. },
  39. errorExpected: false,
  40. marshaled: []byte("foo"),
  41. },
  42. {
  43. name: "double",
  44. logRecord: func() plog.LogRecord {
  45. lr := plog.NewLogRecord()
  46. lr.Body().SetDouble(float64(1.64))
  47. return lr
  48. },
  49. errorExpected: false,
  50. marshaled: []byte("1.64"),
  51. },
  52. {
  53. name: "int",
  54. logRecord: func() plog.LogRecord {
  55. lr := plog.NewLogRecord()
  56. lr.Body().SetInt(int64(456))
  57. return lr
  58. },
  59. errorExpected: false,
  60. marshaled: []byte("456"),
  61. },
  62. {
  63. name: "empty",
  64. logRecord: func() plog.LogRecord {
  65. lr := plog.NewLogRecord()
  66. return lr
  67. },
  68. countExpected: ptr(0),
  69. errorExpected: false,
  70. marshaled: []byte{},
  71. },
  72. {
  73. name: "bool",
  74. logRecord: func() plog.LogRecord {
  75. lr := plog.NewLogRecord()
  76. lr.Body().SetBool(false)
  77. return lr
  78. },
  79. errorExpected: false,
  80. marshaled: []byte("false"),
  81. },
  82. {
  83. name: "slice",
  84. logRecord: func() plog.LogRecord {
  85. lr := plog.NewLogRecord()
  86. slice := lr.Body().SetEmptySlice()
  87. slice.AppendEmpty().SetStr("foo")
  88. slice.AppendEmpty().SetStr("bar")
  89. slice.AppendEmpty().SetBool(false)
  90. return lr
  91. },
  92. errorExpected: false,
  93. marshaled: []byte(`["foo","bar",false]`),
  94. },
  95. {
  96. name: "map",
  97. logRecord: func() plog.LogRecord {
  98. lr := plog.NewLogRecord()
  99. m := lr.Body().SetEmptyMap()
  100. m.PutStr("foo", "foo")
  101. m.PutStr("bar", "bar")
  102. m.PutBool("foobar", false)
  103. return lr
  104. },
  105. errorExpected: false,
  106. marshaled: []byte(`{"bar":"bar","foo":"foo","foobar":false}`),
  107. },
  108. }
  109. for _, test := range tests {
  110. t.Run(test.name, func(t *testing.T) {
  111. r := newRawMarshaler()
  112. logs := plog.NewLogs()
  113. lr := test.logRecord()
  114. lr.MoveTo(logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty())
  115. messages, err := r.Marshal(logs, "foo")
  116. if test.errorExpected {
  117. require.Error(t, err)
  118. } else {
  119. require.NoError(t, err)
  120. }
  121. countExpected := 1
  122. if test.countExpected != nil {
  123. countExpected = *test.countExpected
  124. }
  125. assert.Len(t, messages, countExpected)
  126. if countExpected > 0 {
  127. bytes, ok := messages[0].Value.(sarama.ByteEncoder)
  128. require.True(t, ok)
  129. assert.Equal(t, test.marshaled, bytes)
  130. }
  131. })
  132. }
  133. }