Files
ServerKit/backend/tests/test_docker_list_unreachable.py
Juan Denis db5f4df74a Show Docker list errors and ignore stale loads
The API now answers 503 when docker ps fails instead of an empty list,
so the Containers page no longer says "No containers" on a host
running many. The Containers and Images tabs show that error, and a
late answer from the previously selected server can no longer replace
the current server's list. Also fixes the local branch of the remote
Docker listing, which passed all= to a parameter named all_containers
and always failed.
2026-10-10 14:01:56 -04:00

41 lines
1.8 KiB
Python

"""Docker being down is an error, not an empty host.
list_containers returned [] when `docker ps` failed, so the Containers
page said "no containers" on a box running dozens. The local branch of
RemoteDockerService also passed `all=` to a function whose parameter is
`all_containers`, so it always failed with a TypeError.
"""
import pytest
from app.exceptions import DependencyUnavailableError
from app.services.docker_service import DockerService
from app.services.remote_docker_service import RemoteDockerService
from factories import headers_for, make_user
PS_ROW = ('{"ID": "abc", "Names": "web", "Image": "nginx", "Status": "Up", '
'"State": "running", "Ports": "", "CreatedAt": "now", "Size": "0B"}')
def test_daemon_down_raises_instead_of_listing_nothing(fake_subprocess):
fake_subprocess.script(['docker', 'ps'], returncode=1,
stderr='Cannot connect to the Docker daemon')
with pytest.raises(DependencyUnavailableError, match='Cannot connect'):
DockerService.list_containers()
def test_containers_route_answers_503_when_docker_is_down(app, client, fake_subprocess):
from app import db
headers = headers_for(make_user(db, 'dk_admin', 'admin'))
fake_subprocess.script(['docker', 'ps'], returncode=1,
stderr='Cannot connect to the Docker daemon')
resp = client.get('/api/v1/docker/containers', headers=headers)
assert resp.status_code == 503
assert 'Docker is not reachable' in resp.get_json()['error']
def test_local_remote_docker_listing_works(fake_subprocess):
fake_subprocess.script(['docker', 'ps'], stdout=PS_ROW)
result = RemoteDockerService.list_containers('local', all=True)
assert result['success'], result
assert [c['name'] for c in result['data']] == ['web']