Skip to content

test(api): cover the install endpoint through steps 1-8 of 9 - #8

Merged
nusendra merged 1 commit into
fix/stale-icon-in-render-testsfrom
test/install-endpoint
Oct 29, 2026
Merged

nusendra merged 1 commit into
fix/stale-icon-in-render-testsfrom
test/install-endpoint

Conversation

@nusendra

Copy link
Copy Markdown
Owner

Stacked on #7 (base is fix/stale-icon-in-render-tests; GitHub will retarget this to main automatically when #7 merges).

37 tests for POST /api/v1/templates/:slug/install, in crates/ployer-api/src/routes/templates_tests.rs. No production logic changed — the only edit to templates.rs is the four-line #[cfg(test)] mod tests; wiring the crate already uses for helpers_tests.rs, jwt_tests.rs and friends.

Coverage

The handler is nine steps. Steps 1-8 are covered; step 9 is not reachable.

# Step Covered
1 extract_user_id yes — 4 tests (missing header, non-bearer, foreign secret, and that a 401 writes nothing)
2 app_name / server_id validation yes — 5 tests (empty + whitespace for each, and that a 400 writes nothing)
3 templates.get(&slug) yes — 2 tests (404 on unknown slug, and that it writes nothing)
4 render yes — 6 tests (missing input 400, non-numeric number 400, no write on failure, supplied value wins, default fallback, empty-string-is-absent)
5 add_ports_to_all_services yes — 6 tests (default binding, host-port override, every service, no-port template is a no-op, no expose publishes nothing, host_ports inert without expose)
6 create_from_template yes — 5 tests (compose is rendered not raw, template_slug, docker_compose strategy + idle status, name trimmed, dangling server_id → 500)
7 EnvVarRepository::create + crypto::encrypt yes — 4 tests (all inputs stored, round-trip through config.get_secret_key(), generated secret absent from the ciphertext but present in the compose, no inputs → no rows)
8 Docker gate yes — 1 test (503)
9 deploy_compose no — unreachable, see below

Plus 2 tests for the InstallRequest body contract (serde defaults, and that app_name is mandatory so the body never reaches the handler), and 2 named characterises_defect_* tests described below.

Why step 9 is out of reach

AppState.docker is a concrete Option<Arc<DockerClient>> (bollard) with no trait seam, so a test can only ever pass None, which makes step 8 return 503. Reaching a 200 requires DockerClient and CaddyClient behind traits — a production refactor, deliberately not done here.

Two defects pinned, not fixed

characterises_defect_a_503_leaves_an_orphaned_application_behind — steps 6 and 7 write the application and its encrypted env vars before step 8 checks for Docker, with no rollback. A Docker-less host answers 503 having persisted an application that was never deployed.

characterises_defect_the_same_app_name_can_be_installed_twice — nothing rejects a repeated name, and containers are named ployer-<app.slug>-<service>, so two same-named applications render colliding container names.

Both are named so a fix produces a loud, self-explanatory failure rather than a silent behaviour change. Note that fixing the first one makes steps 6 and 7 unobservable until the Docker seam exists — today the bug is the only reason those steps are testable at all.

Hermeticity

No network, no clock, no shared state. Each test gets its own in-memory SQLite pool (max_connections(1), foreign_keys(true) to match ployer_db::create_pool) and its own temp registry cache, removed on drop. registry_url uses a scheme reqwest refuses in the request builder, so Registry::index() fails without opening a socket and falls back to the pre-seeded disk cache.

Verification

  • SQLX_OFFLINE=true cargo test --workspace — 178 passed, 0 failed (was 141).
  • cargo clippy --workspace --all-targets -- -D warnings clean on stable and on pinned 1.99.1.
  • 12 mutations of the handler and of add_ports_to_all_services were each caught by the test that claims the behaviour; no test passes regardless of the implementation.
  • Passes single-threaded and in parallel, three consecutive runs, no leftover temp dirs.
  • ployer-api binary suite: 4.10s → 7.06s (+37 tests; the cost is running the 7 migrations once per test).

@nusendra

Copy link
Copy Markdown
Owner Author

Heads-up on a cross-PR break, the kind DOCUMENTATION.md warns about: each PR is green on its own head, and git merge-tree reports no conflict, but cargo test on main fails after both land.

#10 (bind published ports to loopback by default) adds a bind: BindAddress field to InstallRequest in routes/templates.rs, and makes the install endpoint write the chosen interface into the stored compose.

Two things in templates_tests.rs break, whichever of us merges second:

  1. request() at line 318 builds InstallRequest as a struct literal, so it needs bind: BindAddress::default(), — otherwise E0063, missing field.

  2. Three assertions expect the two-part port spec. The compose now records the interface explicitly:

    • expose_publishes_each_template_port_on_the_matching_host_port: vec!["8080:8080"] -> vec!["127.0.0.1:8080:8080"]
    • expose_honours_an_explicit_host_port_override: vec!["18080:8080"] -> vec!["127.0.0.1:18080:8080"]
    • expose_publishes_the_ports_on_every_service: both vec!["8080:8080", "9090:9090"] -> vec!["127.0.0.1:8080:8080", "127.0.0.1:9090:9090"]

expose_on_a_template_without_ports_leaves_the_compose_unchanged, without_expose_the_compose_publishes_no_ports and host_ports_are_ignored_when_expose_is_false are unaffected.

Worth adding while you are in there: req.bind = BindAddress::AllInterfaces should produce 0.0.0.0:8080:8080. That is the one install-endpoint behaviour #10 cannot pin from ployer-templates or ployer-docker — it only covers the two ends, not the handler that wires them.

No action needed yet; just so neither of us merges into a red main.

@nusendra
nusendra merged commit 6638a1f into fix/stale-icon-in-render-tests Oct 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant