-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
integration_test.go
112 lines (93 loc) · 3.06 KB
/
integration_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build integration
package awscloudwatchreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscloudwatchreceiver"
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
)
func TestLoggingIntegration(t *testing.T) {
mc := &mockClient{}
mc.On("DescribeLogGroupsWithContext", mock.Anything, mock.Anything, mock.Anything).
Return(loadLogGroups(t), nil)
mc.On("FilterLogEventsWithContext", mock.Anything, mock.Anything, mock.Anything).
Return(loadLogEvents(t), nil)
sink := &consumertest.LogsSink{}
cfg := createDefaultConfig().(*Config)
cfg.Region = "us-east-2"
cfg.Logs.PollInterval = time.Second
cfg.Logs.Groups.AutodiscoverConfig = &AutodiscoverConfig{
Limit: 1,
}
recv, err := NewFactory().CreateLogs(
context.Background(),
receivertest.NewNopSettings(),
cfg,
sink,
)
require.NoError(t, err)
rcvr, ok := recv.(*logsReceiver)
require.True(t, ok)
rcvr.client = mc
err = recv.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
require.Eventually(t, func() bool {
return sink.LogRecordCount() > 0
}, 5*time.Second, 10*time.Millisecond)
err = recv.Shutdown(context.Background())
require.NoError(t, err)
logs := sink.AllLogs()[0]
expectedLogs, err := golden.ReadLogs(filepath.Join("testdata", "golden", "autodiscovered.yaml"))
require.NoError(t, err)
require.NoError(t, plogtest.CompareLogs(expectedLogs, logs, plogtest.IgnoreObservedTimestamp()))
}
var (
logGroupFiles = []string{
filepath.Join("testdata", "log-groups", "group-1.json"),
}
logEventsFiles = []string{
filepath.Join("testdata", "events", "event-1.json"),
}
)
func loadLogGroups(t *testing.T) *cloudwatchlogs.DescribeLogGroupsOutput {
output := make([]*cloudwatchlogs.LogGroup, len(logGroupFiles))
for i, lg := range logGroupFiles {
bytes, err := os.ReadFile(lg)
require.NoError(t, err)
var logGroup cloudwatchlogs.LogGroup
err = json.Unmarshal(bytes, &logGroup)
require.NoError(t, err)
output[i] = &logGroup
}
return &cloudwatchlogs.DescribeLogGroupsOutput{
LogGroups: output,
NextToken: nil,
}
}
func loadLogEvents(t *testing.T) *cloudwatchlogs.FilterLogEventsOutput {
output := make([]*cloudwatchlogs.FilteredLogEvent, len(logEventsFiles))
for i, lg := range logEventsFiles {
bytes, err := os.ReadFile(lg)
require.NoError(t, err)
var event cloudwatchlogs.FilteredLogEvent
err = json.Unmarshal(bytes, &event)
require.NoError(t, err)
output[i] = &event
}
return &cloudwatchlogs.FilterLogEventsOutput{
Events: output,
NextToken: nil,
}
}