From d9102e50617d5954657c5c497c15d2099f5f8aa1 Mon Sep 17 00:00:00 2001
From: Diego MENDEZ <Diego.MENDEZ@ext.ec.europa.eu>
Date: Mon, 28 Oct 2024 17:29:56 +0200
Subject: [PATCH] Adding resource group to CI.

---
 .gitlab-ci.yml   | 93 ++++--------------------------------------------
 .gitlab/test.yml | 91 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+), 86 deletions(-)
 create mode 100644 .gitlab/test.yml

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c65518f..1146c94 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -40,93 +40,14 @@ build-job:       # This job runs in the build stage, which runs first.
   script:
     - ansible-playbook ansible/build.yml
 
-.test-core: &test-core
+run-test:
   stage: test
-  tags:
-    - debian12_ovh
-  variables:
-    TEST_TAG: test
-    TEST_HOST: localhost
-    TEST_PORT: 8000
-
-up-test-env-job:
-  <<: *test-core
-  before_script:
-    # check that no other unrelated Netbox is up on destination port.
-    - if [[ $(curl -f -s http://${TEST_HOST}:${TEST_PORT}/metrics |wc -l) != 0 ]];
-        then echo "WARNING!! An unrelated Netbox instace has not been shut down properly. Try again, or execute down-test-env-job manually."; exit 1;
-      fi
-    # check that runner can do `docker-in-docker`.
-    - docker info
-    # Build image to test
-    - docker build --no-cache -t code.europa.eu:4567/digit-c4/netbox-plugins:${TEST_TAG} .
-  script:
-    # Netbox up `http://localhost:8000`
-    - env TAG=${TEST_TAG} PORT=${TEST_PORT} docker compose -f docker-compose.yml -f docker-compose.test.yml up -d
-    # wait for netbox to be up and running
-    - for ((i=1; i<20; i++)); do
-        if [[ $(curl -f -s http://${TEST_HOST}:${TEST_PORT}/metrics |wc -l) < 250 ]];
-          then echo "not yet up, sleep for 20 seconds.."; sleep 20;
-          else echo "Netbox up, we are able to reach metrics page. http://${TEST_HOST}:${TEST_PORT}/metrics."; break;
-        fi;
-        if [[ $i == 19 ]];
-          then echo "Netbox seems not able to start on ${TEST_HOST}:${TEST_PORT}."; exit 1;
-        fi;
-      done
-
-run-test-job:
-  <<: *test-core
-  before_script:
-    - python3 -m venv "$CI_PROJECT_DIR/plugins/venv"
-    - source "$CI_PROJECT_DIR/plugins/venv/bin/activate"
-    - pip install -r "$CI_PROJECT_DIR/tests/requirements.e2e.txt"
-  script:
-    - env HOST="${TEST_HOST}" PORT="${TEST_PORT}" API_KEY="$API_KEY" pytest -s --junit-xml=tests-report-netbox-dist-plugin.xml "$CI_PROJECT_DIR/tests/e2e/"
-  after_script:
-    - deactivate
-    - rm -rf "$CI_PROJECT_DIR/plugins/venv"
-  artifacts:
-    when: always
-    reports:
-      junit: tests-report-netbox-*-plugin.xml
-  needs:
-    - up-test-env-job
-
-run-robot-test-job:
-  <<: *test-core
-  before_script:
-    - python3 -m venv "$CI_PROJECT_DIR/plugins/venv"
-    - source "$CI_PROJECT_DIR/plugins/venv/bin/activate"
-    - docker build -t robotframework:latest ./tests/docker/
-  script:
-    - docker run --rm -v $CI_PROJECT_DIR:/project --network "host" robotframework:latest bash -c
-      "robot -v HOST:"${TEST_HOST}" -v PORT:"${TEST_PORT}" -v API_TOKEN:"$API_KEY" --outputdir /project/reports /project/tests/robot/"
-  after_script:
-    - sudo chmod -R 777 reports
-    - docker image rm robotframework:latest --force
-    - deactivate
-    - rm -rf "$CI_PROJECT_DIR/plugins/venv"
-  artifacts:
-    when: always
-    paths:
-      - reports/*
-  needs:
-    - up-test-env-job
-    - run-test-job
-
-down-test-env-job:
-  <<: *test-core
-  script:
-    - echo "Test Env Down"
-    - env TAG=${TEST_TAG} PORT=${TEST_PORT} docker compose -f docker-compose.yml -f docker-compose.test.yml down -v
-  after_script:
-    - docker image rm code.europa.eu:4567/digit-c4/netbox-plugins:${TEST_TAG} --force
-    - docker image ls
-  needs:
-    - up-test-env-job
-    - run-test-job
-    - run-robot-test-job
-  when: always
+  trigger:
+    include:
+      - local: '.gitlab/test.yml'
+    strategy: depend
+  # Create 'resource_group', to avoid collision when executing many pipelines at the same time.
+  resource_group: test-group
 
 deliver-latest-job:
   stage: deliver
diff --git a/.gitlab/test.yml b/.gitlab/test.yml
new file mode 100644
index 0000000..585eaed
--- /dev/null
+++ b/.gitlab/test.yml
@@ -0,0 +1,91 @@
+workflow:
+  rules:
+    - if: $CI_PIPELINE_SOURCE == "parent_pipeline"
+
+.test-core: &test-core
+  stage: test
+  tags:
+    - debian12_ovh
+  variables:
+    TEST_TAG: test
+    TEST_HOST: localhost
+    TEST_PORT: 8000
+
+up-test-env-job:
+  <<: *test-core
+  before_script:
+    # check that no other unrelated Netbox is up on destination port.
+    - if [[ $(curl -f -s http://${TEST_HOST}:${TEST_PORT}/metrics |wc -l) != 0 ]];
+        then echo "WARNING!! An unrelated Netbox instace has not been shut down properly. Try again, or execute down-test-env-job manually."; exit 1;
+      fi
+    # check that runner can do `docker-in-docker`.
+    - docker info
+    # Build image to test
+    - docker build --no-cache -t code.europa.eu:4567/digit-c4/netbox-plugins:${TEST_TAG} .
+  script:
+    # Netbox up `http://localhost:8000`
+    - env TAG=${TEST_TAG} PORT=${TEST_PORT} docker compose -f docker-compose.yml -f docker-compose.test.yml up -d
+    # wait for netbox to be up and running
+    - for ((i=1; i<20; i++)); do
+        if [[ $(curl -f -s http://${TEST_HOST}:${TEST_PORT}/metrics |wc -l) < 250 ]];
+          then echo "not yet up, sleep for 20 seconds.."; sleep 20;
+          else echo "Netbox up, we are able to reach metrics page. http://${TEST_HOST}:${TEST_PORT}/metrics."; break;
+        fi;
+        if [[ $i == 19 ]];
+          then echo "Netbox seems not able to start on ${TEST_HOST}:${TEST_PORT}."; exit 1;
+        fi;
+      done
+
+run-test-job:
+  <<: *test-core
+  before_script:
+    - python3 -m venv "$CI_PROJECT_DIR/plugins/venv"
+    - source "$CI_PROJECT_DIR/plugins/venv/bin/activate"
+    - pip install -r "$CI_PROJECT_DIR/tests/requirements.e2e.txt"
+  script:
+    - env HOST="${TEST_HOST}" PORT="${TEST_PORT}" API_KEY="$API_KEY" pytest -s --junit-xml=tests-report-netbox-dist-plugin.xml "$CI_PROJECT_DIR/tests/e2e/"
+  after_script:
+    - deactivate
+    - rm -rf "$CI_PROJECT_DIR/plugins/venv"
+  artifacts:
+    when: always
+    reports:
+      junit: tests-report-netbox-*-plugin.xml
+  needs:
+    - up-test-env-job
+
+run-robot-test-job:
+  <<: *test-core
+  before_script:
+    - python3 -m venv "$CI_PROJECT_DIR/plugins/venv"
+    - source "$CI_PROJECT_DIR/plugins/venv/bin/activate"
+    - docker build -t robotframework:latest ./tests/docker/
+  script:
+    - docker run --rm -v $CI_PROJECT_DIR:/project --network "host" robotframework:latest bash -c
+      "robot -v HOST:"${TEST_HOST}" -v PORT:"${TEST_PORT}" -v API_TOKEN:"$API_KEY" --outputdir /project/reports /project/tests/robot/"
+  after_script:
+    - sudo chmod -R 777 reports
+    - docker image rm robotframework:latest --force
+    - deactivate
+    - rm -rf "$CI_PROJECT_DIR/plugins/venv"
+  artifacts:
+    when: always
+    paths:
+      - reports/*
+  needs:
+    - up-test-env-job
+    - run-test-job
+
+down-test-env-job:
+  <<: *test-core
+  script:
+    - echo "Test Env Down"
+    - env TAG=${TEST_TAG} PORT=${TEST_PORT} docker compose -f docker-compose.yml -f docker-compose.test.yml down -v
+  after_script:
+    - docker image rm code.europa.eu:4567/digit-c4/netbox-plugins:${TEST_TAG} --force
+    - docker image ls
+  needs:
+    - up-test-env-job
+    - run-test-job
+    - run-robot-test-job
+  when: always
-- 
GitLab