encore-dev 2.0.0-beta.15

Rust SDK for Encore applications
Documentation
//! The runtime config of a test against a real server, at `ENCORE_TEST_PG`.

use encore_runtimecfg::pb;

/// Points the runtime at the test's server before anything starts it: one
/// database, `name`, with a read-write pool of a single connection, so every
/// handle shares it.
pub fn setup(name: &str) {
    let uri = std::env::var("ENCORE_TEST_PG")
        .unwrap_or_else(|_| "postgresql://postgres@127.0.0.1:5432/postgres".to_string());
    let config: tokio_postgres::Config = uri.parse().expect("ENCORE_TEST_PG is not a valid URI");
    let host = match &config.get_hosts()[0] {
        tokio_postgres::config::Host::Tcp(host) => host.clone(),
        #[cfg(unix)]
        tokio_postgres::config::Host::Unix(path) => path.display().to_string(),
    };
    let port = config.get_ports().first().copied().unwrap_or(5432);
    let cfg = pb::RuntimeConfig {
        environment: Some(pb::Environment {
            app_id: "app".into(),
            env_name: "local".into(),
            ..Default::default()
        }),
        deployment: Some(pb::Deployment {
            hosted_services: vec![pb::HostedService {
                name: "svc".into(),
                ..Default::default()
            }],
            ..Default::default()
        }),
        infra: Some(pb::Infrastructure {
            resources: Some(pb::infrastructure::Resources {
                sql_clusters: vec![pb::SqlCluster {
                    rid: "cluster".into(),
                    servers: vec![pb::SqlServer {
                        rid: "server".into(),
                        host: format!("{host}:{port}"),
                        kind: pb::ServerKind::Primary as i32,
                        tls_config: None,
                    }],
                    databases: vec![pb::SqlDatabase {
                        rid: "db".into(),
                        encore_name: name.into(),
                        cloud_name: config.get_dbname().unwrap_or("postgres").into(),
                        conn_pools: vec![pb::SqlConnectionPool {
                            role_rid: "role".into(),
                            max_connections: 1,
                            ..Default::default()
                        }],
                    }],
                }],
                ..Default::default()
            }),
            credentials: Some(pb::infrastructure::Credentials {
                sql_roles: vec![pb::SqlRole {
                    rid: "role".into(),
                    username: config.get_user().unwrap_or("postgres").into(),
                    password: config.get_password().map(|password| pb::SecretData {
                        source: Some(pb::secret_data::Source::Embedded(password.to_vec())),
                        ..Default::default()
                    }),
                    client_cert_rid: None,
                    ..Default::default()
                }],
                ..Default::default()
            }),
        }),
        ..Default::default()
    };
    // Before the runtime reads the environment: a test binary's only test.
    std::env::set_var(
        "ENCORE_RUNTIME_CONFIG",
        encore_runtimecfg::encoding::encode_env_value(&cfg),
    );
}