Add unit tests for run_subprocess

This commit is contained in:
yuvipanda
2019-05-19 14:24:57 -07:00
parent 7071332445
commit f5ecce77a1
4 changed files with 31 additions and 10 deletions

View File

@@ -44,16 +44,16 @@ def run_subprocess(cmd, *args, **kwargs):
In TLJH, this sends successful output to the installer log, In TLJH, this sends successful output to the installer log,
and failed output directly to the user's screen and failed output directly to the user's screen
""" """
logger = logging.getLogger('tljh')
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, *args, **kwargs) proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, *args, **kwargs)
printable_command = ' '.join(cmd) printable_command = ' '.join(cmd)
if proc.returncode != 0: if proc.returncode != 0:
# Our process failed! Show output to the user # Our process failed! Show output to the user
logger.error(proc.stdout.decode()) logger.error('Ran {command} with exit code {code}'.format(
e = Exception( 'command {command} failed with return code {code}'.format( command=printable_command, code=proc.returncode
printable_command, proc.returncode
)) ))
logging.exception(e) logger.error(proc.stdout.decode())
raise e raise subprocess.CalledProcessError(cmd=cmd, returncode=proc.returncode)
else: else:
# This goes into installer.log # This goes into installer.log
logger.debug('Ran {command} with exit code {code}'.format( logger.debug('Ran {command} with exit code {code}'.format(

View File

@@ -1,4 +1,5 @@
pytest pytest
pytest-cov pytest-cov
pytest-mock
codecov codecov
pytoml pytoml

21
tests/test_utils.py Normal file
View File

@@ -0,0 +1,21 @@
import pytest
from tljh import utils
import subprocess
import logging
def test_run_subprocess_exception(mocker):
logger = logging.getLogger('tljh')
mocker.patch.object(logger, 'error')
with pytest.raises(subprocess.CalledProcessError):
utils.run_subprocess(
['/bin/bash', '-c', 'echo error; exit 1']
)
logger.error.assert_called_with('error\n')
def test_run_subprocess(mocker):
logger = logging.getLogger('tljh')
mocker.patch.object(logger, 'debug')
utils.run_subprocess(['/bin/bash', '-c', 'echo success'])
logger.debug.assert_called_with('success\n')

View File

@@ -21,12 +21,11 @@ def run_subprocess(cmd, *args, **kwargs):
printable_command = ' '.join(cmd) printable_command = ' '.join(cmd)
if proc.returncode != 0: if proc.returncode != 0:
# Our process failed! Show output to the user # Our process failed! Show output to the user
logger.error(proc.stdout.decode()) logger.error('Ran {command} with exit code {code}'.format(
e = Exception( 'command {command} failed with return code {code}'.format( command=printable_command, code=proc.returncode
printable_command, proc.returncode
)) ))
logger.exception(e) logger.error(proc.stdout.decode())
raise e raise subprocess.CalledProcessError(cmd=cmd, returncode=proc.returncode)
else: else:
# This goes into installer.log # This goes into installer.log
logger.debug('Ran {command} with exit code {code}'.format( logger.debug('Ran {command} with exit code {code}'.format(