This PR contains the work done to introduce a _Fluent_-based persistence layer for the Website service, selectable at runtime alongside the existing in-memory default, plus the local dev tooling and docs to support it. To provide further details about the work: * Persistence package * The `Driver` and `TLS` enumerations * The `Configuration` type * The `Service` factory that builds the service * `PrepareDB` for migrations registration * The `Probe` for readiness checks. * App integration * Builds the driver, registers migrations, and attaches `Fluent` to the service lifecycle so it starts/stops with the HTTP server. * Migrate-on-boot is gated to the in-memory backend; MySQL/MariaDB is migrated out of band via --database-migrate so shared databases never race on startup. * The `ConfigReader+Properties` extension maps database.* config keys onto the driver. * Library * Added database configuration constants. * The `HealthController` controller gains a readiness probe: `GET /health/ready` checks whether the database is reachable, separate from the existing liveness check. * Others * Updated the `docker-compose` files to support a database service behind a database profile, and hardened for local development * New database targets on the `Makefile` file and overall documentation updated * Updated the `.env.local`, `Dockerfile`, and `README` files to document the persistence workflow, config keys, and local DB commands Reviewed-on: rock-n-code/loud-amsterdam#13 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
143 lines
4.1 KiB
Swift
143 lines
4.1 KiB
Swift
import Hummingbird
|
|
import HummingbirdTesting
|
|
import Logging
|
|
import NIOCore
|
|
import Persistence
|
|
import Testing
|
|
|
|
@testable import WebsiteCore
|
|
|
|
@Suite("HealthController controller")
|
|
struct HealthControllerTests {
|
|
|
|
// MARK: Functional tests
|
|
|
|
@Test
|
|
func `serves the status payload at the health path`() async throws {
|
|
try await app(
|
|
probe: nil
|
|
).test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/health",
|
|
method: .get
|
|
) { response in
|
|
let body = String(buffer: response.body)
|
|
|
|
#expect(response.status == .ok)
|
|
#expect(response.headers[.contentType] == "application/json")
|
|
#expect(body == #"{"status":"ok"}"#)
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func `serves ready at the readiness path when the database is reachable`() async throws {
|
|
let service = Service(
|
|
driver: .inMemory,
|
|
logger: Logger(label: "test")
|
|
)
|
|
let fluent = service()
|
|
|
|
do {
|
|
try await app(
|
|
probe: Probe(fluent: fluent)
|
|
).test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/health/ready",
|
|
method: .get
|
|
) { response in
|
|
let body = String(buffer: response.body)
|
|
|
|
#expect(response.status == .ok)
|
|
#expect(response.headers[.contentType] == "application/json")
|
|
#expect(body == #"{"status":"ready"}"#)
|
|
}
|
|
}
|
|
} catch {
|
|
try? await fluent.shutdown()
|
|
|
|
throw error
|
|
}
|
|
|
|
try await fluent.shutdown()
|
|
}
|
|
|
|
@Test
|
|
func `serves unavailable at the readiness path when the database is unreachable`() async throws {
|
|
// Port 1 on the loopback interface has nothing listening, so the probe's connection is refused
|
|
// immediately instead of timing out.
|
|
let service = Service(
|
|
driver: .mysql(
|
|
.init(
|
|
host: "127.0.0.1",
|
|
port: 1,
|
|
name: "unreachable",
|
|
username: "nobody",
|
|
password: "nothing",
|
|
tls: .off,
|
|
maxConnectionsPerEventLoop: 1
|
|
)
|
|
),
|
|
logger: Logger(label: "test")
|
|
)
|
|
let fluent = service()
|
|
|
|
do {
|
|
try await app(
|
|
probe: Probe(fluent: fluent)
|
|
).test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/health/ready",
|
|
method: .get
|
|
) { response in
|
|
let body = String(buffer: response.body)
|
|
|
|
#expect(response.status == .serviceUnavailable)
|
|
#expect(response.headers[.contentType] == "application/json")
|
|
#expect(body == #"{"status":"unavailable"}"#)
|
|
}
|
|
}
|
|
} catch {
|
|
try? await fluent.shutdown()
|
|
|
|
throw error
|
|
}
|
|
|
|
try await fluent.shutdown()
|
|
}
|
|
|
|
@Test
|
|
func `does not serve the readiness path without a probe`() async throws {
|
|
try await app(probe: nil).test(.router) { client in
|
|
try await client.execute(
|
|
uri: "/health/ready",
|
|
method: .get
|
|
) { response in
|
|
#expect(response.status == .notFound)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private extension HealthControllerTests {
|
|
|
|
/// Builds a test application serving the ``HealthController`` routes for the given probe.
|
|
/// - Parameter probe: the probe supplied to the controller, or `nil` for liveness only.
|
|
/// - Returns: the configured test application.
|
|
func app(
|
|
probe: Probe?
|
|
) -> some ApplicationProtocol {
|
|
Application(router: {
|
|
let router = Router()
|
|
|
|
router.addRoutes(HealthController<BasicRequestContext>(probe: probe).routes)
|
|
|
|
return router
|
|
}())
|
|
}
|
|
|
|
}
|