From 3f600a21a247d612da211f7a62fca7115e1ffff3 Mon Sep 17 00:00:00 2001
From: Neil-Jocelyn Schark <neil.schark@h-da.de>
Date: Thu, 7 Mar 2024 07:38:06 +0000
Subject: [PATCH] Resolve "Investigate GitLab-CI services don't reach each
 other"

See merge request danet/gosdn!773
---
 .gitlab-ci.yml                                |  1 +
 .gitlab/ci/.test.yml                          | 22 +++++++------------
 .gitlab/ci/gosdn-integration.Dockerfile       |  4 ----
 controller/eventService/Service.go            | 20 +++++++++++++----
 .../application_tests/application_test.go     | 10 +++++++--
 5 files changed, 33 insertions(+), 24 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index a3efdf1c9..4c3aaaf8b 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -7,6 +7,7 @@ variables:
     GOLANG_VERSION: "1.22"
     GNMI_TARGET_IMAGE: registry.code.fbi.h-da.de/danet/gnmi-target/debian:master
     GNMI_TARGET_INTEGRATION_IMAGE: "$CI_REGISTRY_IMAGE/gnmi-target-integration-test:${CI_COMMIT_SHA}"
+    DOCKER_TLS_CERTDIR: "/certs"
 
 workflow:
   rules:
diff --git a/.gitlab/ci/.test.yml b/.gitlab/ci/.test.yml
index 6fe9f251d..20cd508b9 100644
--- a/.gitlab/ci/.test.yml
+++ b/.gitlab/ci/.test.yml
@@ -11,7 +11,8 @@ integration-test-gosdn:
         MONGO_INITDB_ROOT_PASSWORD: example
         INTEGRATION_TEST_TARGET_A: gnmi-target_A:7030
         INTEGRATION_TEST_TARGET_B: gnmi-target_B:7030
-        RABBITMQ_HOSTNAME: rabbitmq
+        INTEGRATION_TEST_RABBITMQ_HOSTNAME: rabbitmq
+        INTEGRATION_TEST_CONTROLLER_URL: gosdn:55055
     services:
       - name: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/mongo:7
         alias: mongo
@@ -28,20 +29,13 @@ integration-test-gosdn:
         alias: gnmi-target_B
         command: ["start", "--cert", "/etc/gnmi-target/ssl/certs/gnmi-target-selfsigned.crt", "--key", "/etc/gnmi-target/ssl/private/gnmi-target-selfsigned.key", "--ca_file", "/etc/gnmi-target/ssl/ca.crt"]
       # Use gosdn docker image from this branch
-      # Uncomment later when fixed. See: https://code.fbi.h-da.de/danet/gosdn/-/issues/335
-      #- name: "${CI_REGISTRY_IMAGE}:${DOCKER_TAG}_integration-test"
-      #  alias: gosdn
-      #  variables:
-      #    GOSDN_ADMIN_PASSWORD: TestPassword
-      #  command: ["--config", "/app/configs/integration-test-gosdn.toml", "--security", "secure"]
+      - name: "${CI_REGISTRY_IMAGE}:${DOCKER_TAG}_integration-test"
+        alias: gosdn
+        variables:
+          GOSDN_ADMIN_PASSWORD: TestPassword
+        command: ["--config", "/app/configs/integration-test-gosdn.toml", "--security", "secure"]
     before_script:
         - bash -c "$(curl -sL https://get-gnmic.openconfig.net)"
     script:
-        # Remove start of goSDN later when fixed. See: https://code.fbi.h-da.de/danet/gosdn/-/issues/335
-        - cp -r artifacts/ssl/gosdn/certs artifacts/ssl/
-        - cp -r artifacts/ssl/gosdn/private artifacts/ssl/
-        - mkdir artifacts/configs && cp controller/configs/gNMISubscriptions.txt.example artifacts/configs/gNMISubscriptions.txt
-        - make build-gosdn
-        - cd artifacts && GOSDN_ADMIN_PASSWORD=TestPassword ./gosdn --config ../controller/configs/integration-test-gosdn.toml --security secure &
-        - INTEGRATION_TEST_CONTROLLER_URL="localhost:55055" go test -p 1 ./integration-tests/*
+        - go test -p 1 ./integration-tests/*
     <<: *test
diff --git a/.gitlab/ci/gosdn-integration.Dockerfile b/.gitlab/ci/gosdn-integration.Dockerfile
index 7629a5218..51e3a406c 100644
--- a/.gitlab/ci/gosdn-integration.Dockerfile
+++ b/.gitlab/ci/gosdn-integration.Dockerfile
@@ -4,7 +4,3 @@ FROM ${GOSDN_IMAGE}
 
 RUN mkdir /app/ssl
 COPY ./artifacts/ssl/gosdn /app/ssl
-
-#COPY .gitlab/ci/test-start.sh test-start.sh
-#RUN chmod +x test-start.sh
-#ENTRYPOINT ["./test-start.sh"]
diff --git a/controller/eventService/Service.go b/controller/eventService/Service.go
index 7c822fc8a..330cb195b 100644
--- a/controller/eventService/Service.go
+++ b/controller/eventService/Service.go
@@ -13,6 +13,7 @@ import (
 
 	amqp "github.com/rabbitmq/amqp091-go"
 	"github.com/sethvargo/go-retry"
+	"github.com/sirupsen/logrus"
 	log "github.com/sirupsen/logrus"
 )
 
@@ -80,12 +81,23 @@ func NewEventService() (interfaces.Service, error) {
 }
 
 func connect(addr string) (*amqp.Connection, error) {
-	conn, err := amqp.Dial(addr)
-	if err != nil {
-		return nil, &customerrs.AMQPInitFailError{Action: "failed to connect to RabbitMQ", Err: err}
+	var err error
+	// Will equal two Minuten of retries
+	retries := 60
+
+	logrus.Infof("will try to connect to rabbitmq: %s", addr)
+
+	for i := 0; i < retries; i++ {
+		conn, err := amqp.Dial(addr)
+		if err == nil {
+			logrus.Info("Connected to RabbitMQ")
+			return conn, nil
+		}
+		logrus.Errorf("could not connect to RabbitMQ with error: %s. Retrying in 2 seconds.", err.Error())
+		time.Sleep(2 * time.Second)
 	}
 
-	return conn, nil
+	return nil, &customerrs.AMQPInitFailError{Action: "finally failed to connect to RabbitMQ", Err: err}
 }
 
 // Reconnect attempts to setup a new connection to the RabbitMQ server after an disconnect.
diff --git a/integration-tests/application_tests/application_test.go b/integration-tests/application_tests/application_test.go
index 721bd2cfa..926f4d68c 100644
--- a/integration-tests/application_tests/application_test.go
+++ b/integration-tests/application_tests/application_test.go
@@ -61,12 +61,18 @@ func TestMain(m *testing.M) {
 	topics := []event.Topic{event.ManagedNetworkElement, event.User, event.Role}
 
 	rabbitMQAddress := ""
-	envVarRabbitmq := os.Getenv("RABBITMQ_HOSTNAME")
+	envVarRabbitmq := os.Getenv("INTEGRATION_TEST_RABBITMQ_HOSTNAME")
 	if envVarRabbitmq != "" {
 		rabbitMQAddress = envVarRabbitmq
 	}
 
-	application = NewApplication(ctx, conn, ":55055", topics, rabbitMQAddress)
+	controllerUrl := "localhost:55055"
+	controllerEnv := os.Getenv("INTEGRATION_TEST_CONTROLLER_URL")
+	if controllerEnv != "" {
+		controllerUrl = controllerEnv
+	}
+
+	application = NewApplication(ctx, conn, controllerUrl, topics, rabbitMQAddress)
 
 	eventTypeCallbackTuples := []event.TypeToCallbackTuple{
 		{
-- 
GitLab