irctest/irctest/basecontrollers.py

91 lines
3.2 KiB
Python
Raw Normal View History

2015-12-19 22:09:06 +00:00
import os
import shutil
import socket
import tempfile
import time
2015-12-19 22:09:06 +00:00
import subprocess
import psutil
2015-12-19 22:09:06 +00:00
from .runner import NotImplementedByController
2015-12-20 12:12:54 +00:00
2015-12-19 00:11:57 +00:00
class _BaseController:
2015-12-20 12:47:30 +00:00
"""Base class for software controllers.
A software controller is an object that handles configuring and running
a process (eg. a server or a client), as well as sending it instructions
that are not part of the IRC specification."""
2015-12-19 00:11:57 +00:00
pass
2015-12-19 22:09:06 +00:00
class DirectoryBasedController(_BaseController):
2015-12-20 12:47:30 +00:00
"""Helper for controllers whose software configuration is based on an
arbitrary directory."""
2015-12-19 22:09:06 +00:00
def __init__(self):
super().__init__()
self.directory = None
self.proc = None
2015-12-20 00:48:56 +00:00
def kill_proc(self):
2015-12-20 12:47:30 +00:00
"""Terminates the controlled process, waits for it to exit, and
eventually kills it."""
2015-12-20 00:48:56 +00:00
self.proc.terminate()
try:
self.proc.wait(5)
except subprocess.TimeoutExpired:
self.proc.kill()
self.proc = None
2015-12-19 22:09:06 +00:00
def kill(self):
2015-12-20 12:47:30 +00:00
"""Calls `kill_proc` and cleans the configuration."""
2015-12-19 22:09:06 +00:00
if self.proc:
2015-12-20 00:48:56 +00:00
self.kill_proc()
2015-12-19 22:09:06 +00:00
if self.directory:
shutil.rmtree(self.directory)
def open_file(self, name, mode='a'):
2015-12-20 12:47:30 +00:00
"""Open a file in the configuration directory."""
2015-12-19 22:09:06 +00:00
assert self.directory
if os.sep in name:
dir_ = os.path.join(self.directory, os.path.dirname(name))
if not os.path.isdir(dir_):
os.makedirs(dir_)
assert os.path.isdir(dir_)
return open(os.path.join(self.directory, name), mode)
def create_config(self):
self.directory = tempfile.mkdtemp()
2015-12-25 14:45:06 +00:00
def gen_ssl(self):
self.csr_path = os.path.join(self.directory, 'ssl.csr')
self.key_path = os.path.join(self.directory, 'ssl.key')
self.pem_path = os.path.join(self.directory, 'ssl.pem')
self.dh_path = os.path.join(self.directory, 'dh.pem')
subprocess.check_output([self.openssl_bin, 'req', '-new', '-newkey', 'rsa',
2015-12-25 14:45:06 +00:00
'-nodes', '-out', self.csr_path, '-keyout', self.key_path,
'-batch'],
stderr=subprocess.DEVNULL)
subprocess.check_output([self.openssl_bin, 'x509', '-req',
2015-12-25 14:45:06 +00:00
'-in', self.csr_path, '-signkey', self.key_path,
'-out', self.pem_path],
stderr=subprocess.DEVNULL)
subprocess.check_output([self.openssl_bin, 'dhparam',
2015-12-25 14:45:06 +00:00
'-out', self.dh_path, '128'],
stderr=subprocess.DEVNULL)
2015-12-19 00:11:57 +00:00
class BaseClientController(_BaseController):
2015-12-20 12:47:30 +00:00
"""Base controller for IRC clients."""
2015-12-19 16:52:38 +00:00
def run(self, hostname, port, auth):
2015-12-19 00:11:57 +00:00
raise NotImplementedError()
class BaseServerController(_BaseController):
2015-12-20 12:47:30 +00:00
"""Base controller for IRC server."""
port_open = False
2015-12-22 21:33:23 +00:00
def run(self, hostname, port, password,
valid_metadata_keys, invalid_metadata_keys):
2015-12-20 12:12:54 +00:00
raise NotImplementedError()
2015-12-20 14:11:56 +00:00
def registerUser(self, case, username, password=None):
raise NotImplementedByController('account registration')
def wait_for_port(self):
while not self.port_open:
time.sleep(0.1)
for conn in psutil.Process(self.proc.pid).connections():
if conn.laddr[1] == self.port:
self.port_open = True
2015-12-20 12:12:54 +00:00