Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Expand tar export testing
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Nov 8, 2023
1 parent 396363a commit d73ce6b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/privateer2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def machine_config(self, name):
# this could be put elsewhere; we find the plausible sources (original
# clients) that backed up a source to any server.
def find_source(cfg, volume, source):
if volume not in cfg.list_volumes():
msg = f"Unknown volume '{volume}'"
raise Exception(msg)
for v in cfg.volumes:
if v.name == volume and v.local:
if source is not None:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def test_can_find_appropriate_source():
msg = "Invalid source 'alice': valid options: 'bob', 'carol'"
with pytest.raises(Exception, match=msg):
find_source(cfg, "data", "alice")
with pytest.raises(Exception, match="Unknown volume 'unknown'"):
find_source(cfg, "unknown", "alice")


def test_can_find_appropriate_source_if_local():
Expand Down
49 changes: 49 additions & 0 deletions tests/test_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tarfile
from unittest.mock import MagicMock, call

import pytest
import vault_dev

import docker
Expand Down Expand Up @@ -90,3 +91,51 @@ def test_can_export_managed_volume(monkeypatch, managed_docker):
tarfile = call_args[0][3]
src = "/privateer/bob/data"
assert call_args == call(mounts, src, path, tarfile, False)


def test_can_export_local_managed_volume(monkeypatch, managed_docker):
mock_tar_local = MagicMock()
mock_tar_create = MagicMock()
monkeypatch.setattr(privateer2.tar, "_run_tar_create", mock_tar_create)
monkeypatch.setattr(privateer2.tar, "export_tar_local", mock_tar_local)
with vault_dev.Server(export_token=True) as server:
cfg = read_config("example/local.json")
cfg.vault.url = server.url()
vol_keys = managed_docker("volume")
vol_data = managed_docker("volume")
vol_other = managed_docker("volume")
name = managed_docker("container")
cfg.servers[0].key_volume = vol_keys
cfg.servers[0].data_volume = vol_data
cfg.servers[0].container = name
cfg.volumes[1].name = vol_other
keygen_all(cfg)
configure(cfg, "alice")
path = export_tar(cfg, "alice", vol_other)
assert mock_tar_create.call_count == 0
assert mock_tar_local.call_count == 1
assert mock_tar_local.call_args == call(
vol_other, to_dir=None, dry_run=False
)
assert path == mock_tar_local.return_value


def test_throw_if_local_volume_does_not_exist(managed_docker):
vol = managed_docker("volume")
msg = f"Volume '{vol}' does not exist"
with pytest.raises(Exception, match=msg):
export_tar_local(vol)


def test_throw_if_volume_does_not_exist(managed_docker):
with vault_dev.Server(export_token=True) as server:
cfg = read_config("example/simple.json")
cfg.vault.url = server.url()
vol = managed_docker("volume")
cfg.servers[0].key_volume = managed_docker("volume")
cfg.servers[0].data_volume = vol
cfg.servers[0].container = managed_docker("container")
keygen_all(cfg)
configure(cfg, "alice")
with pytest.raises(Exception, match="Unknown volume 'unknown'"):
export_tar(cfg, "alice", "unknown")

0 comments on commit d73ce6b

Please sign in to comment.