You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
test(api): cover the install endpoint through steps 1-8 of 9 - #8
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.
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).
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:
request() at line 318 builds InstallRequest as a struct literal, so it needs bind: BindAddress::default(), — otherwise E0063, missing field.
Three assertions expect the two-part port spec. The compose now records the interface explicitly:
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #7 (base is
fix/stale-icon-in-render-tests; GitHub will retarget this tomainautomatically when #7 merges).37 tests for
POST /api/v1/templates/:slug/install, incrates/ployer-api/src/routes/templates_tests.rs. No production logic changed — the only edit totemplates.rsis the four-line#[cfg(test)] mod tests;wiring the crate already uses forhelpers_tests.rs,jwt_tests.rsand friends.Coverage
The handler is nine steps. Steps 1-8 are covered; step 9 is not reachable.
extract_user_idapp_name/server_idvalidationtemplates.get(&slug)renderadd_ports_to_all_servicesexposepublishes nothing,host_portsinert withoutexpose)create_from_templatetemplate_slug,docker_composestrategy + idle status, name trimmed, danglingserver_id→ 500)EnvVarRepository::create+crypto::encryptconfig.get_secret_key(), generated secret absent from the ciphertext but present in the compose, no inputs → no rows)deploy_composePlus 2 tests for the
InstallRequestbody contract (serdedefaults, and thatapp_nameis mandatory so the body never reaches the handler), and 2 namedcharacterises_defect_*tests described below.Why step 9 is out of reach
AppState.dockeris a concreteOption<Arc<DockerClient>>(bollard) with no trait seam, so a test can only ever passNone, which makes step 8 return 503. Reaching a 200 requiresDockerClientandCaddyClientbehind 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 namedployer-<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 matchployer_db::create_pool) and its own temp registry cache, removed on drop.registry_urluses a scheme reqwest refuses in the request builder, soRegistry::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 warningsclean on stable and on pinned 1.99.1.add_ports_to_all_serviceswere each caught by the test that claims the behaviour; no test passes regardless of the implementation.ployer-apibinary suite: 4.10s → 7.06s (+37 tests; the cost is running the 7 migrations once per test).