-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcommon.rs
88 lines (78 loc) · 2.75 KB
/
common.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use bollard::secret::HealthConfig;
use datafusion_table_providers::sql::db_connection_pool::mysqlpool::MySQLConnectionPool;
use secrecy::SecretString;
use std::collections::HashMap;
use tracing::instrument;
use crate::{
container_registry,
docker::{ContainerRunnerBuilder, RunningContainer},
};
const MYSQL_ROOT_PASSWORD: &str = "integration-test-pw";
const MYSQL_DOCKER_CONTAINER: &str = "runtime-integration-test-mysql";
pub(super) fn get_mysql_params(port: usize) -> HashMap<String, SecretString> {
let mut params = HashMap::new();
params.insert(
"mysql_host".to_string(),
SecretString::from("localhost".to_string()),
);
params.insert(
"mysql_tcp_port".to_string(),
SecretString::from(port.to_string()),
);
params.insert(
"mysql_user".to_string(),
SecretString::from("root".to_string()),
);
params.insert(
"mysql_pass".to_string(),
SecretString::from(MYSQL_ROOT_PASSWORD.to_string()),
);
params.insert(
"mysql_db".to_string(),
SecretString::from("mysqldb".to_string()),
);
params.insert(
"mysql_sslmode".to_string(),
SecretString::from("disabled".to_string()),
);
params
}
#[instrument]
pub async fn start_mysql_docker_container(port: usize) -> Result<RunningContainer, anyhow::Error> {
let container_name = format!("{MYSQL_DOCKER_CONTAINER}-{port}");
let port = port.try_into().unwrap_or(15432);
let mysql_docker_image = std::env::var("MYSQL_DOCKER_IMAGE")
.unwrap_or_else(|_| format!("{}mysql:latest", container_registry()));
let running_container = ContainerRunnerBuilder::new(container_name)
.image(mysql_docker_image)
.add_port_binding(3306, port)
.add_env_var("MYSQL_ROOT_PASSWORD", MYSQL_ROOT_PASSWORD)
.add_env_var("MYSQL_DATABASE", "mysqldb")
.healthcheck(HealthConfig {
test: Some(vec![
"CMD-SHELL".to_string(),
format!(
"mysqladmin ping --host=127.0.0.1 --port=3306 --password={MYSQL_ROOT_PASSWORD}"
),
]),
interval: Some(500_000_000), // 250ms
timeout: Some(100_000_000), // 100ms
retries: Some(5),
start_period: Some(500_000_000), // 100ms
start_interval: None,
})
.build()?
.run()
.await?;
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
Ok(running_container)
}
#[instrument]
pub(super) async fn get_mysql_connection_pool(
port: usize,
) -> Result<MySQLConnectionPool, anyhow::Error> {
let mysql_pool = MySQLConnectionPool::new(get_mysql_params(port))
.await
.expect("Failed to create MySQL Connection Pool");
Ok(mysql_pool)
}