-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
access_logs_integration_test.go
101 lines (85 loc) · 2.75 KB
/
access_logs_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build integration
package mongodbatlasreceiver
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.mongodb.org/atlas/mongodbatlas"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
)
func TestAccessLogsIntegration(t *testing.T) {
mockClient := mockAccessLogsClient{}
payloadFile, err := os.ReadFile(filepath.Join("testdata", "accesslogs", "sample-payloads", "sample-access-logs.json"))
require.NoError(t, err)
var accessLogs []*mongodbatlas.AccessLogs
err = json.Unmarshal(payloadFile, &accessLogs)
require.NoError(t, err)
mockClient.On("GetProject", mock.Anything, testProjectName).Return(&mongodbatlas.Project{
ID: testProjectID,
Name: testProjectName,
OrgID: testOrgID,
}, nil)
mockClient.On("GetClusters", mock.Anything, testProjectID).Return(
[]mongodbatlas.Cluster{
{
GroupID: testProjectID,
Name: testClusterName,
ProviderSettings: &mongodbatlas.ProviderSettings{
ProviderName: testProviderName,
RegionName: testRegionName,
},
},
},
nil)
mockClient.On("GetAccessLogs", mock.Anything, testProjectID, testClusterName, mock.Anything).Return(accessLogs, nil)
sink := &consumertest.LogsSink{}
fact := NewFactory()
recv, err := fact.CreateLogs(
context.Background(),
receivertest.NewNopSettings(),
&Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Logs: LogConfig{
Enabled: true,
Projects: []*LogsProjectConfig{
{
ProjectConfig: ProjectConfig{
Name: testProjectName,
},
AccessLogs: &AccessLogsConfig{
PollInterval: 1 * time.Second,
},
},
},
},
},
sink,
)
require.NoError(t, err)
rcvr, ok := recv.(*combinedLogsReceiver)
require.True(t, ok)
rcvr.accessLogs.client = &mockClient
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", "accesslogs", "golden", "retrieved-logs.yaml"))
require.NoError(t, err)
require.NoError(t, plogtest.CompareLogs(expectedLogs, logs, plogtest.IgnoreObservedTimestamp()))
}