forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rabbitmq_exporter_test.go
172 lines (136 loc) · 5.53 KB
/
rabbitmq_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package rabbitmqexporter
import (
"context"
"crypto/tls"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/exporter/exportertest"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/rabbitmqexporter/internal/publisher"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
)
const (
routingKey = "routing_key"
connectionName = "connection_name"
)
func TestStartAndShutdown(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
pub := mockPublisher{}
var pubFactory = func(publisher.DialConfig) (publisher.Publisher, error) {
return &pub, nil
}
exporter := newRabbitmqExporter(cfg, exportertest.NewNopSettings().TelemetrySettings, pubFactory, newTLSFactory(cfg), routingKey, connectionName)
err := exporter.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
pub.On("Close").Return(nil)
err = exporter.shutdown(context.Background())
require.NoError(t, err)
pub.AssertExpectations(t)
}
func TestStart_UnknownMarshallerEncoding(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
pub := mockPublisher{}
var pubFactory = func(publisher.DialConfig) (publisher.Publisher, error) {
return &pub, nil
}
unknownExtensionID := component.NewID(component.MustNewType("invalid_encoding"))
cfg.EncodingExtensionID = &unknownExtensionID
host := mockHost{}
exporter := newRabbitmqExporter(cfg, exportertest.NewNopSettings().TelemetrySettings, pubFactory, newTLSFactory(cfg), routingKey, connectionName)
err := exporter.start(context.Background(), host)
assert.EqualError(t, err, "unknown encoding \"invalid_encoding\"")
err = exporter.shutdown(context.Background())
require.NoError(t, err)
}
func TestStart_PublisherCreationErr(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
var pubFactory = func(publisher.DialConfig) (publisher.Publisher, error) {
return nil, errors.New("simulating error creating publisher")
}
exporter := newRabbitmqExporter(cfg, exportertest.NewNopSettings().TelemetrySettings, pubFactory, newTLSFactory(cfg), routingKey, connectionName)
err := exporter.start(context.Background(), componenttest.NewNopHost())
assert.EqualError(t, err, "simulating error creating publisher")
err = exporter.shutdown(context.Background())
require.NoError(t, err)
}
func TestStart_TLSError(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
pub := mockPublisher{}
var pubFactory = func(publisher.DialConfig) (publisher.Publisher, error) {
return &pub, nil
}
tlsFactory := func(context.Context) (*tls.Config, error) {
return nil, errors.New("simulating tls config error")
}
exporter := newRabbitmqExporter(cfg, exportertest.NewNopSettings().TelemetrySettings, pubFactory, tlsFactory, routingKey, connectionName)
err := exporter.start(context.Background(), componenttest.NewNopHost())
assert.EqualError(t, err, "simulating tls config error")
err = exporter.shutdown(context.Background())
require.NoError(t, err)
}
func TestPublishMetrics(t *testing.T) {
pub, exporter := exporterForPublishing(t)
pub.On("Publish", mock.Anything, mock.MatchedBy(func(message publisher.Message) bool {
return message.RoutingKey == routingKey && len(message.Body) > 0 && message.Exchange == ""
})).Return(nil)
err := exporter.publishMetrics(context.Background(), testdata.GenerateMetricsOneMetric())
require.NoError(t, err)
pub.AssertExpectations(t)
}
func TestPublishTraces(t *testing.T) {
pub, exporter := exporterForPublishing(t)
pub.On("Publish", mock.Anything, mock.MatchedBy(func(message publisher.Message) bool {
return message.RoutingKey == routingKey && len(message.Body) > 0 && message.Exchange == ""
})).Return(nil)
err := exporter.publishTraces(context.Background(), testdata.GenerateTracesOneSpan())
require.NoError(t, err)
pub.AssertExpectations(t)
}
func TestPublishLogs(t *testing.T) {
pub, exporter := exporterForPublishing(t)
pub.On("Publish", mock.Anything, mock.MatchedBy(func(message publisher.Message) bool {
return message.RoutingKey == routingKey && len(message.Body) > 0 && message.Exchange == ""
})).Return(nil)
err := exporter.publishLogs(context.Background(), testdata.GenerateLogsOneLogRecord())
require.NoError(t, err)
pub.AssertExpectations(t)
}
func exporterForPublishing(t *testing.T) (*mockPublisher, *rabbitmqExporter) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
pub := mockPublisher{}
var pubFactory = func(publisher.DialConfig) (publisher.Publisher, error) {
return &pub, nil
}
exporter := newRabbitmqExporter(cfg, exportertest.NewNopSettings().TelemetrySettings, pubFactory, newTLSFactory(cfg), routingKey, connectionName)
err := exporter.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
return &pub, exporter
}
type mockPublisher struct {
mock.Mock
}
func (c *mockPublisher) Publish(ctx context.Context, message publisher.Message) error {
args := c.Called(ctx, message)
return args.Error(0)
}
func (c *mockPublisher) Close() error {
args := c.Called()
return args.Error(0)
}
type mockHost struct {
component.Host
}
func (h mockHost) GetExtensions() map[component.ID]component.Component {
return make(map[component.ID]component.Component)
}