mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 01:46:18 -05:00
A WROLPi serves its library over rsync, and makes a database snapshot on request (#988)
* A WROLPi serves its library over rsync, and makes a database snapshot on request The first part of replacing the sync: a secondary will copy its primary's whole media directory with rsync and replace its database with the primary's, instead of downloading the files of FileGroups over HTTP (which never copies an ignored directory, such as the map's). - An rsync daemon serves the media directory read-only on port 8873, on every WROLPi. It never serves the TLS keys, the cookies, or the database in use. On a Raspberry Pi and Debian it is the wrolpi-rsyncd unit; in Docker the Controller runs it in a helper container made from the api container (its image, user and volumes), so the media directory is still configured in one place. All three read controller/rsyncd.conf. - POST /api/sync/snapshot asks for a snapshot of the database, which the daemon serves from config/.sync. An unchanged database is not written again. - /api/sync/info also says the database's migration and whether the media directory is mounted. Nothing uses these yet; the existing sync is unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * The rsync daemon follows no links, and no snapshot is made in WROL Mode - A client that asked the daemon to follow symbolic links could read what a link in the library points at. A relative link to config/ssl was read this way although that directory is left out of the module. The daemon now refuses the three options that follow links; a link is sent as a link. - A database snapshot is a new file on the primary's drive, so WROL Mode refuses it, as it does a backup. A snapshot made earlier is still served. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * No rsync helper from an image without rsync, and the helper follows the api container Review found: - A helper made from an api image built before rsync was added exits at once, and Docker restarted it forever while the Controller reported it created. The image is now looked at first; no helper is made from one without rsync, an older helper is left serving, and the reason is logged once. - The helper was only checked when the Controller started, so a recreated or rebuilt api container left it stale. It is now checked every minute. - A snapshot cut off by its process being killed stayed marked as running and held back every later request. - The snapshot's half-written temporary file was served by the daemon. - The comment on the daemon's refused options claimed more than they give: a link that is part of the requested path is still followed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
bb7a66b0d6
commit
2ac4a3a294
19 changed files with 712 additions and 10 deletions
|
|
@ -81,6 +81,14 @@ DEFAULT_CONFIG = {
|
|||
"use_https": True,
|
||||
"description": "Kiwix/Zim server",
|
||||
},
|
||||
{
|
||||
"name": "wrolpi-rsyncd",
|
||||
"systemd_name": "wrolpi-rsyncd",
|
||||
"port": 8873,
|
||||
"viewable": False,
|
||||
"use_https": False,
|
||||
"description": "Library for secondary WROLPis (rsync)",
|
||||
},
|
||||
# System services
|
||||
{
|
||||
"name": "caddy",
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ except ImportError:
|
|||
CONTAINER_PREFIX = os.environ.get("COMPOSE_PROJECT_NAME", "wrolpi")
|
||||
|
||||
# Containers that should not have a "View" button (non-HTTP services)
|
||||
NON_VIEWABLE_CONTAINERS = {"redis", "memcached", "samba"}
|
||||
NON_VIEWABLE_CONTAINERS = {"redis", "memcached", "samba", "rsyncd"}
|
||||
|
||||
# Containers that should use HTTPS
|
||||
HTTPS_CONTAINERS = {"web"}
|
||||
|
|
|
|||
133
controller/lib/rsyncd.py
Normal file
133
controller/lib/rsyncd.py
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
"""
|
||||
The rsync daemon that serves this WROLPi's library to secondary WROLPis.
|
||||
|
||||
On a Raspberry Pi and on Debian it is the `wrolpi-rsyncd` systemd unit. In Docker the Controller runs it in a helper
|
||||
container made from the api container: the same image, user and volumes (read-only), so the media directory is
|
||||
configured in one place, the api service. Both read `controller/rsyncd.conf`.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from controller.lib.docker_services import NotFound, _get_client, _get_container_name, can_manage_containers
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
RSYNCD_CONFIG = Path(__file__).parent.parent / "rsyncd.conf"
|
||||
# Named like a compose service's container, so the Control page can start, stop and restart it.
|
||||
HELPER_NAME = _get_container_name("rsyncd")
|
||||
# What a helper was made from; a helper made from anything else is replaced.
|
||||
SOURCE_LABEL = "wrolpi.rsyncd.source"
|
||||
# The api image starts the API; the helper starts rsync instead. The config arrives in the environment because the
|
||||
# helper shares no directory with the Controller.
|
||||
HELPER_COMMAND = ('printf "%s" "$RSYNCD_CONF" > /tmp/rsyncd.conf'
|
||||
' && exec rsync --daemon --no-detach --config=/tmp/rsyncd.conf')
|
||||
|
||||
|
||||
def read_rsyncd_config() -> str:
|
||||
return RSYNCD_CONFIG.read_text()
|
||||
|
||||
|
||||
def get_rsyncd_port(config: str = None) -> int:
|
||||
"""The port in rsyncd.conf, so the published port cannot differ from the one the daemon listens on."""
|
||||
match = re.search(r"^port\s*=\s*(\d+)\s*$", config or read_rsyncd_config(), re.MULTILINE)
|
||||
if not match:
|
||||
raise RuntimeError(f"No port in {RSYNCD_CONFIG}")
|
||||
return int(match.group(1))
|
||||
|
||||
|
||||
# The api container can be recreated or rebuilt while the Controller runs.
|
||||
CHECK_INTERVAL_SECONDS = 60
|
||||
# Images found to have no rsync (built before it was added), so they are not tried again at every check.
|
||||
_images_without_rsync = set()
|
||||
# The last failure logged, so a failure that lasts is logged once.
|
||||
_last_error = None
|
||||
|
||||
|
||||
def _image_has_rsync(client, image_id: str) -> bool:
|
||||
"""A helper made from an image without rsync exits at once and, restarted by Docker, never stops trying."""
|
||||
if image_id in _images_without_rsync:
|
||||
return False
|
||||
try:
|
||||
client.containers.run(image_id, ["-c", "command -v rsync"], entrypoint="sh", remove=True)
|
||||
return True
|
||||
except Exception as e:
|
||||
if getattr(e, "exit_status", None) is None:
|
||||
# Docker failed, not the command; this says nothing about the image.
|
||||
raise
|
||||
_images_without_rsync.add(image_id)
|
||||
return False
|
||||
|
||||
|
||||
def _failed(error: str) -> dict:
|
||||
global _last_error
|
||||
if error != _last_error:
|
||||
logger.warning("Could not start the rsync daemon container: %s", error)
|
||||
_last_error = error
|
||||
return {"success": False, "error": error}
|
||||
|
||||
|
||||
async def rsync_daemon_loop():
|
||||
"""Docker only: keep the helper matching the api container for as long as the Controller runs."""
|
||||
while True:
|
||||
await asyncio.to_thread(ensure_rsync_daemon)
|
||||
await asyncio.sleep(CHECK_INTERVAL_SECONDS)
|
||||
|
||||
|
||||
def ensure_rsync_daemon() -> dict:
|
||||
"""Docker only: make sure the helper container serves the api container's volumes.
|
||||
|
||||
A helper made from this api container, image and config is left alone (started if it stopped); any other is
|
||||
replaced. Never raises: a WROLPi without the daemon still works, it only cannot be copied by a secondary."""
|
||||
global _last_error
|
||||
if not can_manage_containers():
|
||||
return {"success": False, "error": "Docker management not available"}
|
||||
|
||||
try:
|
||||
client = _get_client()
|
||||
config = read_rsyncd_config()
|
||||
port = get_rsyncd_port(config)
|
||||
api = client.containers.get(_get_container_name("api"))
|
||||
source = hashlib.sha256(f"{api.id}:{api.image.id}:{config}".encode()).hexdigest()
|
||||
|
||||
try:
|
||||
helper = client.containers.get(HELPER_NAME)
|
||||
except NotFound:
|
||||
helper = None
|
||||
if helper is not None:
|
||||
if helper.labels.get(SOURCE_LABEL) == source:
|
||||
if helper.status != "running":
|
||||
logger.info("Starting the rsync daemon container %s", HELPER_NAME)
|
||||
helper.start()
|
||||
_last_error = None
|
||||
return {"success": True, "action": "kept"}
|
||||
|
||||
if not _image_has_rsync(client, api.image.id):
|
||||
# A helper from an older api container is left as it is; it still serves the library.
|
||||
return _failed("The api image has no rsync; rebuild it (docker compose build api)")
|
||||
|
||||
if helper is not None:
|
||||
logger.info("Replacing the rsync daemon container %s", HELPER_NAME)
|
||||
helper.remove(force=True)
|
||||
|
||||
client.containers.run(
|
||||
api.image.id,
|
||||
["-c", HELPER_COMMAND],
|
||||
entrypoint="sh",
|
||||
name=HELPER_NAME,
|
||||
detach=True,
|
||||
user=api.attrs["Config"].get("User") or None,
|
||||
volumes_from=[f"{api.name}:ro"],
|
||||
ports={f"{port}/tcp": port},
|
||||
environment={"RSYNCD_CONF": config},
|
||||
labels={SOURCE_LABEL: source},
|
||||
restart_policy={"Name": "unless-stopped"},
|
||||
)
|
||||
logger.info("Started the rsync daemon container %s on port %s", HELPER_NAME, port)
|
||||
_last_error = None
|
||||
return {"success": True, "action": "created"}
|
||||
except Exception as e:
|
||||
return _failed(str(e))
|
||||
|
|
@ -43,6 +43,7 @@ from controller.lib.config import (
|
|||
save_config,
|
||||
update_config,
|
||||
)
|
||||
from controller.lib.rsyncd import rsync_daemon_loop
|
||||
from controller.lib.scripts import get_script_status
|
||||
from controller.lib.docker_services import (
|
||||
can_manage_containers,
|
||||
|
|
@ -123,6 +124,10 @@ async def lifespan(app: FastAPI):
|
|||
else:
|
||||
logger.info("Primary drive not mounted - using default configuration")
|
||||
|
||||
# Docker: the library is served to secondary WROLPis from a helper container, kept matching the api container.
|
||||
if is_docker_mode():
|
||||
app.state.rsync_daemon_task = asyncio.create_task(rsync_daemon_loop())
|
||||
|
||||
# After the config (and any migrated hotspot settings) are loaded. Starting can take a while; do not block.
|
||||
from controller.lib.admin import start_hotspot_on_boot
|
||||
app.state.hotspot_on_boot_task = asyncio.create_task(asyncio.to_thread(start_hotspot_on_boot))
|
||||
|
|
@ -142,6 +147,8 @@ async def lifespan(app: FastAPI):
|
|||
await status_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
if rsync_daemon_task := getattr(app.state, "rsync_daemon_task", None):
|
||||
rsync_daemon_task.cancel()
|
||||
|
||||
|
||||
# Create FastAPI app
|
||||
|
|
|
|||
22
controller/rsyncd.conf
Normal file
22
controller/rsyncd.conf
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# How a secondary WROLPi copies this WROLPi's library: an rsync daemon that serves the media directory read-only.
|
||||
# The library is no more private here than it is over HTTPS at /media/.
|
||||
#
|
||||
# One file for every deployment: the systemd unit reads it on a Raspberry Pi and on Debian, and the Controller hands
|
||||
# it to the helper container in Docker (see controller/lib/rsyncd.py).
|
||||
port = 8873
|
||||
# Run as the unprivileged wrolpi user: no chroot, and no connection limit (which needs a lock file under /var/run).
|
||||
use chroot = no
|
||||
log file = /dev/stderr
|
||||
|
||||
[media]
|
||||
path = /media/wrolpi
|
||||
read only = yes
|
||||
list = yes
|
||||
# A symbolic link is sent as a link. Asked to follow links while it copies a directory, the daemon would send
|
||||
# the files left out below through a link to them from elsewhere in the library. (A link that is itself part of
|
||||
# the path a client asks for is still followed: the files below are left out by where they are, nothing more.)
|
||||
refuse options = copy-links copy-unsafe-links copy-dirlinks
|
||||
# Never served: this machine's TLS keys and cookies, and the database in use (a copy of a database that is
|
||||
# being written can be torn; a secondary copies the snapshot under config/.sync instead), and a snapshot that
|
||||
# is still being written.
|
||||
exclude = /config/ssl/ /config/cookies.txt /config/cookies.txt.enc /config/wrolpi.db /config/wrolpi.db-wal /config/wrolpi.db-shm /config/wrolpi.db.lock .wrolpi.db.backup /lost+found/
|
||||
|
|
@ -57,7 +57,7 @@ class TestManagedServicesConfig:
|
|||
def test_managed_services_count(self):
|
||||
"""Should have expected number of managed services."""
|
||||
services = DEFAULT_CONFIG["managed_services"]
|
||||
assert len(services) == 11
|
||||
assert len(services) == 12
|
||||
|
||||
@pytest.mark.parametrize("name,expected", [
|
||||
("wrolpi-api",
|
||||
|
|
|
|||
236
controller/test/test_rsyncd.py
Normal file
236
controller/test/test_rsyncd.py
Normal file
|
|
@ -0,0 +1,236 @@
|
|||
"""
|
||||
Unit tests for controller.lib.rsyncd: the rsync daemon that serves the library to secondary WROLPis.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from controller.lib import rsyncd
|
||||
from controller.lib.docker_services import NON_VIEWABLE_CONTAINERS
|
||||
|
||||
REPOSITORY = Path(__file__).parent.parent.parent
|
||||
|
||||
|
||||
class FakeNotFound(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def make_client(helper=None):
|
||||
api = mock.Mock()
|
||||
api.id, api.name, api.image.id = "api-id", "wrolpi-api-1", "sha256:image"
|
||||
api.attrs = {"Config": {"User": "1000:1000"}}
|
||||
client = mock.Mock()
|
||||
|
||||
def get(name):
|
||||
if name == "wrolpi-api-1":
|
||||
return api
|
||||
if helper is not None:
|
||||
return helper
|
||||
raise FakeNotFound(name)
|
||||
|
||||
client.containers.get.side_effect = get
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def forget_images():
|
||||
rsyncd._images_without_rsync.clear()
|
||||
rsyncd._last_error = None
|
||||
|
||||
|
||||
class FakeContainerError(Exception):
|
||||
exit_status = 127
|
||||
|
||||
|
||||
def helper_runs(client) -> list:
|
||||
"""The calls that made a helper (the others only look for rsync in the image)."""
|
||||
return [i for i in client.containers.run.call_args_list if i.kwargs.get("detach")]
|
||||
|
||||
|
||||
def ensure(client):
|
||||
with mock.patch.object(rsyncd, "can_manage_containers", return_value=True), \
|
||||
mock.patch.object(rsyncd, "_get_client", return_value=client), \
|
||||
mock.patch.object(rsyncd, "NotFound", FakeNotFound):
|
||||
return rsyncd.ensure_rsync_daemon()
|
||||
|
||||
|
||||
def current_source(client) -> str:
|
||||
"""The label a helper made by this code from this api container carries."""
|
||||
ensure(client)
|
||||
return helper_runs(client)[0].kwargs["labels"][rsyncd.SOURCE_LABEL]
|
||||
|
||||
|
||||
class TestConfig:
|
||||
|
||||
def test_port(self):
|
||||
assert rsyncd.get_rsyncd_port() == 8873
|
||||
assert rsyncd.get_rsyncd_port("port = 9000\n[media]\n") == 9000
|
||||
with pytest.raises(RuntimeError):
|
||||
rsyncd.get_rsyncd_port("[media]\n")
|
||||
|
||||
def test_unit_reads_the_same_config(self):
|
||||
"""A Raspberry Pi, Debian and Docker all serve by one config file."""
|
||||
unit = (REPOSITORY / "etc/raspberrypios/wrolpi-rsyncd.service").read_text()
|
||||
assert "--config=/opt/wrolpi/controller/rsyncd.conf" in unit
|
||||
assert rsyncd.RSYNCD_CONFIG == REPOSITORY / "controller/rsyncd.conf"
|
||||
assert "User=wrolpi" in unit
|
||||
|
||||
def test_units_installed_and_enabled(self):
|
||||
for script in ("repair.sh", "pi-gen/stage2/04-wrolpi/03-run-chroot.sh",
|
||||
"debian-live-config/config/includes.chroot/usr/local/sbin/wrolpi-bootstrap.sh"):
|
||||
assert "wrolpi-rsyncd.service" in (REPOSITORY / script).read_text(), script
|
||||
assert "rsync" in (REPOSITORY / "docker/api/Dockerfile").read_text()
|
||||
|
||||
|
||||
@pytest.mark.skipif(not shutil.which("rsync") or b"rsync version 3" not in subprocess.run(
|
||||
["rsync", "--version"], capture_output=True).stdout, reason="needs rsync 3")
|
||||
def test_daemon_serves_the_library_read_only(tmp_path):
|
||||
"""The daemon run with the real config (only its port and path changed) lists the library, never serves the
|
||||
database in use or the TLS keys, serves the snapshot, and refuses a write."""
|
||||
media = tmp_path / "media"
|
||||
for path in ("videos/a.mp4", "config/tags.yaml", "config/wrolpi.db", "config/wrolpi.db-wal",
|
||||
"config/ssl/ca.key", "config/cookies.txt.enc", "config/.sync/config/wrolpi.db",
|
||||
"config/.sync/config/.wrolpi.db.backup"):
|
||||
(media / path).parent.mkdir(parents=True, exist_ok=True)
|
||||
(media / path).write_text(path)
|
||||
# Links that anyone able to write to the library could make, to what the daemon leaves out. Relative: rsync
|
||||
# already makes an absolute link point nowhere.
|
||||
(media / "videos/keys").symlink_to("../config/ssl")
|
||||
(media / "videos/key").symlink_to("../config/ssl/ca.key")
|
||||
with socket.socket() as probe:
|
||||
probe.bind(("127.0.0.1", 0))
|
||||
port = probe.getsockname()[1]
|
||||
config = rsyncd.read_rsyncd_config().replace("port = 8873", f"port = {port}\naddress = 127.0.0.1") \
|
||||
.replace("path = /media/wrolpi", f"path = {media}").replace("log file = /dev/stderr", "")
|
||||
assert str(media) in config and str(port) in config
|
||||
config_file = tmp_path / "rsyncd.conf"
|
||||
config_file.write_text(config)
|
||||
|
||||
daemon = subprocess.Popen(["rsync", "--daemon", "--no-detach", f"--config={config_file}"])
|
||||
try:
|
||||
url = f"rsync://127.0.0.1:{port}/media/"
|
||||
for _ in range(50):
|
||||
listing = subprocess.run(["rsync", "-r", "--list-only", url], capture_output=True, text=True)
|
||||
if listing.returncode == 0:
|
||||
break
|
||||
time.sleep(0.1)
|
||||
assert listing.returncode == 0, listing.stderr
|
||||
listed = {line.split()[-1] for line in listing.stdout.splitlines() if line.startswith("-")}
|
||||
assert listed == {"videos/a.mp4", "config/tags.yaml", "config/.sync/config/wrolpi.db"}
|
||||
|
||||
# Followed links are refused; without the option the links arrive as links, never as what they point at.
|
||||
for option in ("--copy-links", "--copy-unsafe-links", "--copy-dirlinks"):
|
||||
followed = subprocess.run(["rsync", "-r", option, url, str(tmp_path / "followed")],
|
||||
capture_output=True, text=True)
|
||||
assert followed.returncode != 0, option
|
||||
assert "refuse" in followed.stderr, followed.stderr
|
||||
leaked = [i for i in (tmp_path / "followed").rglob("*") if i.is_file() and "ssl/ca.key" in i.read_text()]
|
||||
assert not leaked, "a link let a client read the TLS key"
|
||||
copied = tmp_path / "copied"
|
||||
assert subprocess.run(["rsync", "-a", url, f"{copied}/"], capture_output=True).returncode == 0
|
||||
assert (copied / "videos/key").is_symlink() and (copied / "videos/keys").is_symlink()
|
||||
assert not (copied / "config/ssl").exists()
|
||||
|
||||
(tmp_path / "new.txt").write_text("new")
|
||||
write = subprocess.run(["rsync", str(tmp_path / "new.txt"), url], capture_output=True, text=True)
|
||||
assert write.returncode != 0 and "read only" in write.stderr
|
||||
assert not (media / "new.txt").exists()
|
||||
finally:
|
||||
daemon.terminate()
|
||||
daemon.wait(timeout=10)
|
||||
|
||||
|
||||
class TestEnsureRsyncDaemon:
|
||||
|
||||
def test_not_in_docker(self):
|
||||
with mock.patch.object(rsyncd, "can_manage_containers", return_value=False):
|
||||
assert rsyncd.ensure_rsync_daemon()["success"] is False
|
||||
|
||||
def test_creates_the_helper_from_the_api_container(self):
|
||||
client = make_client()
|
||||
assert ensure(client) == {"success": True, "action": "created"}
|
||||
(call,) = helper_runs(client)
|
||||
args, kwargs = call.args, call.kwargs
|
||||
assert args[0] == "sha256:image", "the api container's own image"
|
||||
assert kwargs["entrypoint"] == "sh" and "rsync --daemon" in args[1][1], "not the image's API entrypoint"
|
||||
assert kwargs["volumes_from"] == ["wrolpi-api-1:ro"], "the api container's volumes, read-only"
|
||||
assert kwargs["user"] == "1000:1000"
|
||||
assert kwargs["ports"] == {"8873/tcp": 8873}
|
||||
assert kwargs["environment"]["RSYNCD_CONF"] == rsyncd.read_rsyncd_config()
|
||||
assert kwargs["name"] == "wrolpi-rsyncd-1" and kwargs["detach"] is True
|
||||
assert kwargs["restart_policy"] == {"Name": "unless-stopped"}
|
||||
|
||||
def test_keeps_its_own_helper(self):
|
||||
helper = mock.Mock(status="running")
|
||||
helper.labels = {rsyncd.SOURCE_LABEL: current_source(make_client())}
|
||||
client = make_client(helper)
|
||||
assert ensure(client) == {"success": True, "action": "kept"}
|
||||
client.containers.run.assert_not_called(), "not even to look for rsync"
|
||||
helper.start.assert_not_called()
|
||||
helper.remove.assert_not_called()
|
||||
|
||||
def test_starts_a_stopped_helper(self):
|
||||
helper = mock.Mock(status="exited")
|
||||
helper.labels = {rsyncd.SOURCE_LABEL: current_source(make_client())}
|
||||
assert ensure(make_client(helper))["action"] == "kept"
|
||||
helper.start.assert_called_once()
|
||||
|
||||
def test_replaces_a_helper_made_from_another_api_container(self):
|
||||
"""A recreated api container may have other volumes; the helper must serve what the API has now."""
|
||||
helper = mock.Mock(status="running")
|
||||
helper.labels = {rsyncd.SOURCE_LABEL: "made from an older api container"}
|
||||
client = make_client(helper)
|
||||
assert ensure(client)["action"] == "created"
|
||||
helper.remove.assert_called_once_with(force=True)
|
||||
assert len(helper_runs(client)) == 1
|
||||
|
||||
def test_no_helper_from_an_image_without_rsync(self):
|
||||
"""An api image built before rsync was added: a helper made from it would exit and be restarted forever.
|
||||
None is made, an older helper is left serving, and the image is not tried again at every check."""
|
||||
helper = mock.Mock(status="running")
|
||||
helper.labels = {rsyncd.SOURCE_LABEL: "made from an older api container"}
|
||||
client = make_client(helper)
|
||||
client.containers.run.side_effect = FakeContainerError("sh: 1: rsync: not found")
|
||||
|
||||
for _ in range(3):
|
||||
result = ensure(client)
|
||||
assert result["success"] is False and "no rsync" in result["error"]
|
||||
assert not helper_runs(client)
|
||||
helper.remove.assert_not_called()
|
||||
assert client.containers.run.call_count == 1, "the image is looked at once"
|
||||
|
||||
def test_docker_failing_says_nothing_about_the_image(self):
|
||||
client = make_client()
|
||||
client.containers.run.side_effect = RuntimeError("docker is restarting")
|
||||
assert ensure(client)["success"] is False
|
||||
assert not rsyncd._images_without_rsync
|
||||
|
||||
client.containers.run.side_effect = None
|
||||
assert ensure(client)["action"] == "created"
|
||||
|
||||
def test_never_raises(self):
|
||||
client = make_client()
|
||||
client.containers.run.side_effect = RuntimeError("no such image")
|
||||
result = ensure(client)
|
||||
assert result["success"] is False and "no such image" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_checked_for_as_long_as_the_controller_runs(self, monkeypatch):
|
||||
"""The api container can be recreated while the Controller runs; the helper is checked again and again."""
|
||||
calls = []
|
||||
monkeypatch.setattr(rsyncd, "ensure_rsync_daemon", lambda: calls.append(1))
|
||||
monkeypatch.setattr(rsyncd, "CHECK_INTERVAL_SECONDS", 0.01)
|
||||
task = asyncio.create_task(rsyncd.rsync_daemon_loop())
|
||||
await asyncio.sleep(0.2)
|
||||
task.cancel()
|
||||
assert len(calls) >= 3
|
||||
|
||||
def test_not_given_a_view_button(self):
|
||||
assert "rsyncd" in NON_VIEWABLE_CONTAINERS
|
||||
Loading…
Add table
Add a link
Reference in a new issue