Merge pull request #1201 from YunoHost/helpers-tests

Add quick-and-dirty test system for helpers
This commit is contained in:
Alexandre Aubin 2021-04-01 19:53:38 +02:00 committed by GitHub
commit d5ecdca896
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 169 additions and 2 deletions

View file

@ -53,6 +53,12 @@ root-tests:
script: script:
- python3 -m pytest tests - python3 -m pytest tests
test-helpers:
extends: .test-stage
script:
- cd tests
- bash test_helpers.sh
test-apps: test-apps:
extends: .test-stage extends: .test-stage
script: script:

View file

@ -217,11 +217,13 @@ ynh_setup_source () {
fi fi
# Apply patches # Apply patches
if (( $(find $YNH_APP_BASEDIR/sources/patches/ -type f -name "${source_id}-*.patch" 2> /dev/null | wc --lines) > "0" )) local patches_folder=$(realpath $YNH_APP_BASEDIR/sources/patches/)
if (( $(find $patches_folder -type f -name "${source_id}-*.patch" 2> /dev/null | wc --lines) > "0" ))
then then
(cd "$dest_dir" (cd "$dest_dir"
for p in $YNH_APP_BASEDIR/sources/patches/${source_id}-*.patch for p in $patches_folder/${source_id}-*.patch
do do
echo $p
patch --strip=1 < $p patch --strip=1 < $p
done) || ynh_die --message="Unable to apply patches" done) || ynh_die --message="Unable to apply patches"
fi fi

View file

@ -0,0 +1,81 @@
_make_dummy_src() {
echo "test coucou"
if [ ! -e $HTTPSERVER_DIR/dummy.tar.gz ]
then
pushd "$HTTPSERVER_DIR"
mkdir dummy
pushd dummy
echo "Lorem Ipsum" > index.html
echo '{"foo": "bar"}' > conf.json
mkdir assets
echo '.some.css { }' > assets/main.css
echo 'var some="js";' > assets/main.js
popd
tar -czf dummy.tar.gz dummy
popd
fi
echo "SOURCE_URL=http://127.0.0.1:$HTTPSERVER_PORT/dummy.tar.gz"
echo "SOURCE_SUM=$(sha256sum $HTTPSERVER_DIR/dummy.tar.gz | awk '{print $1}')"
}
ynhtest_setup_source_nominal() {
final_path="$(mktemp -d -p $VAR_WWW)"
_make_dummy_src > ../conf/dummy.src
ynh_setup_source --dest_dir="$final_path" --source_id="dummy"
test -e "$final_path"
test -e "$final_path/index.html"
}
ynhtest_setup_source_nominal_upgrade() {
final_path="$(mktemp -d -p $VAR_WWW)"
_make_dummy_src > ../conf/dummy.src
ynh_setup_source --dest_dir="$final_path" --source_id="dummy"
test "$(cat $final_path/index.html)" == "Lorem Ipsum"
# Except index.html to get overwritten during next ynh_setup_source
echo "IEditedYou!" > $final_path/index.html
test "$(cat $final_path/index.html)" == "IEditedYou!"
ynh_setup_source --dest_dir="$final_path" --source_id="dummy"
test "$(cat $final_path/index.html)" == "Lorem Ipsum"
}
ynhtest_setup_source_with_keep() {
final_path="$(mktemp -d -p $VAR_WWW)"
_make_dummy_src > ../conf/dummy.src
echo "IEditedYou!" > $final_path/index.html
echo "IEditedYou!" > $final_path/test.txt
ynh_setup_source --dest_dir="$final_path" --source_id="dummy" --keep="index.html test.txt"
test -e "$final_path"
test -e "$final_path/index.html"
test -e "$final_path/test.txt"
test "$(cat $final_path/index.html)" == "IEditedYou!"
test "$(cat $final_path/test.txt)" == "IEditedYou!"
}
ynhtest_setup_source_with_patch() {
final_path="$(mktemp -d -p $VAR_WWW)"
_make_dummy_src > ../conf/dummy.src
mkdir -p ../sources/patches
cat > ../sources/patches/dummy-index.html.patch << EOF
--- a/index.html
+++ b/index.html
@@ -1 +1,1 @@
-Lorem Ipsum
+Lorem Ipsum dolor sit amet
EOF
ynh_setup_source --dest_dir="$final_path" --source_id="dummy"
test "$(cat $final_path/index.html)" == "Lorem Ipsum dolor sit amet"
}

78
tests/test_helpers.sh Normal file
View file

@ -0,0 +1,78 @@
#!/bin/bash
readonly NORMAL=$(printf '\033[0m')
readonly BOLD=$(printf '\033[1m')
readonly RED=$(printf '\033[31m')
readonly GREEN=$(printf '\033[32m')
readonly ORANGE=$(printf '\033[33m')
function log_test()
{
echo -n "${BOLD}$1${NORMAL} ... "
}
function log_passed()
{
echo "${BOLD}${GREEN}✔ Passed${NORMAL}"
}
function log_failed()
{
echo "${BOLD}${RED}✘ Failed${NORMAL}"
}
function cleanup()
{
[ -n "$HTTPSERVER" ] && kill "$HTTPSERVER"
[ -d "$HTTPSERVER_DIR" ] && rm -rf "$HTTPSERVER_DIR"
[ -d "$VAR_WWW" ] && rm -rf "$VAR_WWW"
}
trap cleanup EXIT SIGINT
# =========================================================
# Dummy http server, to serve archives for ynh_setup_source
HTTPSERVER_DIR=$(mktemp -d)
HTTPSERVER_PORT=1312
pushd "$HTTPSERVER_DIR" >/dev/null
python -m SimpleHTTPServer $HTTPSERVER_PORT &>/dev/null &
HTTPSERVER="$!"
popd >/dev/null
VAR_WWW=$(mktemp -d)/var/www
mkdir -p $VAR_WWW
# =========================================================
source /usr/share/yunohost/helpers
for TEST_SUITE in $(ls test_helpers.d/*)
do
source $TEST_SUITE
done
# Hack to list all known function, keep only those starting by ynhtest_
TESTS=$(declare -F | grep ' ynhtest_' | awk '{print $3}')
global_result=0
for TEST in $TESTS
do
log_test $TEST
cd $(mktemp -d)
(app=ynhtest
YNH_APP_ID=$app
mkdir conf
mkdir scripts
cd scripts
set -eux
$TEST
) > ./test.log 2>&1
if [[ $? == 0 ]]
then
set +x; log_passed;
else
set +x; echo -e "\n----------"; cat ./test.log; echo -e "----------"; log_failed; global_result=1;
fi
done
exit $global_result