From 04225e5c09501b89b110b3d707cfc415b0bb3de2 Mon Sep 17 00:00:00 2001 From: Deepak Punia Date: Thu, 14 Nov 2024 20:31:04 +0530 Subject: [PATCH] Automated Rancher Setup, Chart Installation, and E2E Testing Workflow - Adds a GitHub Actions workflow to automate Rancher RKE1 setup using docker, chart installation, and E2E tests. - Sets up Go environment and installs dependencies for testing. - Runs installation and E2E tests with results saved in XML and HTML formats. - Uploads test artifacts and performs a container cleanup after the job. --- .github/workflows/docker_run.yaml | 114 +++++++++++++++++++++++ README.md | 6 +- tests/helper/charts/ranchermonitoring.go | 11 ++- 3 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/docker_run.yaml diff --git a/.github/workflows/docker_run.yaml b/.github/workflows/docker_run.yaml new file mode 100644 index 0000000..ac8e63c --- /dev/null +++ b/.github/workflows/docker_run.yaml @@ -0,0 +1,114 @@ +name: Rancher Setup, Chart Installation & E2E Tests + +on: + push: + workflow_dispatch: + schedule: + - cron: '* */8 * * *' + +jobs: + setup_rancher: + name: Setup Rancher - Tag - ${{ matrix.tag }} + runs-on: ubuntu-latest + strategy: + matrix: + tag: ['v2.8-head', 'v2.9-head', 'v2.10-head', 'head'] # Rancher version tags + fail-fast: false + + steps: + - name: Generate random password + id: password_gen + run: echo "PASSWORD=$(openssl rand -base64 16)" >> $GITHUB_ENV + + - name: Start Rancher container + run: | + docker run -d -it --name rancher \ + -e "CATTLE_BOOTSTRAP_PASSWORD=${{ env.PASSWORD }}" --restart unless-stopped \ + -p 80:80 -p 443:443 -p 6443:6443 \ + --privileged "rancher/rancher:${{ matrix.tag }}" + + - name: Wait for Rancher to initialize + run: sleep 3m #Rancher's startup time + + - name: Generate Rancher API token + id: get_token + run: | + for i in {1..3}; do + LOGIN_RESPONSE=$(curl --silent -X POST -H 'Content-Type: application/json' -d '{"username":"admin","password":"'${{ env.PASSWORD }}'"}' https://localhost/v3-public/localProviders/local?action=login --insecure) + TOKEN=$(echo $LOGIN_RESPONSE | jq -r .token) + if [ -z "$NODE_STATUS" ]; then + echo "RANCHER_TOKEN=$TOKEN" >> $GITHUB_ENV + else + echo "Retrying in 20 seconds..." + sleep 20 + fi + done + + - name: Check Rancher node status + run: | + for i in {1..15}; do + NODE_STATUS=$(curl -k --silent -H "Authorization: Bearer ${{ env.RANCHER_TOKEN }}" https://localhost/v3/nodes | jq -r '.data[] | .state' | grep -v "active" || true) + if [ -z "$NODE_STATUS" ]; then + echo "All nodes are active and running!" + break + else + echo "Some nodes are not yet active. Retrying in 20 seconds..." + sleep 20 + fi + done + + - name: Rancher permanent token for e2e tests + id: get_permanent_token + run: | + PERMANENT_TOKEN_RESPONSE=$(curl --silent -X POST -H 'Content-Type: application/json' -H "Authorization: Bearer ${{ env.RANCHER_TOKEN }}" -d '{"type":"token","description":"e2e-tests"}' https://localhost/v3/token --insecure) + PERMANENT_TOKEN=$(echo $PERMANENT_TOKEN_RESPONSE | jq -r .token) + if [ "$PERMANENT_TOKEN" == "null" ] || [ -z "$PERMANENT_TOKEN" ]; then + echo "Failed to obtain permanent token. Exiting." + exit 1 + fi + echo "RANCHER_PERMANENT_TOKEN=$PERMANENT_TOKEN" >> $GITHUB_ENV + + - name: Setup Rancher Config File in Home Directory + run: | + cat << EOF > ~/cattle-config.yaml + rancher: + host: localhost + adminToken: ${{ env.RANCHER_PERMANENT_TOKEN }} + insecure: True + clusterName: local + cleanup: true + EOF + + - name: Export CATTLE_TEST_CONFIG environment variable + run: echo "CATTLE_TEST_CONFIG=$HOME/cattle-config.yaml" >> $GITHUB_ENV + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version-file: './go.mod' + + - name: Create artifacts directory + run: mkdir -p ~/artifacts + + - name: Installation Charts + run: | + TEST_LABEL_FILTER=installation go test -timeout 20m github.com/rancher/observability-e2e/tests/e2e -v -count=1 -ginkgo.v | tee ~/artifacts/test-output-installation-${{ matrix.tag }}.txt + + - name: Run E2E Tests + run: | + TEST_LABEL_FILTER=E2E go test -timeout 30m github.com/rancher/observability-e2e/tests/e2e -v -count=1 -ginkgo.v | tee ~/artifacts/test-output-e2e-${{ matrix.tag }}.txt + + - name: Upload artifacts + uses: actions/upload-artifact@v3 + with: + name: test-artifacts + path: ~/artifacts + + - name: Cleanup all containers + if: always() + run: | + echo "Cleaning up all containers..." + docker ps -q | xargs -r docker rm -f diff --git a/README.md b/README.md index e3c6d74..81a1647 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ -![Lint Code Status](https://github.com/rancher/observability-e2e/actions/workflows/verify-changes.yaml/badge.svg) - # Observability E2E Tests +[![Lint Code Status](https://github.com/rancher/observability-e2e/actions/workflows/verify-changes.yaml/badge.svg?branch=main)](https://github.com/rancher/observability-e2e/actions/workflows/verify-changes.yaml) + +[![E2E Docker Run Status](https://github.com/rancher/observability-e2e/actions/workflows/docker_run.yaml/badge.svg?branch=main)](https://github.com/rancher/observability-e2e/actions/workflows/docker_run.yaml) + This repository contains end-to-end (E2E) tests for monitoring installations in Rancher. Follow the steps below to set up and run the tests. ## Prerequisites diff --git a/tests/helper/charts/ranchermonitoring.go b/tests/helper/charts/ranchermonitoring.go index 6f3de4f..255acbd 100644 --- a/tests/helper/charts/ranchermonitoring.go +++ b/tests/helper/charts/ranchermonitoring.go @@ -30,6 +30,13 @@ func InstallRancherMonitoringChart(client *rancher.Client, installOptions *Insta return err } + // Ensure the server URL has a scheme + serverURL := serverSetting.Value + if !strings.HasPrefix(serverURL, "http://") && !strings.HasPrefix(serverURL, "https://") { + // Assume HTTPS if no scheme is present + serverURL = "https://" + serverURL + } + // Retrieve the default registry setting. registrySetting, err := client.Management.Setting.ByID(defaultRegistrySettingID) if err != nil { @@ -76,7 +83,7 @@ func InstallRancherMonitoringChart(client *rancher.Client, installOptions *Insta installOptions.Version, installOptions.Cluster.ID, installOptions.Cluster.Name, - serverSetting.Value, + serverURL, rancherChartsName, installOptions.ProjectID, registrySetting.Value, @@ -87,7 +94,7 @@ func InstallRancherMonitoringChart(client *rancher.Client, installOptions *Insta installOptions.Version, installOptions.Cluster.ID, installOptions.Cluster.Name, - serverSetting.Value, + serverURL, rancherChartsName, installOptions.ProjectID, registrySetting.Value,