From 3e6a327647980f4ee4ae1bbfd261036827c6e3e4 Mon Sep 17 00:00:00 2001 From: Edwin Eefting Date: Mon, 21 Feb 2022 12:31:19 +0100 Subject: [PATCH] zfs-check broken pipe handling tests --- tests/test_check.py | 18 ++++++++++++++++-- zfs_autobackup/ZfsAutobackup.py | 4 ++-- zfs_autobackup/ZfsAutoverify.py | 4 ++-- zfs_autobackup/ZfsCheck.py | 3 +-- zfs_autobackup/util.py | 4 ++++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/tests/test_check.py b/tests/test_check.py index 5c6f77d..9689fda 100644 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -107,7 +107,21 @@ dir/testfile 0 2e863f1fcccd6642e4e28453eba10d2d3f74d798 """, buf.getvalue()) - # def test_brokenpipe_cleanup_filesystem(self): - # """test if stuff is cleaned up correctly, in debugging mode , when a pipe breaks. """ + def test_brokenpipe_cleanup_filesystem(self): + """test if stuff is cleaned up correctly, in debugging mode , when a pipe breaks. """ + + prepare_zpools() + shelltest("cp tests/data/whole /test_source1/testfile") + shelltest("zfs snapshot test_source1@test") + + #breaks pipe when grep exists: + #important to use --debug, since that generates extra output which would be problematic if we didnt do correct SIGPIPE handling + shelltest("python -m zfs_autobackup.ZfsCheck test_source1@test --debug | grep -m1 Hashing") + + #should NOT be mounted anymore if cleanup went ok: + self.assertNotRegex(shelltest("mount"), "test_source1@test") + + + diff --git a/zfs_autobackup/ZfsAutobackup.py b/zfs_autobackup/ZfsAutobackup.py index 37fcfe5..0b899b2 100644 --- a/zfs_autobackup/ZfsAutobackup.py +++ b/zfs_autobackup/ZfsAutobackup.py @@ -2,7 +2,7 @@ import time import argparse from signal import signal, SIGPIPE -from .util import output_redir +from .util import output_redir, sigpipe_handler from .ZfsAuto import ZfsAuto @@ -492,7 +492,7 @@ class ZfsAutobackup(ZfsAuto): def cli(): import sys - signal(SIGPIPE, output_redir) + signal(SIGPIPE, sigpipe_handler) sys.exit(ZfsAutobackup(sys.argv[1:], False).run()) diff --git a/zfs_autobackup/ZfsAutoverify.py b/zfs_autobackup/ZfsAutoverify.py index 7e8c158..37caf05 100644 --- a/zfs_autobackup/ZfsAutoverify.py +++ b/zfs_autobackup/ZfsAutoverify.py @@ -1,6 +1,6 @@ # from util import activate_volume_snapshot, create_mountpoints, cleanup_mountpoint from signal import signal, SIGPIPE -from .util import output_redir +from .util import output_redir, sigpipe_handler from .ZfsAuto import ZfsAuto from .ZfsNode import ZfsNode @@ -305,7 +305,7 @@ class ZfsAutoverify(ZfsAuto): def cli(): import sys - signal(SIGPIPE, output_redir) + signal(SIGPIPE, sigpipe_handler) sys.exit(ZfsAutoverify(sys.argv[1:], False).run()) diff --git a/zfs_autobackup/ZfsCheck.py b/zfs_autobackup/ZfsCheck.py index 69789be..2e2828e 100644 --- a/zfs_autobackup/ZfsCheck.py +++ b/zfs_autobackup/ZfsCheck.py @@ -147,8 +147,7 @@ class ZfsCheck(CliBase): def cli(): import sys - - signal(SIGPIPE, output_redir) + signal(SIGPIPE, sigpipe_handler) sys.exit(ZfsCheck(sys.argv[1:], False).run()) diff --git a/zfs_autobackup/util.py b/zfs_autobackup/util.py index 0e27e7d..e072379 100644 --- a/zfs_autobackup/util.py +++ b/zfs_autobackup/util.py @@ -21,6 +21,7 @@ import sys import time + def block_hash(fname, count=10000, bs=4096): """This function was created to checksum huge files and blockdevices (TB's) Instead of one sha1sum of the whole file, it generates sha1susms of chunks of the file. @@ -100,3 +101,6 @@ def output_redir(): os.dup2(devnull, sys.stdout.fileno()) os.dup2(devnull, sys.stderr.fileno()) +def sigpipe_handler(sig, stack): + #redir output so we dont get more SIGPIPES during cleanup. (which my try to write to stdout) + output_redir()