Stream the pool-retry test through the shared stub

The streaming retry test patched subprocess.Popen by hand, which
the stub-adoption ratchet counts. The shared FakePopen could not
stand in because its stdout had no readline(); it does now, so the
test scripts the two compose runs through fake_subprocess instead.
This commit is contained in:
Juan Denis committed 2026-11-14 22:54:09 -05:00
1 parent 9d97a1c305
commit 43c4872b56
2 files changed
+32 -23

No files matched your search

+23 -3
View File
@@ -88,6 +88,27 @@ class FakeProc:
f'stdout={self.stdout!r}, stderr={self.stderr!r})')
class _Lines:
"""A pipe's line stream: iterable, and readable the way
``iter(stream.readline, '')`` reads it, which is how the streaming
callers consume a live process."""
def __init__(self, text):
self._lines = iter(text.splitlines(keepends=True)) if text else iter(())
def __iter__(self):
return self
def __next__(self):
return next(self._lines)
def readline(self):
return next(self._lines, '')
def close(self):
pass
class FakePopen:
"""Minimal ``Popen`` stand-in over a :class:`FakeProc`.
@@ -101,9 +122,8 @@ class FakePopen:
self._proc = proc
self.args = proc.args
self.returncode = None
text = proc.stdout or ''
self.stdout = iter(text.splitlines(keepends=True)) if text else iter(())
self.stderr = iter(())
self.stdout = _Lines(proc.stdout or '')
self.stderr = _Lines('')
def wait(self, timeout=None):
self.returncode = self._proc.returncode
+9 -20
View File
@@ -16,6 +16,7 @@ import yaml
from app.services import compose_network_service as cns
from app.services.docker_service import DockerService
from subprocess_stub import FakeProc
EXHAUSTED = ('failed to create network site_default: Error response from daemon: '
'all predefined address pools have been fully subnetted')
@@ -220,30 +221,18 @@ class TestComposeUpRetries:
assert not result['success'] and len(calls) == 1
assert 'networks' not in _compose(path)
def test_streaming_up_pins_retries_and_says_what_it_did(self, tmp_path, monkeypatch):
def test_streaming_up_pins_retries_and_says_what_it_did(self, tmp_path, monkeypatch,
fake_subprocess):
path = _project(tmp_path, SIMPLE)
runs = []
class _Stdout:
def __init__(self, lines):
self._lines = iter(lines)
def up(argv, kwargs):
runs.append(argv)
if len(runs) == 1:
return FakeProc(returncode=1, stdout=EXHAUSTED + '\n', args=argv)
return FakeProc(stdout='started\n', args=argv)
def readline(self):
return next(self._lines, '')
def close(self):
pass
class _Proc:
def __init__(self, cmd, **kwargs):
runs.append(cmd)
self._code = 1 if len(runs) == 1 else 0
self.stdout = _Stdout([EXHAUSTED + '\n'] if self._code else ['started\n'])
def wait(self):
return self._code
monkeypatch.setattr('subprocess.Popen', _Proc)
fake_subprocess.when(['docker', 'compose'], up)
monkeypatch.setattr(DockerService, '_is_compose_v2', classmethod(lambda cls: True))
lines = []
result = DockerService.compose_up_streaming(path, lines.append)