#106: tests: aircox_streamer (#110)

- Writes tests for aircox streamer application;
- Add test utilities in aircox

Co-authored-by: bkfox <thomas bkfox net>
Reviewed-on: rc/aircox#110
This commit is contained in:
Thomas Kairos
2023-06-18 17:00:08 +02:00
parent 73c7c471ea
commit b453c821c7
30 changed files with 2232 additions and 897 deletions

View File

@ -0,0 +1,39 @@
import atexit as o_atexit
import subprocess as o_subprocess
import psutil as o_psutil
from . import atexit, subprocess, psutil
modules = [
(o_atexit, atexit, {}),
(o_subprocess, subprocess, {}),
(o_psutil, psutil, {}),
]
def init_mappings():
for original, spoof, mapping in modules:
if mapping:
continue
mapping.update(
{
attr: (getattr(original, attr, None), spoofed)
for attr, spoofed in vars(spoof).items()
if not attr.startswith("_") and hasattr(original, attr)
}
)
def setup():
for original, spoof, mappings in modules:
for attr, (orig, spoofed) in mappings.items():
setattr(original, attr, spoofed)
def setdown():
for original, spoof, mappings in modules:
for attr, (orig, spoofed) in mappings.items():
setattr(original, attr, orig)
init_mappings()

View File

@ -0,0 +1,10 @@
registered = []
"""Items registered by register()"""
def register(func, *args, **kwargs):
registered.append(func)
def unregister(func):
registered.remove(func)

View File

@ -0,0 +1,15 @@
"""Spoof psutil module in order to run and check tests."""
class FakeNetConnection:
def __init__(self, laddr, pid=None):
self.laddr = laddr
self.pid = pid
def net_connections(*args, **kwargs):
return net_connections.result
net_connections.result = []
"""Result value of net_connections call."""

View File

@ -0,0 +1,39 @@
"""Spoof psutil module in order to run and check tests Resulting values of
method calls are set inside `fixtures` module."""
STDOUT = 1
STDERR = 2
STDIN = 3
class FakeProcess:
args = None
kwargs = None
"""Kwargs passed to Popen."""
killed = False
"""kill() have been called."""
waited = False
"""wait() have been called."""
polled = False
"""poll() have been called."""
poll_result = None
"""Result of poll() method."""
def __init__(self, args=[], kwargs={}):
self.pid = -13
self.args = args
self.kwargs = kwargs
def kill(self):
self.killed = True
def wait(self):
self.waited = True
def poll(self):
self.polled = True
return self.poll_result
def Popen(args, **kwargs):
return FakeProcess(args, kwargs)