From 2ee47e448f63c2f9748a05ea5cab5d034b2c02c3 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 30 Oct 2024 12:56:54 +0100 Subject: [PATCH] chore(rabbitmq): refactor assertEntity in tests (#2856) Signed-off-by: Matthieu MOREL --- modules/rabbitmq/rabbitmq_test.go | 60 +++++++++---------------------- testing.go | 16 +++++++++ 2 files changed, 33 insertions(+), 43 deletions(-) diff --git a/modules/rabbitmq/rabbitmq_test.go b/modules/rabbitmq/rabbitmq_test.go index 45c0f5e2c1..1e05b8f3aa 100644 --- a/modules/rabbitmq/rabbitmq_test.go +++ b/modules/rabbitmq/rabbitmq_test.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io" "os" "strings" "testing" @@ -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() @@ -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 } diff --git a/testing.go b/testing.go index 1670d7c059..35ce4f0a39 100644 --- a/testing.go +++ b/testing.go @@ -3,6 +3,7 @@ package testcontainers import ( "context" "fmt" + "io" "regexp" "testing" @@ -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) + + checkBytes, err := io.ReadAll(out) + require.NoError(t, err) + return string(checkBytes) +}