Skip to content

Commit

Permalink
Fix: Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bokysan committed Oct 29, 2023
1 parent 1d74829 commit 4b2f481
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 55 deletions.
33 changes: 16 additions & 17 deletions unit-tests/001_verify_read_write_properties.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@
load /code/scripts/common.sh
load /code/scripts/common-run.sh

declare temporary_file
setup() {
temporary_file="$(mktemp -t)"
cp /etc/postfix/main.cf "${temporary_file}"
}

teardown() {
cat "${temporary_file}" > /etc/postfix/main.cf
rm -rf "${temporary_file}"
}

@test "verify reading and writting propreties" {
@test "verify reading empty property" {
local value
local old_value
old_value="$(get_postconf "mydestination")"

do_postconf -# mydestination
do_postconf -e "mydestination="

value="$(get_postconf "mydestination")"
if [[ -n "${value}" ]]; then
echo "Expected '', got: '$value' for 'mydestination'" >&2
exit 1
fi
}

@test "verify reading full property" {
do_postconf -e 'mydestination=$myhostname, localhost.$mydomain $mydomain'

value="$(get_postconf "mydestination")"
if [[ "${value}" != 'mydestination=$myhostname, localhost.$mydomain $mydomain' ]]; then
echo "Expected 'mydestination=\$myhostname, localhost.\$mydomain \$mydomain', got: '$value' for mydestination" >&2
exit 1
fi

do_postconf -# mydestination
echo " mydestination = localhost" >> /etc/postfix/main.cf

value="$(get_postconf "mydestination")"
if [[ "${value}" != "localhost" ]]; then
echo "Expected 'localhost', got: '$value' for mydestination" >&2
if [[ "${value}" != '$myhostname, localhost.$mydomain $mydomain' ]]; then
echo "Expected '\$myhostname, localhost.\$mydomain \$mydomain', got: '$value' for mydestination" >&2
exit 1
fi

do_postconf -e "mydestination=${old_value}"
}
103 changes: 65 additions & 38 deletions unit-tests/002_postfix_upgrade_daemon_directory.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,101 @@
load /code/scripts/common.sh
load /code/scripts/common-run.sh

declare temporary_file
declare dir_debian="/usr/lib/postfix/sbin"
declare dir_alpine="/usr/libexec/postfix"
declare dir_temp

@test "verify reading of daemon_directory" {
local daemon_directory
local old_daemon_directory
local dir_debian="/usr/lib/postfix/sbin"
local dir_alpine="/usr/libexec/postfix"
local dir_temp="/tmp/deamon_directory"
old_daemon_directory="$(get_postconf "daemon_directory")"

setup() {
temporary_file="$(mktemp -t)"
dir_temp="$(mktemp -d)"
mkdir -p "${dir_temp}"
cp /etc/postfix/main.cf "${temporary_file}"

rm -rf "${dir_debian}.bak" "${dir_alpine}.bak"
if [[ -d "${dir_debian}" ]]; then
cp -r ${dir_debian} ${dir_temp}
mv ${dir_debian} ${dir_debian}.bak
cp -r "${dir_debian}" "${dir_temp}/postfix"
mv "${dir_debian}" "${dir_debian}.bak"
fi

if [[ -d "${dir_alpine}" ]]; then
cp -r ${dir_alpine} ${dir_temp}
mv ${dir_alpine} ${dir_alpine}.bak
cp -r "${dir_alpine}" "${dir_temp}/postfix"
mv "${dir_alpine}" "${dir_alpine}.bak"
fi

}

teardown() {
rm -rf "${dir_temp}"

if [[ -d "${dir_debian}.bak" ]]; then
mv ${dir_debian}.bak ${dir_debian}
fi

if [[ -d "${dir_alpine}.bak" ]]; then
mv ${dir_alpine}.bak ${dir_alpine}
fi

# Test if Debian/Ubuntu directory remains the same when run on Debian/Ubuntu
cp -r ${dir_temp} ${dir_debian}
cat "${temporary_file}" > /etc/postfix/main.cf
rm -rf "${temporary_file}"
}

@test "Test if Debian/Ubuntu directory remains the same when run on Debian/Ubuntu" {
local daemon_directory

rm -rf "${dir_debian}" "${dir_alpine}"
error "$(ls -lah ${dir_temp})"
cp -r "${dir_temp}/postfix" "${dir_debian}"
do_postconf -e "daemon_directory=${dir_debian}"
postfix_upgrade_daemon_directory
postfix check
rm -rf ${dir_debian} ${dir_alpine}
rm -rf "${dir_debian}" "${dir_alpine}"
[ "$(get_postconf "daemon_directory")" == "${dir_debian}" ]
}

@test "Test if Debian/Ubuntu directory gets changes the same when run on Alpine" {
local daemon_directory

# Test if Debian/Ubuntu directory gets changes the same when run on Alpine
cp -r ${dir_temp} ${dir_alpine}
rm -rf "${dir_debian}" "${dir_alpine}"
cp -r "${dir_temp}/postfix" "${dir_debian}"
do_postconf -e "daemon_directory=${dir_debian}"
postfix_upgrade_daemon_directory
postfix check
rm -rf ${dir_debian} ${dir_alpine}
rm -rf "${dir_debian}" "${dir_alpine}"
[ "$(get_postconf "daemon_directory")" == "${dir_alpine}" ]
}

@test "Test if Alpine directory remains the same when run on Alpine" {
local daemon_directory

# Test if Alpine directory remains the same when run on Alpine
cp -r ${dir_temp} ${dir_alpine}
rm -rf "${dir_debian}" "${dir_alpine}"
cp -r "${dir_temp}/postfix" "${dir_alpine}"
do_postconf -e "daemon_directory=${dir_alpine}"
postfix_upgrade_daemon_directory
postfix check
rm -rf ${dir_debian} ${dir_alpine}
rm -rf "${dir_debian}" "${dir_alpine}"
[ "$(get_postconf "daemon_directory")" == "${dir_alpine}" ]
}

@test "Test if Alpine directory gets changes the same when run on Debian/Ubuntu" {
local daemon_directory

# Test if Alpine directory gets changes the same when run on Debian/Ubuntu
cp -r ${dir_temp} ${dir_debian}
rm -rf "${dir_debian}" "${dir_alpine}"
cp -r "${dir_temp}/postfix" "${dir_alpine}"
do_postconf -e "daemon_directory=${dir_alpine}"
postfix_upgrade_daemon_directory
postfix check
rm -rf ${dir_debian} ${dir_alpine}
rm -rf "${dir_debian}" "${dir_alpine}"
[ "$(get_postconf "daemon_directory")" == "${dir_debian}" ]
}

# Test if things work with custom directory
do_postconf -e "daemon_directory=${dir_temp}"
@test "Test if things work with custom directory" {
local daemon_directory

do_postconf -e "daemon_directory=${dir_temp}/postfix"
postfix_upgrade_daemon_directory
postfix check
rm -rf ${dir_debian} ${dir_alpine}
rm -rf "${dir_debian}" "${dir_alpine}"
[ "$(get_postconf "daemon_directory")" == "${dir_temp}" ]

rm -rf ${dir_temp}

if [[ -d "${dir_debian}.bak" ]]; then
mv ${dir_debian}.bak ${dir_debian}
fi

if [[ -d "${dir_alpine}.bak" ]]; then
mv ${dir_alpine}.bak ${dir_alpine}
fi

do_postconf -e "daemon_directory=${old_daemon_directory}"
}

0 comments on commit 4b2f481

Please sign in to comment.