Skip to content

Commit

Permalink
Merge pull request #230 from dosaboy/add-commit-to-func-tests
Browse files Browse the repository at this point in the history
Report results after charm func tests
  • Loading branch information
nicolasbock authored Sep 10, 2024
2 parents 0336a37 + 368682c commit 4f85c0f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 7 deletions.
59 changes: 53 additions & 6 deletions openstack/tools/charmed_openstack_functest_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ FUNC_TEST_PR=
FUNC_TEST_TARGET=
MODIFY_BUNDLE_CONSTRAINTS=true
SKIP_BUILD=false
WAIT_ON_DESTROY=true

usage () {
cat << EOF
Expand All @@ -32,6 +33,9 @@ OPTIONS:
--func-test-pr
Provides similar functionality to Func-Test-Pr in commit message. Set
to zaza-openstack-tests Pull Request ID.
--no-wait
By default we wait before destroying the model after a test run. This
flag can used to override that behaviour.
--skip-build
Skip building charm if already done to save time.
--skip-modify-bundle-constraints
Expand All @@ -57,6 +61,9 @@ do
FUNC_TEST_PR=$2
shift
;;
--no-wait)
WAIT_ON_DESTROY=false
;;
--skip-modify-bundle-constraints)
MODIFY_BUNDLE_CONSTRAINTS=false
;;
Expand All @@ -76,6 +83,15 @@ do
shift
done

TOOLS_PATH=$(realpath $(dirname $0))/func_test_tools
CHARM_PATH=$(pwd)

# Get commit we are running tests against.
COMMIT_ID=$(git -C $CHARM_PATH rev-parse --short HEAD)
CHARM_NAME=$(awk '/^name: .+/{print $2}' metadata.yaml)

echo "Running functional tests for charm $CHARM_NAME commit $COMMIT_ID"

source ~/novarc
export {,TEST_}CIDR_EXT=`openstack subnet show subnet_${OS_USERNAME}-psd-extra -c cidr -f value`
FIP_MAX=$(ipcalc $CIDR_EXT| awk '$1=="HostMax:" {print $2}')
Expand Down Expand Up @@ -135,10 +151,13 @@ if [[ -n $FUNC_TEST_PR ]]; then
)
fi

declare -A func_targets=()
if [[ -n $FUNC_TEST_TARGET ]]; then
func_targets=( $FUNC_TEST_TARGET )
func_targets[$FUNC_TEST_TARGET]=null
else
func_targets=( $(python3 $(realpath $(dirname $0))/identify_charm_func_tests.py) )
for target in $(python3 $TOOLS_PATH/identify_charm_func_tests.py); do
func_targets[target]=null
done
fi

if $MODIFY_BUNDLE_CONSTRAINTS; then
Expand All @@ -148,13 +167,41 @@ if $MODIFY_BUNDLE_CONSTRAINTS; then
)
fi

for target in ${func_targets[@]}; do
[[ -d src ]] && pushd src || true
tox -re func-target -- $target
for target in ${!func_targets[@]}; do
[[ -d src ]] && pushd src &>/dev/null || true
fail=false
tox -re func-target -- $target || fail=true

if $fail; then
func_targets[$target]='fail'
else
func_targets[$target]='success'
fi

read -p "Destroy model and run next test? [ENTER]"
if $WAIT_ON_DESTROY; then
read -p "Destroy model and run next test? [ENTER]"
fi
# cleanup before next run
model=`juju list-models| egrep -o "^zaza-\S+"|tr -d '*'`
juju destroy-model --no-prompt $model --force --no-wait --destroy-storage
done
popd &>/dev/null || true

# Report results
echo "Test results for charm $CHARM_NAME functional tests @ commit $COMMIT_ID:"
for target in ${!func_targets[@]}; do
if $(python3 $TOOLS_PATH/test_is_voting.py $target); then
voting_info=""
else
voting_info=" (non-voting)"
fi

if [[ ${func_targets[$target]} = null ]]; then
echo " * $target: SKIPPED$voting_info"
elif [[ ${func_targets[$target]} = success ]]; then
echo " * $target: SUCCESS$voting_info"
else
echo " * $target: FAILURE$voting_info"
fi
done

Empty file.
34 changes: 34 additions & 0 deletions openstack/tools/func_test_tools/test_is_voting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Takes a func test target name as input.
- Exit return code 0 == voting
- Exit return code 1 == non-voting
"""
import os
import sys

import yaml

if __name__ == "__main__":
target_name = sys.argv[1]
if not os.path.exists('osci.yaml'):
sys.stderr.write(f"ERROR: osci.yaml not found - assuming "
f"{target_name} is voting.\n")
sys.exit(0)

with open('osci.yaml', encoding='utf-8') as fd:
osci_config = yaml.safe_load(fd)

try:
jobs = osci_config[0]['project']['check']['jobs']
if target_name in jobs:
# default is voting=True
sys.exit(0)

for check in jobs:
if isinstance(check, dict) and target_name in check:
if not check[target_name]['voting']:
sys.exit(1)
except KeyError as exc:
sys.stderr.write(f"ERROR: failed to process osci.yaml - assuming "
f"{target_name} is voting (key {exc} not found).\n")
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import-order-style = pep8
[testenv]
basepython = {env:TOX_PYTHON:python3}
pyfiles =
{toxinidir}/openstack/tools/identify_charm_func_tests.py
{toxinidir}/openstack/tools/func_test_tools
{toxinidir}/tools/parse-bundle.py
{toxinidir}/tools/juju-bundle-applications.py

Expand Down

0 comments on commit 4f85c0f

Please sign in to comment.