Add E2E test
This commit is contained in:
parent
a6de322a00
commit
585efb2c18
|
@ -199,6 +199,7 @@ class IRCCore(object):
|
||||||
'255',
|
'255',
|
||||||
'265',
|
'265',
|
||||||
'266',
|
'266',
|
||||||
|
'331',
|
||||||
'332',
|
'332',
|
||||||
'333',
|
'333',
|
||||||
'353',
|
'353',
|
||||||
|
@ -206,6 +207,7 @@ class IRCCore(object):
|
||||||
'372',
|
'372',
|
||||||
'375',
|
'375',
|
||||||
'376',
|
'376',
|
||||||
|
'401',
|
||||||
'422',
|
'422',
|
||||||
'433',
|
'433',
|
||||||
]
|
]
|
||||||
|
|
|
@ -231,6 +231,7 @@ class PyIRCBot(PrimitiveBot):
|
||||||
self.loop = asyncio.get_event_loop()
|
self.loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
"""Reference to BotRPC thread"""
|
"""Reference to BotRPC thread"""
|
||||||
|
if self.botconfig["bot"]["rpcport"] >= 0:
|
||||||
self.rpc = BotRPC(self)
|
self.rpc = BotRPC(self)
|
||||||
|
|
||||||
ratelimit = self.botconfig["connection"].get("rate_limit", None) or dict(rate_max=5.0, rate_int=1.1)
|
ratelimit = self.botconfig["connection"].get("rate_limit", None) or dict(rate_max=5.0, rate_int=1.1)
|
||||||
|
|
|
@ -3,4 +3,4 @@
|
||||||
export PYTHONUNBUFFERED=1
|
export PYTHONUNBUFFERED=1
|
||||||
export PYTHONPATH=.
|
export PYTHONPATH=.
|
||||||
|
|
||||||
py.test --cov=pyircbot --cov-report html -s tests/
|
py.test --cov=pyircbot --cov-report html tests/
|
||||||
|
|
42
tests/lib.py
42
tests/lib.py
|
@ -1,9 +1,12 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import pytest
|
import pytest
|
||||||
|
from random import randint
|
||||||
|
from threading import Thread
|
||||||
from pyircbot.pyircbot import PrimitiveBot
|
from pyircbot.pyircbot import PrimitiveBot
|
||||||
from pyircbot.irccore import IRCEvent, UserPrefix
|
from pyircbot.irccore import IRCEvent, UserPrefix
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
from tests.miniircd import Server as MiniIrcServer
|
||||||
|
|
||||||
|
|
||||||
sys.path.append(os.path.join(os.path.dirname(__file__), "../pyircbot/modules/"))
|
sys.path.append(os.path.join(os.path.dirname(__file__), "../pyircbot/modules/"))
|
||||||
|
@ -36,6 +39,43 @@ class FakeBaseBot(PrimitiveBot):
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def fakebot():
|
def fakebot():
|
||||||
|
# TODO copy data tree to isolated place so each fakebot() is isolated
|
||||||
bot = FakeBaseBot({"bot": {"datadir": "./examples/data/"},
|
bot = FakeBaseBot({"bot": {"datadir": "./examples/data/"},
|
||||||
"module_configs": {}})
|
"module_configs": {}})
|
||||||
return bot
|
yield bot
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def ircserver():
|
||||||
|
"""
|
||||||
|
Fixture providing an isolated IRC server.
|
||||||
|
|
||||||
|
:return: tuple of (port, server_object)
|
||||||
|
"""
|
||||||
|
port = randint(40000, 65000)
|
||||||
|
|
||||||
|
class IRCOptions(object):
|
||||||
|
channel_log_dir = None
|
||||||
|
chroot = None
|
||||||
|
daemon = None
|
||||||
|
debug = None
|
||||||
|
ipv6 = None
|
||||||
|
listen = "127.0.0.1"
|
||||||
|
log_count = 10
|
||||||
|
log_file = None
|
||||||
|
log_max_size = 10
|
||||||
|
motd = None
|
||||||
|
password = None
|
||||||
|
password_file = None
|
||||||
|
pid_file = None
|
||||||
|
ports = [port]
|
||||||
|
setuid = None
|
||||||
|
ssl_pem_file = None
|
||||||
|
state_dir = None
|
||||||
|
verbose = None
|
||||||
|
|
||||||
|
server = MiniIrcServer(IRCOptions)
|
||||||
|
server_t = Thread(target=server.start, daemon=True)
|
||||||
|
server_t.start()
|
||||||
|
yield port, server
|
||||||
|
server.stop()
|
||||||
|
|
|
@ -1080,5 +1080,6 @@ def main(argv):
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
server.print_error("Interrupted.")
|
server.print_error("Interrupted.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main(sys.argv)
|
main(sys.argv)
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
import pytest
|
||||||
|
from tests.lib import * # NOQA - fixtures
|
||||||
|
from time import sleep, time
|
||||||
|
from pyircbot import IRCCore
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
logging.getLogger().setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
|
||||||
|
def wait_until(server, channel, nick, func, timeout):
|
||||||
|
start = time()
|
||||||
|
while time() < start + timeout:
|
||||||
|
v = func()
|
||||||
|
if v:
|
||||||
|
return v
|
||||||
|
sleep(0.02)
|
||||||
|
raise Exception("Function {} did not settle after {}s".format(func, timeout))
|
||||||
|
|
||||||
|
|
||||||
|
def wait_until_joined(server, channel, nick, timeout=5.0):
|
||||||
|
def in_ch():
|
||||||
|
if channel in server.channels:
|
||||||
|
members = [u.nickname for u in server.channels[channel].members]
|
||||||
|
if nick in members:
|
||||||
|
return members
|
||||||
|
return wait_until(server, channel, nick, in_ch, timeout=timeout)
|
||||||
|
|
||||||
|
|
||||||
|
def wait_until_absent(server, channel, nick, timeout=5.0):
|
||||||
|
def not_in_ch():
|
||||||
|
if channel not in server.channels:
|
||||||
|
return True
|
||||||
|
members = [u.nickname for u in server.channels[channel].members]
|
||||||
|
if nick not in members:
|
||||||
|
return True
|
||||||
|
return wait_until(server, channel, nick, not_in_ch, timeout=timeout)
|
||||||
|
|
||||||
|
|
||||||
|
def test_connect_and_join(livebot):
|
||||||
|
port, server, bot, bot_t, channel, nick = livebot
|
||||||
|
bot_t.start()
|
||||||
|
assert nick in wait_until_joined(server, channel, nick)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.slow
|
||||||
|
def test_hop_to_next_server(livebot):
|
||||||
|
port, server, bot, bot_t, channel, nick = livebot
|
||||||
|
bot.irc.servers = [["localhost", bot.irc.servers[0][1] + 1], bot.irc.servers[0]] # bad port, con refused on 1st srv
|
||||||
|
bot_t.start()
|
||||||
|
assert nick in wait_until_joined(server, channel, nick)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.slow
|
||||||
|
def test_quit_reconnect(livebot):
|
||||||
|
port, server, bot, bot_t, channel, nick = livebot
|
||||||
|
bot.irc.reconnect_delay = 0.1
|
||||||
|
bot_t.start()
|
||||||
|
wait_until_joined(server, channel, nick)
|
||||||
|
bot.act_QUIT("quitting")
|
||||||
|
wait_until_absent(server, channel, nick)
|
||||||
|
assert nick in wait_until_joined(server, channel, nick)
|
||||||
|
|
||||||
|
|
||||||
|
def test_bs():
|
||||||
|
IRCCore.fulltrace()
|
||||||
|
IRCCore.trace()
|
Loading…
Reference in New Issue