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
126 changes: 126 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
name: CI

# switchboard is a small pure-Rust crate — no PHP, no SDK, no Docker. The
# gate is just: does it format, lint clean, and pass its tests. Runs on the
# ephpm org's self-hosted (ephemerd) fleet, same `[self-hosted, linux, x64]`
# label ephpm/ephpm's ci.yml uses so the fleet picks these jobs up.
#
# NOTE: "pure Rust" does NOT mean "no C toolchain". The ephemerd runner images
# ship without a compiler, and several unavoidable transitive deps have build
# scripts (proc-macro2, libc) or compile C (ring, via reqwest's rustls stack).
# Without `cc` on PATH every compile job dies at "linker `cc` not found" before
# a single crate of ours is checked — which is exactly what the first run of
# this workflow did. So every job that COMPILES installs build-essential first,
# mirroring ephpm/ephpm's ci.yml. The fmt job is the one exception: it parses,
# never compiles, so it needs no toolchain beyond rustfmt.
on:
push:
branches: [main]
pull_request:
# `main` plus long-lived feature branches other PRs stack onto — without
# the glob a PR targeting e.g. feat/deploy-pipeline gets no checks at all.
branches: [main, "feat/**"]

env:
CARGO_TERM_COLOR: always

jobs:
fmt:
name: Format
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
# No rustfmt.toml in this repo — no nightly-only options (group_imports /
# imports_granularity), so stable rustfmt is sufficient and correct.
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- run: cargo fmt --all -- --check

clippy:
name: Clippy
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
- name: Install build prerequisites
# switchboard needs only a C compiler/linker (`cc`) — no bindgen, no
# openssl-sys (reqwest uses rustls). Some fleet runners come up as root
# without `sudo`, others as non-root with it; a bare `sudo apt-get`
# exits 127 on the former and reds the job for reasons unrelated to the
# change. Resolve the escalation method at run time. (Pattern lifted
# from ephpm/ephpm's ci.yml.)
run: |
set -eu
if ! command -v apt-get >/dev/null 2>&1; then
echo "apt-get unavailable; assuming the image already provides build prerequisites"
exit 0
fi
if [ "$(id -u)" -eq 0 ]; then
SUDO=""
elif command -v sudo >/dev/null 2>&1; then
SUDO="sudo"
else
echo "::error::not running as root and sudo is unavailable"
exit 1
fi
$SUDO apt-get update
$SUDO apt-get install -y --no-install-recommends build-essential pkg-config
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --all-targets -- -D warnings

test:
name: Test (linux-x64)
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
- name: Install build prerequisites
run: |
set -eu
if ! command -v apt-get >/dev/null 2>&1; then
echo "apt-get unavailable; assuming the image already provides build prerequisites"
exit 0
fi
if [ "$(id -u)" -eq 0 ]; then
SUDO=""
elif command -v sudo >/dev/null 2>&1; then
SUDO="sudo"
else
echo "::error::not running as root and sudo is unavailable"
exit 1
fi
$SUDO apt-get update
$SUDO apt-get install -y --no-install-recommends build-essential pkg-config
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo test

msrv:
name: MSRV (cargo check)
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
- name: Install build prerequisites
run: |
set -eu
if ! command -v apt-get >/dev/null 2>&1; then
echo "apt-get unavailable; assuming the image already provides build prerequisites"
exit 0
fi
if [ "$(id -u)" -eq 0 ]; then
SUDO=""
elif command -v sudo >/dev/null 2>&1; then
SUDO="sudo"
else
echo "::error::not running as root and sudo is unavailable"
exit 1
fi
$SUDO apt-get update
$SUDO apt-get install -y --no-install-recommends build-essential pkg-config
# Pinned to the crate's declared rust-version (Cargo.toml). A build that
# needs a newer toolchain than we advertise is a bug in the manifest.
- uses: dtolnay/rust-toolchain@1.85
- uses: Swatinem/rust-cache@v2
- run: cargo check --all-targets
92 changes: 92 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,95 @@ pub struct Config {
#[arg(long, default_value_t = 2, env = "SWITCHBOARD_HEALTH_INTERVAL_SECS")]
pub health_interval_secs: u64,
}

#[cfg(test)]
mod tests {
use super::*;

/// The three flags with no default and no `Option` — a parse must supply
/// them or fail. Kept as a helper so each test states only what it varies.
const REQUIRED: &[&str] = &[
"switchboard",
"--webhook-secret",
"s3cr3t",
"--app-key",
"/etc/switchboard/app.pem",
"--app-id",
"12345",
];

fn parse(extra: &[&str]) -> Config {
let args = REQUIRED.iter().chain(extra.iter());
Config::try_parse_from(args).expect("expected a valid config parse")
}

#[test]
fn defaults_applied_when_only_required_given() {
let c = parse(&[]);
assert_eq!(c.listen, "0.0.0.0:9090");
assert_eq!(c.sites_dir, PathBuf::from("/var/www/sites"));
assert_eq!(c.preview_domain, "preview.ephpm.dev");
assert_eq!(c.composer, "composer");
assert_eq!(c.health_timeout_secs, 60);
assert_eq!(c.health_interval_secs, 2);
// Optional-with-no-default stays None.
assert!(c.secrets_file.is_none());
// Required values round-trip.
assert_eq!(c.webhook_secret, "s3cr3t");
assert_eq!(c.app_key, PathBuf::from("/etc/switchboard/app.pem"));
assert_eq!(c.app_id, 12345);
}

#[test]
fn explicit_flags_override_defaults() {
let c = parse(&[
"--listen",
"127.0.0.1:1234",
"--sites-dir",
"/srv/previews",
"--preview-domain",
"pr.example.com",
"--composer",
"/usr/local/bin/composer",
"--secrets-file",
"/etc/switchboard/secrets.yaml",
"--health-timeout-secs",
"5",
"--health-interval-secs",
"1",
]);
assert_eq!(c.listen, "127.0.0.1:1234");
assert_eq!(c.sites_dir, PathBuf::from("/srv/previews"));
assert_eq!(c.preview_domain, "pr.example.com");
assert_eq!(c.composer, "/usr/local/bin/composer");
assert_eq!(
c.secrets_file,
Some(PathBuf::from("/etc/switchboard/secrets.yaml"))
);
assert_eq!(c.health_timeout_secs, 5);
assert_eq!(c.health_interval_secs, 1);
}

#[test]
fn missing_required_flag_is_an_error() {
// Drop --app-id (and its value) — parsing must fail rather than
// silently defaulting a security-relevant field.
let args = ["switchboard", "--webhook-secret", "x", "--app-key", "/k"];
assert!(Config::try_parse_from(args).is_err());
}

#[test]
fn non_numeric_app_id_is_rejected() {
// app_id is a u64; a non-numeric value must fail parsing, not truncate.
let args = [
"switchboard",
"--webhook-secret",
"x",
"--app-key",
"/k",
"--app-id",
"not-a-number",
];
assert!(Config::try_parse_from(args).is_err());
}
}
Loading
Loading