import tempfile import unittest from pathlib import Path from truenas_sync import ( Dataset, Nas, SyncError, _is_child_path, classify_inventory, load_config, make_plan, parse_dataset_output, ) def nas(name: str = "test", device_id: str = "AAAA-BBBB") -> Nas: return Nas(name, "root@example", "http://localhost:8384", "secret", device_id, "/data/stor0") class DatasetDiscoveryTests(unittest.TestCase): def test_parse_zfs_output(self): result = parse_dataset_output( "stor0\tfilesystem\t/mnt/stor0\ton\tyes\n" "stor0/Documents\tfilesystem\t/mnt/stor0/Documents\ton\tyes\n" ) self.assertEqual([item.name for item in result], ["stor0", "stor0/Documents"]) def test_bad_zfs_output_fails_closed(self): with self.assertRaises(SyncError): parse_dataset_output("stor0 filesystem /mnt/stor0 on yes\n") def test_classifies_ordinary_datasets_and_exclusions(self): inventory = classify_inventory( nas(), "stor0", ["containers", "vm_stor"], [ Dataset("stor0", "filesystem", "/mnt/stor0", "on", "yes"), Dataset("stor0/Documents", "filesystem", "/mnt/stor0/Documents", "on", "yes"), Dataset("stor0/series", "filesystem", "/mnt/stor0/series", "on", "yes"), Dataset("stor0/containers", "filesystem", "/mnt/stor0/containers", "on", "yes"), Dataset("stor0/containers/mail", "filesystem", "/mnt/stor0/containers/mail", "on", "yes"), Dataset("stor0/vm_stor", "filesystem", "/mnt/stor0/vm_stor", "on", "yes"), Dataset("stor0/other-vol", "volume", "-", "on", "-"), ], ) self.assertEqual([item.name for item in inventory.ordinary], ["stor0", "stor0/Documents", "stor0/series"]) self.assertEqual( [item.name for item in inventory.excluded], ["stor0/containers", "stor0/containers/mail", "stor0/vm_stor"], ) self.assertEqual([item.name for item in inventory.volumes], ["stor0/other-vol"]) def test_unmounted_eligible_dataset_is_an_error(self): with self.assertRaisesRegex(SyncError, "not mounted"): classify_inventory( nas(), "stor0", ["containers", "vm_stor"], [ Dataset("stor0", "filesystem", "/mnt/stor0", "on", "yes"), Dataset("stor0/Documents", "filesystem", "/mnt/stor0/Documents", "on", "no"), ], ) def test_custom_mount_outside_root_is_an_error(self): with self.assertRaisesRegex(SyncError, "mounted outside"): classify_inventory( nas(), "stor0", ["containers", "vm_stor"], [ Dataset("stor0", "filesystem", "/mnt/stor0", "on", "yes"), Dataset("stor0/Documents", "filesystem", "/mnt/archive/Documents", "on", "yes"), ], ) def test_nested_dataset_can_be_used_as_a_pilot_root(self): inventory = classify_inventory( nas(), "stor0/sync_test", [], [ Dataset("stor0/sync_test", "filesystem", "/mnt/stor0/sync_test", "on", "yes"), Dataset("stor0/sync_test/subdir", "filesystem", "/mnt/stor0/sync_test/subdir", "on", "yes"), ], ) self.assertEqual(inventory.root_mountpoint, "/mnt/stor0/sync_test") self.assertEqual([item.name for item in inventory.ordinary], ["stor0/sync_test", "stor0/sync_test/subdir"]) def test_path_check_does_not_accept_sibling_prefix(self): self.assertTrue(_is_child_path("/mnt/stor0", "/mnt/stor0/Documents")) self.assertFalse(_is_child_path("/mnt/stor0", "/mnt/stor00/Documents")) class ConfigTests(unittest.TestCase): def test_loads_names_and_sync_options(self): content = ''' [general] dataset_root = "stor0" exclude = ["containers", "vm_stor"] [helsinki] ssh_target = "root@nas1" api_url = "http://nas1:8384" api_key = "key1" device_id = "AAAA-BBBB" sync_path = "/data/stor0" [jarvenpaa] ssh_target = "root@nas2" api_url = "http://nas2:8384" api_key = "key2" device_id = "CCCC-DDDD" sync_path = "/data/stor0" ''' with tempfile.TemporaryDirectory() as directory: config = Path(directory) / "config.toml" config.write_text(content, encoding="utf-8") general, first, second = load_config(config) self.assertEqual(general["root"], "stor0") self.assertEqual(general["folder_id"], "truenas-stor0-files") self.assertEqual(general["versioning_max_age_days"], 180) self.assertEqual(first.name, "helsinki") self.assertEqual(second.name, "jarvenpaa") self.assertEqual(first.device_id, "AAAA-BBBB") def test_rejects_same_device_id_even_with_different_hyphenation(self): content = ''' [general] [helsinki] ssh_target = "root@nas1" api_url = "http://nas1:8384" api_key = "key1" device_id = "AAAA-BBBB" sync_path = "/data/stor0" [jarvenpaa] ssh_target = "root@nas2" api_url = "http://nas2:8384" api_key = "key2" device_id = "AAAABBBB" sync_path = "/data/stor0" ''' with tempfile.TemporaryDirectory() as directory: config = Path(directory) / "config.toml" config.write_text(content, encoding="utf-8") with self.assertRaisesRegex(SyncError, "must be different"): load_config(config) class PlanTests(unittest.TestCase): def test_plan_uses_bidirectional_root_and_root_anchored_exclusions(self): first = nas("helsinki", "AAAA-BBBB") second = nas("jarvenpaa", "CCCC-DDDD") rows = [ Dataset("stor0", "filesystem", "/mnt/stor0", "on", "yes"), Dataset("stor0/Documents", "filesystem", "/mnt/stor0/Documents", "on", "yes"), ] inventories = ( classify_inventory(first, "stor0", ["containers", "vm_stor"], rows), classify_inventory(second, "stor0", ["containers", "vm_stor"], rows), ) class ReadOnlyApi: def __init__(self, peer_id): self.peer_id = peer_id def get_state(self, folder_id): return {"devices": [{"deviceID": self.peer_id}], "folder": None, "ignores": []} general = { "folder_id": "truenas-stor0-files", "folder_label": "stor0 files", "exclude": ["containers", "vm_stor"], "rescan_interval_seconds": 60, "versioning_max_age_days": 180, } plan = make_plan(inventories, general, (ReadOnlyApi("CCCC-DDDD"), ReadOnlyApi("AAAA-BBBB"))) self.assertEqual(plan.excludes, ("/containers", "/vm_stor")) self.assertEqual(plan.versioning_max_age_days, 180) self.assertEqual(len(plan.actions), 6) if __name__ == "__main__": unittest.main()