make tests python2 compat

This commit is contained in:
Edwin Eefting 2020-07-08 17:53:54 +02:00
parent a6cdd4b89e
commit f2b284c407
3 changed files with 39 additions and 9 deletions

View File

@ -10,10 +10,42 @@ import time
from pprint import *
from bin.zfs_autobackup import *
from mock import *
import contextlib
import sys
import io
TEST_POOLS="test_source1 test_source2 test_target1"
# for python2 compatibility
if sys.version_info.major==2:
OutputIO=io.BytesIO
else:
OutputIO=io.StringIO
# for python2 compatibility (python 3 has this already)
@contextlib.contextmanager
def redirect_stdout(target):
original = sys.stdout
try:
sys.stdout = target
yield
finally:
sys.stdout = original
# for python2 compatibility (python 3 has this already)
@contextlib.contextmanager
def redirect_stderr(target):
original = sys.stderr
try:
sys.stderr = target
yield
finally:
sys.stderr = original
def shelltest(cmd):
"""execute and print result as nice copypastable string for unit tests (adds extra newlines on top/bottom)"""
ret=(subprocess.check_output(cmd , shell=True).decode('utf-8'))

View File

@ -1,7 +1,6 @@
from basetest import *
import contextlib
import io
class TestZfsNode(unittest2.TestCase):
@ -32,9 +31,10 @@ class TestZfsNode(unittest2.TestCase):
r=shelltest("rm /test_target1/waste")
r=shelltest("zfs umount test_target1")
#should resume and succeed
with io.StringIO() as buf:
with contextlib.redirect_stdout(buf):
with OutputIO() as buf:
with redirect_stdout(buf):
with patch('time.strftime', return_value="20101111000002"):
self.assertFalse(ZfsAutobackup("test test_target1 --verbose --allow-empty".split(" ")).run())

View File

@ -1,7 +1,5 @@
from basetest import *
import time
import contextlib
import io
@ -16,13 +14,13 @@ class TestZfsAutobackup(unittest2.TestCase):
with self.subTest("no datasets selected"):
#should resume and succeed
with io.StringIO() as buf:
with contextlib.redirect_stderr(buf):
with OutputIO() as buf:
with redirect_stderr(buf):
with patch('time.strftime', return_value="20101111000000"):
self.assertTrue(ZfsAutobackup("nonexisting test_target1 --verbose --debug".split(" ")).run())
print(buf.getvalue())
#did we really resume?
#correct message?
self.assertIn("No source filesystems selected", buf.getvalue())