cvmfs_test_name="Repository gateway - missing reflog"
cvmfs_test_autofs_on_startup=false
cvmfs_test_suites="quick"


clean_up() {
    echo "Cleaning up"
}

check_status() {
    echo $(( $1 || 0 ))
}

run_tests() {
    set_up_repository_gateway

    local test_repo="test.repo.org"

    echo "*** Check transaction with missing reflog"
    sudo mv /var/spool/cvmfs/${test_repo}/reflog.chksum /tmp
    cvmfs_server transaction ${test_repo}

    local output=$(cvmfs_server publish ${test_repo} 2>&1 | grep "missing_reflog")

    if [ x"$output" = x"" ]; then
        echo -n "Error: the missing reflog should have been reported "
        echo "as the cause of the publication error"

        cvmfs_server abort -f ${test_repo}

        return 1;
    fi

    echo "*** Aborting transaction"
    cvmfs_server abort -f ${test_repo}

    # Run repository check
    echo "*** Repository verification"
    cvmfs_server check ${test_repo}
    if [ $? -ne 0 ]; then
        echo "Repository integrity check failed with \"reflog.chksum\" present"
        return 1;
    fi

    # Move reflog.chksum out of the way to reproduce the environment of a
    # publisher running on a separate machine
    mv /var/spool/cvmfs/${test_repo}/reflog.chksum /tmp/

    # Re-run repository check
    cvmfs_server check ${test_repo}
    if [ $? -ne 0 ]; then
        echo "Repository integrity check failed with \"reflog.chksum\" missing"
        return 1;
    fi

    return 0;
}

cvmfs_run_test() {
    trap clean_up EXIT HUP INT TERM || return $?

    run_tests
    local status=$?

    return $(check_status $status)
}

