I'm having trouble running my automated tests with GitHub actions. I can't figure out why I can't connect with the MongoDB service running my integration tests. I tried different hosts: localhost, 127.0.0.1, 0.0.0.0, but none of them can connect with the database.
It works perfectly fine in my docker setup, but for some reason not with GitHub actions.
name: CI master
on: [push, pull_request]
env:
RUST_BACKTRACE: 1
CARGO_TERM_COLOR: always
APP_ENV: development
APP_MONGO_USER: test
APP_MONGO_PASS: password
APP_MONGO_DB: test
jobs:
# Run tests
test:
name: Test
runs-on: ubuntu-latest
services:
mongo:
image: mongo
env:
MONGO_INITDB_ROOT_USERNAME: ${APP_MONGO_USER}
MONGO_INITDB_ROOT_PASSWORD: ${APP_MONGO_PASS}
MONGO_INITDB_DATABASE: ${APP_MONGO_DB}
ports:
- 27017:27017
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
Config file (development.toml).
[application]
host = "127.0.0.1"
port = 8080
Connecting to the database. The environment variables and config file get merged and I'm accessing them here through config: &Settings.
pub async fn init(config: &Settings) -> Result<Database> {
let client_options = ClientOptions::parse(
format!(
"mongodb://{}:{}@{}:27017",
config.mongo.user, config.mongo.pass, config.application.host
)
.as_str(),
)
.await?;
let client = Client::with_options(client_options)?;
let database = client.database("test"); // TODO: replace with env var
database.run_command(doc! {"ping": 1}, None).await?;
println!("Connected successfully.");
Ok(database)
}
Calling the init function.
// Mongo
let mongo = mongo::init(&config).await.expect("Failed to init mongo");
The error I get.
thread 'health_check' panicked at 'Failed to init mongo: Error { kind: ServerSelectionError { message: "Server selection timeout: No available servers. Topology: { Type: Unknown, Servers: [ { Address: 127.0.0.1:27017, Type: Unknown, Error: Connection refused (os error 111) }, ] }" }, labels: [] }', tests/health_check.rs:31:44