From d1b5e837a8c631a75e70280b16692ca99aebda75 Mon Sep 17 00:00:00 2001 From: Shelly Miron Date: Mon, 28 Oct 2024 14:35:02 +0200 Subject: [PATCH] Add eco vcenter upstream ci capabilities (#538) SUMMARY This PR aims to insert self hosted runner to enable integration tests to run on the eco vcenter environment. This includes several adjustments to the code and additional action to be triggered for each PR, and run all the integration tests on that env with the PR's new code. Reviewed-by: mikemorency Reviewed-by: Anna Savina --- .github/eco-vcenter-ci.yml | 46 +++++++++++++++++++ Makefile | 36 +++++++++++++++ changelogs/fragments/538-add-eco-ci-job.yml | 2 + .../generate_integration_config.sh | 14 ++++++ tests/integration/integration_config.yml.tpl | 6 +++ tests/integration/targets/init-eco.sh | 14 ++++++ tests/integration/targets/runme.sh | 14 ++++++ tools/prepare_symlinks.yml | 20 ++++++++ 8 files changed, 152 insertions(+) create mode 100644 .github/eco-vcenter-ci.yml create mode 100644 Makefile create mode 100644 changelogs/fragments/538-add-eco-ci-job.yml create mode 100644 tests/integration/generate_integration_config.sh create mode 100644 tests/integration/integration_config.yml.tpl create mode 100644 tests/integration/targets/init-eco.sh create mode 100644 tests/integration/targets/runme.sh create mode 100644 tools/prepare_symlinks.yml diff --git a/.github/eco-vcenter-ci.yml b/.github/eco-vcenter-ci.yml new file mode 100644 index 000000000..55ea72531 --- /dev/null +++ b/.github/eco-vcenter-ci.yml @@ -0,0 +1,46 @@ +--- +name: Ansible Eco vCenter Integration Test +on: + pull_request_target: + types: [opened, synchronize] + push: + branches: + - main + - 'release-\d.\d' +permissions: + contents: read +jobs: + ansible_integration_test: + runs-on: ["self-hosted", linux, X64] + steps: + - name: Update pip, git + if: runner.os == 'Linux' && startsWith(runner.name, 'ubuntu') + run: | + set -euxo pipefail + sudo apt-get update + sudo apt-get install podman + + - name: Checkout repo + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + path: ansible_collections/vmware/vmware_rest + + - name: Generate integration config + working-directory: ansible_collections/vmware/vmware_rest/tests/integration + run: | + chmod +x generate_integration_config.sh + ./generate_integration_config.sh + env: + VCENTER_HOSTNAME: ${{ secrets.VCENTER_HOSTNAME }} + VCENTER_USERNAME: ${{ secrets.VCENTER_USERNAME }} + VCENTER_PASSWORD: ${{ secrets.VCENTER_PASSWORD }} + + - name: Run integration tests + run: | + python3 -m venv .venv + source .venv/bin/activate + make eco-vcenter-ci + working-directory: ansible_collections/vmware/vmware_rest + env: + ANSIBLE_COLLECTIONS_PATH: "${{ github.workspace }}" \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..106af1553 --- /dev/null +++ b/Makefile @@ -0,0 +1,36 @@ +# Define ANSI escape codes for colors +GREEN=\033[0;32m +RED=\033[0;31m +NC=\033[0m # No Color + +.PHONY: prepare_symlinks +prepare_symlinks: + ansible-playbook tools/prepare_symlinks.yml + +.PHONY: remove_aliases +remove_aliases: + @find tests/integration/targets/ -name "aliases" -exec rm -f {} + + +.PHONY: eco-vcenter-ci +eco-vcenter-ci: prepare_symlinks remove_aliases + @[ -f /tmp/vmware_rest_tests_report.txt ] && rm /tmp/vmware_rest_tests_report.txt || true; \ + @failed=0; \ + total=0; \ + echo "===============" >> /tmp/vmware_rest_tests_report.txt; \ + echo "Tests Summary" >> /tmp/vmware_rest_tests_report.txt; \ + echo "===============" >> /tmp/vmware_rest_tests_report.txt; \ + for dir in $(shell ansible-test integration --list-target --no-temp-workdir | grep 'vmware_rest_'); do \ + echo "Running integration test for $$dir"; \ + total=$$((total + 1)); \ + if ansible-test integration --no-temp-workdir $$dir; then \ + echo -e "Test: $$dir ${GREEN}Passed${NC}" | tee -a /tmp/vmware_rest_tests_report.txt; \ + else \ + echo -e "Test: $$dir ${RED}Failed${NC}" | tee -a /tmp/vmware_rest_tests_report.txt; \ + failed=$$((failed + 1)); \ + fi; \ + done; \ + echo "$$failed test(s) failed out of $$total." >> /tmp/vmware_rest_tests_report.txt; \ + cat /tmp/vmware_rest_tests_report.txt; \ + if [ $$failed -gt 0 ]; then \ + exit 1; \ + fi diff --git a/changelogs/fragments/538-add-eco-ci-job.yml b/changelogs/fragments/538-add-eco-ci-job.yml new file mode 100644 index 000000000..14b1fb5dc --- /dev/null +++ b/changelogs/fragments/538-add-eco-ci-job.yml @@ -0,0 +1,2 @@ +minor_changes: + - add a new ci job to the collection to run integration tests on bm vmware env \ No newline at end of file diff --git a/tests/integration/generate_integration_config.sh b/tests/integration/generate_integration_config.sh new file mode 100644 index 000000000..5605416ef --- /dev/null +++ b/tests/integration/generate_integration_config.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2155,SC2086 + +# Resolve the script's directory reliably +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" +cd "$SCRIPT_DIR" || exit 1 + +# Truncate the output file +truncate -s 0 integration_config.yml + +# Read the template and substitute environment variables +while read -r line; do + eval 'echo "'"$line"'"' >> integration_config.yml +done < "integration_config.yml.tpl" diff --git a/tests/integration/integration_config.yml.tpl b/tests/integration/integration_config.yml.tpl new file mode 100644 index 000000000..3e3c671dd --- /dev/null +++ b/tests/integration/integration_config.yml.tpl @@ -0,0 +1,6 @@ +--- +VMWARE_HOST: ${VCENTER_HOSTNAME} +VMWARE_USER: ${VCENTER_USERNAME} +VMWARE_PASSWORD: ${VCENTER_PASSWORD} +VMWARE_VALIDATE_CERTS=no +ansible_tags: eco-vcenter-ci diff --git a/tests/integration/targets/init-eco.sh b/tests/integration/targets/init-eco.sh new file mode 100644 index 000000000..a524e2606 --- /dev/null +++ b/tests/integration/targets/init-eco.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2155,SC2086 + +export DEFAULT_COLLECTIONS_PATH="$ANSIBLE_COLLECTIONS_PATH/ansible_collections" + +# Check if the variable is already set (e.g., in CI) +if [ -z "$ANSIBLE_COLLECTIONS_PATH" ]; then + # If not, use base collections path + ANSIBLE_COLLECTIONS_PATH="$DEFAULT_COLLECTIONS_PATH" +fi + +echo "ANSIBLE_COLLECTIONS_PATH: $ANSIBLE_COLLECTIONS_PATH" +BASE_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +export ANSIBLE_ROLES_PATH=${BASE_DIR} \ No newline at end of file diff --git a/tests/integration/targets/runme.sh b/tests/integration/targets/runme.sh new file mode 100644 index 000000000..ba0cf3fe4 --- /dev/null +++ b/tests/integration/targets/runme.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +source ../init-eco.sh + +# Generates a string starting with 'test-' followed by 4 random lowercase characters +TINY_PREFIX="test-$(uuidgen | tr -d '-' | cut -c1-4 | tr '[:upper:]' '[:lower:]')" + +# Extract the ansible_tags from integration_config.yml +ANSIBLE_TAGS=$(awk '/ansible_tags/ {print $2}' ../../integration_config.yml) + +# Check if the ANSIBLE_TAGS variable is set +if [[ -n "$ANSIBLE_TAGS" ]]; then + echo "ANSIBLE_TAGS is set to: $ANSIBLE_TAGS" + exec ansible-playbook playbook.yml --tags "$ANSIBLE_TAGS" --extra-vars "tiny_prefix=$TINY_PREFIX" +fi diff --git a/tools/prepare_symlinks.yml b/tools/prepare_symlinks.yml new file mode 100644 index 000000000..934a79cb2 --- /dev/null +++ b/tools/prepare_symlinks.yml @@ -0,0 +1,20 @@ +--- +- name: Create symlink to the runme.sh script in specific target directories + hosts: localhost + tasks: + - name: Find all target directories with specific prefix + ansible.builtin.find: + paths: ../tests/integration/targets + recurse: false + file_type: directory + register: target_dirs + + - name: Create symlink and set executable permission in each target directory with prefix 'vmware_rest' + ansible.builtin.file: + src: "{{ playbook_dir }}/../tests/integration/targets/runme.sh" + dest: "{{ item.path }}/runme.sh" + state: link + mode: '0755' + force: true + loop: "{{ target_dirs.files }}" + when: item.path | basename | regex_search('^vmware_rest')