Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/rabbitmq] Fix flaky integration test #16240

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions receiver/rabbitmqreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ package rabbitmqreceiver

import (
"context"
"errors"
"fmt"
"io"
"path/filepath"
"strings"
"testing"
"time"
"unicode"

"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
Expand All @@ -42,8 +46,7 @@ var (
},
ExposedPorts: []string{"15672:15672"},
Hostname: "localhost",
WaitingFor: wait.ForListeningPort("15672").
WithStartupTimeout(2 * time.Minute),
WaitingFor: waitStrategy{},
}
)

Expand All @@ -54,6 +57,8 @@ func TestRabbitmqIntegration(t *testing.T) {
defer func() {
require.NoError(t, container.Terminate(context.Background()))
}()
require.NoError(t, container.Start(context.Background()))

hostname, err := container.Host(context.Background())
require.NoError(t, err)

Expand Down Expand Up @@ -93,12 +98,36 @@ func getContainer(t *testing.T, req testcontainers.ContainerRequest) testcontain
Started: true,
})
require.NoError(t, err)
return container
}

code, _, err := container.Exec(context.Background(), []string{"./setup.sh"})
require.NoError(t, err)
require.Equal(t, 0, code)
type waitStrategy struct{}

err = container.Start(context.Background())
require.NoError(t, err)
return container
func (ws waitStrategy) WaitUntilReady(ctx context.Context, st wait.StrategyTarget) error {
if err := wait.ForListeningPort("15672").
WithStartupTimeout(2*time.Minute).
WaitUntilReady(ctx, st); err != nil {
return err
}

code, r, err := st.Exec(context.Background(), []string{"./setup.sh"})
if err != nil {
return err
}
if code == 0 {
return nil
}

// Try to read the error message for the sake of debugging
if errBytes, readerErr := io.ReadAll(r); readerErr == nil {
// Error message may have non-printable chars, so clean it up
errStr := strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}, string(errBytes))
return errors.New(strings.TrimSpace(errStr))
}
return errors.New("setup script returned non-zero exit code")
}
16 changes: 11 additions & 5 deletions receiver/rabbitmqreceiver/testdata/integration/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ set -e
USER='otelu'
ROOT_PASS='otelp'

echo "creating user: ${USER} . . ."
rabbitmqctl add_user ${USER} ${ROOT_PASS} > /dev/null
# Test if rabbitmqctl is working at all
rabbitmqctl list_users > /dev/null

echo "Configuring ${USER} permissions. . ."
rabbitmqctl set_user_tags ${USER} administrator > /dev/null
rabbitmqctl set_permissions -p / ${USER} ".*" ".*" ".*" > /dev/null
# Don't recreate user if already exists
if ! rabbitmqctl list_users | grep otelu > /dev/null; then
echo "creating user: ${USER} . . ."
rabbitmqctl add_user ${USER} ${ROOT_PASS} > /dev/null

echo "Configuring ${USER} permissions. . ."
rabbitmqctl set_user_tags ${USER} administrator > /dev/null
rabbitmqctl set_permissions -p / ${USER} ".*" ".*" ".*" > /dev/null
fi

echo "create exchange and queue. . ."
rabbitmqadmin -u ${USER} -p ${ROOT_PASS} declare exchange --vhost=/ name=some_exchange type=direct > /dev/null
Expand Down