From 4e4fa87826451e490dc1f35384b613a3f989d3e6 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Thu, 12 Dec 2024 11:42:44 -0800 Subject: [PATCH 1/2] Overhaul latest_commit scripts for E2E, improve logging and reduce GHA api calls Signed-off-by: Derek Nola --- tests/e2e/mixedos/Vagrantfile | 6 +++-- tests/e2e/scripts/latest_commit.ps1 | 13 +++++++-- tests/e2e/scripts/latest_commit.sh | 42 ++++++++++++++++++++--------- tests/e2e/scripts/run_tests.sh | 5 ++++ tests/e2e/vagrantdefaults.rb | 8 +++--- 5 files changed, 55 insertions(+), 19 deletions(-) diff --git a/tests/e2e/mixedos/Vagrantfile b/tests/e2e/mixedos/Vagrantfile index 67e58b1cfc..e0dc56295b 100644 --- a/tests/e2e/mixedos/Vagrantfile +++ b/tests/e2e/mixedos/Vagrantfile @@ -28,9 +28,11 @@ def provision(vm, role, role_num, node_num) vm.provision "shell", path: "../scripts/install-bgp.ps1" if RELEASE_VERSION == "skip" install_type = "-ArtifactPath 'C:\tmp'" - elsif !RELEASE_VERSION.empty? + elsif !RELEASE_VERSION.empty? && RELEASE_VERSION.start_with?("v1") install_type = "-Version #{RELEASE_VERSION}" - else + elsif !RELEASE_VERSION.empty? + install_type = "-Commit #{RELEASE_VERSION}" + else vm.provision "shell", path: "../scripts/latest_commit.ps1", args: [GITHUB_BRANCH, "./rke2_commits.txt"] install_type = "-Commit (Get-Content -TotalCount 1 ./rke2_commits.txt)" end diff --git a/tests/e2e/scripts/latest_commit.ps1 b/tests/e2e/scripts/latest_commit.ps1 index 67a9742b20..eab6607f60 100644 --- a/tests/e2e/scripts/latest_commit.ps1 +++ b/tests/e2e/scripts/latest_commit.ps1 @@ -1,7 +1,16 @@ # Grabs the last 5 commit SHA's from the given branch, then purges any commits that do not have a passing CI build param ($Branch, $CommitFile) -(Invoke-RestMethod "https://api.github.com/repos/rancher/rke2/commits?per_page=5&sha=$Branch").sha | ` -Out-File -FilePath $CommitFile +$response = (Invoke-RestMethod "https://api.github.com/repos/rancher/rke2/commits?per_page=5&sha=$Branch") +if ($response -is [System.Array]) { + $response.sha | Out-File -FilePath $CommitFile +} if ($response -is [PSCustomObject]) { + if ($response.message -like "API rate limit exceeded for *") { + Write-Host "Github API rate limit exceeded" + Exit 1 + } + Write-Host "Github API returned a non-expected response $($response.message)" + Exit 1 +} $StorageUrl = "https://rke2-ci-builds.s3.amazonaws.com/rke2-images.windows-amd64-" $TopCommit = (Get-Content -TotalCount 1 $CommitFile) diff --git a/tests/e2e/scripts/latest_commit.sh b/tests/e2e/scripts/latest_commit.sh index 429e1f89dd..5a514bceaa 100755 --- a/tests/e2e/scripts/latest_commit.sh +++ b/tests/e2e/scripts/latest_commit.sh @@ -1,7 +1,25 @@ #!/bin/bash -# Grabs the last 5 commit SHA's from the given branch, then purges any commits that do not have a passing CI build + +branch=$1 +output_file=$2 +# Grabs the last 10 commit SHA's from the given branch, then purges any commits that do not have a passing CI build iterations=0 -response=$(curl -s -H 'Accept: application/vnd.github.v3+json' "https://api.github.com/repos/rancher/rke2/commits?per_page=5&sha=$1") + +# The VMs take time on startup to hit aws, wait loop until we can +while ! curl -s --fail https://rke2-ci-builds.s3.amazonaws.com > /dev/null; do + ((iterations++)) + if [ "$iterations" -ge 30 ]; then + echo "Unable to hit https://rke2-ci-builds.s3.amazonaws.com" + exit 1 + fi + sleep 1 +done + +if [ -n "$GH_TOKEN" ]; then + response=$(curl -s -H "Authorization: token $GH_TOKEN" -H 'Accept: application/vnd.github.v3+json' "https://api.github.com/repos/rancher/rke2/commits?per_page=10&sha=$branch") +else + response=$(curl -s -H 'Accept: application/vnd.github.v3+json' "https://api.github.com/repos/rancher/rke2/commits?per_page=10&sha=$branch") +fi type=$(echo "$response" | jq -r type) # Verify if the response is an array with the rke2 commits @@ -14,17 +32,17 @@ if [[ $type == "object" ]]; then echo "Github API returned a non-expected response ${message}" exit 1 elif [[ $type == "array" ]]; then - echo ${response} | jq -r '.[] | .sha' &> "$2" + commits_str=$(echo "$response" | jq -j -r '.[] | .sha, " "') fi -curl -s --fail https://rke2-ci-builds.s3.amazonaws.com/rke2-images.linux-amd64-$(head -n 1 $2).tar.zst.sha256sum -while [ $? -ne 0 ]; do - ((iterations++)) - if [ "$iterations" -ge 6 ]; then - echo "No valid commits found" - exit 1 +read -a commits <<< "$commits_str" + +for commit in "${commits[@]}"; do + if curl -s --fail https://rke2-ci-builds.s3.amazonaws.com/rke2-images.linux-amd64-$commit.tar.zst.sha256sum > /dev/null; then + echo "$commit" > "$output_file" + exit 0 fi - sed -i 1d "$2" - sleep 1 - curl -s --fail https://rke2-ci-builds.s3.amazonaws.com/rke2-images.linux-amd64-$(head -n 1 $2).tar.zst.sha256sum done + +echo "Failed to find a valid commit, checked: " "${commits[@]}" +exit 1 diff --git a/tests/e2e/scripts/run_tests.sh b/tests/e2e/scripts/run_tests.sh index 72f4544e43..2ae96eb82f 100755 --- a/tests/e2e/scripts/run_tests.sh +++ b/tests/e2e/scripts/run_tests.sh @@ -21,6 +21,11 @@ cd rke2 git pull --rebase origin master /usr/local/go/bin/go mod tidy cd tests/e2e + +# To reduce GH API requsts, we grab the latest commit on the host and pass it to the tests +./scripts/latest_commit.sh master latest_commit.txt +E2E_RELEASE_VERSION=$(cat latest_commit.txt) && export E2E_RELEASE_VERSION + # create directory to store reports if it does not exists if [ ! -d createreport ] then diff --git a/tests/e2e/vagrantdefaults.rb b/tests/e2e/vagrantdefaults.rb index 1fe1e7b123..fcd5d95e8b 100644 --- a/tests/e2e/vagrantdefaults.rb +++ b/tests/e2e/vagrantdefaults.rb @@ -13,12 +13,14 @@ def defaultOSConfigure(vm) def getInstallType(vm, version, branch) if version == "skip" return "INSTALL_RKE2_ARTIFACT_PATH=/tmp" - elsif !version.empty? + elsif !version.empty? && version.start_with?("v1") return "INSTALL_RKE2_VERSION=#{version}" + elsif !version.empty? + return "INSTALL_RKE2_COMMIT=#{version}" end - # Grabs the last 5 commit SHA's from the given branch, then purges any commits that do not have a passing CI build + # Grabs the last 10 commit SHA's from the given branch, then purges any commits that do not have a passing CI build scripts_location = Dir.exist?("./scripts") ? "./scripts" : "../scripts" - vm.provision "shell", path: scripts_location + "/latest_commit.sh", args: [branch, "/tmp/rke2_commits"] + vm.provision "shell", path: scripts_location + "/latest_commit.sh", env: {GH_TOKEN:ENV['GH_TOKEN']}, args: [branch, "/tmp/rke2_commits"] return "INSTALL_RKE2_COMMIT=$(head\ -n\ 1\ /tmp/rke2_commits)" end From f567249a20cbb14a9841ff05323bf83fef24544b Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Thu, 12 Dec 2024 11:54:36 -0800 Subject: [PATCH 2/2] Address review comments, fix typo Signed-off-by: Derek Nola --- tests/e2e/scripts/latest_commit.sh | 2 +- tests/e2e/scripts/run_tests.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/e2e/scripts/latest_commit.sh b/tests/e2e/scripts/latest_commit.sh index 5a514bceaa..7b14f83f96 100755 --- a/tests/e2e/scripts/latest_commit.sh +++ b/tests/e2e/scripts/latest_commit.sh @@ -6,7 +6,7 @@ output_file=$2 iterations=0 # The VMs take time on startup to hit aws, wait loop until we can -while ! curl -s --fail https://rke2-ci-builds.s3.amazonaws.com > /dev/null; do +while ! curl -s --fail https://rke2-ci-builds.s3.amazonaws.com?max-keys=0 > /dev/null; do ((iterations++)) if [ "$iterations" -ge 30 ]; then echo "Unable to hit https://rke2-ci-builds.s3.amazonaws.com" diff --git a/tests/e2e/scripts/run_tests.sh b/tests/e2e/scripts/run_tests.sh index 2ae96eb82f..f9fc9d5308 100755 --- a/tests/e2e/scripts/run_tests.sh +++ b/tests/e2e/scripts/run_tests.sh @@ -22,7 +22,7 @@ git pull --rebase origin master /usr/local/go/bin/go mod tidy cd tests/e2e -# To reduce GH API requsts, we grab the latest commit on the host and pass it to the tests +# To reduce GH API requests, we grab the latest commit on the host and pass it to the tests ./scripts/latest_commit.sh master latest_commit.txt E2E_RELEASE_VERSION=$(cat latest_commit.txt) && export E2E_RELEASE_VERSION