123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package tcplogreceiver
- import (
- "context"
- "fmt"
- "net"
- "path/filepath"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component"
- "go.opentelemetry.io/collector/component/componenttest"
- "go.opentelemetry.io/collector/confmap/confmaptest"
- "go.opentelemetry.io/collector/consumer/consumertest"
- "go.opentelemetry.io/collector/receiver/receivertest"
- "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
- "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
- "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/tcp"
- )
- func TestTcp(t *testing.T) {
- testTCP(t, testdataConfigYaml())
- }
- func testTCP(t *testing.T, cfg *TCPLogConfig) {
- numLogs := 5
- f := NewFactory()
- sink := new(consumertest.LogsSink)
- rcvr, err := f.CreateLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, sink)
- require.NoError(t, err)
- require.NoError(t, rcvr.Start(context.Background(), componenttest.NewNopHost()))
- var conn net.Conn
- conn, err = net.Dial("tcp", "127.0.0.1:29018")
- require.NoError(t, err)
- for i := 0; i < numLogs; i++ {
- msg := fmt.Sprintf("<86>1 2021-02-28T00:0%d:02.003Z test msg %d\n", i, i)
- _, err = conn.Write([]byte(msg))
- require.NoError(t, err)
- }
- require.NoError(t, conn.Close())
- require.Eventually(t, expectNLogs(sink, numLogs), 2*time.Second, time.Millisecond)
- require.NoError(t, rcvr.Shutdown(context.Background()))
- require.Len(t, sink.AllLogs(), 1)
- resourceLogs := sink.AllLogs()[0].ResourceLogs().At(0)
- logs := resourceLogs.ScopeLogs().At(0).LogRecords()
- for i := 0; i < numLogs; i++ {
- log := logs.At(i)
- msg := log.Body()
- require.Equal(t, msg.Str(), fmt.Sprintf("<86>1 2021-02-28T00:0%d:02.003Z test msg %d", i, i))
- }
- }
- func TestLoadConfig(t *testing.T) {
- cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
- require.NoError(t, err)
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- sub, err := cm.Sub("tcplog")
- require.NoError(t, err)
- require.NoError(t, component.UnmarshalConfig(sub, cfg))
- assert.NoError(t, component.ValidateConfig(cfg))
- assert.Equal(t, testdataConfigYaml(), cfg)
- }
- func testdataConfigYaml() *TCPLogConfig {
- return &TCPLogConfig{
- BaseConfig: adapter.BaseConfig{
- Operators: []operator.Config{},
- },
- InputConfig: func() tcp.Config {
- c := tcp.NewConfig()
- c.ListenAddress = "127.0.0.1:29018"
- return *c
- }(),
- }
- }
- func TestDecodeInputConfigFailure(t *testing.T) {
- factory := NewFactory()
- badCfg := &TCPLogConfig{
- BaseConfig: adapter.BaseConfig{
- Operators: []operator.Config{},
- },
- InputConfig: func() tcp.Config {
- c := tcp.NewConfig()
- c.Encoding = "fake"
- return *c
- }(),
- }
- receiver, err := factory.CreateLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), badCfg, consumertest.NewNop())
- require.Error(t, err, "receiver creation should fail if input config isn't valid")
- require.Nil(t, receiver, "receiver creation should fail if input config isn't valid")
- }
- func expectNLogs(sink *consumertest.LogsSink, expected int) func() bool {
- return func() bool {
- return sink.LogRecordCount() == expected
- }
- }
|