Commit 531b3dbb authored by Jérome Perrin's avatar Jérome Perrin

tests: support running tests with py.test

 - rename test files as test_*.py, this is how tests are collected by
   py.test

 - take into account that test runner might disable log level and patch
   sys.stdout already

/reviewed-on nexedi/slapos.core!92
parent 75a80515
......@@ -97,7 +97,8 @@ class TestCliProxyShow(CliMixin):
self.conf.logger = self.logger
# load database
schema = bytes2str(pkg_resources.resource_string('slapos.tests.slapproxy', 'database_dump_version_current.sql'))
schema = bytes2str(pkg_resources.resource_string(
'slapos.tests.test_slapproxy', 'database_dump_version_current.sql'))
db = sqlite_connect(self.db_file.name)
db.cursor().executescript(schema)
db.commit()
......@@ -148,39 +149,32 @@ class TestCliProxyShow(CliMixin):
self.logger.info.assert_not_called()
def test_proxy_show_displays_on_stdout(self):
saved_stderr = sys.stderr
saved_stdout = sys.stdout
sys.stderr = stderr = StringIO()
sys.stdout = stdout = StringIO()
try:
# we patch logging to make sure our messages are outputed, even with test
# runners like pytest which allows disabling output
with patch.object(sys, 'stdout', StringIO()) as stdout, \
patch.object(sys, 'stderr', StringIO()) as stderr, \
patch('logging.Logger.isEnabledFor', returned_value=True):
do_show(self.conf)
finally:
sys.stderr = saved_stderr
sys.stdout = saved_stdout
# 287375f0cba269902ba1bc50242839d7 is the hash of an installed software
# in our setup database
# pytest users, be sure to use --log-level=DEBUG
self.assertIn('287375f0cba269902ba1bc50242839d7', stdout.getvalue())
self.assertEqual('', stderr.getvalue())
def test_proxy_show_use_pager(self):
saved_stderr = sys.stderr
saved_stdout = sys.stdout
sys.stderr = stderr = StringIO()
sys.stdout = stdout = StringIO()
stdout.isatty = lambda *args: True
# use a pager that just output to a file.
tmp = tempfile.NamedTemporaryFile(delete=False)
self.addCleanup(os.unlink, tmp.name)
os.environ['PAGER'] = 'cat > {}'.format(tmp.name)
try:
# we patch logging to make sure our messages are outputed, even with test
# runners like pytest which allows disabling output
with patch.object(sys, 'stdout', StringIO()) as stdout, \
patch.object(sys, 'stderr', StringIO()) as stderr, \
patch('logging.Logger.isEnabledFor', returned_value=True):
stdout.isatty = lambda *args: True
# use a pager that just output to a file.
tmp = tempfile.NamedTemporaryFile(delete=False)
self.addCleanup(os.unlink, tmp.name)
os.environ['PAGER'] = 'cat > {}'.format(tmp.name)
do_show(self.conf)
finally:
sys.stderr = saved_stderr
sys.stdout = saved_stdout
self.assertEqual('', stdout.getvalue())
self.assertEqual('', stderr.getvalue())
......
......@@ -68,36 +68,40 @@ class SlapPopenTestCase(unittest.TestCase):
self.script.write(b'#!/bin/sh\necho "exit code?"\nread rc\nexit $rc')
self.script.close()
# keep a reference to stdin and stdout to restore them later
stdin_backup = os.dup(sys.stdin.fileno())
stdout_backup = os.dup(sys.stdout.fileno())
# replace stdin with a pipe that will write 123
child_stdin_r, child_stdin_w = os.pipe()
os.write(child_stdin_w, b"123")
os.close(child_stdin_w)
os.dup2(child_stdin_r, sys.stdin.fileno())
# and stdout with the pipe to capture output
child_stdout_r, child_stdout_w = os.pipe()
os.dup2(child_stdout_w, sys.stdout.fileno())
try:
program = slapos.grid.utils.SlapPopen(
self.script.name,
debug=True,
logger=logging.getLogger())
# program output
self.assertEqual(b'exit code?\n', os.read(child_stdout_r, 1024))
self.assertEqual(123, program.returncode)
self.assertEqual('(output not captured in debug mode)', program.output)
finally:
# restore stdin & stderr
os.dup2(stdin_backup, sys.stdin.fileno())
os.dup2(stdout_backup, sys.stdout.fileno())
# close all fds open for the test
for fd in (child_stdin_r, child_stdout_r, child_stdout_w, stdin_backup, stdout_backup):
os.close(fd)
# when running under pytest we want to disable capture
with mock.patch.object(sys, 'stdin', sys.__stdin__), \
mock.patch.object(sys, 'stdout', sys.__stdout__):
# keep a reference to stdin and stdout to restore them later
stdin_backup = os.dup(sys.stdin.fileno())
stdout_backup = os.dup(sys.stdout.fileno())
# replace stdin with a pipe that will write 123
child_stdin_r, child_stdin_w = os.pipe()
os.write(child_stdin_w, b"123")
os.close(child_stdin_w)
os.dup2(child_stdin_r, sys.stdin.fileno())
# and stdout with the pipe to capture output
child_stdout_r, child_stdout_w = os.pipe()
os.dup2(child_stdout_w, sys.stdout.fileno())
try:
program = slapos.grid.utils.SlapPopen(
self.script.name,
debug=True,
logger=logging.getLogger())
# program output
self.assertEqual(b'exit code?\n', os.read(child_stdout_r, 1024))
self.assertEqual(123, program.returncode)
self.assertEqual('(output not captured in debug mode)', program.output)
finally:
# restore stdin & stderr
os.dup2(stdin_backup, sys.stdin.fileno())
os.dup2(stdout_backup, sys.stdout.fileno())
# close all fds open for the test
for fd in (child_stdin_r, child_stdout_r, child_stdout_w, stdin_backup, stdout_backup):
os.close(fd)
......@@ -36,7 +36,7 @@ from slapos.grid.SlapObject import Partition, Software
from slapos.grid import utils
from slapos.grid import networkcache
# XXX: BasicMixin should be in a separated module, not in slapgrid test module.
from slapos.tests.slapgrid import BasicMixin
from slapos.tests.test_slapgrid import BasicMixin
# Mockup
# XXX: Ambiguous name
......
......@@ -49,7 +49,7 @@ import pwd
import time
import mock
from .slapgrid import DummyManager
from .test_slapgrid import DummyManager
import six
......
......@@ -1136,7 +1136,7 @@ database_uri = %(tempdir)s/lib/external_proxy.db
behaviours.
"""
configuration = bytes2str(pkg_resources.resource_string(
'slapos.tests.slapproxy', 'slapos_multimaster.cfg.in'
'slapos.tests.test_slapproxy', 'slapos_multimaster.cfg.in'
)) % {
'tempdir': self._tempdir, 'proxyaddr': self.proxyaddr,
'external_proxy_host': self.external_proxy_host,
......@@ -1354,7 +1354,7 @@ class TestMigrateVersion10To12(TestInformation, TestRequest, TestSlaveRequest, T
def setUp(self):
super(TestMigrateVersion10To12, self).setUp()
schema = bytes2str(pkg_resources.resource_string(
'slapos.tests.slapproxy',
'slapos.tests.test_slapproxy',
'database_dump_version_10.sql'
)) % dict(version='12')
self.db = sqlite_connect(self.proxy_db)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment