mirror of
https://github.com/mail-in-a-box/mailinabox.git
synced 2026-03-05 15:57:23 +01:00
Test upgrade to LDAP from upstream Mail-in-a-Box/sqlite
This commit is contained in:
16
tests/lib/all.sh
Normal file
16
tests/lib/all.sh
Normal file
@@ -0,0 +1,16 @@
|
||||
#
|
||||
# source all lib scripts
|
||||
#
|
||||
# from your script, supply the path to this directory as the first argument
|
||||
#
|
||||
# eg source "tests/lib/all.sh" "tests/lib"
|
||||
#
|
||||
# failure to load any script is fatal!
|
||||
|
||||
. "$1/color-output.sh" || exit 1
|
||||
. "$1/locations.sh" || exit 2
|
||||
. "$1/misc.sh" || exit 3
|
||||
. "$1/rest.sh" || exit 4
|
||||
. "$1/system.sh" || exit 5
|
||||
|
||||
|
||||
32
tests/lib/color-output.sh
Normal file
32
tests/lib/color-output.sh
Normal file
@@ -0,0 +1,32 @@
|
||||
# ansi escapes for hilighting text
|
||||
F_DANGER=$(echo -e "\033[31m")
|
||||
F_WARN=$(echo -e "\033[93m")
|
||||
F_RESET=$(echo -e "\033[39m")
|
||||
|
||||
|
||||
danger() {
|
||||
local echoarg
|
||||
case "$1" in
|
||||
-n )
|
||||
echoarg="$1"
|
||||
shift
|
||||
;;
|
||||
* )
|
||||
echoarg=""
|
||||
esac
|
||||
echo $echoarg "${F_DANGER}$1${F_RESET}"
|
||||
}
|
||||
|
||||
warn() {
|
||||
local echoarg
|
||||
case "$1" in
|
||||
-n )
|
||||
echoarg="$1"
|
||||
shift
|
||||
;;
|
||||
* )
|
||||
echoarg=""
|
||||
esac
|
||||
echo "${F_WARN}$1${F_RESET}"
|
||||
}
|
||||
|
||||
8
tests/lib/locations.sh
Normal file
8
tests/lib/locations.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
#
|
||||
|
||||
# where webmail.sh installs roundcube
|
||||
RCM_DIR=/usr/local/lib/roundcubemail
|
||||
RCM_PLUGIN_DIR=${RCM_DIR}/plugins
|
||||
|
||||
# where zpush.sh installs z-push
|
||||
ZPUSH_DIR=/usr/local/lib/z-push
|
||||
65
tests/lib/misc.sh
Normal file
65
tests/lib/misc.sh
Normal file
@@ -0,0 +1,65 @@
|
||||
#
|
||||
# misc helpful functions
|
||||
#
|
||||
# requirements:
|
||||
# system packages: [ python3 ]
|
||||
|
||||
|
||||
array_contains() {
|
||||
local searchfor="$1"
|
||||
shift
|
||||
local item
|
||||
for item; do
|
||||
[ "$item" == "$searchfor" ] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
is_true() {
|
||||
# empty string is not true
|
||||
if [ "$1" == "true" \
|
||||
-o "$1" == "TRUE" \
|
||||
-o "$1" == "True" \
|
||||
-o "$1" == "yes" \
|
||||
-o "$1" == "YES" \
|
||||
-o "$1" == "Yes" \
|
||||
-o "$1" == "1" ]
|
||||
then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
is_false() {
|
||||
if is_true $@; then return 1; fi
|
||||
return 0
|
||||
}
|
||||
|
||||
email_localpart() {
|
||||
local addr="$1"
|
||||
awk -F@ '{print $1}' <<<"$addr"
|
||||
}
|
||||
|
||||
email_domainpart() {
|
||||
local addr="$1"
|
||||
awk -F@ '{print $2}' <<<"$addr"
|
||||
}
|
||||
|
||||
|
||||
generate_uuid() {
|
||||
local uuid
|
||||
uuid=$(python3 -c "import uuid; print(uuid.uuid4())")
|
||||
[ $? -ne 0 ] && die "Unable to generate a uuid"
|
||||
echo "$uuid"
|
||||
}
|
||||
|
||||
generate_qa_password() {
|
||||
echo "Test1234."
|
||||
}
|
||||
|
||||
sha1() {
|
||||
local txt="$1"
|
||||
python3 -c "import hashlib; m=hashlib.sha1(); m.update(bytearray(r'''$txt''','utf-8')); print(m.hexdigest());" || die "Unable to generate sha1 hash"
|
||||
}
|
||||
|
||||
106
tests/lib/rest.sh
Normal file
106
tests/lib/rest.sh
Normal file
@@ -0,0 +1,106 @@
|
||||
#
|
||||
# REST helper functions
|
||||
#
|
||||
# requirements:
|
||||
# system packages: [ curl ]
|
||||
# lib scripts: [ color-output.sh ]
|
||||
#
|
||||
|
||||
rest_urlencoded() {
|
||||
# Issue a REST call having data urlencoded
|
||||
#
|
||||
# eg: rest_urlencoded POST /admin/mail/users/add "email=alice@abc.com" "password=secret"
|
||||
#
|
||||
# When providing a URI (/mail/users/add) and not a URL
|
||||
# (https://host/mail/users/add), PRIMARY_HOSTNAME must be set!
|
||||
#
|
||||
# The function will set the following global variables regardless
|
||||
# of exit c ode:
|
||||
# REST_HTTP_CODE
|
||||
# REST_OUTPUT
|
||||
# REST_ERROR
|
||||
#
|
||||
# Return values:
|
||||
# 0 indicates success (curl returned 0 or a code deemed to be
|
||||
# successful and HTTP status is >=200 but <300)
|
||||
# 1 curl returned with non-zero code that indicates and error
|
||||
# 2 the response status was <200 or >= 300
|
||||
#
|
||||
# Messages, errors, and output are all sent to stderr, there is no
|
||||
# stdout output
|
||||
#
|
||||
local verb="$1" # eg "POST"
|
||||
local uri="$2" # eg "/mail/users/add"
|
||||
local auth_user="$3"
|
||||
local auth_pass="$4"
|
||||
shift; shift; shift; shift # remaining arguments are data or curl args
|
||||
|
||||
local url
|
||||
case "$uri" in
|
||||
http:* | https:* )
|
||||
url="$uri"
|
||||
;;
|
||||
* )
|
||||
url="https://$PRIMARY_HOSTNAME${uri}"
|
||||
;;
|
||||
esac
|
||||
|
||||
local data=()
|
||||
local item output onlydata="false"
|
||||
|
||||
for item; do
|
||||
case "$item" in
|
||||
-- )
|
||||
onlydata="true"
|
||||
;;
|
||||
--* )
|
||||
# curl argument
|
||||
if $onlydata; then
|
||||
data+=("--data-urlencode" "$item");
|
||||
else
|
||||
data+=("$item")
|
||||
fi
|
||||
;;
|
||||
* )
|
||||
onlydata="true"
|
||||
data+=("--data-urlencode" "$item");
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
echo "spawn: curl -w \"%{http_code}\" -X $verb --user \"${auth_user}:xxx\" ${data[@]} $url" 1>&2
|
||||
output=$(curl -s -S -w "%{http_code}" -X $verb --user "${auth_user}:${auth_pass}" "${data[@]}" $url)
|
||||
local code=$?
|
||||
|
||||
# http status is last 3 characters of output, extract it
|
||||
REST_HTTP_CODE=$(awk '{S=substr($0,length($0)-2)} END {print S}' <<<"$output")
|
||||
REST_OUTPUT=$(awk 'BEGIN{L=""}{ if(L!="") print L; L=$0 } END { print substr(L,1,length(L)-3) }' <<<"$output")
|
||||
REST_ERROR=""
|
||||
[ -z "$REST_HTTP_CODE" ] && REST_HTTP_CODE="000"
|
||||
|
||||
if [ $code -ne 0 ]; then
|
||||
if [ $code -eq 56 -a $REST_HTTP_CODE -eq 200 ]; then
|
||||
# this is okay, I guess. happens sometimes during
|
||||
# POST /admin/mail/aliases/remove
|
||||
# 56=Unexpected EOF
|
||||
echo "Ignoring curl return code 56 due to 200 status" 1>&2
|
||||
|
||||
elif [ $code -ne 16 -o $REST_HTTP_CODE -ne 200 ]; then
|
||||
# any error code will fail the rest call except for a 16
|
||||
# with a 200 HTTP status.
|
||||
# 16="a problem was detected in the HTTP2 framing layer. This is somewhat generic and can be one out of several problems"
|
||||
REST_ERROR="CURL failed with code $code"
|
||||
echo "${F_DANGER}$REST_ERROR${F_RESET}" 1>&2
|
||||
echo "$output" 1>&2
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
if [ $REST_HTTP_CODE -lt 200 -o $REST_HTTP_CODE -ge 300 ]; then
|
||||
REST_ERROR="REST status $REST_HTTP_CODE: $REST_OUTPUT"
|
||||
echo "${F_DANGER}$REST_ERROR${F_RESET}" 1>&2
|
||||
return 2
|
||||
fi
|
||||
echo "CURL succeded, HTTP status $REST_HTTP_CODE" 1>&2
|
||||
echo "$output" 1>&2
|
||||
return 0
|
||||
}
|
||||
102
tests/lib/system.sh
Normal file
102
tests/lib/system.sh
Normal file
@@ -0,0 +1,102 @@
|
||||
|
||||
wait_for_apt() {
|
||||
# check to see if other package managers have a lock on new
|
||||
# installs, and wait for them to finish
|
||||
#
|
||||
# returns non-zero if waiting times out (currently ~600 seconds)
|
||||
local count=0
|
||||
while fuser /var/lib/dpkg/lock >/dev/null 2>&1 || fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1; do
|
||||
sleep 6
|
||||
let count+=1
|
||||
if [ $count -eq 1 ]; then
|
||||
echo -n "Waiting for other package manager to finish..."
|
||||
elif [ $count -gt 100 ]; then
|
||||
echo -n "FAILED"
|
||||
return 1
|
||||
else
|
||||
echo -n "${count}.."
|
||||
fi
|
||||
done
|
||||
[ $count -ge 1 ] && echo ""
|
||||
}
|
||||
|
||||
dump_file() {
|
||||
local log_file="$1"
|
||||
local lines="$2"
|
||||
local title="DUMP OF $log_file"
|
||||
echo ""
|
||||
echo "--------"
|
||||
echo -n "-------- $log_file"
|
||||
if [ ! -z "$lines" ]; then
|
||||
echo " (last $line lines)"
|
||||
else
|
||||
echo ""
|
||||
fi
|
||||
echo "--------"
|
||||
|
||||
if [ !-e "$log_file" ]; then
|
||||
echo "DOES NOT EXIST"
|
||||
elif [ ! -z "$lines" ]; then
|
||||
tail -$lines "$log_file"
|
||||
else
|
||||
cat "$log_file"
|
||||
fi
|
||||
}
|
||||
|
||||
dump_file_if_exists() {
|
||||
[ ! -e "$1" ] && return
|
||||
dump_file "$@"
|
||||
}
|
||||
|
||||
update_system_time() {
|
||||
if [ ! -x /usr/sbin/ntpdate ]; then
|
||||
wait_for_apt
|
||||
apt-get install -y -qq ntpdate || return 1
|
||||
fi
|
||||
ntpdate -s ntp.ubuntu.com && echo "System time updated"
|
||||
}
|
||||
|
||||
set_system_hostname() {
|
||||
# set the system hostname to the FQDN specified or
|
||||
# PRIMARY_HOSTNAME if no FQDN was given
|
||||
local fqdn="${1:-$PRIMARY_HOSTNAME}"
|
||||
local host="$(awk -F. '{print $1}' <<< "$fqdn")"
|
||||
sed -i 's/^127\.0\.1\.1[ \t].*/127.0.1.1 '"$fqdn $host ip4-loopback/" /etc/hosts || return 1
|
||||
#hostname "$host" || return 1
|
||||
#echo "$host" > /etc/hostname
|
||||
return 0
|
||||
}
|
||||
|
||||
install_docker() {
|
||||
if [ -x /usr/bin/docker ]; then
|
||||
echo "Docker already installed"
|
||||
return 0
|
||||
fi
|
||||
|
||||
wait_for_apt
|
||||
apt-get install -y -qq \
|
||||
apt-transport-https \
|
||||
ca-certificates \
|
||||
curl \
|
||||
gnupg-agent \
|
||||
software-properties-common \
|
||||
|| return 1
|
||||
|
||||
wait_for_apt
|
||||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - \
|
||||
|| return 2
|
||||
|
||||
wait_for_apt
|
||||
apt-key fingerprint 0EBFCD88 || return 3
|
||||
|
||||
wait_for_apt
|
||||
add-apt-repository -y --update "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" || return 4
|
||||
|
||||
wait_for_apt
|
||||
apt-get install -y -qq \
|
||||
docker-ce \
|
||||
docker-ce-cli \
|
||||
containerd.io \
|
||||
|| return 5
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user