def test_create_pure_tar(tmp_path): """Test to create a tar file without encryption.""" # Prepair test folder temp_orig = tmp_path.joinpath("orig") fixture_data = Path(__file__).parents[1].joinpath("fixtures/tar_data") shutil.copytree(fixture_data, temp_orig, symlinks=True) # Create Tarfile temp_tar = tmp_path.joinpath("backup.tar") with SecureTarFile(temp_tar, "w") as tar_file: atomic_contents_add( tar_file, temp_orig, excludes=[], arcname=".", ) assert temp_tar.exists() # Restore temp_new = tmp_path.joinpath("new") with SecureTarFile(temp_tar, "r") as tar_file: tar_file.extractall(path=temp_new, members=tar_file) assert temp_new.is_dir() assert temp_new.joinpath("test_symlink").is_symlink() assert temp_new.joinpath("test1").is_dir() assert temp_new.joinpath("test1/script.sh").is_file() # 775 is correct for local, but in GitHub action it's 755, both is fine assert oct(temp_new.joinpath("test1/script.sh").stat().st_mode)[-3:] in [ "755", "775", ] assert temp_new.joinpath("README.md").is_file()
def test_create_pure_tar(): """Test to create a tar file without encryption.""" with TemporaryDirectory() as temp_dir: temp = Path(temp_dir) # Prepair test folder temp_orig = temp.joinpath("orig") fixture_data = Path(__file__).parents[1].joinpath("fixtures/tar_data") shutil.copytree(fixture_data, temp_orig, symlinks=True) # Create Tarfile temp_tar = temp.joinpath("backup.tar") with SecureTarFile(temp_tar, "w") as tar_file: atomic_contents_add( tar_file, temp_orig, excludes=[], arcname=".", ) assert temp_tar.exists() # Restore temp_new = temp.joinpath("new") with SecureTarFile(temp_tar, "r") as tar_file: tar_file.extractall(path=temp_new, members=tar_file) assert temp_new.is_dir() assert temp_new.joinpath("test_symlink").is_symlink() assert temp_new.joinpath("test1").is_dir() assert temp_new.joinpath("test1/script.sh").is_file() assert temp_new.joinpath("test1/script.sh").stat().st_mode == 33261 assert temp_new.joinpath("README.md").is_file()
async def test_clear_snapshots(coresys: CoreSys, tmp_path): """Test snapshot cleanup.""" for slug in ["sn1", "sn2", "sn3", "sn4", "sn5"]: temp_tar = Path(tmp_path, f"{slug}.tar") with SecureTarFile(temp_tar, "w"): pass snapshot = Snapshot(coresys, temp_tar) snapshot._data = { # pylint: disable=protected-access ATTR_SLUG: slug, ATTR_DATE: utcnow().isoformat(), ATTR_TYPE: SNAPSHOT_PARTIAL if "1" in slug or "5" in slug else SNAPSHOT_FULL, } coresys.snapshots.snapshots_obj[snapshot.slug] = snapshot newest_full_snapshot = coresys.snapshots.snapshots_obj["sn4"] assert newest_full_snapshot in coresys.snapshots.list_snapshots assert (len([ x for x in coresys.snapshots.list_snapshots if x.sys_type == SNAPSHOT_FULL ]) == 3) coresys.resolution.storage.clean_full_snapshots() assert newest_full_snapshot in coresys.snapshots.list_snapshots assert (len([ x for x in coresys.snapshots.list_snapshots if x.sys_type == SNAPSHOT_FULL ]) == 1)
async def test_fixup(coresys: CoreSys, tmp_path): """Test fixup.""" clear_full_backup = FixupClearFullBackup(coresys) assert not clear_full_backup.auto coresys.resolution.suggestions = Suggestion( SuggestionType.CLEAR_FULL_BACKUP, ContextType.SYSTEM) for slug in ["sn1", "sn2", "sn3", "sn4", "sn5"]: temp_tar = Path(tmp_path, f"{slug}.tar") with SecureTarFile(temp_tar, "w"): pass backup = Backup(coresys, temp_tar) backup._data = { # pylint: disable=protected-access ATTR_SLUG: slug, ATTR_DATE: utcnow().isoformat(), ATTR_TYPE: BackupType.PARTIAL if "1" in slug or "5" in slug else BackupType.FULL, } coresys.backups._backups[backup.slug] = backup newest_full_backup = coresys.backups._backups["sn4"] assert newest_full_backup in coresys.backups.list_backups assert (len([ x for x in coresys.backups.list_backups if x.sys_type == BackupType.FULL ]) == 3) await clear_full_backup() assert newest_full_backup in coresys.backups.list_backups assert (len([ x for x in coresys.backups.list_backups if x.sys_type == BackupType.FULL ]) == 1) assert len(coresys.resolution.suggestions) == 0
async def test_fixup(coresys: CoreSys, tmp_path): """Test fixup.""" clear_full_snapshot = FixupClearFullSnapshot(coresys) assert not clear_full_snapshot.auto coresys.resolution.suggestions = Suggestion( SuggestionType.CLEAR_FULL_SNAPSHOT, ContextType.SYSTEM) for slug in ["sn1", "sn2", "sn3", "sn4", "sn5"]: temp_tar = Path(tmp_path, f"{slug}.tar") with SecureTarFile(temp_tar, "w"): pass snapshot = Snapshot(coresys, temp_tar) snapshot._data = { # pylint: disable=protected-access ATTR_SLUG: slug, ATTR_DATE: utcnow().isoformat(), ATTR_TYPE: SNAPSHOT_PARTIAL if "1" in slug or "5" in slug else SNAPSHOT_FULL, } coresys.snapshots.snapshots_obj[snapshot.slug] = snapshot newest_full_snapshot = coresys.snapshots.snapshots_obj["sn4"] assert newest_full_snapshot in coresys.snapshots.list_snapshots assert (len([ x for x in coresys.snapshots.list_snapshots if x.sys_type == SNAPSHOT_FULL ]) == 3) await clear_full_snapshot() assert newest_full_snapshot in coresys.snapshots.list_snapshots assert (len([ x for x in coresys.snapshots.list_snapshots if x.sys_type == SNAPSHOT_FULL ]) == 1) assert len(coresys.resolution.suggestions) == 0