feat(ci): use environment variables for database url
This commit is contained in:
parent
eb77908353
commit
cf01284fbe
|
|
@ -37,6 +37,13 @@ jobs:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Remove local env files to prevent conflicts
|
||||||
|
run: |
|
||||||
|
# Remove or rename env files so they don't override CI environment variables
|
||||||
|
[ -f .env ] && mv .env .env.backup || true
|
||||||
|
[ -f .env.test ] && mv .env.test .env.test.backup || true
|
||||||
|
echo "Removed local env files to ensure CI env vars take precedence"
|
||||||
|
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
|
|
@ -68,6 +75,7 @@ jobs:
|
||||||
|
|
||||||
- name: Start readur server
|
- name: Start readur server
|
||||||
run: |
|
run: |
|
||||||
|
echo "Starting server with DATABASE_URL: $DATABASE_URL"
|
||||||
./target/release/readur > server.log 2>&1 &
|
./target/release/readur > server.log 2>&1 &
|
||||||
echo $! > readur.pid
|
echo $! > readur.pid
|
||||||
sleep 2
|
sleep 2
|
||||||
|
|
@ -75,9 +83,9 @@ jobs:
|
||||||
env:
|
env:
|
||||||
DATABASE_URL: ${{ env.DATABASE_URL }}
|
DATABASE_URL: ${{ env.DATABASE_URL }}
|
||||||
JWT_SECRET: test-secret-key
|
JWT_SECRET: test-secret-key
|
||||||
PORT: 8000
|
SERVER_ADDRESS: 0.0.0.0:8000
|
||||||
UPLOAD_PATH: ./uploads
|
UPLOAD_PATH: ./uploads
|
||||||
WATCH_PATH: ./watch
|
WATCH_FOLDER: ./watch
|
||||||
|
|
||||||
- name: Wait for server to be ready
|
- name: Wait for server to be ready
|
||||||
run: |
|
run: |
|
||||||
|
|
@ -116,6 +124,9 @@ jobs:
|
||||||
|
|
||||||
- name: Run integration tests
|
- name: Run integration tests
|
||||||
run: |
|
run: |
|
||||||
|
echo "Running tests with DATABASE_URL: $DATABASE_URL"
|
||||||
|
echo "Environment check:"
|
||||||
|
env | grep -E "(DATABASE_URL|JWT_SECRET|API_URL)" | sort
|
||||||
cargo test --test '*' -- --test-threads=1
|
cargo test --test '*' -- --test-threads=1
|
||||||
env:
|
env:
|
||||||
DATABASE_URL: ${{ env.DATABASE_URL }}
|
DATABASE_URL: ${{ env.DATABASE_URL }}
|
||||||
|
|
|
||||||
|
|
@ -56,9 +56,12 @@ fn create_test_user_with_suffix(suffix: &str) -> CreateUser {
|
||||||
|
|
||||||
async fn create_test_app_state() -> Result<Arc<AppState>> {
|
async fn create_test_app_state() -> Result<Arc<AppState>> {
|
||||||
let config = Config::from_env().unwrap_or_else(|_| {
|
let config = Config::from_env().unwrap_or_else(|_| {
|
||||||
// Create a test config if env fails
|
// Create a test config if env fails - use DATABASE_URL env var or fallback
|
||||||
|
let database_url = std::env::var("DATABASE_URL")
|
||||||
|
.or_else(|_| std::env::var("TEST_DATABASE_URL"))
|
||||||
|
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/readur_test".to_string());
|
||||||
Config {
|
Config {
|
||||||
database_url: "postgresql://readur:readur@localhost:5432/readur".to_string(),
|
database_url,
|
||||||
server_address: "127.0.0.1:8000".to_string(),
|
server_address: "127.0.0.1:8000".to_string(),
|
||||||
jwt_secret: "test-secret".to_string(),
|
jwt_secret: "test-secret".to_string(),
|
||||||
upload_path: "./test-uploads".to_string(),
|
upload_path: "./test-uploads".to_string(),
|
||||||
|
|
@ -75,7 +78,7 @@ async fn create_test_app_state() -> Result<Arc<AppState>> {
|
||||||
cpu_priority: "normal".to_string(),
|
cpu_priority: "normal".to_string(),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let db = Database::new("postgresql://readur:readur@localhost:5432/readur").await?;
|
let db = Database::new(&config.database_url).await?;
|
||||||
let queue_service = std::sync::Arc::new(
|
let queue_service = std::sync::Arc::new(
|
||||||
readur::ocr_queue::OcrQueueService::new(db.clone(), db.get_pool().clone(), 1)
|
readur::ocr_queue::OcrQueueService::new(db.clone(), db.get_pool().clone(), 1)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,11 @@ use readur::{
|
||||||
models::{Document, CreateUser, UserRole},
|
models::{Document, CreateUser, UserRole},
|
||||||
};
|
};
|
||||||
|
|
||||||
const TEST_DB_URL: &str = "postgresql://readur:readur@localhost:5432/readur";
|
fn get_test_db_url() -> String {
|
||||||
|
std::env::var("DATABASE_URL")
|
||||||
|
.or_else(|_| std::env::var("TEST_DATABASE_URL"))
|
||||||
|
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/readur_test".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
// Helper function to create a test user with unique identifier
|
// Helper function to create a test user with unique identifier
|
||||||
async fn create_test_user(db: &Database, username: &str) -> Result<Uuid> {
|
async fn create_test_user(db: &Database, username: &str) -> Result<Uuid> {
|
||||||
|
|
@ -60,7 +64,7 @@ fn create_test_document(user_id: Uuid, filename: &str, file_hash: Option<String>
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_get_document_by_user_and_hash_found() -> Result<()> {
|
async fn test_get_document_by_user_and_hash_found() -> Result<()> {
|
||||||
let db = Database::new(TEST_DB_URL).await?;
|
let db = Database::new(&get_test_db_url()).await?;
|
||||||
let user_id = create_test_user(&db, "testuser1").await?;
|
let user_id = create_test_user(&db, "testuser1").await?;
|
||||||
let file_hash = "abcd1234567890";
|
let file_hash = "abcd1234567890";
|
||||||
|
|
||||||
|
|
@ -82,7 +86,7 @@ async fn test_get_document_by_user_and_hash_found() -> Result<()> {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_get_document_by_user_and_hash_not_found() -> Result<()> {
|
async fn test_get_document_by_user_and_hash_not_found() -> Result<()> {
|
||||||
let db = Database::new(TEST_DB_URL).await?;
|
let db = Database::new(&get_test_db_url()).await?;
|
||||||
let user_id = Uuid::new_v4();
|
let user_id = Uuid::new_v4();
|
||||||
let non_existent_hash = "nonexistent1234567890";
|
let non_existent_hash = "nonexistent1234567890";
|
||||||
|
|
||||||
|
|
@ -96,7 +100,7 @@ async fn test_get_document_by_user_and_hash_not_found() -> Result<()> {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_get_document_by_user_and_hash_different_user() -> Result<()> {
|
async fn test_get_document_by_user_and_hash_different_user() -> Result<()> {
|
||||||
let db = Database::new(TEST_DB_URL).await?;
|
let db = Database::new(&get_test_db_url()).await?;
|
||||||
let user1_id = create_test_user(&db, "testuser2").await?;
|
let user1_id = create_test_user(&db, "testuser2").await?;
|
||||||
let user2_id = create_test_user(&db, "testuser3").await?;
|
let user2_id = create_test_user(&db, "testuser3").await?;
|
||||||
let file_hash = "shared_hash_1234567890";
|
let file_hash = "shared_hash_1234567890";
|
||||||
|
|
@ -115,7 +119,7 @@ async fn test_get_document_by_user_and_hash_different_user() -> Result<()> {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_duplicate_hash_prevention_same_user() -> Result<()> {
|
async fn test_duplicate_hash_prevention_same_user() -> Result<()> {
|
||||||
let db = Database::new(TEST_DB_URL).await?;
|
let db = Database::new(&get_test_db_url()).await?;
|
||||||
let user_id = create_test_user(&db, "testuser4").await?;
|
let user_id = create_test_user(&db, "testuser4").await?;
|
||||||
let file_hash = "duplicate_hash_1234567890";
|
let file_hash = "duplicate_hash_1234567890";
|
||||||
|
|
||||||
|
|
@ -136,7 +140,7 @@ async fn test_duplicate_hash_prevention_same_user() -> Result<()> {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_same_hash_different_users_allowed() -> Result<()> {
|
async fn test_same_hash_different_users_allowed() -> Result<()> {
|
||||||
let db = Database::new(TEST_DB_URL).await?;
|
let db = Database::new(&get_test_db_url()).await?;
|
||||||
let user1_id = create_test_user(&db, "testuser5").await?;
|
let user1_id = create_test_user(&db, "testuser5").await?;
|
||||||
let user2_id = create_test_user(&db, "testuser6").await?;
|
let user2_id = create_test_user(&db, "testuser6").await?;
|
||||||
let file_hash = "shared_content_hash_1234567890";
|
let file_hash = "shared_content_hash_1234567890";
|
||||||
|
|
@ -164,7 +168,7 @@ async fn test_same_hash_different_users_allowed() -> Result<()> {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_null_hash_allowed_multiple() -> Result<()> {
|
async fn test_null_hash_allowed_multiple() -> Result<()> {
|
||||||
let db = Database::new(TEST_DB_URL).await?;
|
let db = Database::new(&get_test_db_url()).await?;
|
||||||
let user_id = create_test_user(&db, "testuser7").await?;
|
let user_id = create_test_user(&db, "testuser7").await?;
|
||||||
|
|
||||||
// Create multiple documents with null hash (should be allowed)
|
// Create multiple documents with null hash (should be allowed)
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,11 @@ use readur::{
|
||||||
};
|
};
|
||||||
|
|
||||||
// Use the same database URL as the running server
|
// Use the same database URL as the running server
|
||||||
const DB_URL: &str = "postgresql://readur:readur@localhost:5432/readur";
|
fn get_test_db_url() -> String {
|
||||||
|
std::env::var("DATABASE_URL")
|
||||||
|
.or_else(|_| std::env::var("TEST_DATABASE_URL"))
|
||||||
|
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/readur_test".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
struct SimpleThrottleTest {
|
struct SimpleThrottleTest {
|
||||||
pool: PgPool,
|
pool: PgPool,
|
||||||
|
|
@ -28,13 +32,14 @@ struct SimpleThrottleTest {
|
||||||
|
|
||||||
impl SimpleThrottleTest {
|
impl SimpleThrottleTest {
|
||||||
async fn new() -> Result<Self> {
|
async fn new() -> Result<Self> {
|
||||||
|
let db_url = get_test_db_url();
|
||||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||||
.max_connections(20)
|
.max_connections(20)
|
||||||
.acquire_timeout(Duration::from_secs(10))
|
.acquire_timeout(Duration::from_secs(10))
|
||||||
.connect(DB_URL)
|
.connect(&db_url)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let db = Database::new(DB_URL).await?;
|
let db = Database::new(&db_url).await?;
|
||||||
|
|
||||||
// Create queue service with throttling (max 15 concurrent jobs)
|
// Create queue service with throttling (max 15 concurrent jobs)
|
||||||
let queue_service = Arc::new(OcrQueueService::new(
|
let queue_service = Arc::new(OcrQueueService::new(
|
||||||
|
|
|
||||||
|
|
@ -94,8 +94,11 @@ async fn create_test_user(db: &Database, username: &str) -> Result<Uuid> {
|
||||||
|
|
||||||
async fn create_test_app_state() -> Result<Arc<AppState>> {
|
async fn create_test_app_state() -> Result<Arc<AppState>> {
|
||||||
let config = Config::from_env().unwrap_or_else(|_| {
|
let config = Config::from_env().unwrap_or_else(|_| {
|
||||||
|
let database_url = std::env::var("DATABASE_URL")
|
||||||
|
.or_else(|_| std::env::var("TEST_DATABASE_URL"))
|
||||||
|
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/readur_test".to_string());
|
||||||
Config {
|
Config {
|
||||||
database_url: "postgresql://readur:readur@localhost:5432/readur".to_string(),
|
database_url,
|
||||||
server_address: "127.0.0.1:8000".to_string(),
|
server_address: "127.0.0.1:8000".to_string(),
|
||||||
jwt_secret: "test-secret".to_string(),
|
jwt_secret: "test-secret".to_string(),
|
||||||
upload_path: "./test-uploads".to_string(),
|
upload_path: "./test-uploads".to_string(),
|
||||||
|
|
@ -112,7 +115,7 @@ async fn create_test_app_state() -> Result<Arc<AppState>> {
|
||||||
cpu_priority: "normal".to_string(),
|
cpu_priority: "normal".to_string(),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let db = Database::new("postgresql://readur:readur@localhost:5432/readur").await?;
|
let db = Database::new(&config.database_url).await?;
|
||||||
let queue_service = std::sync::Arc::new(
|
let queue_service = std::sync::Arc::new(
|
||||||
readur::ocr_queue::OcrQueueService::new(db.clone(), db.get_pool().clone(), 1)
|
readur::ocr_queue::OcrQueueService::new(db.clone(), db.get_pool().clone(), 1)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,11 @@ use readur::{
|
||||||
request_throttler::RequestThrottler,
|
request_throttler::RequestThrottler,
|
||||||
};
|
};
|
||||||
|
|
||||||
const TEST_DB_URL: &str = "postgresql://readur:readur@localhost:5432/readur";
|
fn get_test_db_url() -> String {
|
||||||
|
std::env::var("DATABASE_URL")
|
||||||
|
.or_else(|_| std::env::var("TEST_DATABASE_URL"))
|
||||||
|
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/readur_test".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
struct ThrottledTestHarness {
|
struct ThrottledTestHarness {
|
||||||
db: Database,
|
db: Database,
|
||||||
|
|
@ -40,10 +44,10 @@ impl ThrottledTestHarness {
|
||||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||||
.max_connections(30) // Higher limit for stress testing
|
.max_connections(30) // Higher limit for stress testing
|
||||||
.acquire_timeout(std::time::Duration::from_secs(15))
|
.acquire_timeout(std::time::Duration::from_secs(15))
|
||||||
.connect(TEST_DB_URL)
|
.connect(&get_test_db_url())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let db = Database::new(TEST_DB_URL).await?;
|
let db = Database::new(&get_test_db_url()).await?;
|
||||||
|
|
||||||
// Initialize services
|
// Initialize services
|
||||||
let file_service = FileService::new("./test_uploads".to_string());
|
let file_service = FileService::new("./test_uploads".to_string());
|
||||||
|
|
|
||||||
|
|
@ -98,8 +98,11 @@ async fn create_test_user(db: &Database, username: &str) -> Result<Uuid> {
|
||||||
|
|
||||||
async fn create_test_app_state() -> Result<Arc<AppState>> {
|
async fn create_test_app_state() -> Result<Arc<AppState>> {
|
||||||
let config = Config::from_env().unwrap_or_else(|_| {
|
let config = Config::from_env().unwrap_or_else(|_| {
|
||||||
|
let database_url = std::env::var("DATABASE_URL")
|
||||||
|
.or_else(|_| std::env::var("TEST_DATABASE_URL"))
|
||||||
|
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/readur_test".to_string());
|
||||||
Config {
|
Config {
|
||||||
database_url: "postgresql://readur:readur@localhost:5432/readur".to_string(),
|
database_url,
|
||||||
server_address: "127.0.0.1:8000".to_string(),
|
server_address: "127.0.0.1:8000".to_string(),
|
||||||
jwt_secret: "test-secret".to_string(),
|
jwt_secret: "test-secret".to_string(),
|
||||||
upload_path: "./test-uploads".to_string(),
|
upload_path: "./test-uploads".to_string(),
|
||||||
|
|
@ -116,7 +119,7 @@ async fn create_test_app_state() -> Result<Arc<AppState>> {
|
||||||
cpu_priority: "normal".to_string(),
|
cpu_priority: "normal".to_string(),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let db = Database::new("postgresql://readur:readur@localhost:5432/readur").await?;
|
let db = Database::new(&config.database_url).await?;
|
||||||
let queue_service = std::sync::Arc::new(
|
let queue_service = std::sync::Arc::new(
|
||||||
readur::ocr_queue::OcrQueueService::new(db.clone(), db.get_pool().clone(), 1)
|
readur::ocr_queue::OcrQueueService::new(db.clone(), db.get_pool().clone(), 1)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue