def test_decrypted_device(encrypted_device) -> None: config = encrypted_device with dm.decrypted_device( device=config.device(), pass_cmd=config.DevicePassCmd ) as dd: assert dd.exists() assert not dd.exists()
def test_decrypted_device_closes_in_case_of_exception(encrypted_device) -> None: config = encrypted_device with pytest.raises(MyCustomTestException): with dm.decrypted_device( device=config.device(), pass_cmd=config.DevicePassCmd ) as dd: raise MyCustomTestException assert not dd.exists()
def test_decrypted_device_closes_in_case_of_exception( encrypted_device) -> None: passphrase, device = encrypted_device with pytest.raises(MyCustomTestException): with dm.decrypted_device(device=device, pass_cmd=f"echo {passphrase}") as dd: raise MyCustomTestException assert not dd.exists()
def test_decrypted_device_can_use_home_for_passcmd(encrypted_device) -> None: # Regression Test # Test if `decrypted_device` can use a program that is located in PATH. For # some reason, when passing `{}` as environment, `echo` works, but `pass` # did not. This test ensures that the necessary fix is not reverted again. passphrase, device = encrypted_device relative_home = Path("~") # must be relative to trigger regression with NamedTemporaryFile(dir=relative_home.expanduser()) as pwd_f: absolute_pwd_f = Path(pwd_f.name) relative_pwd_f = relative_home / absolute_pwd_f.name absolute_pwd_f.write_text(passphrase) with dm.decrypted_device(device=device, pass_cmd=f"cat {relative_pwd_f}") as dd: assert dd.exists() assert not dd.exists()
def do_backup(config: Union[cp.BtrfsConfig, cp.ResticConfig]) -> None: if config.device().exists(): with dm.decrypted_device(config.device(), config.DevicePassCmd) as decrypted: with dm.mounted_device(decrypted) as mount_dir: if isinstance(config, cp.BtrfsConfig): do_butter_backup(config, mount_dir) elif isinstance(config, cp.ResticConfig): do_restic_backup(config, mount_dir) else: # Should be unreachable! btrfs_name = str(cp.BtrfsConfig) # type: ignore[unreachable] restic_name = str(cp.ResticConfig) raise ValueError( f"Nur {btrfs_name} und {restic_name} erlaubt, aber {type(config)} erhalten!" )
def test_decrypted_device(encrypted_device) -> None: passphrase, device = encrypted_device with dm.decrypted_device(device=device, pass_cmd=f"echo {passphrase}") as dd: assert dd.exists() assert not dd.exists()
def encrypted_btrfs_device(encrypted_device): password, device = encrypted_device with dm.decrypted_device(device=device, pass_cmd=f"echo {password}") as dd: _mkfs_btrfs(dd) return encrypted_device
def mounted_btrfs_device(encrypted_device): config = encrypted_device with dm.decrypted_device(config.device(), config.DevicePassCmd) as decrypted: with dm.mounted_device(decrypted) as mounted_device: yield config, mounted_device
def btrfs_device(encrypted_btrfs_device): config = encrypted_btrfs_device with dm.decrypted_device(config.device(), config.DevicePassCmd) as decrypted: yield decrypted