Skip to content

Commit 17493a3

Browse files
j7nw4rclaude
andcommitted
fix(tests): use shared Docker network for CI compatibility
- Replace host.docker.internal with shared network approach - Containers now communicate via container names on a shared network - Works on both Docker Desktop and Linux CI (GitHub Actions) - Generate unique network/container names per test for isolation - Update README with new networking pattern 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 12a3bec commit 17493a3

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,41 @@ tokio = { version = "1", features = ["full"] }
1919
## Quick Start
2020

2121
```rust
22-
use supabase_testcontainers_modules::{Auth, AUTH_PORT, DOCKER_INTERNAL_HOST, LOCAL_HOST};
22+
use supabase_testcontainers_modules::{Auth, AUTH_PORT, LOCAL_HOST};
2323
use testcontainers::{runners::AsyncRunner, ImageExt};
2424
use testcontainers_modules::postgres::Postgres;
2525

2626
#[tokio::test]
2727
async fn test_auth() -> anyhow::Result<()> {
28-
// Start PostgreSQL (requires version 12+)
29-
let postgres = Postgres::default().with_tag("15-alpine").start().await?;
28+
// Use shared network for container-to-container communication
29+
let network = "test-network";
30+
let pg_container = "postgres-db";
31+
32+
// Start PostgreSQL 15 on shared network
33+
let postgres = Postgres::default()
34+
.with_tag("15-alpine")
35+
.with_network(network)
36+
.with_container_name(pg_container)
37+
.start()
38+
.await?;
3039
let pg_port = postgres.get_host_port_ipv4(5432).await?;
3140

32-
// Connection strings
41+
// Auth connects via container name, schema init via localhost
3342
let auth_db_url = format!(
34-
"postgres://supabase_auth_admin:password@{}:{}/postgres",
35-
DOCKER_INTERNAL_HOST, pg_port
43+
"postgres://supabase_auth_admin:password@{}:5432/postgres",
44+
pg_container
3645
);
3746
let local_db_url = format!(
3847
"postgres://postgres:postgres@{}:{}/postgres",
3948
LOCAL_HOST, pg_port
4049
);
4150

42-
// Start Auth container
51+
// Initialize schema and start Auth on same network
4352
let auth = Auth::default()
4453
.with_db_url(&auth_db_url)
4554
.init_db_schema(&local_db_url, "password")
4655
.await?
56+
.with_network(network)
4757
.start()
4858
.await?;
4959

tests/auth_integration.rs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
//! Run with: `cargo test --features auth,const --test auth_integration`
55
66
use anyhow::Result;
7-
use supabase_testcontainers_modules::{Auth, AUTH_PORT, DOCKER_INTERNAL_HOST, LOCAL_HOST};
7+
use supabase_testcontainers_modules::{Auth, AUTH_PORT, LOCAL_HOST};
88
use testcontainers::runners::AsyncRunner;
99
use testcontainers::{ContainerAsync, ImageExt};
1010
use testcontainers_modules::postgres::Postgres;
1111
use tokio_postgres::NoTls;
1212

1313
/// PostgreSQL port constant
1414
const POSTGRES_PORT: u16 = 5432;
15+
/// Network name for container-to-container communication
16+
const TEST_NETWORK: &str = "supabase-test-network";
17+
/// PostgreSQL container alias on the shared network
18+
const POSTGRES_ALIAS: &str = "postgres";
1519

1620
/// Helper struct containing both containers and connection info
1721
pub struct AuthTestContext {
@@ -42,14 +46,27 @@ pub struct AuthTestContext {
4246
/// // Containers are automatically stopped when ctx goes out of scope
4347
/// ```
4448
pub async fn setup_auth_with_postgres() -> Result<AuthTestContext> {
45-
// Start PostgreSQL 15 container (Auth migrations require PostgreSQL 12+ for generated columns)
46-
let postgres = Postgres::default().with_tag("15-alpine").start().await?;
49+
// Generate unique network and container names for test isolation
50+
let test_id = std::time::SystemTime::now()
51+
.duration_since(std::time::UNIX_EPOCH)
52+
.unwrap()
53+
.as_nanos();
54+
let network_name = format!("{}-{}", TEST_NETWORK, test_id);
55+
let postgres_name = format!("{}-{}", POSTGRES_ALIAS, test_id);
56+
57+
// Start PostgreSQL 15 on shared network with a known container name
58+
let postgres = Postgres::default()
59+
.with_tag("15-alpine")
60+
.with_network(&network_name)
61+
.with_container_name(&postgres_name)
62+
.start()
63+
.await?;
4764
let postgres_port = postgres.get_host_port_ipv4(POSTGRES_PORT).await?;
4865

49-
// Connection string for Auth container (uses docker internal host)
66+
// Connection string for Auth container (uses container name on shared network)
5067
let auth_db_url = format!(
5168
"postgres://supabase_auth_admin:testpassword@{}:{}/postgres",
52-
DOCKER_INTERNAL_HOST, postgres_port
69+
postgres_name, POSTGRES_PORT
5370
);
5471

5572
// Connection string for schema init (uses localhost from host machine)
@@ -58,14 +75,15 @@ pub async fn setup_auth_with_postgres() -> Result<AuthTestContext> {
5875
LOCAL_HOST, postgres_port
5976
);
6077

61-
// Initialize Auth with database schema and start container
78+
// Initialize Auth with database schema and start container on same network
6279
let auth = Auth::default()
6380
.with_db_url(&auth_db_url)
6481
.with_mailer_autoconfirm(true)
6582
.with_sms_autoconfirm(true)
6683
.with_anonymous_users(true)
6784
.init_db_schema(&local_db_url, "testpassword")
6885
.await?
86+
.with_network(&network_name)
6987
.start()
7088
.await?;
7189

@@ -180,25 +198,39 @@ mod tests {
180198
/// Test that signup is rejected when disabled
181199
#[tokio::test]
182200
async fn test_signup_rejected_when_disabled() -> Result<()> {
183-
// Start PostgreSQL 15 container (Auth migrations require PostgreSQL 12+)
184-
let postgres = Postgres::default().with_tag("15-alpine").start().await?;
201+
// Generate unique network and container names for test isolation
202+
let test_id = std::time::SystemTime::now()
203+
.duration_since(std::time::UNIX_EPOCH)
204+
.unwrap()
205+
.as_nanos();
206+
let network_name = format!("{}-{}", TEST_NETWORK, test_id);
207+
let postgres_name = format!("{}-{}", POSTGRES_ALIAS, test_id);
208+
209+
// Start PostgreSQL 15 on shared network
210+
let postgres = Postgres::default()
211+
.with_tag("15-alpine")
212+
.with_network(&network_name)
213+
.with_container_name(&postgres_name)
214+
.start()
215+
.await?;
185216
let postgres_port = postgres.get_host_port_ipv4(POSTGRES_PORT).await?;
186217

187218
let auth_db_url = format!(
188219
"postgres://supabase_auth_admin:testpassword@{}:{}/postgres",
189-
DOCKER_INTERNAL_HOST, postgres_port
220+
postgres_name, POSTGRES_PORT
190221
);
191222
let local_db_url = format!(
192223
"postgres://postgres:postgres@{}:{}/postgres",
193224
LOCAL_HOST, postgres_port
194225
);
195226

196-
// Create Auth with signup disabled
227+
// Create Auth with signup disabled on same network
197228
let auth = Auth::default()
198229
.with_db_url(&auth_db_url)
199230
.with_signup_disabled(true)
200231
.init_db_schema(&local_db_url, "testpassword")
201232
.await?
233+
.with_network(&network_name)
202234
.start()
203235
.await?;
204236

0 commit comments

Comments
 (0)