Browse Source

add unit test for daemon

auth
clowwindy 10 years ago
parent
commit
28347b685e
  1. 1
      .travis.yml
  2. 10
      shadowsocks/daemon.py
  3. 42
      tests/test_daemon.sh

1
.travis.yml

@ -18,6 +18,7 @@ script:
- pyflakes . - pyflakes .
- coverage run tests/nose_plugin.py -v - coverage run tests/nose_plugin.py -v
- python setup.py sdist - python setup.py sdist
- tests/test_daemon.sh
- python tests/test.py --with-coverage -c tests/aes.json - python tests/test.py --with-coverage -c tests/aes.json
- python tests/test.py --with-coverage -c tests/aes-ctr.json - python tests/test.py --with-coverage -c tests/aes-ctr.json
- python tests/test.py --with-coverage -c tests/aes-cfb1.json - python tests/test.py --with-coverage -c tests/aes-cfb1.json

10
shadowsocks/daemon.py

@ -105,11 +105,14 @@ def daemon_start(pid_file, log_file):
assert pid != -1 assert pid != -1
def handle_exit(signum, _): def handle_exit(signum, _):
if signum == signal.SIGTERM:
sys.exit(0) sys.exit(0)
sys.exit(1)
if pid > 0: if pid > 0:
# parent waits for its child # parent waits for its child
signal.signal(signal.SIGINT, handle_exit) signal.signal(signal.SIGINT, handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
time.sleep(5) time.sleep(5)
sys.exit(0) sys.exit(0)
@ -121,11 +124,16 @@ def daemon_start(pid_file, log_file):
sys.exit(1) sys.exit(1)
print('started') print('started')
os.kill(ppid, signal.SIGINT) os.kill(ppid, signal.SIGTERM)
sys.stdin.close() sys.stdin.close()
try:
freopen(log_file, 'a', sys.stdout) freopen(log_file, 'a', sys.stdout)
freopen(log_file, 'a', sys.stderr) freopen(log_file, 'a', sys.stderr)
except IOError as e:
logging.error(e)
os.kill(ppid, signal.SIGINT)
sys.exit(1)
def daemon_stop(pid_file): def daemon_stop(pid_file):

42
tests/test_daemon.sh

@ -0,0 +1,42 @@
#!/bin/bash
function test {
expected=$1
shift
echo "running test: $command $@"
$command $@
status=$?
if [ $status -ne $expected ]; then
echo "exit $status != $expected"
exit 1
fi
echo "exit status $status == $expected"
echo OK
return
}
for module in local server
do
command="coverage run -p -a shadowsocks/$module.py"
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 0 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 0 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 1 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 0 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 0 -c tests/aes.json -d restart --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 0 -c tests/aes.json -d restart --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 0 -c tests/aes.json -d stop --pid-file /tmp/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 1 -c tests/aes.json -d start --pid-file /tmp/not_exist/shadowsocks.pid --log-file /tmp/shadowsocks.log
test 1 -c tests/aes.json -d start --pid-file /tmp/shadowsocks.pid --log-file /tmp/not_exist/shadowsocks.log
done
Loading…
Cancel
Save