2021-03-26 17:04:59 +00:00
|
|
|
#!/bin/bash
|
2020-12-07 12:55:49 +00:00
|
|
|
# Copyright The Lightning AI team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-01-05 10:01:59 +00:00
|
|
|
set -e
|
2022-09-01 22:13:12 +00:00
|
|
|
# THIS FILE ASSUMES IT IS RUN INSIDE THE tests/tests_<package> DIRECTORY
|
2021-03-26 17:04:59 +00:00
|
|
|
|
2022-07-27 12:36:22 +00:00
|
|
|
# Batch size for testing: Determines how many standalone test invocations run in parallel
|
2022-07-28 23:33:22 +00:00
|
|
|
# It can be set through the env variable PL_STANDALONE_TESTS_BATCH_SIZE and defaults to 6 if not set
|
|
|
|
test_batch_size="${PL_STANDALONE_TESTS_BATCH_SIZE:-6}"
|
2023-02-20 14:58:44 +00:00
|
|
|
source="${PL_STANDALONE_TESTS_SOURCE:-"lightning"}"
|
2024-11-19 08:50:26 +00:00
|
|
|
# this is the directory where the tests are located
|
|
|
|
test_dir=$1 # parse the first argument
|
|
|
|
COLLECTED_TESTS_FILE="collected_tests.txt"
|
|
|
|
|
|
|
|
ls -lh . # show the contents of the directory
|
2022-07-27 12:36:22 +00:00
|
|
|
|
2021-03-26 17:04:59 +00:00
|
|
|
# this environment variable allows special tests to run
|
2021-11-26 17:13:14 +00:00
|
|
|
export PL_RUN_STANDALONE_TESTS=1
|
2021-03-26 17:04:59 +00:00
|
|
|
# python arguments
|
2023-12-13 19:27:49 +00:00
|
|
|
defaults=" -m coverage run --source ${source} --append -m pytest --no-header -v -s --timeout 120 "
|
2023-02-20 14:58:44 +00:00
|
|
|
echo "Using defaults: ${defaults}"
|
2021-03-26 17:04:59 +00:00
|
|
|
|
2024-11-19 08:50:26 +00:00
|
|
|
# get the list of parametrizations. we need to call them separately. the last two lines are removed.
|
|
|
|
# note: if there's a syntax error, this will fail with some garbled output
|
|
|
|
python3 -um pytest $test_dir -q --collect-only --pythonwarnings ignore 2>&1 > $COLLECTED_TESTS_FILE
|
|
|
|
# early terminate if collection failed (e.g. syntax error)
|
|
|
|
if [[ $? != 0 ]]; then
|
|
|
|
cat $COLLECTED_TESTS_FILE
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-07-18 12:10:35 +00:00
|
|
|
|
2024-11-19 08:50:26 +00:00
|
|
|
# removes the last line of the file
|
|
|
|
sed -i '$d' $COLLECTED_TESTS_FILE
|
2023-12-21 02:22:25 +00:00
|
|
|
|
2024-11-19 08:50:26 +00:00
|
|
|
# Get test list and run each test individually
|
|
|
|
tests=($(grep -oP '\S+::test_\S+' "$COLLECTED_TESTS_FILE"))
|
|
|
|
test_count=${#tests[@]}
|
|
|
|
# present the collected tests
|
|
|
|
printf "collected $test_count tests:\n-------------------\n"
|
|
|
|
# replace space with new line
|
|
|
|
echo "${tests[@]}" | tr ' ' '\n'
|
|
|
|
printf "\n===================\n"
|
2021-03-26 17:04:59 +00:00
|
|
|
|
2024-11-19 08:50:26 +00:00
|
|
|
# if test count is one print warning
|
|
|
|
if [[ $test_count -eq 1 ]]; then
|
|
|
|
printf "WARNING: only one test found!\n"
|
|
|
|
elif [ $test_count -eq 0 ]; then
|
|
|
|
printf "ERROR: no tests found!\n"
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-12-21 02:22:25 +00:00
|
|
|
|
2024-11-19 08:50:26 +00:00
|
|
|
# clear all the collected reports
|
|
|
|
rm -f parallel_test_output-*.txt # in case it exists, remove it
|
2021-03-26 17:04:59 +00:00
|
|
|
|
2023-11-21 22:11:00 +00:00
|
|
|
|
2024-11-19 08:50:26 +00:00
|
|
|
status=0 # reset the script status
|
|
|
|
report="" # final report
|
|
|
|
pids=() # array of PID for running tests
|
|
|
|
test_ids=() # array of indexes of running tests
|
|
|
|
printf "Running $test_count tests in batches of $test_batch_size\n"
|
|
|
|
for i in "${!tests[@]}"; do
|
|
|
|
# remove initial "tests/" from the test name
|
|
|
|
test=${tests[$i]/tests\//}
|
|
|
|
printf "Running test $((i+1))/$test_count: $test\n"
|
2023-11-21 22:11:00 +00:00
|
|
|
|
2023-12-13 19:27:49 +00:00
|
|
|
# execute the test in the background
|
2024-11-19 08:50:26 +00:00
|
|
|
# redirect to a log file that buffers test output. since the tests will run in the background,
|
|
|
|
# we cannot let them output to std{out,err} because the outputs would be garbled together
|
|
|
|
python3 ${defaults} "$test" 2>&1 > "standalone_test_output-$i.txt" &
|
|
|
|
test_ids+=($i) # save the test's id in an array with running tests
|
|
|
|
pids+=($!) # save the PID in an array with running tests
|
2021-03-26 17:04:59 +00:00
|
|
|
|
2024-11-19 08:50:26 +00:00
|
|
|
# if we reached the batch size, wait for all tests to finish
|
|
|
|
if (( (($i + 1) % $test_batch_size == 0) || $i == $test_count-1 )); then
|
|
|
|
printf "Waiting for batch to finish: $(IFS=' '; echo "${pids[@]}")\n"
|
2022-07-18 12:10:35 +00:00
|
|
|
# wait for running tests
|
2024-11-19 08:50:26 +00:00
|
|
|
for j in "${!test_ids[@]}"; do
|
|
|
|
i=${test_ids[$j]} # restore the global test's id
|
|
|
|
pid=${pids[$j]} # restore the particular PID
|
|
|
|
test=${tests[$i]} # restore the test name
|
|
|
|
printf "Waiting for $tests >> standalone_test_output-$i.txt (PID: $pid)\n"
|
|
|
|
wait -n $pid
|
|
|
|
# get the exit status of the test
|
|
|
|
test_status=$?
|
|
|
|
# add row to the final report
|
|
|
|
report+="Ran\t$test\t>> exit:$test_status\n"
|
|
|
|
if [[ $test_status != 0 ]]; then
|
|
|
|
# show the output of the failed test
|
|
|
|
cat "standalone_test_output-$i.txt"
|
|
|
|
# Process exited with a non-zero exit status
|
|
|
|
status=$test_status
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
test_ids=() # reset the test's id array
|
|
|
|
pids=() # reset the PID array
|
2022-07-18 12:10:35 +00:00
|
|
|
fi
|
2021-03-26 17:04:59 +00:00
|
|
|
done
|
2021-07-14 11:25:36 +00:00
|
|
|
|
2021-03-26 17:04:59 +00:00
|
|
|
# echo test report
|
|
|
|
printf '=%.s' {1..80}
|
|
|
|
printf "\n$report"
|
|
|
|
printf '=%.s' {1..80}
|
|
|
|
printf '\n'
|
2024-11-19 08:50:26 +00:00
|
|
|
|
|
|
|
# exit with the worst test result
|
|
|
|
exit $status
|