Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/queue-rs-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ on:
- 'scripts/after_install.sh'
- 'scripts/validate_service.sh'
- 'scripts/tests/test_api_health.py'
# The shadow service's own definition and the environment its test stack starts from.
- 'docker-compose.yml'
- 'test.env'
- 'file-server/nginx/**'
- 'file-server/nginx.Dockerfile'
- 'server/postgres/migrations/**'
Expand Down Expand Up @@ -77,6 +80,9 @@ on:
- 'scripts/after_install.sh'
- 'scripts/validate_service.sh'
- 'scripts/tests/test_api_health.py'
# The shadow service's own definition and the environment its test stack starts from.
- 'docker-compose.yml'
- 'test.env'
- 'file-server/nginx/**'
- 'file-server/nginx.Dockerfile'
- 'server/postgres/migrations/**'
Expand Down Expand Up @@ -162,7 +168,9 @@ jobs:
polis-api:
name: polis-api build, clippy, tests, image and nginx routing
runs-on: ubuntu-24.04
timeout-minutes: 40
# Room for the shadow stack test and the comment shadow's behaviour
# series, which run in this job after the images are built.
timeout-minutes: 60
services:
postgres:
image: postgres:17-alpine
Expand Down
15 changes: 13 additions & 2 deletions ci/local/polis-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
. "$(dirname "$0")/runtime.sh"
python3 "$CHECK_ROOT/ci/vendor_postgres.py"
python3 -m unittest discover -s "$CHECK_ROOT/ci" -p test_vendor_postgres.py -v
python3 "$CHECK_ROOT/ci/host_ports.py" env POLIS_API_TEST_DATABASE_URL POLIS_AUTH_TEST_DATABASE_URL IMPORTANCE_PG_PORT IMPORTANCE_API_PORT IMPORTANCE_OFF_PORT X54_NGINX_PORT X54_FILES_PORT
python3 "$CHECK_ROOT/ci/host_ports.py" env POLIS_API_TEST_DATABASE_URL POLIS_AUTH_TEST_DATABASE_URL IMPORTANCE_PG_PORT IMPORTANCE_API_PORT IMPORTANCE_OFF_PORT X54_NGINX_PORT X54_FILES_PORT SHADOW_STACK_PORT SHADOW_STACK_TLS_PORT
GITHUB_SHA="${GITHUB_SHA:-$(git rev-parse --verify HEAD)}"
export GITHUB_SHA
: "${POLIS_API_TEST_DATABASE_URL:?provide an isolated PostgreSQL 17 database}"
Expand All @@ -14,8 +14,10 @@ export IMPORTANCE_PROJECT="${IMPORTANCE_PROJECT:-statement-importance-ci}"
export IMPORTANCE_PG_PORT="${IMPORTANCE_PG_PORT:-25463}"
export IMPORTANCE_API_PORT="${IMPORTANCE_API_PORT:-25464}"
export IMPORTANCE_OFF_PORT="${IMPORTANCE_OFF_PORT:-25465}"
python3 "$CHECK_ROOT/ci/host_ports.py" env POLIS_API_TEST_DATABASE_URL POLIS_AUTH_TEST_DATABASE_URL IMPORTANCE_PG_PORT IMPORTANCE_API_PORT IMPORTANCE_OFF_PORT X54_NGINX_PORT X54_FILES_PORT
python3 "$CHECK_ROOT/ci/host_ports.py" env POLIS_API_TEST_DATABASE_URL POLIS_AUTH_TEST_DATABASE_URL IMPORTANCE_PG_PORT IMPORTANCE_API_PORT IMPORTANCE_OFF_PORT X54_NGINX_PORT X54_FILES_PORT SHADOW_STACK_PORT SHADOW_STACK_TLS_PORT
check_project "$IMPORTANCE_PROJECT"
export SHADOW_STACK_PROJECT="${SHADOW_STACK_PROJECT:-shadow-stack-ci}"
check_project "$SHADOW_STACK_PROJECT"


(
Expand Down Expand Up @@ -100,6 +102,15 @@ check_project "$IMPORTANCE_PROJECT"
queue-rs/polis-api/conformance/nginx-shadow.sh
)

(
cd "$CHECK_ROOT/."
# The shadow service as docker-compose.yml defines it, with the override
# the web deploy writes, behind nginx and beside the Node image built above
# and a database built from the migrations: started without and with the
# comment observer's switch, paused and stopped, under real comments.
SHADOW_STACK_SERVER_IMAGE="$POLIS_VERSION_SERVER_IMAGE" queue-rs/polis-api/conformance/shadow-stack.sh
)

(
cd "$CHECK_ROOT/."
export COMPOSE_PROJECT_NAME="$IMPORTANCE_PROJECT"
Expand Down
171 changes: 169 additions & 2 deletions delphi/tests/test_after_install_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ def _find(rel):
# Real jq consumes its input; the fake must too, or pipefail can report
# SIGPIPE from the producer when this reader exits before it writes.
# The hook reads RUST_API_ROUTES from the composed config with jq.
"jq": RECORD + 'cat >/dev/null\n' + 'case "$*" in *POLIS_API_DB_TRANSPORT*) printf "%s\\n" "${FAKE_POLIS_API_TRANSPORT:-tls}";; *POLIS_API_START*) printf "%s\\n" "${FAKE_POLIS_API_START-0}";; *RUST_API_ROUTES*) printf "%s\\n" "$FAKE_RUST_API_ROUTES";; *) echo value;; esac\n',
# The comment observer's switch is answered as Compose would compose it:
# from the override file the hook wrote in its working directory, unless
# a test says what the composed configuration holds.
"jq": RECORD + 'cat >/dev/null\n' + 'case "$*" in *POLIS_API_DB_TRANSPORT*) printf "%s\\n" "${FAKE_POLIS_API_TRANSPORT:-tls}";; *POLIS_API_START*) printf "%s\\n" "${FAKE_POLIS_API_START-0}";; *RUST_API_ROUTES*) printf "%s\\n" "$FAKE_RUST_API_ROUTES";; *POLIS_API_POST_COMMENTS*) if [ -n "${FAKE_SHADOW_COMMENTS_SWITCH+set}" ]; then printf "%s\\n" "$FAKE_SHADOW_COMMENTS_SWITCH"; elif grep -qx " - POLIS_API_POST_COMMENTS=observe" docker-compose.images.yml 2>/dev/null; then echo observe; else echo; fi;; *) echo value;; esac\n',
"git": RECORD + ('case "$1" in clone) mkdir -p "${@: -1}";; rev-parse) echo 0123456789abcdef0123456789abcdef01234567;;\n'
' merge-base) [ -z "$FAKE_NOT_ON_BRANCH" ] || exit 1;; esac\n'),
"aws": RECORD + (
Expand Down Expand Up @@ -188,8 +191,10 @@ def _manifest(revision=REVISION, changes=None):

def _deploy(tmp_path, service_type, migration_failure="", revision=REVISION,
not_on_branch=False, images="default", previous=None, pull_failure_ref="", rust_api_routes="", polis_api_start="0", rust_start_failure=False, transport="tls",
shadow_start_failure=False, **kw):
shadow_start_failure=False, shadow_comments_switch=None, **kw):
root, env = _box(tmp_path, service_type, **kw)
if shadow_comments_switch is not None:
env["FAKE_SHADOW_COMMENTS_SWITCH"] = shadow_comments_switch
env["FAKE_MIGRATION_FAILURE"] = migration_failure
env["FAKE_PULL_FAILURE_REF"] = pull_failure_ref
env["FAKE_RUST_API_ROUTES"] = rust_api_routes
Expand Down Expand Up @@ -611,3 +616,165 @@ def test_the_shadow_service_carries_its_own_writer_settings():
(rate,) = [line for line in lines if line.startswith("- POLIS_API_SHADOW_MAX_PER_SECOND=")]
assert 1 <= int(rate.split("=", 1)[1]) <= 25
assert not [line for line in lines if line.startswith("ports:")], "the shadow publishes no port"


# The comment shadow (RUST_API_ROUTES names commentsShadow): nginx copies each
# comment Node answers to the same polis-api-shadow service, which starts when
# either shadow is selected. Its comment observer's switch is derived here
# from the route list and written into the Compose override the hook writes:
# observe exactly when commentsShadow is named, and otherwise nothing, which
# leaves docker-compose.yml's empty switch.
SHADOW_COMMENTS_SWITCH = '-r .services["polis-api-shadow"].environment.POLIS_API_POST_COMMENTS // ""'
SHADOW_CONFIG = COMPOSE_FILES + "--profile rust-api config --format json"
SHADOW_IMAGE_LINES = f" polis-api-shadow:\n image: {IMAGES['polis-api']}\n"
SHADOW_OBSERVE_LINES = " environment:\n - POLIS_API_POST_COMMENTS=observe\n"


def _override(tmp_path):
return (tmp_path / "root/opt/polis/polis/docker-compose.images.yml").read_text()


@pytest.mark.parametrize("routes", ["commentsShadow", "pca2,commentsShadow", " pca2 , commentsShadow ",
"pca2 commentsShadow", "commentsShadow,commentsShadow",
"comments,commentsShadow", "pca2,votesShadow,commentsShadow",
"commentsShadow,votesShadow,pca2", "pca2 votesShadow commentsShadow",
"commentsShadow,postVotes"])
def test_the_shadow_service_starts_with_the_comment_observer_when_selected(tmp_path, routes):
proc, log = _deploy(tmp_path, "server", rust_api_routes=routes)
assert proc.returncode == 0, proc.stdout + proc.stderr
calls = _compose(log)
assert [c for c in calls if c.startswith("up")] == UNCHANGED["server"] + [POLIS_API_UP, SHADOW_UP]
assert calls.index("--profile rust-api down") < calls.index(UNCHANGED["server"][0])
# One service, started once, on the polis-api digest; nothing else is pulled.
assert calls.count(SHADOW_UP) == 1
pulls = [c for c in _calls(log, "docker") if c.startswith("pull ")]
assert pulls == [f"pull {IMAGES[name]}" for name in ["migrate", *ROLE_IMAGES["server"]]]
# The override names the image and switches the comment observer on, and
# says nothing else about the service.
override = _override(tmp_path)
assert override.endswith(SHADOW_IMAGE_LINES + SHADOW_OBSERVE_LINES)
assert override.count("POLIS_API_POST_COMMENTS") == 1 and override.count("environment:") == 1
# The composed switch is read back before anything is replaced.
assert SHADOW_COMMENTS_SWITCH in _calls(log, "jq")
assert log.index(f"jq {SHADOW_COMMENTS_SWITCH}") < log.index(
f"docker-compose {COMPOSE_FILES}--profile rust-api down")
assert SHADOW_CONFIG in _calls(log, "docker-compose")
assert SHADOW_TRANSPORT in _calls(log, "jq")


@pytest.mark.parametrize("routes", ["votesShadow", "pca2,votesShadow", " pca2 , votesShadow ",
"pca2 votesShadow", "votesRead,votesShadow", "votesShadow,postComments"])
def test_the_vote_shadow_alone_leaves_the_comment_observer_off(tmp_path, routes):
proc, log = _deploy(tmp_path, "server", rust_api_routes=routes)
assert proc.returncode == 0, proc.stdout + proc.stderr
assert [c for c in _compose(log) if c.startswith("up")] == UNCHANGED["server"] + [POLIS_API_UP, SHADOW_UP]
# The override is what it was before the comment shadow existed: images only.
override = _override(tmp_path)
expected = "services:\n" + "".join(f" {name}:\n image: {IMAGES[name]}\n"
for name in ROLE_IMAGES["server"]) + SHADOW_IMAGE_LINES
assert override == expected
assert "environment" not in override and "POLIS_API_POST_COMMENTS" not in override
# The composed switch is still read back, and must be empty.
assert SHADOW_COMMENTS_SWITCH in _calls(log, "jq")


@pytest.mark.parametrize("routes", ["", " , ", "pca2", "pca2,version", "comments", "pca2,comments",
"commentsShadows", "xcommentsShadow", "commentsshadow", "CommentsShadow",
"commentShadow", "postComments"])
def test_no_shadow_selected_starts_no_shadow_service_and_writes_no_switch(tmp_path, routes):
proc, log = _deploy(tmp_path, "server", rust_api_routes=routes)
assert proc.returncode == 0, proc.stdout + proc.stderr
calls = _compose(log)
assert SHADOW_UP not in calls
assert SHADOW_TRANSPORT not in _calls(log, "jq")
assert SHADOW_COMMENTS_SWITCH not in _calls(log, "jq")
override = _override(tmp_path)
assert override.endswith(SHADOW_IMAGE_LINES)
assert "environment" not in override and "POLIS_API_POST_COMMENTS" not in override
assert calls.index("--profile rust-api down") < calls.index(UNCHANGED["server"][0])


@pytest.mark.parametrize("routes", ["postComments,commentsShadow", "commentsShadow postComments",
"pca2,votesShadow, postComments ,commentsShadow"])
def test_the_comment_shadow_and_a_live_comment_route_are_refused_together(tmp_path, routes):
proc, log = _deploy(tmp_path, "server", rust_api_routes=routes)
assert proc.returncode != 0
assert "commentsShadow and postComments" in proc.stderr
# Refused before any image, migration or service replacement.
assert not [c for c in _calls(log, "docker") if c.startswith(("pull", "run"))]
assert not [c for c in _compose(log) if c.endswith("down") or c.startswith("up")]


def test_the_vote_refusal_still_applies_beside_the_comment_shadow(tmp_path):
proc, log = _deploy(tmp_path, "server", rust_api_routes="commentsShadow,postVotes,votesShadow")
assert proc.returncode != 0
assert "votesShadow and postVotes" in proc.stderr
assert not [c for c in _calls(log, "docker") if c.startswith(("pull", "run"))]


@pytest.mark.parametrize("routes,composed", [
("pca2,commentsShadow", ""), ("pca2,commentsShadow", "0"), ("pca2,commentsShadow", "1"),
("pca2,votesShadow,commentsShadow", ""), ("pca2,votesShadow,commentsShadow", "Observe"),
("pca2,votesShadow", "observe"), ("pca2,votesShadow", "1"), ("pca2,votesShadow", "0"),
("votesShadow", "value"),
])
def test_a_composed_switch_that_does_not_follow_the_route_list_is_refused_before_replacement(tmp_path, routes, composed):
proc, log = _deploy(tmp_path, "server", rust_api_routes=routes, shadow_comments_switch=composed)
assert proc.returncode != 0
assert "comment observer switch does not follow RUST_API_ROUTES" in proc.stderr
assert not [c for c in _compose(log) if c.endswith("down") or c.startswith("up")]
assert not (tmp_path / "root/opt/polis/deployed-images.txt").exists()


@pytest.mark.parametrize("routes,composed", [("pca2,commentsShadow", "observe"), ("pca2,votesShadow", ""),
("pca2,votesShadow,commentsShadow", "observe")])
def test_a_composed_switch_that_follows_the_route_list_is_accepted(tmp_path, routes, composed):
proc, log = _deploy(tmp_path, "server", rust_api_routes=routes, shadow_comments_switch=composed)
assert proc.returncode == 0, proc.stdout + proc.stderr
assert SHADOW_UP in _compose(log)


@pytest.mark.parametrize("routes", ["commentsShadow", "pca2,votesShadow,commentsShadow"])
def test_a_comment_shadow_start_failure_fails_the_deploy(tmp_path, routes):
proc, log = _deploy(tmp_path, "server", rust_api_routes=routes, shadow_start_failure=True)
assert proc.returncode != 0
assert "polis-api-shadow start failed" in proc.stderr
assert not (tmp_path / "root/opt/polis/deployed-images.txt").exists()


@pytest.mark.parametrize("transport", ["docker-internal", "loopback", "local"])
def test_the_comment_shadow_refuses_unverified_transport_before_down(tmp_path, transport):
proc, log = _deploy(tmp_path, "server", rust_api_routes="commentsShadow", transport=transport)
assert proc.returncode != 0
assert not [c for c in _compose(log) if c.endswith("down") or c.startswith("up")]


@pytest.mark.parametrize("service_type,worker_class", [("delphi", None), ("math", None),
("delphi-large", "large"), ("delphi-worker", "delphi")])
def test_other_roles_never_read_or_write_the_comment_switch(tmp_path, service_type, worker_class):
proc, log = _deploy(tmp_path, service_type, worker_class=worker_class,
rust_api_routes="pca2,votesShadow,commentsShadow")
assert proc.returncode == 0, proc.stdout + proc.stderr
assert SHADOW_COMMENTS_SWITCH not in _calls(log, "jq")
assert "polis-api-shadow" not in _override(tmp_path)


def test_the_shadow_service_carries_the_comment_observers_settings_with_its_switch_off():
# The switch is a literal empty value, so neither this file nor the env
# document can turn the comment observer on; what the observer requires is
# always present, so the hook's override line is the only thing that changes.
compose = _find(Path("docker-compose.yml")).read_text()
block = compose.split("\n polis-api-shadow:\n", 1)[1].split("\n\n", 1)[0]
lines = [line.strip() for line in block.splitlines()]
assert "- POLIS_API_POST_COMMENTS=" in lines
assert not [line for line in lines if line.startswith("- POLIS_API_POST_COMMENTS=") and line != "- POLIS_API_POST_COMMENTS="]
assert "- POLIS_API_POST_COMMENTS_DATABASE_URL=${DATABASE_URL:-}" in lines
assert "- SHOULD_USE_TRANSLATION_API=false" in lines
(rate,) = [line for line in lines if line.startswith("- POLIS_API_SHADOW_COMMENTS_MAX_PER_SECOND=")]
assert 1 <= int(rate.split("=", 1)[1]) <= 6
# The vote observer's settings are untouched.
assert "- POLIS_API_POST_VOTES=observe" in lines
assert "- POLIS_API_SHADOW_MAX_PER_SECOND=8" in lines
# Nothing else in the file names the comment handler's switch: the main
# polis-api takes it from the env document, as it takes the vote switch.
assert compose.count("POLIS_API_POST_COMMENTS=") == 1
60 changes: 59 additions & 1 deletion delphi/tests/test_validate_service_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def probe(tmp_path, role='server', state='running false none', http=0, missing=F
case "$1" in
ps) [ "$MISSING" = 1 ] || case "${@: -1}" in polis-api) echo rust;; polis-api-shadow) echo shadow;; *) echo owned;; esac ;;
exec) case "$*" in *polis-api:5100*) echo rust >> "$CALLS"; exit "$RUST_HTTP";;
*polis-api-shadow:5101*) echo shadow >> "$CALLS"; exit "$SHADOW_HTTP";;
*polis-api-shadow:5101*) echo shadow >> "$CALLS"; echo "${@: -1}" >> "$CALLS.comments"; exit "$SHADOW_HTTP";;
*) echo http >> "$CALLS"; exit "$HTTP_STATUS";; esac ;;
*) exit 90 ;; esac''',
'jq':'''cat >/dev/null
Expand Down Expand Up @@ -130,3 +130,61 @@ def test_shadow_with_the_live_vote_writer_is_refused_before_polling(tmp_path, ro
assert 'votesShadow and postVotes' in p.stderr
assert not (tmp_path/'calls').exists()


# The comment shadow (commentsShadow) runs in the same service. The shadow's
# health is required when either shadow is selected, and its predicate is
# told, as its last argument, whether the comment observer must be built
# ("1") or must not be ("0").
def comment_arguments(tmp_path):
path = tmp_path/'calls.comments'
return path.read_text().splitlines() if path.exists() else []


@pytest.mark.parametrize("routes,probed,comments", [
("", False, None), ("pca2", False, None), ("version", False, None), ("pca2,version", False, None),
("pca2,comments", False, None), ("votesRead", False, None),
("votesShadow", True, "0"), ("pca2,votesShadow", True, "0"), (" pca2 votesShadow ", True, "0"),
("pca2,version,votesShadow", True, "0"), ("votesRead,votesShadow", True, "0"),
("commentsShadow", True, "1"), ("pca2,commentsShadow", True, "1"), (" pca2 commentsShadow ", True, "1"),
("pca2,comments,commentsShadow", True, "1"),
("pca2,votesShadow,commentsShadow", True, "1"), ("commentsShadow,votesShadow", True, "1"),
("pca2 votesShadow commentsShadow", True, "1"), ("pca2,commentsShadow,commentsShadow", True, "1"),
("commentsShadow,postVotes", True, "1"), ("votesShadow,postComments", True, "0"),
("commentsShadows", False, None), ("xcommentsShadow", False, None), ("commentsshadow", False, None),
("CommentsShadow", False, None), ("pca2,commentShadow", False, None),
])
def test_the_shadow_is_probed_for_either_shadow_and_told_which(tmp_path, routes, probed, comments):
# A selected route also makes the main polis-api's health required, as before.
named = bool(routes.replace(',', ' ').split())
p = probe(tmp_path, routes=routes)
assert p.returncode == 0, p.stderr
expected = ['http'] + (['rust'] if named else []) + (['shadow'] if probed else [])
assert (tmp_path/'calls').read_text().splitlines() == expected * 3
assert p.stdout.count('polis-api-shadow health ok') == (3 if probed else 0)
assert comment_arguments(tmp_path) == ([comments] * 3 if probed else [])


@pytest.mark.parametrize("routes", ["commentsShadow", "pca2,votesShadow,commentsShadow"])
@pytest.mark.parametrize("kw", [dict(shadow_http=1), dict(shadow_state='running true none'),
dict(shadow_state='exited false none')])
def test_comment_shadow_failure_rejects_validation(tmp_path, routes, kw):
p = probe(tmp_path, routes=routes, **kw)
assert p.returncode != 0
assert 'polis-api-shadow health failed' in p.stderr
assert 'ValidateService failed' in p.stderr


@pytest.mark.parametrize("routes", ["postComments,commentsShadow", "commentsShadow postComments",
"pca2,votesShadow, postComments ,commentsShadow"])
def test_comment_shadow_with_a_live_comment_route_is_refused_before_polling(tmp_path, routes):
p = probe(tmp_path, routes=routes)
assert p.returncode != 0
assert 'commentsShadow and postComments' in p.stderr
assert not (tmp_path/'calls').exists()


def test_both_refusals_name_the_vote_pair_first(tmp_path):
p = probe(tmp_path, routes='votesShadow,postVotes,commentsShadow,postComments')
assert p.returncode != 0
assert 'votesShadow and postVotes' in p.stderr
assert not (tmp_path/'calls').exists()
Loading
Loading