Skip to content

Commit

Permalink
tests: Use a function to wait for pid file to appear
Browse files Browse the repository at this point in the history
Introduce wait_for_file function to wait for a file for a max. amount of
time and have that function poll for the file to appear.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
  • Loading branch information
stefanberger committed Jun 26, 2016
1 parent 01eb4d3 commit 70f3e24
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions tests/Makefile.am
Expand Up @@ -51,5 +51,6 @@ EXTRA_DIST=$(TESTS) \
data/tpmstate2/tpm-00.volatilestate \
data/tpmstate2/pwdfile.txt \
data/tpmstate2/README \
test_common \
test_cuse \
test_swtpm_cert
17 changes: 17 additions & 0 deletions tests/test_common
@@ -0,0 +1,17 @@

# For the license, see the LICENSE file in the root directory.

function wait_for_file()
{
local filename="$1"
local timeout="$2"

local loops=$((timeout * 10)) loop

for ((loop=0; loop<loops; loop++)); do
[ -f "${filename}" ] && return 1
sleep 0.1
done
return 0
}

14 changes: 6 additions & 8 deletions tests/test_ctrlchannel
Expand Up @@ -12,6 +12,8 @@ SOCK_PATH=$TPMDIR/sock
CMD_PATH=$TPMDIR/cmd
RESP_PATH=$TPMDIR/resp

source ${DIR}/test_common

trap "cleanup" SIGTERM EXIT

function cleanup()
Expand All @@ -27,9 +29,8 @@ function cleanup()
# use a pseudo terminal
exec 100<>/dev/ptmx
$SWTPM_EXE chardev --fd 100 --tpmstate dir=$TPMDIR --pid file=$PID_FILE --ctrl type=unixio,path=$SOCK_PATH &
sleep 0.5

if [ ! -r $PID_FILE ]; then
if wait_for_file $PID_FILE 3; then
echo "Error: Chardev TPM did not write pidfile."
exit 1
fi
Expand Down Expand Up @@ -163,9 +164,8 @@ echo "OK"

# use a pseudo terminal
$SWTPM_EXE socket --server port=65530,disconnect=true --tpmstate dir=$TPMDIR --pid file=$PID_FILE --ctrl type=unixio,path=$SOCK_PATH &
sleep 0.5

if [ ! -r $PID_FILE ]; then
if wait_for_file $PID_FILE 3; then
echo "Error: Socket TPM did not write pidfile."
exit 1
fi
Expand Down Expand Up @@ -464,9 +464,8 @@ $SWTPM_EXE socket \
--pid file=$PID_FILE \
--ctrl type=unixio,path=$SOCK_PATH \
--key pwdfile=${PWD}/${DIR}/data/tpmstate2/pwdfile.txt &
sleep 0.5

if [ ! -r $PID_FILE ]; then
if wait_for_file $PID_FILE 3; then
echo "Error: Socket TPM did not write pidfile."
exit 1
fi
Expand Down Expand Up @@ -538,9 +537,8 @@ $SWTPM_EXE socket \
--pid file=$PID_FILE \
--ctrl type=unixio,path=$SOCK_PATH \
--key pwdfile=${PWD}/${DIR}/data/tpmstate2/pwdfile.txt &
sleep 0.5

if [ ! -r $PID_FILE ]; then
if wait_for_file $PID_FILE 3; then
echo "Error: Socket TPM did not write pidfile."
exit 1
fi
Expand Down
14 changes: 6 additions & 8 deletions tests/test_ctrlchannel2
Expand Up @@ -14,6 +14,8 @@ CMD_PATH=$TPMDIR/cmd
RESP_PATH=$TPMDIR/resp
VOLATILESTATE=$TPMDIR/volatile

source ${DIR}/test_common

trap "cleanup" SIGTERM EXIT

function cleanup()
Expand All @@ -29,9 +31,8 @@ function cleanup()
# use a pseudo terminal
exec 100<>/dev/ptmx
$SWTPM_EXE chardev --fd 100 --tpmstate dir=$TPMDIR --pid file=$PID_FILE --ctrl type=unixio,path=$SOCK_PATH &
sleep 0.5

if [ ! -r $PID_FILE ]; then
if wait_for_file $PID_FILE 3; then
echo "Error: Chardev TPM did not write pidfile."
exit 1
fi
Expand Down Expand Up @@ -118,9 +119,8 @@ echo "OK"

# use a pseudo terminal
$SWTPM_EXE socket --server port=65531,disconnect=true --tpmstate dir=$TPMDIR --pid file=$PID_FILE --ctrl type=unixio,path=$SOCK_PATH &
sleep 0.5

if [ ! -r $PID_FILE ]; then
if wait_for_file $PID_FILE 3; then
echo "Error: Socket TPM did not write pidfile."
exit 1
fi
Expand Down Expand Up @@ -312,9 +312,8 @@ $SWTPM_EXE socket \
--pid file=$PID_FILE \
--ctrl type=unixio,path=$SOCK_PATH \
--key pwdfile=${PWD}/${DIR}/data/tpmstate2/pwdfile.txt &
sleep 0.5

if [ ! -r $PID_FILE ]; then
if wait_for_file $PID_FILE 3; then
echo "Error: Socket TPM did not write pidfile."
exit 1
fi
Expand Down Expand Up @@ -370,9 +369,8 @@ $SWTPM_EXE socket \
--pid file=$PID_FILE \
--ctrl type=unixio,path=$SOCK_PATH \
--key pwdfile=${PWD}/${DIR}/data/tpmstate2/pwdfile.txt &
sleep 0.5

if [ ! -r $PID_FILE ]; then
if wait_for_file $PID_FILE 3; then
echo "Error: Socket TPM did not write pidfile."
exit 1
fi
Expand Down

0 comments on commit 70f3e24

Please sign in to comment.