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

chore(rabbitmq): refactor assertEntity in tests #2856

Merged
merged 1 commit into from
Oct 30, 2024
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
60 changes: 17 additions & 43 deletions modules/rabbitmq/rabbitmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -212,38 +211,29 @@ func TestRunContainer_withAllSettings(t *testing.T) {
testcontainers.CleanupContainer(t, rabbitmqContainer)
require.NoError(t, err)

require.True(t, assertEntity(t, rabbitmqContainer, "queues", "queue1", "queue2", "queue3", "queue4"))
require.True(t, assertEntity(t, rabbitmqContainer, "exchanges", "direct-exchange", "topic-exchange", "topic-exchange-2", "topic-exchange-3", "topic-exchange-4"))
require.True(t, assertEntity(t, rabbitmqContainer, "users", "user1", "user2"))
require.True(t, assertEntity(t, rabbitmqContainer, "policies", "max length policy", "alternate exchange policy"))
require.True(t, assertEntityWithVHost(t, rabbitmqContainer, "policies", 2, "max length policy", "alternate exchange policy"))
require.True(t, assertEntity(t, rabbitmqContainer, "operator_policies", "operator policy 1"))
require.True(t, assertPluginIsEnabled(t, rabbitmqContainer, "rabbitmq_shovel", "rabbitmq_random_exchange"))
requireEntity(t, rabbitmqContainer, "queues", "queue1", "queue2", "queue3", "queue4")
requireEntity(t, rabbitmqContainer, "exchanges", "direct-exchange", "topic-exchange", "topic-exchange-2", "topic-exchange-3", "topic-exchange-4")
requireEntity(t, rabbitmqContainer, "users", "user1", "user2")
requireEntity(t, rabbitmqContainer, "policies", "max length policy", "alternate exchange policy")
requireEntityWithVHost(t, rabbitmqContainer, "policies", 2, "max length policy", "alternate exchange policy")
requireEntity(t, rabbitmqContainer, "operator_policies", "operator policy 1")
requirePluginIsEnabled(t, rabbitmqContainer, "rabbitmq_shovel", "rabbitmq_random_exchange")
}

func assertEntity(t *testing.T, container testcontainers.Container, listCommand string, entities ...string) bool {
func requireEntity(t *testing.T, container testcontainers.Container, listCommand string, entities ...string) {
t.Helper()

ctx := context.Background()

cmd := []string{"rabbitmqadmin", "list", listCommand}

_, out, err := container.Exec(ctx, cmd)
require.NoError(t, err)

check, err := io.ReadAll(out)
require.NoError(t, err)

check := testcontainers.RequireContainerExec(ctx, t, container, cmd)
for _, e := range entities {
if !strings.Contains(string(check), e) {
return false
}
require.Contains(t, check, e)
}

return true
}

func assertEntityWithVHost(t *testing.T, container testcontainers.Container, listCommand string, vhostID int, entities ...string) bool {
func requireEntityWithVHost(t *testing.T, container testcontainers.Container, listCommand string, vhostID int, entities ...string) {
t.Helper()

ctx := context.Background()
Expand All @@ -253,38 +243,22 @@ func assertEntityWithVHost(t *testing.T, container testcontainers.Container, lis
cmd = append(cmd, fmt.Sprintf("--vhost=vhost%d", vhostID))
}

_, out, err := container.Exec(ctx, cmd)
require.NoError(t, err)

check, err := io.ReadAll(out)
require.NoError(t, err)

check := testcontainers.RequireContainerExec(ctx, t, container, cmd)
for _, e := range entities {
if !strings.Contains(string(check), e) {
return false
}
require.Contains(t, check, e)
}

return true
}

func assertPluginIsEnabled(t *testing.T, container testcontainers.Container, plugins ...string) bool {
func requirePluginIsEnabled(t *testing.T, container testcontainers.Container, plugins ...string) {
t.Helper()

ctx := context.Background()

for _, plugin := range plugins {

_, out, err := container.Exec(ctx, []string{"rabbitmq-plugins", "is_enabled", plugin})
require.NoError(t, err)

check, err := io.ReadAll(out)
require.NoError(t, err)
cmd := []string{"rabbitmq-plugins", "is_enabled", plugin}

if !strings.Contains(string(check), plugin+" is enabled") {
return false
}
check := testcontainers.RequireContainerExec(ctx, t, container, cmd)
require.Contains(t, check, plugin+" is enabled")
}

return true
}
16 changes: 16 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testcontainers
import (
"context"
"fmt"
"io"
"regexp"
"testing"

Expand Down Expand Up @@ -147,3 +148,18 @@ func isCleanupSafe(err error) bool {
return false
}
}

// RequireContainerExec is a helper function that executes a command in a container
// It insures that there is no error during the execution
// Finally returns the output of its execution
func RequireContainerExec(ctx context.Context, t *testing.T, container Container, cmd []string) string {
t.Helper()

code, out, err := container.Exec(ctx, cmd)
require.NoError(t, err)
require.Zero(t, code)
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

checkBytes, err := io.ReadAll(out)
require.NoError(t, err)
return string(checkBytes)
}