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

From lsrGetTests return full path to test playbooks #59

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions library/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ lsrGetRoleDir() {
lsrGetTests() {
local tests_path=$1
local test_playbooks_all test_playbooks
test_playbooks_all=$(find "$tests_path" -maxdepth 1 -type f -name "tests_*.yml" -printf '%f\n')
test_playbooks_all=$(find "$tests_path" -maxdepth 1 -type f -name "tests_*.yml")
if [ -n "$SYSTEM_ROLES_ONLY_TESTS" ]; then
for test_playbook in $test_playbooks_all; do
if echo "$SYSTEM_ROLES_ONLY_TESTS" | grep -q "$test_playbook"; then
playbook_basename=$(basename "$test_playbook")
if echo "$SYSTEM_ROLES_ONLY_TESTS" | grep -q "$playbook_basename"; then
test_playbooks="$test_playbooks $test_playbook"
fi
done
Expand All @@ -91,7 +92,8 @@ lsrGetTests() {
if [ -n "$SYSTEM_ROLES_EXCLUDE_TESTS" ]; then
test_playbooks_excludes=""
for test_playbook in $test_playbooks; do
if ! echo "$SYSTEM_ROLES_EXCLUDE_TESTS" | grep -q "$test_playbook"; then
playbook_basename=$(basename "$test_playbook")
if ! echo "$SYSTEM_ROLES_EXCLUDE_TESTS" | grep -q "$playbook_basename"; then
test_playbooks_excludes="$test_playbooks_excludes $test_playbook"
fi
done
Expand Down Expand Up @@ -387,20 +389,19 @@ lsrArrtoStr() {
}

lsrRunPlaybook() {
local tests_path=$1
local test_playbook=$2
local inventory=$3
local skip_tags=$4
local limit=$5
local LOGFILE=$6
local role_name=$7
local test_playbook=$1
local inventory=$2
local skip_tags=$3
local limit=$4
local LOGFILE=$5
local result=FAIL
local cmd log_msg
local ans_debug=""
local role_name
role_name=$(lsrGetRoleName "$test_playbook")
if [ "${GET_PYTHON_MODULES:-}" = true ]; then
ans_debug="ANSIBLE_DEBUG=true"
ANSIBLE_ENVS[ANSIBLE_DEBUG]=true
fi
cmd="$(lsrArrtoStr ANSIBLE_ENVS) $ans_debug ansible-playbook -i $inventory $skip_tags $limit $tests_path$test_playbook -vv"
cmd="$(lsrArrtoStr ANSIBLE_ENVS) ansible-playbook -i $inventory $skip_tags $limit $test_playbook -vv"
log_msg="Test $test_playbook with ANSIBLE-$ANSIBLE_VER on ${limit/--limit /}"
# If LSR_TFT_DEBUG is true, print output to terminal
if [ "$LSR_TFT_DEBUG" == true ] || [ "$LSR_TFT_DEBUG" == True ]; then
Expand All @@ -421,25 +422,43 @@ lsrRunPlaybook() {
fi
}

lsrGetRoleName() {
local test_playbook=$1
local parent_dir_abs parent_dir role_dir_abs
parent_dir_abs=$(dirname "$test_playbook")
parent_dir="${parent_dir_abs##*/}"
if [ "$parent_dir" = tests ]; then # legacy role format
role_dir_abs=$(dirname "$parent_dir_abs")
echo "${role_dir_abs##*.}"
else # collection format
echo "$parent_dir"
fi
}

lsrRunPlaybooksParallel() {
# Run playbooks on managed nodes one by one
# Supports running against a single node too
local tests_path=$1
local inventory=$2
local skip_tags=$3
local test_playbooks=$4
local managed_nodes=$5
local role_name=$6
local test_playbooks_arr
local inventory=$1
local skip_tags=$2
local test_playbooks=$3
local managed_nodes=$4
local rolename_in_logfile
local role_name test_playbooks_arr

read -ra test_playbooks_arr <<< "$test_playbooks"
while [ "${#test_playbooks_arr[*]}" -gt 0 ]; do
for managed_node in $managed_nodes; do
if ! pgrep -af "ansible-playbook" | grep -q "\--limit $managed_node\s"; then
test_playbook=${test_playbooks_arr[0]}
test_playbooks_arr=("${test_playbooks_arr[@]:1}") # Remove first element from array
LOGFILE="${test_playbook%.*}"-ANSIBLE-"$ANSIBLE_VER"-$tmt_plan
lsrRunPlaybook "$tests_path" "$test_playbook" "$inventory" "$skip_tags" "--limit $managed_node" "$LOGFILE" "$role_name" &
playbook_basename=$(basename "$test_playbook")
if [ "$rolename_in_logfile" == true ] || [ "$rolename_in_logfile" == True ]; then
role_name=$(lsrGetRoleName "$test_playbook")
LOGFILE="$role_name"-"${playbook_basename%.*}"-ANSIBLE-"$ANSIBLE_VER"-$tmt_plan
else
LOGFILE="${playbook_basename%.*}"-ANSIBLE-"$ANSIBLE_VER"-$tmt_plan
fi
lsrRunPlaybook "$test_playbook" "$inventory" "$skip_tags" "--limit $managed_node" "$LOGFILE" &
sleep 1
break
fi
Expand Down Expand Up @@ -576,13 +595,12 @@ lsrMssqlHaUpdateInventory() {
# prepare test playbooks for gathering information about python
# module usage
lsrSetupGetPythonModules() {
local tests_dir test_pb
tests_dir="$1"; shift
for test_pb in "$@"; do
cp "$tests_dir$test_pb" "$tests_dir$test_pb.orig"
local test_pbs=$1
for test_pb in $test_pbs; do
cp "$test_pb" "$test_pb.orig"
sed -e '/^ hosts:/a\
environment:\
PYTHONVERBOSE: "1"' -i "$tests_dir$test_pb"
PYTHONVERBOSE: "1"' -i "$test_pb"
done
}

Expand Down
15 changes: 7 additions & 8 deletions tests/general/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,12 @@ rlJournalStart
lsrGetRoleDir "$REPO_NAME"
# role_path is defined in lsrGetRoleDir
# shellcheck disable=SC2154
lsrGenerateTestDisks "$role_path"/tests
test_playbooks=$(lsrGetTests "$role_path"/tests)
legacy_test_path="$role_path"/tests
lsrGenerateTestDisks "$legacy_test_path"
test_playbooks=$(lsrGetTests "$legacy_test_path")
rlLogInfo "Test playbooks: $test_playbooks"
if [ -z "$test_playbooks" ]; then
rlDie "No test playbooks found"
fi
for test_playbook in $test_playbooks; do
lsrHandleVault "$role_path/tests/$test_playbook"
lsrHandleVault "$test_playbook"
done
lsrSetAnsibleGathering "$ANSIBLE_GATHERING"
lsrGetCollectionPath
Expand All @@ -95,13 +93,14 @@ rlJournalStart
inventory=$(lsrPrepareInventoryVars "$tmt_tree_provision" "$guests_yml")
rlRun "cat $inventory"
tests_path="$collection_path"/ansible_collections/fedora/linux_system_roles/tests/"$REPO_NAME"/
test_playbooks=$(lsrGetTests "$tests_path")
if [ "${GET_PYTHON_MODULES:-}" = true ]; then
# shellcheck disable=SC2086
lsrSetupGetPythonModules "$tests_path" $test_playbooks
lsrSetupGetPythonModules "$test_playbooks"
fi
rlPhaseEnd
rlPhaseStartTest
managed_nodes=$(lsrGetManagedNodes "$guests_yml")
lsrRunPlaybooksParallel "$tests_path" "$inventory" "$SKIP_TAGS" "$test_playbooks" "$managed_nodes"
lsrRunPlaybooksParallel "$inventory" "$SKIP_TAGS" "$test_playbooks" "$managed_nodes" "false"
rlPhaseEnd
rlJournalEnd
24 changes: 12 additions & 12 deletions tests/mssql_ha/mssql_ha.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ rlJournalStart
lsrGetRoleDir "$REPO_NAME"
# role_path is defined in lsrGetRoleDir
# shellcheck disable=SC2154
test_playbooks=$(lsrGetTests "$role_path"/tests)
legacy_test_path="$role_path"/tests
test_playbooks=$(lsrGetTests "$legacy_test_path")
rlLogInfo "Test playbooks: $test_playbooks"
if [ -z "$test_playbooks" ]; then
rlDie "No test playbooks found"
fi
for test_playbook in $test_playbooks; do
lsrHandleVault "$role_path/tests/$test_playbook"
lsrHandleVault "$test_playbook"
done
lsrSetAnsibleGathering "$ANSIBLE_GATHERING"
lsrGetCollectionPath
Expand Down Expand Up @@ -117,6 +115,7 @@ rlJournalStart
fi
# Replace mssql_ha_virtual_ip with our virtualip value
tests_path="$collection_path"/ansible_collections/fedora/linux_system_roles/tests/"$REPO_NAME"/
test_playbooks=$(lsrGetTests "$tests_path")
collection_role_path="$collection_path"/ansible_collections/fedora/linux_system_roles/roles/"$REPO_NAME"
collection_vars_path="$collection_role_path"/vars
sed -i "s/mssql_ha_virtual_ip: .*/mssql_ha_virtual_ip: $virtualip/g" "$tests_path"/tests_configure_ha_cluster_external.yml
Expand All @@ -132,17 +131,18 @@ rlJournalStart
fi
done
for test_playbook in $test_playbooks; do
test_playbook_basename=$(basename "$test_playbook")
for mssql_version in $supported_versions; do
# Replace mssql_version value to one of the supported versions
sed -i "s/mssql_version.*$/mssql_version: $mssql_version/g" "$tests_path/$test_playbook"
rlRun "grep '^ *mssql_version:' $tests_path/$test_playbook"
sed -i "s/mssql_version.*$/mssql_version: $mssql_version/g" "$test_playbook"
rlRun "grep '^ *mssql_version:' $test_playbook"
# tmt_plan is assigned at lsrPrepTestVars
# shellcheck disable=SC2154
LOGFILE="${test_playbook%.*}"-ANSIBLE-"$ANSIBLE_VER"-"$tmt_plan"-"$mssql_version"
if [ "$test_playbook" = "tests_configure_ha_cluster_external.yml" ]; then
lsrRunPlaybook "$tests_path" "$test_playbook" "$inventory_external" "$SKIP_TAGS" "" "$LOGFILE" "$REPO_NAME"
elif [ "$test_playbook" = "tests_configure_ha_cluster_read_scale.yml" ]; then
lsrRunPlaybook "$tests_path" "$test_playbook" "$inventory_read_scale" "$SKIP_TAGS" "" "$LOGFILE" "$REPO_NAME"
LOGFILE="${test_playbook_basename%.*}"-ANSIBLE-"$ANSIBLE_VER"-"$tmt_plan"-"$mssql_version"
if [ "$test_playbook_basename" = "tests_configure_ha_cluster_external.yml" ]; then
lsrRunPlaybook "$test_playbook" "$inventory_external" "$SKIP_TAGS" "" "$LOGFILE"
elif [ "$test_playbook_basename" = "tests_configure_ha_cluster_read_scale.yml" ]; then
lsrRunPlaybook "$test_playbook" "$inventory_read_scale" "$SKIP_TAGS" "" "$LOGFILE"
fi
done
done
Expand Down