# Path: zfs_autobackup/LogStub.py
# class LogStub:
#     """Just a stub, usually overriden in subclasses."""
# 
#     # simple logging stubs
#     def debug(self, txt):
#         print("DEBUG  : " + txt)
# 
#     def verbose(self, txt):
#         print("VERBOSE: " + txt)
# 
#     def warning(self, txt):
#         print("WARNING: " + txt)
# 
#     def error(self, txt):
#         print("ERROR  : " + txt)
# 
# Path: zfs_autobackup/ExecuteNode.py
# class ExecuteError(Exception):
#     pass


from basetest import *
from zfs_autobackup.LogStub import LogStub
from zfs_autobackup.ExecuteNode import ExecuteError
                                             0,
                                             pre_snapshot_cmds=["echo pre1", "echo pre2"],
                                             post_snapshot_cmds=["echo post1 >&2", "echo post2 >&2"]
                                             )

                self.assertIn("STDOUT > pre1", buf.getvalue())
                self.assertIn("STDOUT > pre2", buf.getvalue())
                self.assertIn("STDOUT > post1", buf.getvalue())
                self.assertIn("STDOUT > post2", buf.getvalue())


        with self.subTest("Failure in the middle, only pre1 and both post1 and post2 should be executed, no snapshot should be attempted"):
            with OutputIO() as buf:
                with redirect_stdout(buf):
                    with self.assertRaises(ExecuteError):
                        node.consistent_snapshot(node.selected_datasets(property_name="autobackup:test", exclude_paths=[], exclude_received=False, exclude_unchanged=False, min_change=1), "test-1",
                                                 0,
                                                 pre_snapshot_cmds=["echo pre1", "false", "echo pre2"],
                                                 post_snapshot_cmds=["echo post1", "false", "echo post2"]
                                                 )

                print(buf.getvalue())
                self.assertIn("STDOUT > pre1", buf.getvalue())
                self.assertNotIn("STDOUT > pre2", buf.getvalue())
                self.assertIn("STDOUT > post1", buf.getvalue())
                self.assertIn("STDOUT > post2", buf.getvalue())

        with self.subTest("Snapshot fails"):
            with OutputIO() as buf:
                with redirect_stdout(buf):

# Path: zfs_autobackup/LogStub.py
# class LogStub:
#     """Just a stub, usually overriden in subclasses."""
# 
#     # simple logging stubs
#     def debug(self, txt):
#         print("DEBUG  : " + txt)
# 
#     def verbose(self, txt):
#         print("VERBOSE: " + txt)
# 
#     def warning(self, txt):
#         print("WARNING: " + txt)
# 
#     def error(self, txt):
#         print("ERROR  : " + txt)
# 
# Path: zfs_autobackup/ExecuteNode.py
# class ExecuteError(Exception):
#     pass


# Path: tests/test_zfsnode.py
from basetest import *
from zfs_autobackup.LogStub import LogStub
from zfs_autobackup.ExecuteNode import ExecuteError

                                             0,
                                             pre_snapshot_cmds=["echo pre1", "echo pre2"],
                                             post_snapshot_cmds=["echo post1 >&2", "echo post2 >&2"]
                                             )

                self.assertIn("STDOUT > pre1", buf.getvalue())
                self.assertIn("STDOUT > pre2", buf.getvalue())
                self.assertIn("STDOUT > post1", buf.getvalue())
                self.assertIn("STDOUT > post2", buf.getvalue())


        with self.subTest("Failure in the middle, only pre1 and both post1 and post2 should be executed, no snapshot should be attempted"):
            with OutputIO() as buf:
                with redirect_stdout(buf):
                    with self.assertRaises(ExecuteError):
                        node.consistent_snapshot(node.selected_datasets(property_name="autobackup:test", exclude_paths=[], exclude_received=False, exclude_unchanged=False, min_change=1), "test-1",
                                                 0,
                                                 pre_snapshot_cmds=["echo pre1", "false", "echo pre2"],
                                                 post_snapshot_cmds=["echo post1", "false", "echo post2"]
                                                 )

                print(buf.getvalue())
                self.assertIn("STDOUT > pre1", buf.getvalue())
                self.assertNotIn("STDOUT > pre2", buf.getvalue())
                self.assertIn("STDOUT > post1", buf.getvalue())
                self.assertIn("STDOUT > post2", buf.getvalue())

        with self.subTest("Snapshot fails"):
            with OutputIO() as buf:
                with redirect_stdout(buf):

                    with self.assertRaises(ExecuteError):