helpers: yolo add tests for helpersv2.1

This commit is contained in:
Alexandre Aubin 2024-07-01 20:55:45 +02:00
parent 1ed56952e6
commit 92807afb16
19 changed files with 1119 additions and 5 deletions

View file

@ -57,14 +57,17 @@ test-actionmap:
changes:
- share/actionsmap.yml
test-helpers:
test-helpers2:
extends: .test-stage
script:
- cd tests
- bash test_helpers.sh
# only:
# changes:
# - helpers/*
test-helpers2.1:
extends: .test-stage
script:
- cd tests
- bash test_helpers.sh 2.1
test-domains:
extends: .test-stage

View file

@ -1,5 +1,7 @@
#!/bin/bash
VERSION=${1:-2}
readonly NORMAL=$(printf '\033[0m')
readonly BOLD=$(printf '\033[1m')
readonly RED=$(printf '\033[31m')
@ -47,7 +49,7 @@ getent passwd ynhtest &>/dev/null || useradd --system ynhtest
# =========================================================
for TEST_SUITE in $(ls test_helpers.d/*)
for TEST_SUITE in $(ls test_helpers.v$VERSION.d/*)
do
source $TEST_SUITE
done
@ -64,6 +66,7 @@ do
(mkdir conf
mkdir scripts
cd scripts
export YNH_HELPERS_VERSION=$VERSION
source /usr/share/yunohost/helpers
app=ynhtest
YNH_APP_ID=$app

View file

@ -0,0 +1,22 @@
ynhtest_apt_install_apt_deps_regular() {
dpkg --list | grep -q "ii *$app-ynh-deps" && apt remove $app-ynh-deps --assume-yes || true
dpkg --list | grep -q 'ii *nyancat' && apt remove nyancat --assume-yes || true
dpkg --list | grep -q 'ii *sl' && apt remove sl --assume-yes || true
! _ynh_apt_package_is_installed "$app-ynh-deps"
! _ynh_apt_package_is_installed "nyancat"
! _ynh_apt_package_is_installed "sl"
ynh_apt_install_dependencies "nyancat sl"
_ynh_apt_package_is_installed "$app-ynh-deps"
_ynh_apt_package_is_installed "nyancat"
_ynh_apt_package_is_installed "sl"
ynh_apt_remove_dependencies
! _ynh_apt_package_is_installed "$app-ynh-deps"
! _ynh_apt_package_is_installed "nyancat"
! _ynh_apt_package_is_installed "sl"
}

View file

@ -0,0 +1,92 @@
ynhtest_exec_warn_less() {
FOO='foo'
bar=""
BAR='$bar'
FOOBAR="foo bar"
# These looks like stupid edge case
# but in fact happens when dealing with passwords
# (which could also contain bash chars like [], {}, ...)
# or urls containing &, ...
FOOANDBAR="foo&bar"
FOO1QUOTEBAR="foo'bar"
FOO2QUOTEBAR="foo\"bar"
ynh_hide_warnings uptime
test ! -e $FOO
ynh_hide_warnings touch $FOO
test -e $FOO
rm $FOO
test ! -e $FOO1QUOTEBAR
ynh_hide_warnings touch $FOO1QUOTEBAR
test -e $FOO1QUOTEBAR
rm $FOO1QUOTEBAR
test ! -e $FOO2QUOTEBAR
ynh_hide_warnings touch $FOO2QUOTEBAR
test -e $FOO2QUOTEBAR
rm $FOO2QUOTEBAR
test ! -e $BAR
ynh_hide_warnings touch $BAR
test -e $BAR
rm $BAR
test ! -e "$FOOBAR"
ynh_hide_warnings touch "$FOOBAR"
test -e "$FOOBAR"
rm "$FOOBAR"
test ! -e "$FOOANDBAR"
ynh_hide_warnings touch $FOOANDBAR
test -e "$FOOANDBAR"
rm "$FOOANDBAR"
###########################
# Legacy stuff using eval #
###########################
test ! -e $FOO
ynh_hide_warnings "touch $FOO"
test -e $FOO
rm $FOO
test ! -e $FOO1QUOTEBAR
ynh_hide_warnings "touch \"$FOO1QUOTEBAR\""
# (this works but expliciy *double* quotes have to be provided)
test -e $FOO1QUOTEBAR
rm $FOO1QUOTEBAR
#test ! -e $FOO2QUOTEBAR
#ynh_hide_warnings "touch \'$FOO2QUOTEBAR\'"
## (this doesn't work with simple or double quotes)
#test -e $FOO2QUOTEBAR
#rm $FOO2QUOTEBAR
test ! -e $BAR
ynh_hide_warnings 'touch $BAR'
# That one works because $BAR is only interpreted during eval
test -e $BAR
rm $BAR
#test ! -e $BAR
#ynh_hide_warnings "touch $BAR"
# That one doesn't work because $bar gets interpreted as empty var by eval...
#test -e $BAR
#rm $BAR
test ! -e "$FOOBAR"
ynh_hide_warnings "touch \"$FOOBAR\""
# (works but requires explicit double quotes otherwise eval would interpret 'foo bar' as two separate args..)
test -e "$FOOBAR"
rm "$FOOBAR"
test ! -e "$FOOANDBAR"
ynh_hide_warnings "touch \"$FOOANDBAR\""
# (works but requires explicit double quotes otherwise eval would interpret '&' as a "run command in background" and also bar is not a valid command)
test -e "$FOOANDBAR"
rm "$FOOANDBAR"
}

View file

@ -0,0 +1,71 @@
ynhtest_acceptable_path_to_delete() {
mkdir -p /home/someuser
mkdir -p /home/$app
mkdir -p /home/yunohost.app/$app
mkdir -p /var/www/$app
touch /var/www/$app/bar
touch /etc/cron.d/$app
! _acceptable_path_to_delete /
! _acceptable_path_to_delete ////
! _acceptable_path_to_delete " //// "
! _acceptable_path_to_delete /var
! _acceptable_path_to_delete /var/www
! _acceptable_path_to_delete /var/cache
! _acceptable_path_to_delete /usr
! _acceptable_path_to_delete /usr/bin
! _acceptable_path_to_delete /home
! _acceptable_path_to_delete /home/yunohost.backup
! _acceptable_path_to_delete /home/yunohost.app
! _acceptable_path_to_delete /home/yunohost.app/
! _acceptable_path_to_delete ///home///yunohost.app///
! _acceptable_path_to_delete /home/yunohost.app/$app/..
! _acceptable_path_to_delete ///home///yunohost.app///$app///..//
! _acceptable_path_to_delete /home/yunohost.app/../$app/..
! _acceptable_path_to_delete /home/someuser
! _acceptable_path_to_delete /home/yunohost.app//../../$app
! _acceptable_path_to_delete " /home/yunohost.app/// "
! _acceptable_path_to_delete /etc/cron.d/
! _acceptable_path_to_delete /etc/yunohost/
_acceptable_path_to_delete /home/yunohost.app/$app
_acceptable_path_to_delete /home/yunohost.app/$app/bar
_acceptable_path_to_delete /etc/cron.d/$app
_acceptable_path_to_delete /var/www/$app/bar
_acceptable_path_to_delete /var/www/$app
rm /var/www/$app/bar
rm /etc/cron.d/$app
rmdir /home/yunohost.app/$app
rmdir /home/$app
rmdir /home/someuser
rmdir /var/www/$app
}
ynhtest_safe_rm() {
mkdir -p /home/someuser
mkdir -p /home/yunohost.app/$app
mkdir -p /var/www/$app
mkdir -p /var/whatever
touch /var/www/$app/bar
touch /etc/cron.d/$app
! ynh_safe_rm "/home/someuser"
! ynh_safe_rm "/home/yunohost.app/"
! ynh_safe_rm "/var/whatever"
ynh_safe_rm "/home/yunohost.app/$app"
ynh_safe_rm "/var/www/$app"
ynh_safe_rm "/etc/cron.d/$app"
test -e /home/someuser
test -e /home/yunohost.app
test -e /var/whatever
! test -e /home/yunohost.app/$app
! test -e /var/www/$app
! test -e /etc/cron.d/$app
rmdir /home/someuser
rmdir /var/whatever
}

View file

@ -0,0 +1,63 @@
ynhtest_settings() {
test -n "$app"
mkdir -p "/etc/yunohost/apps/$app"
echo "label: $app" > "/etc/yunohost/apps/$app/settings.yml"
test -z "$(ynh_app_setting_get --key="foo")"
test -z "$(ynh_app_setting_get --key="bar")"
test -z "$(ynh_app_setting_get --app="$app" --key="baz")"
ynh_app_setting_set --key="foo" --value="foovalue"
ynh_app_setting_set --app="$app" --key="bar" --value="barvalue"
ynh_app_setting_set "$app" baz bazvalue
test "$(ynh_app_setting_get --key="foo")" == "foovalue"
test "$(ynh_app_setting_get --key="bar")" == "barvalue"
test "$(ynh_app_setting_get --app="$app" --key="baz")" == "bazvalue"
ynh_app_setting_delete --key="foo"
ynh_app_setting_delete --app="$app" --key="bar"
ynh_app_setting_delete "$app" baz
test -z "$(ynh_app_setting_get --key="foo")"
test -z "$(ynh_app_setting_get --key="bar")"
test -z "$(ynh_app_setting_get --app="$app" --key="baz")"
rm -rf "/etc/yunohost/apps/$app"
}
ynhtest_setting_set_default() {
test -n "$app"
mkdir -p "/etc/yunohost/apps/$app"
echo "label: $app" > "/etc/yunohost/apps/$app/settings.yml"
test -z "$(ynh_app_setting_get --key="foo")"
test -z "${foo:-}"
ynh_app_setting_set_default --key="foo" --value="foovalue"
test "${foo:-}" == "foovalue"
test "$(ynh_app_setting_get --key="foo")" == "foovalue"
ynh_app_setting_set_default --key="foo" --value="bar"
test "${foo:-}" == "foovalue"
test "$(ynh_app_setting_get --key="foo")" == "foovalue"
ynh_app_setting_delete --key="foo"
test "${foo:-}" == "foovalue"
test -z "$(ynh_app_setting_get --key="foo")"
ynh_app_setting_set_default --key="foo" --value="bar"
# Hmmm debatable ? But that's how it works right now because the var still exists
test "${foo:-}" == "foovalue"
test -z "$(ynh_app_setting_get --key="foo")"
rm -rf "/etc/yunohost/apps/$app"
}

View file

@ -0,0 +1,111 @@
_make_dummy_manifest() {
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
cat << EOF
packaging_format = 2
id = "helloworld"
version = "0.1~ynh2"
[resources]
[resources.sources.dummy]
url = "http://127.0.0.1:$HTTPSERVER_PORT/dummy.tar.gz"
sha256 = "$(sha256sum $HTTPSERVER_DIR/dummy.tar.gz | awk '{print $1}')"
[resources.install_dir]
group = "www-data:r-x"
EOF
}
ynhtest_setup_source_nominal() {
install_dir="$(mktemp -d -p $VAR_WWW)"
_make_dummy_manifest > ../manifest.toml
ynh_setup_source --dest_dir="$install_dir" --source_id="dummy"
test -e "$install_dir"
test -e "$install_dir/index.html"
ls -ld "$install_dir" | grep -q "drwxr-x--- . $app www-data"
ls -l "$install_dir/index.html" | grep -q "\-rw-r----- . $app www-data"
}
ynhtest_setup_source_no_group_in_manifest() {
install_dir="$(mktemp -d -p $VAR_WWW)"
_make_dummy_manifest > ../manifest.toml
sed '/www-data/d' -i ../manifest.toml
ynh_setup_source --dest_dir="$install_dir" --source_id="dummy"
test -e "$install_dir"
test -e "$install_dir/index.html"
ls -ld "$install_dir" | grep -q "drwxr-x--- . $app $app"
ls -l "$install_dir/index.html" | grep -q "\-rw-r----- . $app $app"
}
ynhtest_setup_source_nominal_upgrade() {
install_dir="$(mktemp -d -p $VAR_WWW)"
_make_dummy_manifest > ../manifest.toml
ynh_setup_source --dest_dir="$install_dir" --source_id="dummy"
test "$(cat $install_dir/index.html)" == "Lorem Ipsum"
# Except index.html to get overwritten during next ynh_setup_source
echo "IEditedYou!" > $install_dir/index.html
test "$(cat $install_dir/index.html)" == "IEditedYou!"
ynh_setup_source --dest_dir="$install_dir" --source_id="dummy"
test "$(cat $install_dir/index.html)" == "Lorem Ipsum"
}
ynhtest_setup_source_with_keep() {
install_dir="$(mktemp -d -p $VAR_WWW)"
_make_dummy_manifest > ../manifest.toml
echo "IEditedYou!" > $install_dir/index.html
echo "IEditedYou!" > $install_dir/test.txt
ynh_setup_source --dest_dir="$install_dir" --source_id="dummy" --keep="index.html test.txt"
test -e "$install_dir"
test -e "$install_dir/index.html"
test -e "$install_dir/test.txt"
test "$(cat $install_dir/index.html)" == "IEditedYou!"
test "$(cat $install_dir/test.txt)" == "IEditedYou!"
}
ynhtest_setup_source_with_patch() {
install_dir="$(mktemp -d -p $VAR_WWW)"
_make_dummy_manifest > ../manifest.toml
mkdir -p ../patches/dummy/
cat > ../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="$install_dir" --source_id="dummy"
test "$(cat $install_dir/index.html)" == "Lorem Ipsum dolor sit amet"
}

View file

@ -0,0 +1,62 @@
ynhtest_simple_template_app_config() {
mkdir -p /etc/yunohost/apps/$app/
echo "id: $app" > /etc/yunohost/apps/$app/settings.yml
template="$(mktemp -d -p $VAR_WWW)/template.txt"
cat << EOF > $template
app=__APP__
foo=__FOO__
EOF
foo="bar"
ynh_config_add --template="$template" --destination="$VAR_WWW/config.txt"
test "$(cat $VAR_WWW/config.txt)" == "$(echo -ne 'app=ynhtest\nfoo=bar')"
test "$(ls -l $VAR_WWW/config.txt | cut -d' ' -f1-4)" == "-rw-r----- 1 ynhtest ynhtest"
}
ynhtest_simple_template_system_config() {
mkdir -p /etc/yunohost/apps/$app/
echo "id: $app" > /etc/yunohost/apps/$app/settings.yml
rm -f /etc/cron.d/ynhtest_config
template="$(mktemp -d -p $VAR_WWW)/template.txt"
cat << EOF > $template
app=__APP__
foo=__FOO__
EOF
foo="bar"
ynh_config_add --template="$template" --destination="/etc/cron.d/ynhtest_config"
test "$(cat $VAR_WWW/config.txt)" == "$(echo -ne 'app=ynhtest\nfoo=bar')"
test "$(ls -l /etc/cron.d/ynhtest_config | cut -d' ' -f1-4)" == "-r-------- 1 root root"
rm -f /etc/cron.d/ynhtest_config
}
ynhtest_jinja_template_app_config() {
mkdir -p /etc/yunohost/apps/$app/
echo "id: $app" > /etc/yunohost/apps/$app/settings.yml
template="$(mktemp -d -p $VAR_WWW)/template.txt"
cat << EOF > $template
app={{ app }}
{% if foo == "bar" %}foo=true{% endif %}
EOF
foo="bar"
ynh_config_add --template="$template" --destination="$VAR_WWW/config.txt" --jinja
test "$(cat $VAR_WWW/config.txt)" == "$(echo -ne 'app=ynhtest\nfoo=true')"
test "$(ls -l $VAR_WWW/config.txt | cut -d' ' -f1-4)" == "-rw-r----- 1 ynhtest ynhtest"
}

View file

@ -0,0 +1,662 @@
#################
# _ __ _ _ #
# | '_ \| | | | #
# | |_) | |_| | #
# | .__/ \__, | #
# | | __/ | #
# |_| |___/ #
# #
#################
_read_py() {
local file="$1"
local key="$2"
python3 -c "exec(open('$file').read()); print($key)"
}
ynhtest_config_read_py() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.py"
cat << EOF > $dummy_dir/dummy.py
# Some comment
FOO = None
ENABLED = False
# TITLE = "Old title"
TITLE = "Lorem Ipsum"
THEME = "colib'ris"
EMAIL = "root@example.com" # This is a comment without quotes
PORT = 1234 # This is a comment without quotes
URL = 'https://yunohost.org'
DICT = {}
DICT['ldap_base'] = "ou=users,dc=yunohost,dc=org"
DICT['ldap_conf'] = {}
DICT['ldap_conf']['user'] = "camille"
# YNH_ICI
DICT['TITLE'] = "Hello world"
EOF
test "$(_read_py "$file" "FOO")" == "None"
test "$(ynh_read_var_in_file "$file" "FOO")" == "None"
test "$(_read_py "$file" "ENABLED")" == "False"
test "$(ynh_read_var_in_file "$file" "ENABLED")" == "False"
test "$(_read_py "$file" "TITLE")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file "$file" "TITLE")" == "Lorem Ipsum"
test "$(_read_py "$file" "THEME")" == "colib'ris"
test "$(ynh_read_var_in_file "$file" "THEME")" == "colib'ris"
test "$(_read_py "$file" "EMAIL")" == "root@example.com"
test "$(ynh_read_var_in_file "$file" "EMAIL")" == "root@example.com"
test "$(_read_py "$file" "PORT")" == "1234"
test "$(ynh_read_var_in_file "$file" "PORT")" == "1234"
test "$(_read_py "$file" "URL")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "URL")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=users,dc=yunohost,dc=org"
test "$(ynh_read_var_in_file "$file" "user")" == "camille"
test "$(ynh_read_var_in_file "$file" "TITLE" "YNH_ICI")" == "Hello world"
! _read_py "$file" "NONEXISTENT"
test "$(ynh_read_var_in_file "$file" "NONEXISTENT")" == "YNH_NULL"
! _read_py "$file" "ENABLE"
test "$(ynh_read_var_in_file "$file" "ENABLE")" == "YNH_NULL"
}
ynhtest_config_write_py() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.py"
cat << EOF > $dummy_dir/dummy.py
# Some comment
FOO = None
ENABLED = False
# TITLE = "Old title"
TITLE = "Lorem Ipsum"
THEME = "colib'ris"
EMAIL = "root@example.com" # This is a comment without quotes
PORT = 1234 # This is a comment without quotes
URL = 'https://yunohost.org'
DICT = {}
DICT['ldap_base'] = "ou=users,dc=yunohost,dc=org"
# YNH_ICI
DICT['TITLE'] = "Hello world"
EOF
ynh_write_var_in_file "$file" "FOO" "bar"
test "$(_read_py "$file" "FOO")" == "bar"
test "$(ynh_read_var_in_file "$file" "FOO")" == "bar"
ynh_write_var_in_file "$file" "ENABLED" "True"
test "$(_read_py "$file" "ENABLED")" == "True"
test "$(ynh_read_var_in_file "$file" "ENABLED")" == "True"
ynh_write_var_in_file "$file" "TITLE" "Foo Bar"
test "$(_read_py "$file" "TITLE")" == "Foo Bar"
test "$(ynh_read_var_in_file "$file" "TITLE")" == "Foo Bar"
ynh_write_var_in_file "$file" "THEME" "super-awesome-theme"
test "$(_read_py "$file" "THEME")" == "super-awesome-theme"
test "$(ynh_read_var_in_file "$file" "THEME")" == "super-awesome-theme"
ynh_write_var_in_file "$file" "EMAIL" "sam@domain.tld"
test "$(_read_py "$file" "EMAIL")" == "sam@domain.tld"
test "$(ynh_read_var_in_file "$file" "EMAIL")" == "sam@domain.tld"
ynh_write_var_in_file "$file" "PORT" "5678"
test "$(_read_py "$file" "PORT")" == "5678"
test "$(ynh_read_var_in_file "$file" "PORT")" == "5678"
ynh_write_var_in_file "$file" "URL" "https://domain.tld/foobar"
test "$(_read_py "$file" "URL")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file "$file" "URL")" == "https://domain.tld/foobar"
ynh_write_var_in_file "$file" "ldap_base" "ou=users,dc=yunohost,dc=org"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=users,dc=yunohost,dc=org"
ynh_write_var_in_file "$file" "TITLE" "YOLO" "YNH_ICI"
test "$(ynh_read_var_in_file "$file" "TITLE" "YNH_ICI")" == "YOLO"
! ynh_write_var_in_file "$file" "NONEXISTENT" "foobar"
! _read_py "$file" "NONEXISTENT"
test "$(ynh_read_var_in_file "$file" "NONEXISTENT")" == "YNH_NULL"
! ynh_write_var_in_file "$file" "ENABLE" "foobar"
! _read_py "$file" "ENABLE"
test "$(ynh_read_var_in_file "$file" "ENABLE")" == "YNH_NULL"
}
###############
# _ _ #
# (_) (_) #
# _ _ __ _ #
# | | '_ \| | #
# | | | | | | #
# |_|_| |_|_| #
# #
###############
_read_ini() {
local file="$1"
local key="$2"
python3 -c "import configparser; c = configparser.ConfigParser(); c.read('$file'); print(c['main']['$key'])"
}
ynhtest_config_read_ini() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.ini"
cat << EOF > $file
# Some comment
; Another comment
[main]
foo = null
enabled = False
# title = Old title
title = Lorem Ipsum
theme = colib'ris
email = root@example.com ; This is a comment without quotes
port = 1234 ; This is a comment without quotes
url = https://yunohost.org
[dict]
ldap_base = ou=users,dc=yunohost,dc=org
EOF
test "$(_read_ini "$file" "foo")" == "null"
test "$(ynh_read_var_in_file "$file" "foo")" == "null"
test "$(_read_ini "$file" "enabled")" == "False"
test "$(ynh_read_var_in_file "$file" "enabled")" == "False"
test "$(_read_ini "$file" "title")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file "$file" "title")" == "Lorem Ipsum"
test "$(_read_ini "$file" "theme")" == "colib'ris"
test "$(ynh_read_var_in_file "$file" "theme")" == "colib'ris"
#test "$(_read_ini "$file" "email")" == "root@example.com"
test "$(ynh_read_var_in_file "$file" "email")" == "root@example.com"
#test "$(_read_ini "$file" "port")" == "1234"
test "$(ynh_read_var_in_file "$file" "port")" == "1234"
test "$(_read_ini "$file" "url")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "url")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=users,dc=yunohost,dc=org"
! _read_ini "$file" "nonexistent"
test "$(ynh_read_var_in_file "$file" "nonexistent")" == "YNH_NULL"
! _read_ini "$file" "enable"
test "$(ynh_read_var_in_file "$file" "enable")" == "YNH_NULL"
}
ynhtest_config_write_ini() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.ini"
cat << EOF > $file
# Some comment
; Another comment
[main]
foo = null
enabled = False
# title = Old title
title = Lorem Ipsum
theme = colib'ris
email = root@example.com # This is a comment without quotes
port = 1234 # This is a comment without quotes
url = https://yunohost.org
[dict]
ldap_base = ou=users,dc=yunohost,dc=org
EOF
ynh_write_var_in_file "$file" "foo" "bar"
test "$(_read_ini "$file" "foo")" == "bar"
test "$(ynh_read_var_in_file "$file" "foo")" == "bar"
ynh_write_var_in_file "$file" "enabled" "True"
test "$(_read_ini "$file" "enabled")" == "True"
test "$(ynh_read_var_in_file "$file" "enabled")" == "True"
ynh_write_var_in_file "$file" "title" "Foo Bar"
test "$(_read_ini "$file" "title")" == "Foo Bar"
test "$(ynh_read_var_in_file "$file" "title")" == "Foo Bar"
ynh_write_var_in_file "$file" "theme" "super-awesome-theme"
test "$(_read_ini "$file" "theme")" == "super-awesome-theme"
test "$(ynh_read_var_in_file "$file" "theme")" == "super-awesome-theme"
ynh_write_var_in_file "$file" "email" "sam@domain.tld"
test "$(_read_ini "$file" "email")" == "sam@domain.tld # This is a comment without quotes"
test "$(ynh_read_var_in_file "$file" "email")" == "sam@domain.tld"
ynh_write_var_in_file "$file" "port" "5678"
test "$(_read_ini "$file" "port")" == "5678 # This is a comment without quotes"
test "$(ynh_read_var_in_file "$file" "port")" == "5678"
ynh_write_var_in_file "$file" "url" "https://domain.tld/foobar"
test "$(_read_ini "$file" "url")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file "$file" "url")" == "https://domain.tld/foobar"
ynh_write_var_in_file "$file" "ldap_base" "ou=users,dc=yunohost,dc=org"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=users,dc=yunohost,dc=org"
! ynh_write_var_in_file "$file" "nonexistent" "foobar"
! _read_ini "$file" "nonexistent"
test "$(ynh_read_var_in_file "$file" "nonexistent")" == "YNH_NULL"
! ynh_write_var_in_file "$file" "enable" "foobar"
! _read_ini "$file" "enable"
test "$(ynh_read_var_in_file "$file" "enable")" == "YNH_NULL"
}
#############################
# _ #
# | | #
# _ _ __ _ _ __ ___ | | #
# | | | |/ _` | '_ ` _ \| | #
# | |_| | (_| | | | | | | | #
# \__, |\__,_|_| |_| |_|_| #
# __/ | #
# |___/ #
# #
#############################
_read_yaml() {
local file="$1"
local key="$2"
python3 -c "import yaml; print(yaml.safe_load(open('$file'))['$key'])"
}
ynhtest_config_read_yaml() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.yml"
cat << EOF > $file
# Some comment
foo:
enabled: false
# title: old title
title: Lorem Ipsum
theme: colib'ris
email: root@example.com # This is a comment without quotes
port: 1234 # This is a comment without quotes
url: https://yunohost.org
dict:
ldap_base: ou=users,dc=yunohost,dc=org
EOF
test "$(_read_yaml "$file" "foo")" == "None"
test "$(ynh_read_var_in_file "$file" "foo")" == ""
test "$(_read_yaml "$file" "enabled")" == "False"
test "$(ynh_read_var_in_file "$file" "enabled")" == "false"
test "$(_read_yaml "$file" "title")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file "$file" "title")" == "Lorem Ipsum"
test "$(_read_yaml "$file" "theme")" == "colib'ris"
test "$(ynh_read_var_in_file "$file" "theme")" == "colib'ris"
test "$(_read_yaml "$file" "email")" == "root@example.com"
test "$(ynh_read_var_in_file "$file" "email")" == "root@example.com"
test "$(_read_yaml "$file" "port")" == "1234"
test "$(ynh_read_var_in_file "$file" "port")" == "1234"
test "$(_read_yaml "$file" "url")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "url")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=users,dc=yunohost,dc=org"
! _read_yaml "$file" "nonexistent"
test "$(ynh_read_var_in_file "$file" "nonexistent")" == "YNH_NULL"
! _read_yaml "$file" "enable"
test "$(ynh_read_var_in_file "$file" "enable")" == "YNH_NULL"
}
ynhtest_config_write_yaml() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.yml"
cat << EOF > $file
# Some comment
foo:
enabled: false
# title: old title
title: Lorem Ipsum
theme: colib'ris
email: root@example.com # This is a comment without quotes
port: 1234 # This is a comment without quotes
url: https://yunohost.org
dict:
ldap_base: ou=users,dc=yunohost,dc=org
EOF
ynh_write_var_in_file "$file" "foo" "bar"
# cat $dummy_dir/dummy.yml # to debug
! test "$(_read_yaml "$file" "foo")" == "bar" # writing broke the yaml syntax... "foo:bar" (no space aftr :)
test "$(ynh_read_var_in_file "$file" "foo")" == "bar"
ynh_write_var_in_file "$file" "enabled" "true"
test "$(_read_yaml "$file" "enabled")" == "True"
test "$(ynh_read_var_in_file "$file" "enabled")" == "true"
ynh_write_var_in_file "$file" "title" "Foo Bar"
test "$(_read_yaml "$file" "title")" == "Foo Bar"
test "$(ynh_read_var_in_file "$file" "title")" == "Foo Bar"
ynh_write_var_in_file "$file" "theme" "super-awesome-theme"
test "$(_read_yaml "$file" "theme")" == "super-awesome-theme"
test "$(ynh_read_var_in_file "$file" "theme")" == "super-awesome-theme"
ynh_write_var_in_file "$file" "email" "sam@domain.tld"
test "$(_read_yaml "$file" "email")" == "sam@domain.tld"
test "$(ynh_read_var_in_file "$file" "email")" == "sam@domain.tld"
ynh_write_var_in_file "$file" "port" "5678"
test "$(_read_yaml "$file" "port")" == "5678"
test "$(ynh_read_var_in_file "$file" "port")" == "5678"
ynh_write_var_in_file "$file" "url" "https://domain.tld/foobar"
test "$(_read_yaml "$file" "url")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file "$file" "url")" == "https://domain.tld/foobar"
ynh_write_var_in_file "$file" "ldap_base" "ou=foobar,dc=domain,dc=tld"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=foobar,dc=domain,dc=tld"
! ynh_write_var_in_file "$file" "nonexistent" "foobar"
test "$(ynh_read_var_in_file "$file" "nonexistent")" == "YNH_NULL"
! ynh_write_var_in_file "$file" "enable" "foobar"
test "$(ynh_read_var_in_file "$file" "enable")" == "YNH_NULL"
test "$(ynh_read_var_in_file "$file" "enabled")" == "true"
}
#########################
# _ #
# (_) #
# _ ___ ___ _ __ #
# | / __|/ _ \| '_ \ #
# | \__ \ (_) | | | | #
# | |___/\___/|_| |_| #
# _/ | #
# |__/ #
# #
#########################
_read_json() {
local file="$1"
local key="$2"
python3 -c "import json; print(json.load(open('$file'))['$key'])"
}
ynhtest_config_read_json() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.json"
cat << EOF > $file
{
"foo": null,
"enabled": false,
"title": "Lorem Ipsum",
"theme": "colib'ris",
"email": "root@example.com",
"port": 1234,
"url": "https://yunohost.org",
"dict": {
"ldap_base": "ou=users,dc=yunohost,dc=org"
}
}
EOF
test "$(_read_json "$file" "foo")" == "None"
test "$(ynh_read_var_in_file "$file" "foo")" == "null"
test "$(_read_json "$file" "enabled")" == "False"
test "$(ynh_read_var_in_file "$file" "enabled")" == "false"
test "$(_read_json "$file" "title")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file "$file" "title")" == "Lorem Ipsum"
test "$(_read_json "$file" "theme")" == "colib'ris"
test "$(ynh_read_var_in_file "$file" "theme")" == "colib'ris"
test "$(_read_json "$file" "email")" == "root@example.com"
test "$(ynh_read_var_in_file "$file" "email")" == "root@example.com"
test "$(_read_json "$file" "port")" == "1234"
test "$(ynh_read_var_in_file "$file" "port")" == "1234"
test "$(_read_json "$file" "url")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "url")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=users,dc=yunohost,dc=org"
! _read_json "$file" "nonexistent"
test "$(ynh_read_var_in_file "$file" "nonexistent")" == "YNH_NULL"
! _read_json "$file" "enable"
test "$(ynh_read_var_in_file "$file" "enable")" == "YNH_NULL"
}
ynhtest_config_write_json() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.json"
cat << EOF > $file
{
"foo": null,
"enabled": false,
"title": "Lorem Ipsum",
"theme": "colib'ris",
"email": "root@example.com",
"port": 1234,
"url": "https://yunohost.org",
"dict": {
"ldap_base": "ou=users,dc=yunohost,dc=org"
}
}
EOF
ynh_write_var_in_file "$file" "foo" "bar"
cat $file
test "$(_read_json "$file" "foo")" == "bar"
test "$(ynh_read_var_in_file "$file" "foo")" == "bar"
ynh_write_var_in_file "$file" "enabled" "true"
cat $file
test "$(_read_json "$file" "enabled")" == "true"
test "$(ynh_read_var_in_file "$file" "enabled")" == "true"
ynh_write_var_in_file "$file" "title" "Foo Bar"
cat $file
test "$(_read_json "$file" "title")" == "Foo Bar"
test "$(ynh_read_var_in_file "$file" "title")" == "Foo Bar"
ynh_write_var_in_file "$file" "theme" "super-awesome-theme"
cat $file
test "$(_read_json "$file" "theme")" == "super-awesome-theme"
test "$(ynh_read_var_in_file "$file" "theme")" == "super-awesome-theme"
ynh_write_var_in_file "$file" "email" "sam@domain.tld"
cat $file
test "$(_read_json "$file" "email")" == "sam@domain.tld"
test "$(ynh_read_var_in_file "$file" "email")" == "sam@domain.tld"
ynh_write_var_in_file "$file" "port" "5678"
test "$(_read_json "$file" "port")" == "5678"
test "$(ynh_read_var_in_file "$file" "port")" == "5678"
ynh_write_var_in_file "$file" "url" "https://domain.tld/foobar"
test "$(_read_json "$file" "url")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file "$file" "url")" == "https://domain.tld/foobar"
ynh_write_var_in_file "$file" "ldap_base" "ou=foobar,dc=domain,dc=tld"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=foobar,dc=domain,dc=tld"
! ynh_write_var_in_file "$file" "nonexistent" "foobar"
test "$(ynh_read_var_in_file "$file" "nonexistent")" == "YNH_NULL"
! ynh_write_var_in_file "$file" "enable" "foobar"
test "$(ynh_read_var_in_file "$file" "enable")" == "YNH_NULL"
test "$(ynh_read_var_in_file "$file" "enabled")" == "true"
}
#######################
# _ #
# | | #
# _ __ | |__ _ __ #
# | '_ \| '_ \| '_ \ #
# | |_) | | | | |_) | #
# | .__/|_| |_| .__/ #
# | | | | #
# |_| |_| #
# #
#######################
_read_php() {
local file="$1"
local key="$2"
php -r "include '$file'; echo var_export(\$$key);" | sed "s/^'//g" | sed "s/'$//g"
}
ynhtest_config_read_php() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.php"
cat << EOF > $file
<?php
// Some comment
\$foo = NULL;
\$enabled = false;
// \$title = "old title";
\$title = "Lorem Ipsum";
\$theme = "colib'ris";
\$email = "root@example.com"; // This is a comment without quotes
\$port = 1234; // This is a second comment without quotes
\$url = "https://yunohost.org";
\$dict = [
'ldap_base' => "ou=users,dc=yunohost,dc=org",
'ldap_conf' => []
];
\$dict['ldap_conf']['user'] = 'camille';
const DB_HOST = 'localhost';
?>
EOF
test "$(_read_php "$file" "foo")" == "NULL"
test "$(ynh_read_var_in_file "$file" "foo")" == "NULL"
test "$(_read_php "$file" "enabled")" == "false"
test "$(ynh_read_var_in_file "$file" "enabled")" == "false"
test "$(_read_php "$file" "title")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file "$file" "title")" == "Lorem Ipsum"
test "$(_read_php "$file" "theme")" == "colib\\'ris"
test "$(ynh_read_var_in_file "$file" "theme")" == "colib'ris"
test "$(_read_php "$file" "email")" == "root@example.com"
test "$(ynh_read_var_in_file "$file" "email")" == "root@example.com"
test "$(_read_php "$file" "port")" == "1234"
test "$(ynh_read_var_in_file "$file" "port")" == "1234"
test "$(_read_php "$file" "url")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "url")" == "https://yunohost.org"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=users,dc=yunohost,dc=org"
test "$(ynh_read_var_in_file "$file" "user")" == "camille"
test "$(ynh_read_var_in_file "$file" "DB_HOST")" == "localhost"
! _read_php "$file" "nonexistent"
test "$(ynh_read_var_in_file "$file" "nonexistent")" == "YNH_NULL"
! _read_php "$file" "enable"
test "$(ynh_read_var_in_file "$file" "enable")" == "YNH_NULL"
}
ynhtest_config_write_php() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.php"
cat << EOF > $file
<?php
// Some comment
\$foo = NULL;
\$enabled = false;
// \$title = "old title";
\$title = "Lorem Ipsum";
\$theme = "colib'ris";
\$email = "root@example.com"; // This is a comment without quotes
\$port = 1234; // This is a comment without quotes
\$url = "https://yunohost.org";
\$dict = [
'ldap_base' => "ou=users,dc=yunohost,dc=org",
];
?>
EOF
ynh_write_var_in_file "$file" "foo" "bar"
test "$(_read_php "$file" "foo")" == "bar"
test "$(ynh_read_var_in_file "$file" "foo")" == "bar"
ynh_write_var_in_file "$file" "enabled" "true"
test "$(_read_php "$file" "enabled")" == "true"
test "$(ynh_read_var_in_file "$file" "enabled")" == "true"
ynh_write_var_in_file "$file" "title" "Foo Bar"
cat $file
test "$(_read_php "$file" "title")" == "Foo Bar"
test "$(ynh_read_var_in_file "$file" "title")" == "Foo Bar"
ynh_write_var_in_file "$file" "theme" "super-awesome-theme"
cat $file
test "$(_read_php "$file" "theme")" == "super-awesome-theme"
test "$(ynh_read_var_in_file "$file" "theme")" == "super-awesome-theme"
ynh_write_var_in_file "$file" "email" "sam@domain.tld"
cat $file
test "$(_read_php "$file" "email")" == "sam@domain.tld"
test "$(ynh_read_var_in_file "$file" "email")" == "sam@domain.tld"
ynh_write_var_in_file "$file" "port" "5678"
test "$(_read_php "$file" "port")" == "5678"
test "$(ynh_read_var_in_file "$file" "port")" == "5678"
ynh_write_var_in_file "$file" "url" "https://domain.tld/foobar"
test "$(_read_php "$file" "url")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file "$file" "url")" == "https://domain.tld/foobar"
ynh_write_var_in_file "$file" "ldap_base" "ou=foobar,dc=domain,dc=tld"
test "$(ynh_read_var_in_file "$file" "ldap_base")" == "ou=foobar,dc=domain,dc=tld"
! ynh_write_var_in_file "$file" "nonexistent" "foobar"
test "$(ynh_read_var_in_file "$file" "nonexistent")" == "YNH_NULL"
! ynh_write_var_in_file "$file" "enable" "foobar"
test "$(ynh_read_var_in_file "$file" "enable")" == "YNH_NULL"
test "$(ynh_read_var_in_file "$file" "enabled")" == "true"
}

View file

@ -0,0 +1,25 @@
ynhtest_system_user_create() {
username=$(head -c 12 /dev/urandom | md5sum | head -c 12)
! ynh_system_user_exists --username="$username"
ynh_system_user_create --username="$username"
ynh_system_user_exists --username="$username"
ynh_system_user_delete --username="$username"
! ynh_system_user_exists --username="$username"
}
ynhtest_system_user_with_group() {
username=$(head -c 12 /dev/urandom | md5sum | head -c 12)
ynh_system_user_create --username="$username" --groups="ssl-cert,ssh.app"
grep -q "^ssl-cert:.*$username" /etc/group
grep -q "^ssh.app:.*$username" /etc/group
ynh_system_user_delete --username="$username"
}