diff --git a/Makefile b/Makefile index 88c8403a..1de157ea 100755 --- a/Makefile +++ b/Makefile @@ -26,6 +26,9 @@ install-man: clean: bin-clean +test: + $(MAKE) -C tests + TAGS: .PHONY @(if which etags >/dev/null 2>&1 ; then \ echo "Using etags to generate $@" ; \ diff --git a/tests/0000-unit.sh b/tests/0000-unit.sh new file mode 100755 index 00000000..8e63aa8c --- /dev/null +++ b/tests/0000-unit.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# + +# Run builtin unittests +# + +set -e +source helpers.sh + +$KAFKACAT -U diff --git a/tests/0001-exit_eof.sh b/tests/0001-exit_eof.sh new file mode 100755 index 00000000..8da991cd --- /dev/null +++ b/tests/0001-exit_eof.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# + +set -e + +source helpers.sh + + +# +# Verify that -e (exit on eof) works on both low-level and high-level consumer +# + + +topic=$(make_topic_name) + +# Produce some messages to topic +info "Priming producer for $topic" +seq 1 10 | $KAFKACAT -t $topic + + +# Legacy: Consume messages without -e, should timeout. +info "Legacy consumer without -e" +timeout 5 $KAFKACAT -t $topic -o beginning && \ + FAIL "Legacy consumer without -e should have timed out" + + +# Balanced: Consume messages without -e, should timeout. +info "Balanced consumer without -e" +timeout 10 $KAFKACAT -G $topic -o beginning $topic && \ + "Balanced consumer without -e should have timed out" + + +# Legacy: Consume messages with -e, must not timeout +info "Legacy consumer with -e" +timeout 5 $KAFKACAT -t $topic -o beginning -e || \ + FAIL "Legacy consumer with -e timed out" + + +# Balanced: Consume messages without -e, should timeout. +# FIXME: -e and -G doesn't currently work. +if false; then + info "Balanced consumer with -e" + timeout 10 $KAFKACAT -G ${topic}_new -o beginning -e $topic || \ + FAIL "Balanced consumer with -e timed out" +fi + +PASS + + + diff --git a/tests/0002-delim.sh b/tests/0002-delim.sh new file mode 100755 index 00000000..1e9a5ac6 --- /dev/null +++ b/tests/0002-delim.sh @@ -0,0 +1,62 @@ +#!/bin/bash +# + +set -e +source helpers.sh + + +# +# Verify that single and multi-byte delimiters work. +# + + +topic=$(make_topic_name) + + + +# Multi-byte delimiters, partition 0 + +echo -n "Key1;KeyDel;Value1:MyDilemma::MyDilemma:;KeyDel;Value2:MyDilemma:Key3;KeyDel;:MyDilemma:Value4" | + $KAFKACAT -t $topic -p 0 -K ';KeyDel;' -D ':MyDilemma:' -Z + + +output=$($KAFKACAT -C -t $topic -p 0 -o beginning -e -J | + jq -r '.key + "=" + .payload') + +exp="Key1=Value1 +=Value2 +Key3= +=Value4" + +if [[ $output != $exp ]]; then + echo "FAIL: Expected '$exp', not '$output'" + exit 1 +fi + + +# +# Single-byte delimiters, partition 1 +# + +echo "The First;Message1 + +Is The; +;Greatest +For sure" | + $KAFKACAT -t $topic -p 1 -K ';' -Z + + +output=$($KAFKACAT -C -t $topic -p 1 -o beginning -e -J | + jq -r '.key + "=" + .payload') + +exp="The First=Message1 +Is The= +=Greatest +=For sure" + +if [[ $output != $exp ]]; then + echo "FAIL: Expected '$exp', not '$output'" + exit 1 +fi + + diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 00000000..a3fb24d0 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,11 @@ + + +all: run + + +run: + @(for test in 0[0-9][0-9][0-9]*.sh ; do \ + ( ./$$test && \ + echo "=== Test $$test PASSED ===" ) || \ + (echo "=== Test $$test FAILED ===" ; exit 1) ;\ + done) diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..ecbdd036 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,6 @@ +These tests assume a cluster is running and the bootstrap.servers +is set in the $BROKERS environment variable. + +The cluster is assumed to have auto topic creation enabled with +a default partition count of at least 3. + diff --git a/tests/helpers.sh b/tests/helpers.sh new file mode 100644 index 00000000..9ecc9603 --- /dev/null +++ b/tests/helpers.sh @@ -0,0 +1,37 @@ +set -e + +CLR_BGRED="\033[37;41m" +CLR_BGGREEN="\033[37;42m" +CLR_INFO="\033[34m" +CLR="\033[0m" + +if [[ -z "$BROKERS" ]]; then + echo -e "${CLR_BGRED}kafkacat tests requires \$BROKERS to be set${CLR}" + exit 1 +fi + +KAFKACAT="../kafkacat -b $BROKERS" +TEST_NAME=$(basename $0 | sed -e 's/\.sh$//') + +function make_topic_name { + local name=$1 + echo "kafkacat_test_$$_${RANDOM}_${TEST_NAME}name" +} + + + +function info { + local str=$1 + echo -e "${CLR_INFO}${TEST_NAME} | $str${CLR}" +} + +function FAIL { + local str=$1 + echo -e "${CLR_BGRED}${TEST_NAME} | TEST FAILED: $str${CLR}" + exit 1 +} + +function PASS { + local str=$1 + echo -e "${CLR_BGGREEN}${TEST_NAME} | TEST PASSED: $str${CLR}" +}