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,27 +149,24 @@ 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()
# 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.
......@@ -176,11 +174,7 @@ class TestCliProxyShow(CliMixin):
self.addCleanup(os.unlink, tmp.name)
os.environ['PAGER'] = 'cat > {}'.format(tmp.name)
try:
do_show(self.conf)
finally:
sys.stderr = saved_stderr
sys.stdout = saved_stdout
self.assertEqual('', stdout.getvalue())
self.assertEqual('', stderr.getvalue())
......
......@@ -68,6 +68,10 @@ class SlapPopenTestCase(unittest.TestCase):
self.script.write(b'#!/bin/sh\necho "exit code?"\nread rc\nexit $rc')
self.script.close()
# 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())
......
......@@ -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