Skip to content

Commit

Permalink
add tests for reconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
lins05 committed Feb 27, 2015
1 parent a1112a1 commit d6c9694
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
language: python
python: 2.7
before_install:
- cp scripts/slackbot-test-ctl /usr/local/bin
- slackbot-test-ctl init
- slackbot-test-ctl startproxy
install: pip install -r requirements.txt
script: py.test -s -vv
deploy:
Expand Down
60 changes: 60 additions & 0 deletions scripts/slackbot-test-ctl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

set -x -e

ssconfig=/tmp/config.json
tsocksconfig=/tmp/tsocks.conf
export TSOCKS_CONF_FILE=$tsocksconfig

init_tsocks() {
cat >$ssconfig <<EOF
{
"server":"127.0.0.1",
"server_port":8800,
"local_address": "127.0.0.1",
"local_port":1800,
"password":"password",
"timeout":600,
"method":"aes-256-cfb"
}
EOF

cat >$tsocksconfig <<EOF
local = 127.0.0.0/255.0.0.0
server = 127.0.0.1
server_type = 5
server_port = 1800
EOF
}

main() {
local action=$1; shift
case $action in
init)
which sslocal || sudo pip install shadowsocks
which tsocks || sudo apt-get install tsocks
init_tsocks
;;
startproxy)
pgrep -f "sslocal -c $ssconfig" || {
sslocal -c $ssconfig 1>&2 2>/tmp/sslocal.log &
}
pgrep -f "ssserver -c $ssserver" || {
ssserver -c $ssconfig 1>&2 2>/tmp/ssserver.log &
}
;;
stopproxy)
pkill -f "sslocal -c $ssconfig"
pkill -f "ssserver -c $ssconfig"
;;
run)
tsocks "$@"
;;
*)
echo "WARNING: unknown command $action"
exit 1
;;
esac
}

main "$@"
12 changes: 10 additions & 2 deletions tests/functional/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ def start(self):
self._join_test_channel()

def wait_for_bot_online(self):
self._wait_for_bot_presense(True)

def wait_for_bot_offline(self):
self._wait_for_bot_presense(False)

def _wait_for_bot_presense(self, online):
for _ in xrange(10):
time.sleep(2)
if self._is_testbot_online():
if online and self._is_testbot_online():
break
if not online and not self._is_testbot_online():
break
else:
raise AssertionError('test bot is not online')
raise AssertionError('test bot is still %s' % ('offline' if online else 'online'))

def send_direct_message(self, msg):
self._send_message_to_bot(self.dm_chan, msg)
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import os
import time
import subprocess
import pytest
from os.path import join, abspath, dirname, basename
Expand All @@ -14,12 +15,21 @@
driver_apitoken, driver_username, test_channel
)

TRAVIS = 'TRAVIS' in os.environ

def stop_proxy():
os.system('slackbot-test-ctl stopproxy')

def start_proxy():
os.system('slackbot-test-ctl startproxy')

def _start_bot_process():
args = [
'python',
'run.py',
]
if TRAVIS:
args = ['slackbot-test-ctl', 'run'] + args
env = dict(os.environ)
env['SLACKBOT_API_TOKEN'] = testbot_apitoken
env['SLACKBOT_TEST'] = '1'
Expand Down Expand Up @@ -68,3 +78,12 @@ def test_bot_reply_to_channel_message(driver):
def test_bot_ignores_non_related_channel_message(driver):
driver.send_channel_message('hello', tobot=False)
driver.ensure_no_channel_reply_from_bot()

@pytest.mark.skipif(not TRAVIS, reason="only run reconnect test travis") # pylint: disable=E1101
def test_bot_reconnect(driver):
driver.wait_for_bot_online()
stop_proxy()
driver.wait_for_bot_offline()
start_proxy()
driver.wait_for_bot_online()
test_bot_respond_to_simple_message(driver)

0 comments on commit d6c9694

Please sign in to comment.