forked from rc/aircox
		
	- 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:
		
							
								
								
									
										39
									
								
								aircox_streamer/tests/fake_modules/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								aircox_streamer/tests/fake_modules/__init__.py
									
									
									
									
									
										Normal 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()
 | 
			
		||||
							
								
								
									
										10
									
								
								aircox_streamer/tests/fake_modules/atexit.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								aircox_streamer/tests/fake_modules/atexit.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
registered = []
 | 
			
		||||
"""Items registered by register()"""
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def register(func, *args, **kwargs):
 | 
			
		||||
    registered.append(func)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def unregister(func):
 | 
			
		||||
    registered.remove(func)
 | 
			
		||||
							
								
								
									
										15
									
								
								aircox_streamer/tests/fake_modules/psutil.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								aircox_streamer/tests/fake_modules/psutil.py
									
									
									
									
									
										Normal 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."""
 | 
			
		||||
							
								
								
									
										39
									
								
								aircox_streamer/tests/fake_modules/subprocess.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								aircox_streamer/tests/fake_modules/subprocess.py
									
									
									
									
									
										Normal 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)
 | 
			
		||||
		Reference in New Issue
	
	Block a user