mirror of
https://github.com/jupyterhub/the-littlest-jupyterhub.git
synced 2025-12-18 21:54:05 +08:00
Add unit tests for run_subprocess
This commit is contained in:
@@ -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(
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
pytest
|
pytest
|
||||||
pytest-cov
|
pytest-cov
|
||||||
|
pytest-mock
|
||||||
codecov
|
codecov
|
||||||
pytoml
|
pytoml
|
||||||
21
tests/test_utils.py
Normal file
21
tests/test_utils.py
Normal 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')
|
||||||
@@ -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(
|
||||||
|
|||||||
Reference in New Issue
Block a user