Skip to content

Commit

Permalink
[receiver/cloudfoundryreceiver] fix tests
Browse files Browse the repository at this point in the history
Co-authored-by: Jose Riguera <jose.riguera@springer.com>
  • Loading branch information
CemDK and jriguera committed May 13, 2024
1 parent 241167a commit a3900d5
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 6 deletions.
45 changes: 45 additions & 0 deletions receiver/cloudfoundryreceiver/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
)

Expand Down Expand Up @@ -142,6 +143,50 @@ func TestConvertGaugeEnvelope(t *testing.T) {
assertAttributes(t, dataPoint.Attributes(), expectedAttributes)
}

func TestConvertLogsEnvelope(t *testing.T) {
now := time.Now()
before := time.Now().Add(-time.Second)

envelope := loggregator_v2.Envelope{
Timestamp: before.UnixNano(),
SourceId: "uaa",
Tags: map[string]string{
"origin": "gorouter",
"deployment": "cf",
"job": "router",
"index": "bc276108-8282-48a5-bae7-c009c4392246",
"ip": "10.244.0.34",
},
Message: &loggregator_v2.Envelope_Log{
Log: &loggregator_v2.Log{
Payload: []byte("log message payload"),
Type: loggregator_v2.Log_OUT,
},
},
}

logSlice := plog.NewLogRecordSlice()

convertEnvelopeToLogs(&envelope, logSlice, now)

require.Equal(t, 1, logSlice.Len())

log := logSlice.At(0)
assert.Equal(t, "log message payload", log.Body().AsString())
assert.Equal(t, plog.SeverityNumberInfo.String(), log.SeverityText())
assert.Equal(t, pcommon.NewTimestampFromTime(before), log.Timestamp())
assert.Equal(t, pcommon.NewTimestampFromTime(now), log.ObservedTimestamp())

assertAttributes(t, log.Attributes(), map[string]string{
"org.cloudfoundry.source_id": "uaa",
"org.cloudfoundry.origin": "gorouter",
"org.cloudfoundry.deployment": "cf",
"org.cloudfoundry.job": "router",
"org.cloudfoundry.index": "bc276108-8282-48a5-bae7-c009c4392246",
"org.cloudfoundry.ip": "10.244.0.34",
})
}

func assertAttributes(t *testing.T, attributes pcommon.Map, expected map[string]string) {
assert.Equal(t, len(expected), attributes.Len())

Expand Down
14 changes: 12 additions & 2 deletions receiver/cloudfoundryreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ func TestCreateDefaultConfig(t *testing.T) {
assert.NoError(t, componenttest.CheckConfigStruct(cfg))
}

func TestCreateReceiver(t *testing.T) {
func TestCreateMetricsReceiver(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

params := receivertest.NewNopCreateSettings()
tReceiver, err := factory.CreateMetricsReceiver(context.Background(), params, cfg, consumertest.NewNop())
assert.NoError(t, err)
assert.NotNil(t, tReceiver, "receiver creation failed")
assert.NotNil(t, tReceiver, "metrics receiver creation failed")
}

func TestCreateLogsReceiver(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

params := receivertest.NewNopCreateSettings()
tReceiver, err := factory.CreateLogsReceiver(context.Background(), params, cfg, consumertest.NewNop())
assert.NoError(t, err)
assert.NotNil(t, tReceiver, "logs receiver creation failed")
}
31 changes: 28 additions & 3 deletions receiver/cloudfoundryreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,38 @@ import (
"go.opentelemetry.io/collector/receiver/receivertest"
)

// Test to make sure a new receiver can be created properly, started and shutdown with the default config
func TestDefaultValidReceiver(t *testing.T) {
// Test to make sure a new metrics receiver can be created properly, started and shutdown with the default config
func TestDefaultValidMetricsReceiver(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
params := receivertest.NewNopCreateSettings()

receiver, err := newCloudFoundryReceiver(
receiver, err := newCloudFoundryMetricsReceiver(
params,
*cfg,
consumertest.NewNop(),
)

require.NoError(t, err)
require.NotNil(t, receiver, "receiver creation failed")

// Test start
ctx := context.Background()
err = receiver.Start(ctx, componenttest.NewNopHost())
require.NoError(t, err)

// Test shutdown
err = receiver.Shutdown(ctx)
require.NoError(t, err)
}

// Test to make sure a new logs receiver can be created properly, started and shutdown with the default config
func TestDefaultValidLogsReceiver(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
params := receivertest.NewNopCreateSettings()

receiver, err := newCloudFoundryLogsReceiver(
params,
*cfg,
consumertest.NewNop(),
Expand Down
39 changes: 38 additions & 1 deletion receiver/cloudfoundryreceiver/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// Ensure stream create works as expected
func TestValidStream(t *testing.T) {
func TestValidMetricsStream(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)

Expand Down Expand Up @@ -50,3 +50,40 @@ func TestValidStream(t *testing.T) {
cancel()
}

// Ensure stream create works as expected
func TestValidLogsStream(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)

uaa, err := newUAATokenProvider(
zap.NewNop(),
cfg.UAA.LimitedClientConfig,
cfg.UAA.Username,
string(cfg.UAA.Password))

require.NoError(t, err)
require.NotNil(t, uaa)

streamFactory, streamErr := newEnvelopeStreamFactory(
context.Background(),
componenttest.NewNopTelemetrySettings(),
uaa,
cfg.RLPGateway.ClientConfig,
componenttest.NewNopHost())

require.NoError(t, streamErr)
require.NotNil(t, streamFactory)

innerCtx, cancel := context.WithCancel(context.Background())

envelopeStream, createErr := streamFactory.CreateStream(
innerCtx,
cfg.RLPGateway.ShardID,
telemetryTypeLogs,
)

require.NoError(t, createErr)
require.NotNil(t, envelopeStream)

cancel()
}

0 comments on commit a3900d5

Please sign in to comment.