Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verification run scheduler #5647

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
56 changes: 45 additions & 11 deletions script/openqa-clone-custom-git-refspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Options:
-v, --verbose execute with verbose output
-h, -?, --help display this help
-n, --dry-run execute in dry-run mode, do not clone any openQA jobs
-m, --modules specify the tets modules you want to load as a comma-separated list of paths to your test files
foursixnine marked this conversation as resolved.
Show resolved Hide resolved
-c --clone-job-args pass additional parameters to 'openqa-clone-job', e.g. '--clone-job-args="--show-progress"'.
The default parameters are '$clone_args' defined in the env variable \$clone_args.

Expand All @@ -20,23 +21,21 @@ Examples:
openqa-clone-custom-git-refspec https://github.com/coolgw/os-autoinst-distri-opensuse/tree/nfs https://openqa.opensuse.org/tests/835060 DESKTOP=textmode
openqa-clone-custom-git-refspec -n -c '--show-progress' https://github.com/coolgw/os-autoinst-distri-opensuse/tree/nfs https://openqa.opensuse.org/tests/835060 DESKTOP=textmode
openqa-clone-custom-git-refspec https://github.com/foursixnine/os-autoinst-distri-opensuse/tree/oopsitsbrokenagain https://openqa.opensuse.org/tests/3128467 NEEDLES_DIR='https://github.com/foursixnine/os-autoinst-needles-opensuse.git#oopsitsbrokenagain'
openqa-clone-custom-git-refspec -m tests/console/rust_rustup,tests/console/rust_cargo https://github.com/os-autoinst-distri-opensuse/os-autoinst-distri-opensuse/pull/19080 https://openqa.opensuse.org/tests/835060

You need to set the environment variable GITHUB_TOKEN to use authenticated requests.
To use authenticated requests, you need to either provide a GitHub token via a configuration file at '~/.config/openqa/github-token.conf' or set
the GITHUB_TOKEN environment variable.

Example of the config file option:

GITHUB_ACCESS_TOKEN="you_token_here"

EOF
exit "$1"
}

set -o pipefail

if [[ -n "$GITHUB_TOKEN" ]]; then
AUTHENTICATED_REQUEST=" -u $GITHUB_TOKEN:x-oauth-basic"
echo "Github oauth token provided, performing authenticated requests"
fi

curl_github="${curl_github:-"curl${AUTHENTICATED_REQUEST}"}"
curl_openqa="${curl_openqa:-"curl"}"

fail() {
echo "$*" >&2
exit 1
Expand All @@ -55,8 +54,31 @@ extract_urls_from_pr() {
fi
}

opts=$(getopt -o vhnc: --long verbose,dry-run,help,clone-job-args: -n "$0" -- "$@") || usage 1
# Get the Github Token from a config file
# located at ~/.config/openqa/verification.conf
get_token_from_file() {
local config_file="$HOME/.config/openqa/github-token.conf"
if [[ -f $config_file ]]; then
token=$(grep "^GH_ACCESS_TOKEN=" "$config_file" | cut -d'd' -f2)
echo "$token"
fi
}

if [[ -n "$GITHUB_TOKEN" ]]; then
AUTHENTICATED_REQUEST=" -u $GITHUB_TOKEN:x-oauth-basic"
echo "Github oauth token provided, performing authenticated requests"
elif [[ -f "$HOME/.config/openqa/github-token.conf" ]]; then
AUTHENTICATED_REQUEST=" -u $(get_token_from_file):x-oauth-basic"
echo "Github oauth token found in config file, performing authenticated requests"
fi

curl_github="${curl_github:-"curl${AUTHENTICATED_REQUEST}"}"
curl_openqa="${curl_openqa:-"curl"}"


opts=$(getopt -o vhnm:c: --long verbose,dry-run,help,modules:,clone-job-args: -n "$0" -- "$@") || usage 1
eval set -- "$opts"
modules=""
while true; do
case "$1" in
-v | --verbose)
Expand All @@ -68,6 +90,10 @@ while true; do
dry_run=true
shift
;;
-m | --modules)
modules="$2"
shift 2
;;
-c | --clone-job-args)
clone_args="$2 $clone_args"
shift 2
Expand Down Expand Up @@ -97,8 +123,8 @@ if [[ -z "$repo_name" ]] || [[ -z "$pr" ]]; then
casedir="${casedir:-"${forked_repo_part}.git#${branch}"}"
build="${build:-"$repo_name#$branch"}"
fi

fi

if [[ -z "$branch" ]] || [[ -z "$repo_name" ]]; then
pr_url=${target_repo_part/github.com/api.github.com/repos}/pulls/$pr
pr_content=$(eval "${curl_github} -s $pr_url")
Expand Down Expand Up @@ -157,6 +183,7 @@ Please try 'curl $json_url' or select another job, e.g. in the same scenario: $h
local scriptdir
scriptdir=$(dirname "${BASH_SOURCE[0]}")
local cmd="$dry_run $scriptdir/openqa-clone-job $clone_args \"$host\" \"$job\" _GROUP=\"$GROUP\" TEST+=\"$test_suffix\" BUILD=\"$build\" CASEDIR=\"$casedir\" PRODUCTDIR=\"$productdir\" NEEDLES_DIR=\"$needles_dir\""
# FIXME: This does not work yet. Maybe I am checking the wrong list of arguments
[[ ${#args[@]} -ne 0 ]] && cmd=$cmd"$(printf " '%s'" "${args[@]}")"
if [[ -n "$MARKDOWN" ]]; then
eval "$cmd" | sed 's/^Created job.*: \([^ ]*\) -> \(.*\)$/* [\1](\2)/'
Expand All @@ -170,6 +197,13 @@ if [[ -z "$host" ]] && [[ -z "$job_list" ]]; then
exit 1
fi
args=("${@:3}")
if [[ $(echo "${args[*]}" | grep -q "SCHEDULE") ]] && [[ $(echo "${args[*]}" | grep -q "YAML_SCHEDULE") ]]; then
fail "Error: 'SCHEDULE' and 'YAML_SCHEDULE' are set in '--clone-job-args'. Cannot continue as they are mutually exclusive!"
fi
if [[ -n "$modules" ]] && ([[ $(echo "${args[*]}" | grep -q "SCHEDULE") ]] || [[ $(echo "${args[*]}" | grep -q "YAML_SCHEDULE") ]]); then
fail "Error: '--modules' flag cannot exist when passing 'SCHEDULE' or 'YAML_SCHEDULE' with '--clone-job-args'!"
fi

IFS=','
for i in $job_list; do
clone_job "$i"
Expand Down
121 changes: 121 additions & 0 deletions script/openqa-verify-pr
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/bin/bash
# Copyright 2024 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

# OpenQA verification run scheduler
#
# Usage:
# openqa-verify-pr $SOURCE $PR_ID $BASE_TEST_ID $[test/module, test/module,...] $TEST_NAME
#
# Parameters:
# SOURCE - The source you want to schedule a run for. Can be either 'opensuse' or 'suse'.
# PR_ID - The ID of your GitHub pull request.
# BASE_TEST_ID - The ID of the test you want you use as a base.
# [test/module,...] - The modules of your test you want to schedule for.
# TEST_NAME - The Name of your test. (E.g.: TEST=RUST)
#
# Specify your GitHub access token in '~/.config/openqa/verification.conf' like this:
#
# GH_ACCESS_TOKEN="YOUR_TOKEN_HERE"
#
# To specify the URL to your own openQA instance specify it in the same config file like this:
#
# CUSTOM_TARGET="https://openqa.yourdomain.com"
#
# Default test modules
# 'tests/installation/bootloader_start'
# 'tests/boot/boot_to_desktop'
#
# Options:
# -h, --help Display this help text.
#
# Example
#
# Pass arguments to this script like this:
#
# schedule_verification_run opensuse TOKEN 1234 12345 tests/console/rustup,tests/console/cargo RUST


help_text=$(cat <<'EOF'
OpenQA verification run scheduler

Usage:
openqa-verify-pr $SOURCE $PR_ID $BASE_TEST_ID $[test/module, test/module,...] $TEST_NAME

Parameters:
SOURCE - The source you want to schedule a run for. Can be either 'opensuse' or 'suse'.
PR_ID - The ID of your GitHub pull request.
BASE_TEST_ID - The ID of the test you want you use as a base.
[test/module,...] - The modules of your test you want to schedule for.
TEST_NAME - The Name of your test. (E.g.: TEST=RUST)

Specify your GitHub access token in '~/.config/openqa/verification.conf' like this:

GH_ACCESS_TOKEN="YOUR_TOKEN_HERE"

To specify the URL to your own openQA instance specify it in the same config file like this:

CUSTOM_TARGET="https://openqa.yourdomain.com"

Default test modules:
'tests/installation/bootloader_start'
'tests/boot/boot_to_desktop'

Options:
-h, --help Display this help text.

EOF
)

set -o pipefail

source=$1 pr_id=$2 test_id=$3 modules=$4 test_name=$5
config_file=$HOME/.config/openqa/verification.conf config= custom_target=

if [ $1 = '-h' ] || [ $1 = '--help' ]; then
echo "$help_text"
exit 0
fi

# Validate argument count
if [ $# -ne 5 ]; then
echo "Invalid number of arguments. Run with '-h' or '--help' to view usage."
exit 1
fi

# Read GH token from config file.
# TODO: Wrap into function. Call it in clone-custom-refspec when $TOKEN not set.
if [[ -f $config_file ]]; then
config=$(grep "^GH_ACCESS_TOKEN=" "$config_file" | cut -d'"' -f2)
custom_target=$(grep "^CUSTOM_TARGET=" "$config_file" | cut -d'"' -f2)
else
echo "No config file found! Run with '-h' for more info."
exit 1
fi

export GITHUB_TOKEN=$config

if [ $source = 'opensuse' ]; then
echo "Dispatching verification run for opensuse..."
openqa-clone-custom-git-refspec "https://github.com/os-autoinst/os-autoinst-distri-opensuse/pull/$pr_id" \
"https://openqa.opensuse.org/tests/$test_id" \
SCHEDULE="tests/installation/bootloader_start,tests/boot/boot_to_desktop,$modules" \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to move this to the openqa-clone-custom-git-refspec behind a switch i.e --modules a,b,c so it gets appended via the for loop, before calling cone job.

and then there's the consideration that if the caller is passing:

  • YAML_SCHEDULE + SCHEDULE, die (as one of them has to be overwritten.
  • if the switch is passed, and so is either (yaml_schedule or schedule) die, as options aren't compatible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, currently looking into that :)

YAML_SCHEDULE="" \
TEST="$test_name"
elif [ $source = 'suse' ]; then
echo "Dispatching verification run for suse..."
openqa-clonse-custom-git-refspec "https://github.com/os-autoinst-distri-opensuse/pull/$pr_id" \
"https://openqa.suse.de/tests/$test_id" \
SCHEDULE="tests/installation/bootloader_start,tests/boot/boot_to_desktop,$modules" \
TEST="$test_name"
elif [ $source = 'custom' ]; then
echo "Dispatching verification run for your custom target '$custom_target'..."
openqa-clone-custom-git-refspec "https://github.com/os-autoinst-distri-opensuse/pull/$pr_id" \
"$custom_target/tests/$test_id" \
SCHEDULE="tests/installation/bootloader_start,tests/boot/boot_to_desktop,$modules" \
TEST="$test_name"
else
# NOTE: Technically we could support custom targets via a config file. TBI.
echo "Unknown argument '$1'. Must be either 'suse', 'opensuse' or 'custom'."
exit 1
fi
Loading