Tengo problemas para ejecutar mis pruebas automatizadas con acciones de GitHub. No puedo entender por qué no puedo conectarme con el servicio MongoDB ejecutando mis pruebas de integración. Probé diferentes hosts: localhost, 127.0.0.1, 0.0.0.0, pero ninguno de ellos puede conectarse con la base de datos.
Funciona perfectamente bien en la configuración de mi ventana acoplable, pero por alguna razón no con las acciones de GitHub.
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
Archivo de configuración (desarrollo.toml).
[application] host = "127.0.0.1" port = 8080
Conexión a la base de datos. Las variables de entorno y el archivo de configuración se fusionan y estoy accediendo a ellos aquí a través de 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) }
Llamando a la función init.
// Mongo let mongo = mongo::init(&config).await.expect("Failed to init mongo");
El error que obtengo.
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
Eventualmente lo resolví agregando un control de salud a mi servicio. Parece que mi problema tenía algo que ver con que mi base de datos aún no estaba activa antes de ejecutar las pruebas.
services: mongodb: image: mongo env: MONGO_INITDB_ROOT_USERNAME: test MONGO_INITDB_ROOT_PASSWORD: password MONGO_INITDB_DATABASE: test options: >- --health-cmd mongo --health-interval 10s --health-timeout 5s --health-retries 5 ports: - 27017:27017