From ee6f30b380f6965c478dd9820d54e4688cdbb534 Mon Sep 17 00:00:00 2001
From: Fabian Seidl <fabian.seidl@h-da.de>
Date: Thu, 11 Jan 2024 11:15:31 +0100
Subject: [PATCH] check ci tests with rabbit clearing

---
 .../application_tests/application_test.go     |  2 ++
 .../integrationTestUtils.go                   | 36 +++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/integration-tests/application_tests/application_test.go b/integration-tests/application_tests/application_test.go
index 020dd62fa..9a1c62a06 100644
--- a/integration-tests/application_tests/application_test.go
+++ b/integration-tests/application_tests/application_test.go
@@ -99,6 +99,8 @@ func TestMain(m *testing.M) {
 	if os.Getenv("RABBITMQ") == "" {
 		_ = <-application.eventChannel
 		_ = <-application.eventChannel
+	} else {
+		integration_test_utils.ClearRabbitMQQueues()
 	}
 
 	m.Run()
diff --git a/integration-tests/integrationTestUtils/integrationTestUtils.go b/integration-tests/integrationTestUtils/integrationTestUtils.go
index daf272f74..7ac5ccf3b 100644
--- a/integration-tests/integrationTestUtils/integrationTestUtils.go
+++ b/integration-tests/integrationTestUtils/integrationTestUtils.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"fmt"
 	"os"
+	"os/exec"
 	"time"
 
 	configMgmtPb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/configurationmanagement"
@@ -192,3 +193,38 @@ func cleanRolesExceptAdmin(resp *apb.GetRolesResponse, roleService apb.RoleServi
 
 	return nil
 }
+
+func ClearRabbitMQQueues() {
+	rabbitMQAddress := "localhost"
+	envVarRabbitmq := os.Getenv("RABBITMQ")
+	if envVarRabbitmq != "" {
+		rabbitMQAddress = envVarRabbitmq
+	}
+
+	// add more queue names if necessary
+	queues := []string{
+		"user",
+		"role",
+		"managedNetworkElement",
+	}
+
+	for _, queue := range queues {
+		args := []string{
+			"-i",
+			"-u", "guest:guest",
+			"-H", "\"content-type:application/json\"",
+			"-X", "POST",
+			"http://" + rabbitMQAddress + ":15672/api/queues/%2f/" + queue + "/get",
+			"-d", "{\"count\":10,\"encoding\":\"auto\",\"truncate\":50000,\"ackmode\":\"ack_requeue_false\"}",
+		}
+
+		cmd := exec.Command("curl", args...)
+		cmd.Stdout = os.Stdout
+		cmd.Stderr = os.Stderr
+
+		err := cmd.Run()
+		if err != nil {
+			logrus.Info(err)
+		}
+	}
+}
-- 
GitLab