Files
MoneyPrinterTurbo/test/services/test_config_initialization.py
ChandraChaanandHarry 10714e1085 fix(config): explain how to fix a Docker bind-mounted config.toml folder
When config.toml does not exist on the host, `docker compose up` creates
an empty folder in its place and mounts it. load_config() then tries to
remove that folder, but it is the mount point itself, so Linux answers
EBUSY and the container restarts forever with "move or rename it, then
restart", which cannot be done from inside the container (#1751).

An EBUSY from rmdir now raises IsADirectoryError that says the folder
came from Docker and how to fix it on the host: stop the containers,
delete the empty config.toml folder, copy config.example.toml to
config.toml, and start again.
2026-11-02 14:08:30 +08:00

171 lines
7.2 KiB
Python

import errno
import os
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
import pytest
from app.config import config
def test_late_initializer_preserves_config_created_after_missing_check():
with TemporaryDirectory() as directory:
target = Path(directory, 'config.toml')
example = Path(directory, 'config.example.toml')
example.write_text('[app]\napi_key = ""\n', encoding='utf-8')
winner = '[app]\napi_key = "already-configured"\n'
isfile = os.path.isfile
created = False
def create_winner_after_check(path):
nonlocal created
if str(path) == str(target) and not created:
created = True
target.write_text(winner, encoding='utf-8')
return False
return isfile(path)
with (
patch.object(config, 'root_dir', directory),
patch.object(config, 'config_file', str(target)),
patch.object(config.os.path, 'isfile', side_effect=create_winner_after_check),
):
result = config.load_config()
assert result['app']['api_key'] == 'already-configured'
assert target.read_text() == winner
assert not list(Path(directory).glob('.config-init-*'))
def test_first_boot_publishes_only_complete_config():
with TemporaryDirectory() as directory:
target = Path(directory, 'config.toml')
example = Path(directory, 'config.example.toml')
payload = '[app]\napi_key = "complete"\n' + '# filler\n' * 1000
example.write_text(payload, encoding='utf-8')
link = os.link
rename = os.rename
def check_publication(source, destination):
assert not target.exists()
assert Path(source).read_text() == payload
return (rename if os.name == 'nt' else link)(source, destination)
with (
patch.object(config, 'root_dir', directory),
patch.object(config, 'config_file', str(target)),
patch.object(config.os, 'rename' if os.name == 'nt' else 'link', side_effect=check_publication),
):
assert config.load_config()['app']['api_key'] == 'complete'
assert target.read_text() == payload
assert not list(Path(directory).glob('.config-init-*'))
@pytest.mark.parametrize('error', [OSError(errno.EPERM, 'unsupported publication'), OSError(errno.ENOSPC, 'full disk')])
def test_initialization_failure_leaves_no_partial_config_or_temporary_file(error):
with TemporaryDirectory() as directory:
target = Path(directory, 'config.toml')
example = Path(directory, 'config.example.toml')
example.write_text('[app]\napi_key = ""\n', encoding='utf-8')
with (
patch.object(config, 'root_dir', directory),
patch.object(config, 'config_file', str(target)),
patch.object(config.os, 'rename' if os.name == 'nt' else 'link', side_effect=error),
):
with pytest.raises(OSError, match='copy config.example.toml'):
config.load_config()
assert not target.exists()
assert not list(Path(directory).glob('.config-init-*'))
def test_windows_publication_uses_rename_without_hardlinks():
with TemporaryDirectory() as directory:
target = str(Path(directory, 'config.toml'))
example = Path(directory, 'config.example.toml')
example.write_text('[app]\napi_key = ""\n', encoding='utf-8')
rename = os.rename
with (
patch.object(config, 'config_file', target),
patch.object(config.os, 'name', 'nt'),
patch.object(config.os, 'rename', side_effect=rename) as renamed,
patch.object(config.os, 'link') as linked,
):
assert config._initialize_config_from_example(str(example))
renamed.assert_called_once()
linked.assert_not_called()
assert Path(target).is_file()
assert not list(Path(directory).glob('.config-init-*'))
def test_windows_destination_collision_preserves_existing_config():
with TemporaryDirectory() as directory:
target = Path(directory, 'config.toml')
target.write_text('[app]\napi_key = "keep"\n', encoding='utf-8')
example = Path(directory, 'config.example.toml')
example.write_text('[app]\napi_key = ""\n', encoding='utf-8')
with (
patch.object(config, 'config_file', str(target)),
patch.object(config.os, 'name', 'nt'),
patch.object(config.os, 'rename', side_effect=FileExistsError),
):
assert not config._initialize_config_from_example(str(example))
assert '"keep"' in target.read_text()
assert not list(Path(directory).glob('.config-init-*'))
def test_initializer_accepts_winner_published_after_docker_stub_check():
with TemporaryDirectory() as directory:
target = Path(directory, 'config.toml')
target.mkdir()
example = Path(directory, 'config.example.toml')
example.write_text('[app]\napi_key = ""\n', encoding='utf-8')
winner = '[app]\napi_key = "winning-config"\n'
remove_directory = os.rmdir
def publish_winner_before_stub_removal(path, *args, **kwargs):
# Both initializers saw the empty Docker bind-mount stub. The first
# removes it and publishes its complete regular config before the
# second reaches rmdir, which now raises real NotADirectoryError.
if str(path) == str(target):
remove_directory(path)
target.write_text(winner, encoding='utf-8')
return remove_directory(path, *args, **kwargs)
with (
patch.object(config, 'root_dir', directory),
patch.object(config, 'config_file', str(target)),
patch.object(config.os, 'rmdir', side_effect=publish_winner_before_stub_removal),
):
result = config.load_config()
assert result['app']['api_key'] == 'winning-config'
assert target.read_text() == winner
assert not list(Path(directory).glob('.config-init-*'))
def test_not_a_directory_error_does_not_hide_an_unusable_config_path():
with TemporaryDirectory() as directory:
target = Path(directory, 'config.toml')
target.mkdir()
with (
patch.object(config, 'config_file', str(target)),
patch.object(config.os, 'rmdir', side_effect=NotADirectoryError('not a regular winner')),
):
with pytest.raises(IsADirectoryError):
config.load_config()
assert target.is_dir()
def test_busy_docker_bind_mount_stub_explains_the_host_fix():
with TemporaryDirectory() as directory:
target = Path(directory, 'config.toml')
target.mkdir()
with (
patch.object(config, 'root_dir', directory),
patch.object(config, 'config_file', str(target)),
# Linux refuses to remove a live mount point with EBUSY.
patch.object(config.os, 'rmdir', side_effect=OSError(errno.EBUSY, 'Device or resource busy')),
):
with pytest.raises(IsADirectoryError, match='copy config.example.toml to config.toml'):
config.load_config()
assert target.is_dir()