Skip to content

Commit

Permalink
tests/02-kvlds-basic.sh: add
Browse files Browse the repository at this point in the history
The old tests/kvlds/test_kvlds.sh is rewritten as:

- lines 20 - 62: kvlds_check_large()
- lines 64 - 74: kvlds_check_basic()
- lines 64 - 98: kvlds_check_repeated()
- lines 100 - 116: kvlds_check_unclean()
- lines 118 - 145: kvlds_check_killing()
- lines 147 - 165: kvlds_check_nontwo()

Lines 167 and up are using ktrace and kdump on FreeBSD; this is now
covered by the valgrind tests.
  • Loading branch information
gperciva committed Jan 18, 2024
1 parent 2c8644d commit dc1890b
Show file tree
Hide file tree
Showing 3 changed files with 364 additions and 0 deletions.
289 changes: 289 additions & 0 deletions tests/02-kvlds-basic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
#!/bin/sh

### Constants
c_valgrind_min=1
test_kvlds="${scriptdir}/kvlds/test_kvlds"
stor="${s_basename}-stor"
lbs_sock="${stor}/lbs_sock"
lbs_pidfile="${lbs_sock}.pid"
kvlds_sock="${stor}/kvlds_sock"
kvlds_pidfile="${kvlds_sock}.pid"
testkvlds_pidfile="${s_basename}-testkvlds.pid"

### lbs-specific constants
lbs_basic_read_delay=1000000

## clean_storage():
# Remove any old lbs storage directory.
clean_storage() {
rm -rf "${stor}"
}

_kvlds_check_basic() {
_kvlds_check_basic_single=$1
_kvlds_check_basic_description=$2

# Ensure that we're starting with a clean storage directory.
servers_check_leftover
clean_storage

# Start the servers.
lbs_start "${lbs_sock}" "${stor}" "0" \
"${lbs_pidfile}" "${lbs_basic_read_delay}" \
"lbs ${_kvlds_check_basic_description}"
kvlds_start "${kvlds_sock}" "${lbs_sock}" \
"${_kvlds_check_basic_single}" \
"${kvlds_pidfile}" "-v 104 -C 1024" "0" \
"kvlds ${_kvlds_check_basic_description}"

# Run test.
setup_check "test_kvlds ${_kvlds_check_basic_description}"
${c_valgrind_cmd} "${test_kvlds}" "${kvlds_sock}"
echo "$?" > "${c_exitfile}"

# Clean up
kvlds_stop "${kvlds_pidfile}" "${_kvlds_check_basic_single}"
lbs_stop "${lbs_pidfile}" "0"
clean_storage
}

## _lbs_old_blocks():
# Return 0 if there are more than 1 blocks in the LBS storage directory.
_lbs_old_blocks() {
_lbs_old_blocks_num=$(find "${stor}" -name "blks_*" | wc -l)

# Do we have more than 1 block?
test "${_lbs_old_blocks_num}" -gt 1
}

_kvlds_check_repeated() {
_kvlds_check_repeated_description=$1

# Ensure that we're starting with a clean storage directory.
servers_check_leftover
clean_storage

# Start the servers.
lbs_start "${lbs_sock}" "${stor}" "0" "${lbs_pidfile}" \
"${lbs_basic_read_delay}" \
"lbs ${_kvlds_check_repeated_description}"
kvlds_start "${kvlds_sock}" "${lbs_sock}" "0" \
"${kvlds_pidfile}" "-v 104 -C 1024" "0" \
"kvlds ${_kvlds_check_repeated_description}"

# Run test.
setup_check "test_kvlds ${_kvlds_check_repeated_description}"
${c_valgrind_cmd} "${test_kvlds}" "${kvlds_sock}"
echo "$?" > "${c_exitfile}"

# Check that lbs deleted all old blocks; wait up to 10 seconds.
setup_check "verify that old blocks got deleted"
wait_while 10000 _lbs_old_blocks
echo "$?" > "${c_exitfile}"

# Run tests twice
setup_check "test_kvlds ${_kvlds_check_repeated_description} twice 1"
${c_valgrind_cmd} "${test_kvlds}" "${kvlds_sock}"
echo "$?" > "${c_exitfile}"
setup_check "test_kvlds ${_kvlds_check_repeated_description} twice 2"
${c_valgrind_cmd} "${test_kvlds}" "${kvlds_sock}"
echo "$?" > "${c_exitfile}"

# Stop the kvlds server.
kvlds_stop "${kvlds_pidfile}" "0"

# Clean up
lbs_stop "${lbs_pidfile}" "0"
clean_storage
}

_kvlds_check_large() {
_kvlds_check_large_description=$1

# Ensure that we're starting with a clean storage directory.
servers_check_leftover
clean_storage

# Start lbs (with 512-byte blocks).
lbs_start "${lbs_sock}" "${stor}" "0" \
"${lbs_pidfile}" "${lbs_basic_read_delay}" \
"lbs ${_kvlds_check_large_description}"

# Try to start kvlds, but 96-byte keys are too large for lbs' blocks.
kvlds_start "${kvlds_sock}" "${lbs_sock}" "0" \
"${kvlds_pidfile}" "-k 94 -v 32" "1" \
"kvlds -k 96 ${_kvlds_check_large_description}"
rm "${kvlds_sock}"

# Try to start kvlds, but 200-byte values are too large for lbs' blocks.
kvlds_start "${kvlds_sock}" "${lbs_sock}" "0" \
"${kvlds_pidfile}" "-v 200" "1" \
"kvlds -v 200 ${_kvlds_check_large_description}"
rm "${kvlds_sock}"

# Start kvlds with 50-byte values; the server should be ok...
kvlds_start "${kvlds_sock}" "${lbs_sock}" "0" \
"${kvlds_pidfile}" "-v 50" "0" \
"kvlds -v 50 ${_kvlds_check_large_description}"
# ... but the test for 50-byte values should fail.
setup_check \
"test_kvlds ${_kvlds_check_large_description} 50-byte values"
${c_valgrind_cmd} "${test_kvlds}" "${kvlds_sock}" \
2> "${s_basename}-v50.stderr"
expected_exitcode 1 "$?" > "${c_exitfile}"
# Stop the kvlds server and delete its (defunct) socket.
kvlds_stop "${kvlds_pidfile}" "0"
rm "${kvlds_sock}"

# Start kvlds with 5-byte keys; the server should be ok...
kvlds_start "${kvlds_sock}" "${lbs_sock}" "0" \
"${kvlds_pidfile}" "-k 5" "0" \
"kvlds -k 5 ${_kvlds_check_large_description}"
# ... but the test for 5-byte keys should fail.
setup_check "test_kvlds ${_kvlds_check_large_description} 5-byte keys"
${c_valgrind_cmd} "${test_kvlds}" "${kvlds_sock}" \
2> "${s_basename}-k5.stderr"
expected_exitcode 1 "$?" > "${c_exitfile}"
# Stop the kvlds server and delete its (defunct) socket.
kvlds_stop "${kvlds_pidfile}" "0"
rm "${kvlds_sock}"

# Clean up.
lbs_stop "${lbs_pidfile}" "0"
clean_storage
}

_kvlds_check_unclean() {
_kvlds_check_unclean_description=$1

# Ensure that we're starting with a clean storage directory.
servers_check_leftover
clean_storage

# Start the servers.
lbs_start "${lbs_sock}" "${stor}" "0" "${lbs_pidfile}" \
"${lbs_basic_read_delay}" \
"lbs ${_kvlds_check_unclean_description}"
kvlds_start "${kvlds_sock}" "${lbs_sock}" "0" \
"${kvlds_pidfile}" "-v 104 -C 1024" "0" \
"kvlds ${_kvlds_check_unclean_description}"

# Check that an unclean disconnect is handled appropriately.
setup_check "test_kvlds ${_kvlds_check_unclean_description} clean"
# First attempt; unclean
( "${test_kvlds}" "${kvlds_sock}" & \
echo $! > "${testkvlds_pidfile}" ) 2>/dev/null
"${msleep}" 100 && kill "$(cat "${testkvlds_pidfile}")"
rm -f "${testkvlds_pidfile}"
# Second attempt; unclean
( "${test_kvlds}" "${kvlds_sock}" & \
echo $! > "${testkvlds_pidfile}" ) 2>/dev/null
"${msleep}" 100 && kill "$(cat "${testkvlds_pidfile}")"
rm -f "${testkvlds_pidfile}"
# Third attempt; clean
"${test_kvlds}" "${kvlds_sock}"
echo "$?" > "${c_exitfile}"

# Stop the kvlds server.
kvlds_stop "${kvlds_pidfile}" "0"

# Clean up
lbs_stop "${lbs_pidfile}" "0"
clean_storage
}

_kvlds_check_killing() {
_kvlds_check_killing_description=$1

# Ensure that we're starting with a clean storage directory.
servers_check_leftover
clean_storage

# Start the lbs server.
lbs_start "${lbs_sock}" "${stor}" "0" "${lbs_pidfile}" \
"${lbs_basic_read_delay}" \
"lbs ${_kvlds_check_killing_description}"

# Check that killing KVLDS can't break it.
for i in 1 2 3 ; do
# Begin the server...
kvlds_start "${kvlds_sock}" "${lbs_sock}" "0" \
"${kvlds_pidfile}" "-v 104" "0" \
"kvlds ${_kvlds_check_killing_description} $i"
# ... and test_kvlds...
"${test_kvlds}" "${kvlds_sock}"
# ... but kill the server.
"${msleep}" 100 && kill "$(cat "${kvlds_pidfile}")"
# Clean up.
rm -f "${kvlds_pidfile}"
rm -f "${kvlds_sock}"
done
# Final time; don't kill it.
kvlds_start "${kvlds_sock}" "${lbs_sock}" "0" \
"${kvlds_pidfile}" "-v 104" "0" \
"kvlds ${_kvlds_check_killing_description} kvlds"
setup_check "test_kvlds ${_kvlds_check_killing_description} after"
"${test_kvlds}" "${kvlds_sock}"
echo "$?" > "${c_exitfile}"

# Stop the kvlds server.
kvlds_stop "${kvlds_pidfile}" "0"

# Clean up
lbs_stop "${lbs_pidfile}" "0"
clean_storage
}

_kvlds_check_nontwo() {
_kvlds_check_nontwo_description=$1

# Ensure that we're starting with a clean storage directory.
servers_check_leftover
clean_storage

# Start LBS with non-power-of-2 byte pages.
for X in 1000 1023 1025 1100; do
# Start the lbs server.
lbs_start "${lbs_sock}" "${stor}" "0" "${lbs_pidfile}" \
"${lbs_basic_read_delay}" \
"lbs ${_kvlds_check_nontwo_description} ${X}" \
"${X}"
# Start the kvlds server.
kvlds_start "${kvlds_sock}" "${lbs_sock}" "0" \
"${kvlds_pidfile}" "-v 104 -C 1024" "0" \
"kvlds ${_kvlds_check_nontwo_description} ${X}"
# Test it.
setup_check \
"test_kvlds ${_kvlds_check_nontwo_description} ${X}" 0
"${test_kvlds}" "${kvlds_sock}"
echo "$?" > "${c_exitfile}"

# Stop the kvlds server.
kvlds_stop "${kvlds_pidfile}" "0"
# Clean up
lbs_stop "${lbs_pidfile}" "0"
clean_storage
done
}

### Actual command
scenario_cmd() {
# Check kvlds with the server in single-connection mode.
_kvlds_check_basic 1 "basic single"
_kvlds_check_basic 0 "basic non-single"

# Check by running the test twice.
_kvlds_check_repeated "repeated"

# Check with keys and values which are too large.
_kvlds_check_large "key & values too large"

# Check that an unclean disconnect is handled appropriately.
_kvlds_check_unclean "unclean"

# Check that killing kvlds can't break it.
_kvlds_check_killing "killing"

# Check with non-power-of-2 lbs byte pages.
_kvlds_check_nontwo "non-power-of-2"
}
74 changes: 74 additions & 0 deletions tests/kivaloo_servers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ servers_check_leftover() {
printf "Error: Left-over lbs server from previous run\n" 1>&2
exit 1
fi
if has_pid "kvlds/kvlds" ; then
printf "Error: Left-over kvlds server from previous run\n" 1>&2
exit 1
fi
}

## lbs_start(addr, storage_directory, is_single, pidfile, read_delay,
Expand Down Expand Up @@ -96,3 +100,73 @@ lbs_stop() {
wait_while 0 valgrind_incomplete
fi
}

## kvlds_start(addr, lbs_addr, is_single, pidfile, extraargs,
# expected_exitcode, description):
# Start a new kvlds server on ${addr}, using ${lbs_addr} as the lbs backend,
# storing the pid in ${pidfile}. If ${is_single} is greater than 0, the
# server should exit after handling a single connection, and valgrind should
# not be used, even if ${USE_VALGRIND} would otherwise indicate that we
# should. Pass ${extraargs} to the kvlds binary. The kvlds server should
# exit with ${expected_exitcode}. Use ${description} for the test framework.
kvlds_start() {
_kvlds_start_addr=$1
_kvlds_start_lbs_addr=$2
_kvlds_start_is_single=$3
_kvlds_start_pidfile=$4
_kvlds_start_extraargs=$5
_kvlds_start_expected_exitcode=$6
_kvlds_start_description=$7

# Set up check-specific variables; don't check for a previous exit file.
setup_check "${_kvlds_start_description}" 0
_kvlds_start_stderr="${s_basename}-${c_count_str}-kvlds.stderr"

# How to start the server.
_kvlds_start_cmd="${kvlds} -s ${_kvlds_start_addr} \
-l ${_kvlds_start_lbs_addr} -p ${_kvlds_start_pidfile} \
${_kvlds_start_extraargs}"

# Add -1 to exit after a single connection (if applicable).
if [ "${_kvlds_start_is_single}" -gt "0" ]; then
_kvlds_start_cmd="${_kvlds_start_cmd} -1"
fi

# Only use valgrind if it's a single-shot server.
if [ "${_kvlds_start_is_single}" -gt "0" ]; then
_kvlds_start_cmd="${c_valgrind_cmd} ${_kvlds_start_cmd}"
fi

# Start the server.
${_kvlds_start_cmd} 2> "${_kvlds_start_stderr}"
expected_exitcode "${_kvlds_start_expected_exitcode}" "$?" \
> "${c_exitfile}"

# Clean up zero-byte stderr, if applicable.
if [ ! -s "${_kvlds_start_stderr}" ]; then
rm -f "${_kvlds_start_stderr}"
fi
}

## kvlds_stop(pidfile, is_single):
# Wait for a kvlds server to stop. If it's not a single-shot server, kill it.
kvlds_stop() {
_kvlds_stop_pidfile=$1
_kvlds_stop_is_single=$2

# If we're not using a single-shot server and we have a pidfile,
# then kill the server.
if [ "${_kvlds_stop_is_single}" -eq "0" ] && \
[ -e "${_kvlds_stop_pidfile}" ]; then
kill "$(cat "${_kvlds_stop_pidfile}")"
fi
rm -f "${_kvlds_stop_pidfile}"

# Wait for kvlds server to stop.
wait_while 0 has_pid "kvlds/kvlds"

# Give valgrind a chance to finish writing files.
if [ -n "${c_valgrind_cmd}" ]; then
wait_while 0 valgrind_incomplete
fi
}
1 change: 1 addition & 0 deletions tests/test_kivaloo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ out_valgrind="${bindir}/tests-valgrind"

msleep="${bindir}/tests/msleep/msleep"
lbs="${bindir}/lbs/lbs"
kvlds="${bindir}/kvlds/kvlds"

# Functions to help start and stop servers
. "${scriptdir}/kivaloo_servers.sh"
Expand Down

0 comments on commit dc1890b

Please sign in to comment.