Run sedfile tests in container (#2569)

This commit is contained in:
Casper 2022-05-05 12:58:00 +02:00 committed by GitHub
parent 628e902233
commit 18acd7860b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 38 deletions

View file

@ -9,19 +9,11 @@
# Is a file change expected? --> use 'sedfile --strict -i' # Is a file change expected? --> use 'sedfile --strict -i'
# Is a file change only on the first container run expected? --> use 'sedfile -i' # Is a file change only on the first container run expected? --> use 'sedfile -i'
if [[ -e /usr/local/bin/helpers/log.sh ]]
then
# shellcheck source=../scripts/helpers/log.sh
source /usr/local/bin/helpers/log.sh
else
# when running BATS (unit tests), this file is not located
# inside a container; as a consequence, we need to source
# from a different location
source target/scripts/helpers/log.sh
fi
set -ueo pipefail set -ueo pipefail
# shellcheck source=../scripts/helpers/log.sh
source /usr/local/bin/helpers/log.sh
function __usage { echo "Usage: ${0} -i <replace/delete operation> <file>" ; } function __usage { echo "Usage: ${0} -i <replace/delete operation> <file>" ; }
HASHTOOL='sha1sum' HASHTOOL='sha1sum'

View file

@ -1,71 +1,88 @@
load 'test_helper/common' load 'test_helper/common'
CONTAINER='sedfile'
TEST_FILE='/tmp/sedfile-test.txt'
# prepare tests # prepare tests
function setup_file() { function setup_file() {
export CONTAINER_START FILE SEDFILE local PRIVATE_CONFIG
FILE=$(mktemp /tmp/sedfile-test.XXX) PRIVATE_CONFIG="$(duplicate_config_for_container . )"
SEDFILE="/tmp/sedfile"
# workaround, /CONTAINER_START cannot be used (permission denied) docker run -d --name "${CONTAINER}" \
CONTAINER_START="/tmp/CONTAINER_START" -v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \
cp -a "target/bin/sedfile" "${SEDFILE}" -h mail.my-domain.com "${NAME}"
sed -i "s|/CONTAINER_START|${CONTAINER_START}|" "${SEDFILE}"
wait_for_finished_setup_in_container "${CONTAINER}"
}
function setup() {
# create test file # create test file
echo 'foo bar' > "${FILE}" docker exec "${CONTAINER}" bash -c 'echo "foo bar" > "'"${TEST_FILE}"'"'
} }
@test "checking sedfile parameter count" { @test "checking sedfile parameter count" {
run ${SEDFILE} run docker exec "${CONTAINER}" sedfile
assert_failure assert_failure
assert_output --partial 'At least three parameters must be given' assert_output --partial 'At least three parameters must be given'
} }
@test "checking sedfile substitute success" { @test "checking sedfile substitute success" {
# change 'bar' to 'baz' # change 'bar' to 'baz'
run ${SEDFILE} -i 's|bar|baz|' "${FILE}" run docker exec "${CONTAINER}" sedfile -i 's|bar|baz|' "${TEST_FILE}"
assert_success assert_success
assert_output "" assert_output ''
# file modified? # file modified?
run test "$(< "${FILE}")" == 'foo baz' run docker exec "${CONTAINER}" cat "${TEST_FILE}"
assert_success assert_success
assert_output 'foo baz'
} }
@test "checking sedfile substitute failure" { @test "checking sedfile substitute failure (on first container start)" {
run ${SEDFILE} -i 's|bar|baz|' "${FILE}" # delete marker
run docker exec "${CONTAINER}" rm '/CONTAINER_START'
assert_success
# try to change 'baz' to 'something' and fail
run docker exec "${CONTAINER}" sedfile -i 's|baz|something|' "${TEST_FILE}"
assert_failure assert_failure
assert_output --partial "No difference after call to 'sed' in 'sedfile' (sed -i s|bar|baz| /tmp/sedfile-test" assert_output --partial "No difference after call to 'sed' in 'sedfile' (sed -i s|baz|something| /tmp/sedfile-test.txt)"
# file unchanged? # file unchanged?
run test "$(< "${FILE}")" == 'foo baz' run docker exec "${CONTAINER}" cat "${TEST_FILE}"
assert_success
assert_output 'foo bar'
# recreate marker
run docker exec "${CONTAINER}" touch '/CONTAINER_START'
assert_success assert_success
} }
@test "checking sedfile silent failure on substitute" { @test "checking sedfile silent failure on substitute (when DMS was restarted)" {
# create marker to simulate a container restart # try to change 'baz' to 'something' and fail silently
date > "${CONTAINER_START}" run docker exec "${CONTAINER}" sedfile -i 's|baz|something|' "${TEST_FILE}"
run ${SEDFILE} -i 's|bar|baz|' "${FILE}"
assert_success assert_success
assert_output "" assert_output ''
# file unchanged? # file unchanged?
run test "$(< "${FILE}")" == 'foo baz' run docker exec "${CONTAINER}" cat "${TEST_FILE}"
assert_success assert_success
assert_output 'foo bar'
} }
@test "checking sedfile substitude failure (strict)" { @test "checking sedfile substitude failure (strict)" {
run ${SEDFILE} --strict -i 's|bar|baz|' "${FILE}" # try to change 'baz' to 'something' and fail
run docker exec "${CONTAINER}" sedfile --strict -i 's|baz|something|' "${TEST_FILE}"
assert_failure assert_failure
assert_output --partial "No difference after call to 'sed' in 'sedfile' (sed -i s|bar|baz| /tmp/sedfile-test" assert_output --partial "No difference after call to 'sed' in 'sedfile' (sed -i s|baz|something| /tmp/sedfile-test.txt)"
# file unchanged? # file unchanged?
run test "$(< "${FILE}")" == 'foo baz' run docker exec "${CONTAINER}" cat "${TEST_FILE}"
assert_success assert_success
assert_output 'foo bar'
} }
# clean up # clean up
function teardown_file() { function teardown_file() {
rm -f "${CONTAINER_START}" "${FILE}" "${SEDFILE}" docker rm -f "${CONTAINER}"
} }