Skip to content

Commit

Permalink
chore(rabbitmq): refactor assertEntity in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 committed Oct 29, 2024
1 parent 09a5482 commit 7bdd5dc
Showing 1 changed file with 26 additions and 46 deletions.
72 changes: 26 additions & 46 deletions modules/rabbitmq/rabbitmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,79 +212,59 @@ 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 requireContainerExec(t *testing.T, container testcontainers.Container, cmd []string) string {
t.Helper()

ctx := context.Background()

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

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

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

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

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

check := 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 := 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 := requireContainerExec(t, container, cmd)
require.Contains(t, check, plugin+" is enabled")
}

return true
}

0 comments on commit 7bdd5dc

Please sign in to comment.