forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter_test.go
239 lines (216 loc) · 6.78 KB
/
exporter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package syslogexporter
import (
"context"
"crypto/tls"
"errors"
"io"
"net"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/zap"
)
var expectedForm = "<165>1 2003-08-24T12:14:15Z 192.0.2.1 myproc 8710 - - It's time to make the do-nuts.\n"
var originalForm = "<165>1 2003-08-24T05:14:15-07:00 192.0.2.1 myproc 8710 - - It's time to make the do-nuts."
type exporterTest struct {
srv net.TCPListener
exp *syslogexporter
}
func exampleLog(t *testing.T) plog.LogRecord {
buffer := plog.NewLogRecord()
buffer.Body().SetStr(originalForm)
timestamp := "2003-08-24T05:14:15-07:00"
timeStr, err := time.Parse(time.RFC3339, timestamp)
assert.NoError(t, err, "failed to start test syslog server")
ts := pcommon.NewTimestampFromTime(timeStr)
buffer.SetTimestamp(ts)
attrMap := map[string]any{"proc_id": "8710", "message": "It's time to make the do-nuts.",
"appname": "myproc", "hostname": "192.0.2.1", "priority": int64(165),
"version": int64(1)}
for k, v := range attrMap {
if _, ok := v.(string); ok {
buffer.Attributes().PutStr(k, v.(string))
} else {
buffer.Attributes().PutInt(k, v.(int64))
}
}
return buffer
}
func logRecordsToLogs(record plog.LogRecord) plog.Logs {
logs := plog.NewLogs()
logsSlice := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords()
ls := logsSlice.AppendEmpty()
record.CopyTo(ls)
return logs
}
func createExporterCreateSettings() exporter.Settings {
return exporter.Settings{
TelemetrySettings: component.TelemetrySettings{
Logger: zap.NewNop(),
},
}
}
func TestInitExporter(t *testing.T) {
_, err := initExporter(&Config{Endpoint: "test.com",
Network: "tcp",
Port: 514,
Protocol: "rfc5424"}, createExporterCreateSettings())
assert.NoError(t, err)
}
func buildValidExporter(t *testing.T, server net.TCPListener, cfg *Config) (*syslogexporter, error) {
var port string
var err error
hostPort := server.Addr().String()
cfg.Endpoint, port, err = net.SplitHostPort(hostPort)
require.NoError(t, err, "could not parse port")
cfg.Port, err = strconv.Atoi(port)
require.NoError(t, err, "type error")
exp, err := initExporter(cfg, createExporterCreateSettings())
require.NoError(t, err)
return exp, err
}
func buildInvalidExporter(t *testing.T, server net.TCPListener, cfg *Config) (*syslogexporter, error) {
var port string
var err error
hostPort := server.Addr().String()
cfg.Endpoint, port, err = net.SplitHostPort(hostPort)
require.NoError(t, err, "could not parse endpoint")
require.NotNil(t, port)
invalidPort := "112" // Assign invalid port
cfg.Port, err = strconv.Atoi(invalidPort)
require.NoError(t, err, "type error")
exp, err := initExporter(cfg, createExporterCreateSettings())
require.NoError(t, err)
return exp, err
}
func createServer() (net.TCPListener, error) {
var addr net.TCPAddr
addr.IP = net.IP{127, 0, 0, 1}
addr.Port = 0
testServer, err := net.ListenTCP("tcp", &addr)
return *testServer, err
}
func prepareExporterTest(t *testing.T, cfg *Config, invalidExporter bool) *exporterTest {
// Start a test syslog server
var err error
testServer, err := createServer()
require.NoError(t, err, "failed to start test syslog server")
var exp *syslogexporter
if invalidExporter {
exp, err = buildInvalidExporter(t, testServer, cfg)
} else {
exp, err = buildValidExporter(t, testServer, cfg)
}
require.NoError(t, err, "Error building exporter")
require.NotNil(t, exp)
return &exporterTest{
srv: testServer,
exp: exp,
}
}
func createTestConfig() *Config {
config := createDefaultConfig().(*Config)
config.Network = "tcp"
config.TLSSetting.Insecure = true
return config
}
func TestSyslogExportSuccess(t *testing.T) {
test := prepareExporterTest(t, createTestConfig(), false)
require.NotNil(t, test.exp)
defer test.srv.Close()
go func() {
buffer := exampleLog(t)
logs := logRecordsToLogs(buffer)
err := test.exp.pushLogsData(context.Background(), logs)
assert.NoError(t, err, "could not send message")
}()
err := test.srv.SetDeadline(time.Now().Add(time.Second * 1))
require.NoError(t, err, "cannot set deadline")
conn, err := test.srv.AcceptTCP()
require.NoError(t, err, "could not accept connection")
defer conn.Close()
b, err := io.ReadAll(conn)
require.NoError(t, err, "could not read all")
assert.Equal(t, expectedForm, string(b))
}
func TestSyslogExportFail(t *testing.T) {
test := prepareExporterTest(t, createTestConfig(), true)
defer test.srv.Close()
buffer := exampleLog(t)
logs := logRecordsToLogs(buffer)
consumerErr := test.exp.pushLogsData(context.Background(), logs)
var consumerErrorLogs consumererror.Logs
ok := errors.As(consumerErr, &consumerErrorLogs)
assert.True(t, ok)
consumerLogs := consumererror.Logs.Data(consumerErrorLogs)
rls := consumerLogs.ResourceLogs()
require.Equal(t, 1, rls.Len())
scl := rls.At(0).ScopeLogs()
require.Equal(t, 1, scl.Len())
lrs := scl.At(0).LogRecords()
require.Equal(t, 1, lrs.Len())
droppedLog := lrs.At(0).Body().AsString()
err := test.srv.SetDeadline(time.Now().Add(time.Second * 1))
require.NoError(t, err, "cannot set deadline")
conn, err := test.srv.AcceptTCP()
require.ErrorContains(t, err, "i/o timeout")
require.Nil(t, conn)
assert.ErrorContains(t, consumerErr, "dial tcp 127.0.0.1:112: connect")
assert.Equal(t, droppedLog, originalForm)
}
func TestTLSConfig(t *testing.T) {
tests := []struct {
name string
network string
tlsSettings configtls.ClientConfig
tlsConfig *tls.Config
}{
{name: "TCP with TLS configuration",
network: "tcp",
tlsSettings: configtls.ClientConfig{},
tlsConfig: &tls.Config{},
},
{name: "TCP insecure",
network: "tcp",
tlsSettings: configtls.ClientConfig{Insecure: true},
tlsConfig: nil,
},
{name: "UDP with TLS configuration",
network: "udp",
tlsSettings: configtls.ClientConfig{},
tlsConfig: nil,
},
{name: "UDP insecure",
network: "udp",
tlsSettings: configtls.ClientConfig{Insecure: true},
tlsConfig: nil,
},
}
for _, testInstance := range tests {
t.Run(testInstance.name, func(t *testing.T) {
exporter, err := initExporter(
&Config{Endpoint: "test.com",
Network: testInstance.network,
Port: 514,
Protocol: "rfc5424",
TLSSetting: testInstance.tlsSettings},
createExporterCreateSettings())
assert.NoError(t, err)
if testInstance.tlsConfig != nil {
assert.NotNil(t, exporter.tlsConfig)
} else {
assert.Nil(t, exporter.tlsConfig)
}
})
}
}