Compare commits
5 Commits
v0.1.0-dev
...
v0.1.0-dev
Author | SHA1 | Date | |
---|---|---|---|
5b0592f51d | |||
7e189e84d8 | |||
79f6ebdc09 | |||
68a9998572 | |||
b6787de66b |
@@ -32,6 +32,22 @@ jobs:
|
|||||||
test:
|
test:
|
||||||
name: Test Suite
|
name: Test Suite
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
|
# --- Add database service definition ---
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:17.4 # Or pin to a more specific version like 14.9
|
||||||
|
env:
|
||||||
|
# Use secrets for DB init, with fallbacks for flexibility
|
||||||
|
POSTGRES_USER: ${{ secrets.DB_TEST_USER || 'testuser' }}
|
||||||
|
POSTGRES_PASSWORD: ${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}
|
||||||
|
POSTGRES_DB: ${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||||
|
# Options to wait until the database is ready
|
||||||
|
options: >-
|
||||||
|
--health-cmd pg_isready
|
||||||
|
--health-interval 10s
|
||||||
|
--health-timeout 5s
|
||||||
|
--health-retries 5
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
@@ -51,7 +67,34 @@ jobs:
|
|||||||
echo "Docker environment check complete."
|
echo "Docker environment check complete."
|
||||||
# NOTE: Do NOT use continue-on-error here.
|
# NOTE: Do NOT use continue-on-error here.
|
||||||
# If Docker isn't working as expected, the job SHOULD fail here.
|
# If Docker isn't working as expected, the job SHOULD fail here.
|
||||||
- run: |
|
# --- Optional but Recommended: Database Migrations Step ---
|
||||||
|
- name: Run Database Migrations
|
||||||
|
env:
|
||||||
|
# Define TEST_DATABASE_URL using service details and secrets
|
||||||
|
TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||||
|
# Make SSH agent available if migrations fetch private dependencies
|
||||||
|
SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }}
|
||||||
|
run: |
|
||||||
|
echo "Running database migrations..."
|
||||||
|
# ===> IMPORTANT: Replace placeholder below with your actual migration command <===
|
||||||
|
# Example: Install and run sqlx-cli
|
||||||
|
# cargo install sqlx-cli --no-default-features --features native-tls,postgres
|
||||||
|
# sqlx database setup --database-url $TEST_DATABASE_URL
|
||||||
|
|
||||||
|
# Example: Install and run diesel_cli
|
||||||
|
# cargo install diesel_cli --no-default-features --features postgres
|
||||||
|
# diesel migration run --database-url $TEST_DATABASE_URL
|
||||||
|
|
||||||
|
# echo "[Placeholder] Your migration command goes here."
|
||||||
|
# ===> End of Placeholder <===
|
||||||
|
- name: Run tests
|
||||||
|
env:
|
||||||
|
# Define TEST_DATABASE_URL for tests to use
|
||||||
|
TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||||
|
RUST_LOG: info # Optional: configure test log level
|
||||||
|
# Make SSH agent available if tests fetch private dependencies
|
||||||
|
SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }}
|
||||||
|
run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||||
chmod 600 ~/.ssh/gitlab_deploy_key
|
chmod 600 ~/.ssh/gitlab_deploy_key
|
||||||
|
@@ -12,7 +12,7 @@ tracing-subscriber = { version = "0.3.19" }
|
|||||||
tower = { version = "0.5.2" }
|
tower = { version = "0.5.2" }
|
||||||
hyper = { version = "1.6.0" }
|
hyper = { version = "1.6.0" }
|
||||||
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio-native-tls"] }
|
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio-native-tls"] }
|
||||||
dotenv = { version = "0.15" }
|
dotenvy = { version = "0.15.7" }
|
||||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" }
|
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
22
src/lib.rs
22
src/lib.rs
@@ -8,6 +8,13 @@ mod keys {
|
|||||||
pub mod error {
|
pub mod error {
|
||||||
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod test {
|
||||||
|
pub const DBURL: &str = "TEST_DATABASE_URL";
|
||||||
|
pub mod error {
|
||||||
|
pub const ERROR: &str = "TEST_DATABASE_URL must be set in .env";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod connection_settings {
|
mod connection_settings {
|
||||||
@@ -22,12 +29,23 @@ pub mod db_pool {
|
|||||||
use crate::{connection_settings, keys};
|
use crate::{connection_settings, keys};
|
||||||
|
|
||||||
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
dotenv::dotenv().ok();
|
let database_url = get_db_url().await;
|
||||||
let database_url = env::var(keys::DBURL).expect(keys::error::ERROR);
|
println!("Database url: {:?}", database_url);
|
||||||
|
|
||||||
PgPoolOptions::new()
|
PgPoolOptions::new()
|
||||||
.max_connections(connection_settings::MAXCONN)
|
.max_connections(connection_settings::MAXCONN)
|
||||||
.connect(&database_url)
|
.connect(&database_url)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_db_url() -> String {
|
||||||
|
#[cfg(debug_assertions)] // Example: Only load .env in debug builds
|
||||||
|
dotenvy::dotenv().ok();
|
||||||
|
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
env::var(keys::test::DBURL).expect(keys::test::error::ERROR)
|
||||||
|
} else {
|
||||||
|
env::var(keys::DBURL).expect(keys::error::ERROR)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
15
src/main.rs
15
src/main.rs
@@ -21,11 +21,7 @@ async fn main() {
|
|||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn app() -> Router {
|
async fn routes() -> Router {
|
||||||
let pool = icarus_auth::db_pool::create_pool()
|
|
||||||
.await
|
|
||||||
.expect("Failed to create pool");
|
|
||||||
|
|
||||||
// build our application with a route
|
// build our application with a route
|
||||||
Router::new()
|
Router::new()
|
||||||
.route(callers::endpoints::DBTEST, get(callers::common::db_ping))
|
.route(callers::endpoints::DBTEST, get(callers::common::db_ping))
|
||||||
@@ -34,7 +30,14 @@ async fn app() -> Router {
|
|||||||
callers::endpoints::REGISTER,
|
callers::endpoints::REGISTER,
|
||||||
post(callers::register::register_user),
|
post(callers::register::register_user),
|
||||||
)
|
)
|
||||||
.layer(axum::Extension(pool))
|
}
|
||||||
|
|
||||||
|
async fn app() -> Router {
|
||||||
|
let pool = icarus_auth::db_pool::create_pool()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create pool");
|
||||||
|
|
||||||
|
routes().await.layer(axum::Extension(pool))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@@ -1,153 +0,0 @@
|
|||||||
extern crate icarus_auth;
|
|
||||||
|
|
||||||
use crate::icarus_auth::callers;
|
|
||||||
|
|
||||||
// use axum::Extension;
|
|
||||||
use axum::body::Body;
|
|
||||||
// use axum::response::Response;
|
|
||||||
use axum::{
|
|
||||||
Router,
|
|
||||||
http::{Request, StatusCode},
|
|
||||||
routing::get,
|
|
||||||
};
|
|
||||||
// use hyper::client::conn;
|
|
||||||
// use sqlx::PgPool;
|
|
||||||
// use sqlx::postgres::{self, PgPoolOptions};
|
|
||||||
// use testcontainers_modules::testcontainers::runners::AsyncRunner;
|
|
||||||
// use hyper::client;
|
|
||||||
// use sqlx::postgres;
|
|
||||||
// use http::{Request, StatusCode};
|
|
||||||
// use serde_json::json;
|
|
||||||
// use tower::ServiceExt; // for `.oneshot()`
|
|
||||||
use tower::util::ServiceExt;
|
|
||||||
// use testcontainers_modules::testcontainers::core::client::
|
|
||||||
|
|
||||||
const TEST_DATABASE_URL_ENV: &str = "TEST_DATABASE_URL";
|
|
||||||
const DEFAULT_TEST_DATABASE_URL: &str =
|
|
||||||
"postgres://icarus_op_test:password@localhost:5432/icarus_auth_test";
|
|
||||||
|
|
||||||
static SETUP: std::sync::Once = std::sync::Once::new();
|
|
||||||
|
|
||||||
// Ensure tracing is initialized only once for all tests in this file
|
|
||||||
/*
|
|
||||||
static TRACING_INIT: Lazy<()> = Lazy::new(|| {
|
|
||||||
if std::env::var("RUST_LOG").is_err() {
|
|
||||||
// Set default log level if not provided
|
|
||||||
unsafe {
|
|
||||||
std::env::set_var("RUST_LOG", "info,tower_http=debug,your_project_name=debug");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tracing_subscriber::fmt()
|
|
||||||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
|
||||||
.with_test_writer() // Write logs to the test output capture
|
|
||||||
.init();
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
async fn setup_database() -> sqlx::PgPool {
|
|
||||||
let database_url = std::env::var(TEST_DATABASE_URL_ENV)
|
|
||||||
.unwrap_or_else(|_| DEFAULT_TEST_DATABASE_URL.to_string());
|
|
||||||
let pool = sqlx::PgPool::connect(&database_url)
|
|
||||||
.await
|
|
||||||
.expect("Failed to connect to test database");
|
|
||||||
|
|
||||||
let migrator = sqlx::migrate::Migrator::new(std::path::Path::new("./migrations"))
|
|
||||||
.await
|
|
||||||
.expect("Failed to create migrator");
|
|
||||||
migrator.run(&pool).await.expect("Failed to run migrations");
|
|
||||||
|
|
||||||
// Seed here if needed
|
|
||||||
pool
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_db_health() {
|
|
||||||
SETUP.call_once(|| {
|
|
||||||
tokio::runtime::Runtime::new().unwrap().block_on(async {
|
|
||||||
setup_database().await;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
async fn setup_test(pool: sqlx::PgPool) -> Router {
|
|
||||||
Router::new()
|
|
||||||
.route(callers::endpoints::DBTEST, get(callers::common::db_ping))
|
|
||||||
.layer(Extension(pool))
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_hello_world() {
|
|
||||||
let app = Router::new().route(callers::endpoints::ROOT, get(callers::common::root)); // Replace with your handler
|
|
||||||
|
|
||||||
let response = app
|
|
||||||
.oneshot(
|
|
||||||
Request::builder()
|
|
||||||
.uri(callers::endpoints::ROOT)
|
|
||||||
.body(Body::empty())
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(response.status(), StatusCode::OK);
|
|
||||||
|
|
||||||
let body = String::from_utf8(
|
|
||||||
axum::body::to_bytes(response.into_body(), usize::MAX)
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.to_vec(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(body, "Hello, World!");
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
#[tokio::test]
|
|
||||||
async fn _test_db_health_check() {
|
|
||||||
let container = testcontainers_modules::postgres::Postgres::default()
|
|
||||||
.start()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let _host_ip = container.get_host().await.unwrap();
|
|
||||||
let port = 5432;
|
|
||||||
let host_port = container.get_host_port_ipv4(port).await.unwrap();
|
|
||||||
let conn_string = &format!(
|
|
||||||
"postgres://postgres:postgres@localhost:{}/postgres",
|
|
||||||
host_port
|
|
||||||
);
|
|
||||||
|
|
||||||
println!("Test Database: {}", conn_string);
|
|
||||||
|
|
||||||
let app = Router::new().route(callers::endpoints::DBTEST, get(callers::common::db_ping)); // Replace with your handler
|
|
||||||
|
|
||||||
let response = app
|
|
||||||
.oneshot(
|
|
||||||
Request::builder()
|
|
||||||
.uri(callers::endpoints::DBTEST)
|
|
||||||
.body(Body::empty())
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(response.status(), StatusCode::OK);
|
|
||||||
|
|
||||||
match PgPoolOptions::new().connect(conn_string).await {
|
|
||||||
Ok(_) => {
|
|
||||||
assert!(true, "Success");
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
assert!(false, "Error: {:?}", err.to_string());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
*/
|
|
Reference in New Issue
Block a user