11// Copyright The OpenTelemetry Authors
22// SPDX-License-Identifier: Apache-2.0
33
4+ //go:build integration
5+
46package rabbitmqexporter
57
68import (
@@ -17,6 +19,7 @@ import (
1719 "github.com/testcontainers/testcontainers-go/wait"
1820 "go.opentelemetry.io/collector/component/componenttest"
1921 "go.opentelemetry.io/collector/exporter/exportertest"
22+ "go.opentelemetry.io/collector/pdata/plog"
2023
2124 "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
2225)
@@ -41,18 +44,7 @@ func TestExportWithNetworkIssueRecovery(t *testing.T) {
4144 for _ , c := range testCase {
4245 t .Run (c .name , func (t * testing.T ) {
4346 port := randPort ()
44- req := testcontainers.ContainerRequest {
45- Image : c .image ,
46- ExposedPorts : []string {fmt .Sprintf ("%s:5672" , port )},
47- WaitingFor : wait .ForListeningPort ("5672" ).
48- WithStartupTimeout (2 * time .Minute ),
49- Env : map [string ]string {
50- "RABBITMQ_DEFAULT_USER" : username ,
51- "RABBITMQ_DEFAULT_PASS" : password ,
52- "RABBITMQ_DEFAULT_VHOST" : vhost ,
53- },
54- }
55- container := getContainer (t , req )
47+ container := startRabbitMQContainer (t , c .image , port )
5648 defer func () {
5749 err := container .Terminate (context .Background ())
5850 require .NoError (t , err )
@@ -84,7 +76,10 @@ func TestExportWithNetworkIssueRecovery(t *testing.T) {
8476 err = exporter .ConsumeLogs (context .Background (), logs )
8577 require .NoError (t , err )
8678 consumed := <- consumer
87- require .NotNil (t , consumed .Body )
79+ unmarshaller := & plog.ProtoUnmarshaler {}
80+ receivedLogs , err := unmarshaller .UnmarshalLogs (consumed .Body )
81+ require .NoError (t , err )
82+ require .Equal (t , logs , receivedLogs )
8883
8984 // Stop the container before exporting the next logs to simulate a network issue
9085 err = channel .Close ()
@@ -111,11 +106,41 @@ func TestExportWithNetworkIssueRecovery(t *testing.T) {
111106 err = exporter .ConsumeLogs (context .Background (), logs )
112107 require .NoError (t , err )
113108 consumed = <- consumer
114- require .NotNil (t , consumed .Body )
109+ receivedLogs , err = unmarshaller .UnmarshalLogs (consumed .Body )
110+ require .NoError (t , err )
111+ require .Equal (t , logs , receivedLogs )
115112 })
116113 }
117114}
118115
116+ func startRabbitMQContainer (t * testing.T , image string , port string ) testcontainers.Container {
117+ container , err := testcontainers .GenericContainer (
118+ context .Background (),
119+ testcontainers.GenericContainerRequest {
120+ ContainerRequest : testcontainers.ContainerRequest {
121+ Image : image ,
122+ ExposedPorts : []string {fmt .Sprintf ("%s:5672" , port )},
123+ WaitingFor : & wait.MultiStrategy {
124+ Strategies : []wait.Strategy {
125+ wait .ForListeningPort ("5672" ).WithStartupTimeout (1 * time .Minute ),
126+ wait .ForExec ([]string {"rabbitmq-diagnostics" , "check_running" }).WithStartupTimeout (1 * time .Minute ),
127+ },
128+ },
129+ Env : map [string ]string {
130+ "RABBITMQ_DEFAULT_USER" : username ,
131+ "RABBITMQ_DEFAULT_PASS" : password ,
132+ "RABBITMQ_DEFAULT_VHOST" : vhost ,
133+ },
134+ },
135+ Started : true ,
136+ })
137+ require .NoError (t , err )
138+
139+ err = container .Start (context .Background ())
140+ require .NoError (t , err )
141+ return container
142+ }
143+
119144func setupQueueConsumer (t * testing.T , queueName string , endpoint string ) (* amqp.Connection , * amqp.Channel , <- chan amqp.Delivery ) {
120145 connection , err := amqp .DialConfig (endpoint , amqp.Config {
121146 SASL : []amqp.Authentication {
@@ -140,21 +165,6 @@ func setupQueueConsumer(t *testing.T, queueName string, endpoint string) (*amqp.
140165 return connection , channel , consumer
141166}
142167
143- func getContainer (t * testing.T , req testcontainers.ContainerRequest ) testcontainers.Container {
144- require .NoError (t , req .Validate ())
145- container , err := testcontainers .GenericContainer (
146- context .Background (),
147- testcontainers.GenericContainerRequest {
148- ContainerRequest : req ,
149- Started : true ,
150- })
151- require .NoError (t , err )
152-
153- err = container .Start (context .Background ())
154- require .NoError (t , err )
155- return container
156- }
157-
158168func randPort () string {
159169 rs := rand .NewSource (time .Now ().Unix ())
160170 r := rand .New (rs )
0 commit comments