#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: Check univention-samba4-backup warnings are suppressed.
## exposure: dangerous
## bugs: [35907]
## packages:
## - univention-samba4
## roles:
## - domaincontroller_master
## - domaincontroller_backup
## - domaincontroller_slave

LANG=C

# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137

function test_backup {
    local output=$(univention-samba4-backup $@ 2>&1)
    local retval=$?

    if [ $retval -ne 0 ]; then
        echo -e "$output"
        fail_test $retval "univention-samba4-backup exited with code $retval"
    fi

    # grep output for messages apeared in Bug #35392:
    local errors=$(echo "$output" | grep -i -e "socket ignored" \
                                         -e "file changed as we read it")

    if [ -n "$errors" ]; then
        echo -e "$errors"
        fail_test 1 "univention-samba4-backup produced one of the messages that should be suppressed."
    fi
}

# The bug described an error with `tar` while files were modified. This
# simulates the case.
function start_modification {
    local base_dir=$1
    local test_file=$(tempfile -d "$base_dir")
    #`dd` is made as slow as possible to maximize the change of triggering the
    # bug. This creates a 10mb file.
    dd if=/dev/zero of="$test_file" bs=1 count=10485760 2> /dev/null
    rm -f "$test_file"
}

# A basic test as a baseline. This should not fail.
test_backup

# The actual test with concurrent modification of the working directories.
start_modification /var/lib/samba/private/ &
start_modification /var/lib/samba/sysvol/ &
test_backup

wait
exit $RETVAL
