From 33be02c20a5d7f7c6827b5783e3ecc15fe348e25 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Tue, 29 Oct 2024 12:05:51 +0000 Subject: [PATCH] chore(rabbitmq): refactor assertEntity in tests Signed-off-by: Matthieu MOREL --- modules/rabbitmq/rabbitmq_test.go | 66 ++++++++----------------------- testing.go | 16 ++++++++ 2 files changed, 33 insertions(+), 49 deletions(-) diff --git a/modules/rabbitmq/rabbitmq_test.go b/modules/rabbitmq/rabbitmq_test.go index 45c0f5e2c1..098dc08a23 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,79 +211,48 @@ 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(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() - cmd := []string{"rabbitmqadmin", "list", listCommand} if vhostID > 0 { 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(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) + cmd := []string{"rabbitmq-plugins", "is_enabled", plugin} - check, err := io.ReadAll(out) - require.NoError(t, err) - - if !strings.Contains(string(check), plugin+" is enabled") { - return false - } + check := testcontainers.RequireContainerExec(t, container, cmd) + require.Contains(t, check, plugin+" is enabled") } - - return true } diff --git a/testing.go b/testing.go index 1670d7c059..3bba892f03 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(t *testing.T, container Container, cmd []string) string { + t.Helper() + + code, out, err := container.Exec(context.Background(), cmd) + require.NoError(t, err) + require.Zero(t, code) + + checkBytes, err := io.ReadAll(out) + require.NoError(t, err) + return string(checkBytes) +}