views:

46

answers:

2

Hi,

I am looking for a way of programmatically testing a script written with the asyncore Python module. My test consists of launching the script in question -- if a TCP listen socket is opened, the test passes. Otherwise, if the script dies before getting to that point, the test fails.

The purpose of this is knowing if a nightly build works (at least up to a point) or not.

I was thinking the best way to test would be to launch the script in some kind of sandbox wrapper which waits for a socket request. I don't care about actually listening for anything on that port, just intercepting the request and using that as an indication that my test passed.

I think it would be preferable to intercept the open socket request, rather than polling at set intervals (I hate polling!). But I'm a bit out of my depths as far as how exactly to do this.

Can I do this with a shell script? Or perhaps I need to override the asyncore module at the Python level?

Thanks in advance,
- B

A: 

This felt like code-golf:

#!/bin/sh
# iamwaiting: run a command for a specified time then kill it
# returns the status of cmd on normal termination


opath=$PATH
PATH=/bin:/usr:/bin

SIGNAL=
case $1 in
    -*) SIGNAL=$1; shift;;
esac

case $# in
    0|1) echo 'usage iamwaiting [-signal] wait cmd [args]' 1>&2; exit 2;;
esac

wait=$1 ; shift

PATH=$opath "$@" &
child=$!

# unfortunately, this script takes at least wait seconds to run
sleep $wait

if kill ${SIGNAL:--2} $child 2> /dev/null ; then
    echo "iamwaiting timeout after $wait seconds: $@" 1>&2
    exit 1
else
    wait $child
    exit $?
fi

runs thusly:

$ iamwaiting  5 dd if=/dev/zero of=/dev/null count=5
5+0 records in
5+0 records out
2560 bytes (2.6 kB) copied, 2.0973e-05 s, 122 MB/s
$ echo $?
0
$ iamwaiting -9 5 dd if=/dev/zero of=/dev/null 
iamwaiting timeout after 5 seconds: dd if=/dev/zero of=/dev/null
$ echo $?
1
$ iamwaiting 1 cat /dev/does-not-exist
cat: /dev/does-not-exist: No such file or directory
$ echo $?
1

I'd be wary of testing asyncore programs with asynchore routines, but I'm superstitious that way.

msw
This is not bad, not bad at all. Though I think after the timeout, you want to `exit 0`, not `exit 1`, since timing out is the normal behaviour (the script just sits and waits for an incoming connection).Your script does have the limitation that a) it must run for `$wait` seconds (though you could probably get around this by catching `SIGCHLD`), and b) it assumes that the script in question will open the listening socket within a predictable delay.
BillyBBone
Alas, at least two shells (the somewhat retro `dash` which I use for purposes of broadest compatibility, and bash) swallow SIGCHLD for background processes. I'm afraid this is the best that the shell can do. But thanks for causing me to revisit the post, I found a one character buglette.
msw
A: 

Another option is to mock the socket module before importing the asyncore module. Of course, then you have to make sure that the mock works properly first.

Ignacio Vazquez-Abrams