diff --git a/bin/zfs-autobackup b/bin/zfs-autobackup index 67184a3..88327e9 100755 --- a/bin/zfs-autobackup +++ b/bin/zfs-autobackup @@ -254,10 +254,21 @@ class cached_property(object): return obj._cached_properties[propname] +class Logger(): + + #simple logging stubs + def debug(self, txt): + print("DEBUG : "+txt) + + def verbose(self, txt): + print("VERBOSE: "+txt) + + def error(self, txt): + print("ERROR : "+txt) -class ExecuteNode: +class ExecuteNode(Logger): """an endpoint to execute local or remote commands via ssh""" @@ -299,15 +310,6 @@ class ExecuteNode: else: self.error("STDERR|> "+line.rstrip()) - #simple logging stubs - def debug(self, txt): - print("DEBUG : "+txt) - - def verbose(self, txt): - print("VERBOSE: "+txt) - - def error(self, txt): - print("ERROR : "+txt) def run(self, cmd, input=None, tab_split=False, valid_exitcodes=[ 0 ], readonly=False, hide_errors=False, pipe=False, return_stderr=False): """run a command on the node @@ -1233,14 +1235,14 @@ class ZfsDataset(): class ZfsNode(ExecuteNode): """a node that contains zfs datasets. implements global (systemwide/pool wide) zfs commands""" - def __init__(self, backup_name, zfs_autobackup, ssh_config=None, ssh_to=None, readonly=False, description="", debug_output=False, thinner=Thinner()): + def __init__(self, backup_name, logger, ssh_config=None, ssh_to=None, readonly=False, description="", debug_output=False, thinner=Thinner()): self.backup_name=backup_name - if not description: + if not description and ssh_to: self.description=ssh_to else: self.description=description - self.zfs_autobackup=zfs_autobackup #for logging + self.logger=logger if ssh_config: self.verbose("Using custom SSH config: {}".format(ssh_config)) @@ -1318,13 +1320,13 @@ class ZfsNode(ExecuteNode): self.parse_zfs_progress(line, hide_errors, "STDERR > ") def verbose(self,txt): - self.zfs_autobackup.verbose("{} {}".format(self.description, txt)) + self.logger.verbose("{} {}".format(self.description, txt)) def error(self,txt,titles=[]): - self.zfs_autobackup.error("{} {}".format(self.description, txt)) + self.logger.error("{} {}".format(self.description, txt)) def debug(self,txt, titles=[]): - self.zfs_autobackup.debug("{} {}".format(self.description, txt)) + self.logger.debug("{} {}".format(self.description, txt)) def new_snapshotname(self): """determine uniq new snapshotname""" diff --git a/test_zfsnode.py b/test_zfsnode.py new file mode 100644 index 0000000..5943fc1 --- /dev/null +++ b/test_zfsnode.py @@ -0,0 +1,63 @@ + +#default test stuff +import unittest +from bin.zfs_autobackup import * + +import subprocess +import time +from pprint import pformat + +class TestZfsNode(unittest.TestCase): + + def setUp(self): + print("Preparing zfs filesystems...") + + #need ram blockdevice + # subprocess.call("rmmod brd", shell=True) + subprocess.check_call("modprobe brd rd_size=512000", shell=True) + + #remove old stuff + subprocess.call("zpool destroy test_source1", shell=True) + subprocess.call("zpool destroy test_source2", shell=True) + subprocess.call("zpool destroy test_target1", shell=True) + + #create pools + subprocess.check_call("zpool create test_source1 /dev/ram0", shell=True) + subprocess.check_call("zpool create test_source2 /dev/ram1", shell=True) + subprocess.check_call("zpool create test_target1 /dev/ram2", shell=True) + + #create test structure + subprocess.check_call("zfs create -p test_source1/fs1/sub", shell=True) + subprocess.check_call("zfs create -p test_source2/fs2/sub", shell=True) + subprocess.check_call("zfs create -p test_source2/fs3/sub", shell=True) + subprocess.check_call("zfs set autobackup:test=true test_source1/fs1", shell=True) + subprocess.check_call("zfs set autobackup:test=child test_source2/fs2", shell=True) + + print("Prepare done") + + return super().setUp() + + + + def test_getselected(self): + logger=Logger() + description="[Source]" + node=ZfsNode("test", logger, description=description) + s=pformat(node.selected_datasets) + print(s) + + #basics + self.assertEqual (s, """[(local): test_source1/fs1, + (local): test_source1/fs1/sub, + (local): test_source2/fs2/sub]""") + + #caching, so expect same result + subprocess.check_call("zfs set autobackup:test=true test_source2/fs3", shell=True) + self.assertEqual (s, """[(local): test_source1/fs1, + (local): test_source1/fs1/sub, + (local): test_source2/fs2/sub]""") + + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file