From 28347b685e3e38fdf68aa88732f77e840d4e5c2d Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sun, 21 Dec 2014 13:46:27 +0800 Subject: [PATCH] add unit test for daemon --- .travis.yml | 1 + shadowsocks/daemon.py | 16 ++++++++++++---- tests/test_daemon.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100755 tests/test_daemon.sh diff --git a/.travis.yml b/.travis.yml index 9d7a9bb..1ceac69 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,7 @@ script: - pyflakes . - coverage run tests/nose_plugin.py -v - 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-ctr.json - python tests/test.py --with-coverage -c tests/aes-cfb1.json diff --git a/shadowsocks/daemon.py b/shadowsocks/daemon.py index d4320e0..2eedf03 100644 --- a/shadowsocks/daemon.py +++ b/shadowsocks/daemon.py @@ -105,11 +105,14 @@ def daemon_start(pid_file, log_file): assert pid != -1 def handle_exit(signum, _): - sys.exit(0) + if signum == signal.SIGTERM: + sys.exit(0) + sys.exit(1) if pid > 0: # parent waits for its child signal.signal(signal.SIGINT, handle_exit) + signal.signal(signal.SIGTERM, handle_exit) time.sleep(5) sys.exit(0) @@ -121,11 +124,16 @@ def daemon_start(pid_file, log_file): sys.exit(1) print('started') - os.kill(ppid, signal.SIGINT) + os.kill(ppid, signal.SIGTERM) sys.stdin.close() - freopen(log_file, 'a', sys.stdout) - freopen(log_file, 'a', sys.stderr) + try: + freopen(log_file, 'a', sys.stdout) + 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): diff --git a/tests/test_daemon.sh b/tests/test_daemon.sh new file mode 100755 index 0000000..b05208d --- /dev/null +++ b/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