test/unit: add a library for common functions

I placed the library in the top-level test directory, as the same
assertions could also be used by the integration tests once we rework
them a bit.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
This commit is contained in:
Josh Poimboeuf 2018-04-20 07:25:10 -05:00
parent d6dfa4a992
commit 4c1184dcf7
18 changed files with 54 additions and 85 deletions

12
test/test-functions.sh Normal file
View File

@ -0,0 +1,12 @@
FILE=$1
assert_num_funcs() {
local num_funcs=$(nm $FILE | grep -i " t " | wc -l)
if [[ $num_funcs != $1 ]]; then
echo "$FILE: assertion failed: file has $num_funcs funcs, expected $1" 1>&2
exit 1
fi
return 0
}

View File

@ -11,10 +11,12 @@ MUTE_PASS := >/dev/null
MUTE_FAIL := >/dev/null 2>&1
endif
SRC_PATH ?= $(realpath ../../../)
CDO ?= $(SRC_PATH)/kpatch-build/create-diff-object
SRC_PATH ?= $(realpath ../../../)
CDO ?= $(SRC_PATH)/kpatch-build/create-diff-object
TEST_LIBRARY ?= $(SRC_PATH)/test/test-functions.sh
TENV = PARA_STRUCT_SIZE=16 EX_STRUCT_SIZE=12 BUG_STRUCT_SIZE=12 ALT_STRUCT_SIZE=13
CDO_ENV = PARA_STRUCT_SIZE=16 EX_STRUCT_SIZE=12 BUG_STRUCT_SIZE=12 ALT_STRUCT_SIZE=13
TEST_ENV = KPATCH_TEST_LIBRARY=$(TEST_LIBRARY)
TARGETS = $(patsubst %.$(EXT_ORIG),%.$(EXT_OUTPUT),$(wildcard *.$(EXT_ORIG)))
TEST_TARGETS = $(patsubst %.$(EXT_TEST),%.$(EXT_TEST_OUTPUT),$(wildcard *.$(EXT_TEST)))
@ -41,21 +43,21 @@ all: $(TARGETS) $(TEST_TARGETS)
clean:
rm -f *.$(EXT_TEST_OUTPUT) *.$(EXT_OUTPUT)
%.$(EXT_TEST_OUTPUT): %.$(EXT_OUTPUT) %.$(EXT_TEST)
bash $(@:.$(EXT_TEST_OUTPUT)=.$(EXT_TEST)) $<
%.$(EXT_TEST_OUTPUT): %.$(EXT_OUTPUT) %.$(EXT_TEST) $(TEST_LIBRARY)
$(TEST_ENV) bash $(@:.$(EXT_TEST_OUTPUT)=.$(EXT_TEST)) $<
# Don't rely on script creating this
@touch $@
%.$(EXT_OUTPUT): %.$(EXT_ORIG) %.$(EXT_PATCHED) $(CDO)
$(call check_all,$(TNAME).$(EXT_ORIG))
$(call check_all,$(TNAME).$(EXT_PATCHED))
$(TENV) $(CDO) $(TNAME).$(EXT_ORIG) $(TNAME).$(EXT_PATCHED) \
$(CDO_ENV) $(CDO) $(TNAME).$(EXT_ORIG) $(TNAME).$(EXT_PATCHED) \
$(TNAME).$(EXT_ORIG) $@ /dev/null test_$(TNAME) $(MUTE_PASS)
%.$(EXT_OUTPUT): %.$(EXT_ORIG) %.$(EXT_FAIL) $(CDO)
$(call check_all,$(TNAME).$(EXT_ORIG))
$(call check_all,$(TNAME).$(EXT_FAIL))
! $(TENV) $(CDO) $(TNAME).$(EXT_ORIG) $(TNAME).$(EXT_FAIL) \
! $(CDO_ENV) $(CDO) $(TNAME).$(EXT_ORIG) $(TNAME).$(EXT_FAIL) \
$(TNAME).$(EXT_ORIG) $@ /dev/null test_$(TNAME) $(MUTE_FAIL)
# Expecting to fail, thus create output file manually so we won't rerun the
# test without clean

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0
assert_num_funcs 1

View File

@ -1,9 +1,9 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
assert_num_funcs 1
bind=$(nm $1 |awk '/__kmalloc/ {print $2}')
[[ $bind = 't' ]] || exit 1
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0
assert_num_funcs 1

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0
assert_num_funcs 1

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0
assert_num_funcs 1

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 2 ]] || exit 1
exit 0
assert_num_funcs 2

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0
assert_num_funcs 1

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 2 ]] || exit 1
exit 0
assert_num_funcs 2

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 5 ]] || exit 1
exit 0
assert_num_funcs 5

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0
assert_num_funcs 1

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 2 ]] || exit 1
exit 0
assert_num_funcs 2

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0
assert_num_funcs 1

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0
assert_num_funcs 1

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 2 ]] || exit 1
exit 0
assert_num_funcs 2

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 2 ]] || exit 1
exit 0
assert_num_funcs 2

View File

@ -1,7 +1,4 @@
#!/bin/bash
source $KPATCH_TEST_LIBRARY
num_funcs=$(nm $1 | grep -i " t " | wc -l)
[[ $num_funcs = 1 ]] || exit 1
exit 0
assert_num_funcs 1