From c270f02fc123b7bfd7dab12a0c8e3eea62ca28b9 Mon Sep 17 00:00:00 2001 From: Shubhada Date: Fri, 20 Sep 2024 15:36:23 -0700 Subject: [PATCH] Integrate Docker for E2E testing with Makefile and Azure pipeline orchestration --- .pipelines/ci.yml | 86 ++++++++++++++++++- .../templates/e2e-pipeline-template.yml | 44 ++++++++++ Dockerfile.vpn | 11 +++ Makefile | 45 ++++++++++ docker-compose.yml | 64 ++++++++++++++ 5 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 .pipelines/templates/e2e-pipeline-template.yml create mode 100644 Dockerfile.vpn create mode 100644 docker-compose.yml diff --git a/.pipelines/ci.yml b/.pipelines/ci.yml index cc2fed8cdaa..70c9e28c4f8 100644 --- a/.pipelines/ci.yml +++ b/.pipelines/ci.yml @@ -1,4 +1,4 @@ -# Azure DevOps Pipeline running CI +# Azure DevOps Pipeline running CI and E2E trigger: branches: @@ -101,3 +101,87 @@ stages: acrFQDN: 'arosvcdev.azurecr.io' repository: 'aro' pushLatest: true + + - job: Lint_Az_ARO_Extension + pool: + name: 1es-aro-ci-pool + variables: + HOME: $(Agent.BuildDirectory) + steps: + - template: ./templates/template-checkout.yml + - script: | + set -xe + export AZDEV_CONFIG_DIR=$(Agent.BuildDirectory)/azdev-config + make test-python + [[ -z "$(git status -s)" ]] + target: python + + # New E2E Stage with Docker Compose + - stage: E2E + dependsOn: Containerized + jobs: + - job: Run_E2E_Tests + pool: + name: 1es-aro-ci-pool + steps: + # Checkout the code + - template: ./templates/template-checkout.yml + + # Install Docker Compose and pull the RP image + - template: ./templates/e2e-pipeline-template.yml + parameters: + rpImageACR: 'arosvcdev.azurecr.io' + acrCredentialsJSON: $(acr-credentials) + # Install OpenVPN (the command will depend on the agent OS) + - script: | + if [ -f /etc/debian_version ]; then + sudo apt-get update && sudo apt-get install -y openvpn + elif [ -f /etc/redhat-release ]; then + sudo yum install -y openvpn + elif [ -f /etc/alpine-release ]; then + sudo apk add --no-cache openvpn + fi + # Create the OpenVPN config directory and generate a basic vpn.conf file + - script: | + sudo mkdir -p /etc/openvpn + echo "client + remote vpn-server-address 1194 + proto udp + dev tun + resolv-retry infinite + nobind + persist-key + persist-tun + ca /etc/openvpn/ca.crt + cert /etc/openvpn/client.crt + key /etc/openvpn/client.key + comp-lzo + verb 3" | sudo tee /etc/openvpn/vpn.conf + displayName: Install OpenVPN + # Setup Azure and source secrets/env + - script: | + export RP_IMAGE_ACR=arosvcdev.azurecr.io + export VERSION=${BUILD_BUILDID} + export E2E_FLAGS="--flag1 --flag2" + export E2E_LABEL="test-label" + export E2E_DELETE_CLUSTER="false" + echo "RP_IMAGE_ACR=$RP_IMAGE_ACR" > .env + echo "VERSION=$VERSION" >> .env + echo "E2E_FLAGS=$E2E_FLAGS" >> .env + echo "E2E_LABEL=$E2E_LABEL" >> .env + + cat .env + sudo openvpn --config /etc/openvpn/vpn.conf && docker-compose --env-file .env -f docker-compose.yml up -d + displayName: Start OpenVPN and Run Docker Compose for E2E Services + + # Log the output from the e2e container in case of failure + - script: | + docker-compose logs e2e + displayName: Log E2E Test Output + condition: failed() + + # Clean up Docker Compose + - script: | + docker-compose down + displayName: Cleanup Docker Compose + condition: always() diff --git a/.pipelines/templates/e2e-pipeline-template.yml b/.pipelines/templates/e2e-pipeline-template.yml new file mode 100644 index 00000000000..c74062f01e5 --- /dev/null +++ b/.pipelines/templates/e2e-pipeline-template.yml @@ -0,0 +1,44 @@ +# ./templates/e2e-pipeline-template.yml +parameters: + - name: rpImageACR + type: string + - name: acrCredentialsJSON + type: string + +steps: + # Authenticate to ACR and Install Docker Compose + - task: AzureCLI@2 + displayName: 'Authenticate to ACR and Install Docker Compose' + inputs: + azureSubscription: 'ado-pipeline-dev-image-push' # Replace with your service connection + scriptType: bash + scriptLocation: 'inlineScript' + inlineScript: | + set -xe + # Ensure RP_IMAGE_ACR is correctly passed as a parameter + if [ -z "${{ parameters.rpImageACR }}" ]; then + echo "Error: RP_IMAGE_ACR is not set" + exit 1 + fi + + ACR_FQDN="${{ parameters.rpImageACR }}" + REGISTRY_NAME=$(echo $ACR_FQDN | cut -d'.' -f1) + + # Install Docker Compose + sudo apt-get update + sudo apt-get install -y docker-compose + + # Login to ACR + az acr login --name $REGISTRY_NAME + + # Pull the RP Docker image + - script: | + if [ -z "${{ parameters.rpImageACR }}" ]; then + echo "Error: RP_IMAGE_ACR is not set" + exit 1 + fi + + export RP_IMAGE_ACR=${{ parameters.rpImageACR }} + export VERSION=$(Build.BuildId) + docker pull ${RP_IMAGE_ACR}/aro:${VERSION} + displayName: Pull RP Docker Image diff --git a/Dockerfile.vpn b/Dockerfile.vpn new file mode 100644 index 00000000000..d57e3739e1c --- /dev/null +++ b/Dockerfile.vpn @@ -0,0 +1,11 @@ +# Use a Microsoft-approved image +FROM mcr.microsoft.com/azure-cli:2.61.0 AS base + +# Install OpenVPN +RUN apk add --no-cache openvpn || tdnf install -y openvpn || dnf install -y openvpn + +# Create the config directory and generate a basic vpn.conf file +RUN mkdir -p /etc/openvpn && echo "client\nremote vpn-server-address 1194\nproto udp\ndev tun\nresolv-retry infinite\nnobind\npersist-key\npersist-tun\nca ca.crt\ncert client.crt\nkey client.key\ncomp-lzo\nverb 3" > /etc/openvpn/vpn.conf + +# Run OpenVPN when the container starts +CMD ["openvpn", "--config", "/etc/openvpn/vpn.conf"] diff --git a/Makefile b/Makefile index 99c2cdeeda2..e3233f815a7 100644 --- a/Makefile +++ b/Makefile @@ -379,6 +379,7 @@ LOCAL_ARO_PORTAL_BUILD_IMAGE ?= $(LOCAL_ARO_RP_IMAGE)-portal-build LOCAL_ARO_RP_BUILD_IMAGE ?= $(LOCAL_ARO_RP_IMAGE)-build LOCAL_AZ_EXT_ARO_IMAGE ?= azext-aro LOCAL_TUNNEL_IMAGE ?= aro-tunnel +LOCAL_VPN_IMAGE ?= vpn_image ############################################################################### # Targets @@ -539,3 +540,47 @@ run-rp: ci-rp podman-secrets --secret proxy-client.crt,target=/app/secrets/proxy-client.crt \ --secret proxy.crt,target=/app/secrets/proxy.crt \ $(LOCAL_ARO_RP_IMAGE):$(VERSION) rp + +# Run selenium using Docker +.PHONY: run-selenium +run-selenium: + docker run -d --name selenium-container selenium/standalone-chrome + +# Run RP using Docker +.PHONY: run-rp-docker +run-rp: run-selenium + docker run -d --name rp-container $(ARO_IMAGE_BASE):$(VERSION) + +# Run E2E Tests using Docker +.PHONY: run-e2e +run-e2e: e2e.test + docker-compose run --rm e2e /usr/local/bin/e2e.test $(E2E_FLAGS) --ginkgo.label-filter=$(E2E_LABEL) + +# Clean up containers after E2E tests +.PHONY: e2e-cluster-clean +e2e-cluster-clean: + docker stop selenium-container rp-container e2e-container || true + docker rm selenium-container rp-container e2e-container || true + +# Build the VPN Docker image +.PHONY: build-vpn +build-vpn: + @echo "Building VPN image with VERSION: $(VERSION)" + docker build . $(DOCKER_BUILD_CI_ARGS) \ + -f Dockerfile.vpn \ + -t $(LOCAL_VPN_IMAGE):$(VERSION) + +# Push the VPN image to ACR +.PHONY: push-vpn +push-vpn: build-vpn + @echo "Pushing VPN image to ACR: $(RP_IMAGE_ACR)" + @echo "VERSION is: $(VERSION)" + if [ -z "$(RP_IMAGE_ACR)" ]; then \ + echo "Error: RP_IMAGE_ACR is not set"; \ + exit 1; \ + fi + # Tag the VPN image with the ACR registry and version + docker tag $(LOCAL_VPN_IMAGE):$(VERSION) $(RP_IMAGE_ACR)/vpn_image:$(VERSION) + # Push the VPN image to ACR + docker push $(RP_IMAGE_ACR)/vpn_image:$(VERSION) + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000000..497335d16a2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,64 @@ +version: '3.8' + +services: + vpn: + image: ${RP_IMAGE_ACR}/vpn_image:${VERSION} + container_name: vpn-container + privileged: true + network_mode: host # Use host networking for OpenVPN to access the host network + command: "sudo openvpn --config /etc/openvpn/vpn.conf" # OpenVPN command + healthcheck: + test: ["CMD-SHELL", "ping -c 1 google.com || exit 1"] # Basic health check for VPN + interval: 30s + timeout: 10s + retries: 3 + + selenium: + image: selenium/standalone-chrome + container_name: selenium-container + network_mode: host # Use host networking for Selenium to communicate with other services + ports: + - "4444:4444" + healthcheck: + test: "curl -f http://localhost:4444 || exit 1" # Health check to ensure Selenium is running + interval: 30s + timeout: 10s + retries: 3 + + rp: + image: ${RP_IMAGE_ACR}/aro:${VERSION} + container_name: rp-container + network_mode: host # Use host networking to ensure RP can communicate + healthcheck: + test: "curl -f http://localhost:8443/healthz || exit 1" # Health check to ensure RP is running + interval: 30s + timeout: 10s + retries: 3 + environment: + - CI=true + - RESOURCEGROUP=${RESOURCEGROUP} + - CLUSTER=${CLUSTER} + + e2e: + image: ${RP_IMAGE_ACR}/aro:${VERSION} + container_name: e2e-container + network_mode: host # Use host networking to ensure E2E can reach services + depends_on: + rp: + condition: service_healthy # Wait until RP is healthy + environment: + - CI=true + - LOCATION=${LOCATION} + - CLUSTER=${CLUSTER} + - AZURE_TENANT_ID=${AZURE_TENANT_ID} + - AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID} + - AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET} + - AZURE_CLIENT_ID=${AZURE_CLIENT_ID} + - E2E_FLAGS=${E2E_FLAGS} + - E2E_LABEL=${E2E_LABEL} + entrypoint: ["/usr/local/bin/e2e.test"] # Override ENTRYPOINT to run e2e.test binary + command: ["${E2E_FLAGS}", "--ginkgo.label-filter=${E2E_LABEL}"] # Override CMD to pass arguments + +networks: + default: + external: true # Ensure host network is used