Skip to content

Commit

Permalink
Basic testing
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Feb 1, 2024
1 parent 95a1883 commit 910fe91
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/privateer/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ def server_start(cfg, name, *, dry_run=False):
ports={"22/tcp": machine.port},
dry_run=dry_run,
)
mkdirs_container(machine.container, paths)
print(f"Server {name} now running on port {machine.port}")
if not dry_run:
mkdirs_container(machine.container, paths)
print(f"Server {name} now running on port {machine.port}")


def server_stop(cfg, name):
Expand Down
18 changes: 9 additions & 9 deletions tests/test_replicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def test_can_print_instructions_to_run_replication(capsys, managed_docker):
assert "Command to manually run replication:" in lines
cmd = (
" docker run --rm "
f"-v other:/privateer/local/other:ro "
f"-v {vol}:/privateer/keys:ro "
"-v other:/privateer/local/other:ro "
f"mrcide/privateer-client:{cfg.tag} "
"rsync -av --delete /privateer/local/other "
"carol:/privateer/local/other"
"carol:/privateer/local"
)
assert cmd in lines

Expand All @@ -50,11 +50,11 @@ def test_can_print_instructions_to_run_src_replication(capsys, managed_docker):
assert "Command to manually run replication:" in lines
cmd = (
" docker run --rm "
f"-v privateer_data:/privateer/volumes:ro "
f"-v {vol}:/privateer/keys:ro "
f"mrcide/privateer-client:{cfg.tag} "
"-v privateer_alice_data:/privateer/volumes:ro "
"mrcide/privateer-client:latest "
"rsync -av --delete /privateer/volumes/bob/data "
"carol:/privateer/volumes/bob/data"
"carol:/privateer/volumes/bob"
)
assert cmd in lines

Expand Down Expand Up @@ -102,18 +102,18 @@ def test_can_replicate_between_servers(managed_docker, monkeypatch):
"-av",
"--delete",
f"/privateer/local/{vol_other}",
f"carol:/privateer/local/{vol_other}",
f"carol:/privateer/local",
]
mounts = [
docker.types.Mount(
"/privateer/keys", vol_alice, type="volume", read_only=True
),
docker.types.Mount(
f"/privateer/local/{vol_other}",
vol_other,
type="volume",
read_only=True,
),
docker.types.Mount(
"/privateer/keys", vol_alice, type="volume", read_only=True
),
]
assert mock_run.call_count == 1
assert mock_run.call_args == call(
Expand Down
9 changes: 8 additions & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ def test_can_print_instructions_to_start_server(capsys, managed_docker):
def test_can_start_server(monkeypatch, managed_docker):
mock_docker = MagicMock()
mock_start = MagicMock()
mock_mkdirs = MagicMock()
monkeypatch.setattr(privateer.server, "docker", mock_docker)
monkeypatch.setattr(privateer.server, "service_start", mock_start)
monkeypatch.setattr(privateer.server, "mkdirs_container", mock_mkdirs)
with vault_dev.Server(export_token=True) as server:
cfg = read_config("example/simple.json")
cfg.vault.url = server.url()
Expand Down Expand Up @@ -72,13 +74,17 @@ def test_can_start_server(monkeypatch, managed_docker):
ports=ports,
dry_run=False,
)
assert mock_mkdirs.call_count == 1
assert mock_mkdirs.call_args == call(name, ["/privateer/volumes/bob/data"])


def test_can_start_server_with_local_volume(monkeypatch, managed_docker):
mock_docker = MagicMock()
mock_start = MagicMock()
mock_mkdirs = MagicMock()
monkeypatch.setattr(privateer.server, "docker", mock_docker)
monkeypatch.setattr(privateer.server, "service_start", mock_start)
monkeypatch.setattr(privateer.server, "mkdirs_container", mock_mkdirs)
with vault_dev.Server(export_token=True) as server:
cfg = read_config("example/local.json")
cfg.vault.url = server.url()
Expand Down Expand Up @@ -119,7 +125,8 @@ def test_can_start_server_with_local_volume(monkeypatch, managed_docker):
ports=ports,
dry_run=False,
)

assert mock_mkdirs.call_count == 1
assert mock_mkdirs.call_args == call(name, ["/privateer/volumes/bob/data"])

def test_can_stop_server(monkeypatch):
mock_check = MagicMock()
Expand Down
42 changes: 42 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest.mock import MagicMock
import os
import re
import tarfile
Expand Down Expand Up @@ -194,3 +195,44 @@ def test_can_uniquify_list():
assert privateer.util.unique([]) == []
assert privateer.util.unique([1, 2, 3]) == [1, 2, 3]
assert privateer.util.unique([3, 2, 1, 2, 3]) == [3, 2, 1]


def test_can_create_directories(managed_docker):
name = managed_docker("container")
cl = docker.from_env()
command = ["sleep", "20"]
container = cl.containers.run("alpine", ["sleep", "10"],
detach=True, auto_remove=True)
name = container.name
privateer.util.mkdirs_container(name, ["/a/b/x", "a/b/y"])
container = cl.containers.get(name)
res = privateer.util.exec_safely(container, ["ls", "/a/b"])
container.kill()
assert res.exit_code == 0
assert set(res.output.decode("UTF-8").strip().split("\n")) == {"x", "y"}


def test_no_docker_for_creation_of_0_directories(monkeypatch, managed_docker):
mock_docker = MagicMock()
monkeypatch.setattr(privateer.util, "docker", mock_docker)
name = managed_docker("container")
privateer.util.mkdirs_container(name, [])
assert mock_docker.from_env.call_count == 0


def test_exec_returns_output():
cl = docker.client.from_env()
container = cl.containers.run("alpine", ["sleep", "10"],
detach=True, auto_remove=True)
res = privateer.util.exec_safely(container, "ls")
container.kill()
assert res.exit_code == 0


def test_exec_safely_throws_on_failure():
cl = docker.client.from_env()
container = cl.containers.run("alpine", ["sleep", "10"],
detach=True, auto_remove=True)
with pytest.raises(Exception):
privateer.util.exec_safely(container, "missing_command")
container.kill()

0 comments on commit 910fe91

Please sign in to comment.