|
18 | 18 | //! |
19 | 19 | #![cfg(unix)] |
20 | 20 |
|
21 | | -use std::collections::HashMap; |
| 21 | +use anyhow::Result; |
| 22 | +use opentelemetry::{otel_debug, otel_info}; |
22 | 23 | use std::fs; |
23 | 24 | use std::fs::File; |
24 | 25 | use std::os::unix::fs::PermissionsExt; |
25 | 26 | use std::sync::{Arc, Mutex, Once, OnceLock}; |
26 | | -use testcontainers::{core::{IntoContainerPort, WaitFor}, runners::AsyncRunner, ContainerAsync, GenericImage, ImageExt}; |
27 | | -use testcontainers::core::{ContainerPort, Mount}; |
28 | 27 | use testcontainers::core::wait::HttpWaitStrategy; |
| 28 | +use testcontainers::core::{ContainerPort, Mount}; |
| 29 | +use testcontainers::{ |
| 30 | + core::WaitFor, |
| 31 | + runners::AsyncRunner, |
| 32 | + ContainerAsync, GenericImage, ImageExt, |
| 33 | +}; |
29 | 34 | use tracing_subscriber::FmtSubscriber; |
30 | | -use opentelemetry::{otel_debug, otel_info}; |
31 | | -use anyhow::Result; |
32 | 35 |
|
33 | 36 | // Static references for container management |
34 | 37 | static COLLECTOR_ARC: OnceLock<Mutex<Option<Arc<ContainerAsync<GenericImage>>>>> = OnceLock::new(); |
@@ -71,13 +74,26 @@ pub async fn start_collector_container() -> Result<()> { |
71 | 74 | .with_wait_for(WaitFor::http( |
72 | 75 | HttpWaitStrategy::new("/") |
73 | 76 | .with_expected_status_code(404u16) |
74 | | - .with_port(ContainerPort::Tcp(4318)))) |
| 77 | + .with_port(ContainerPort::Tcp(4318)), |
| 78 | + )) |
75 | 79 | .with_mapped_port(4317, ContainerPort::Tcp(4317)) |
76 | 80 | .with_mapped_port(4318, ContainerPort::Tcp(4318)) |
77 | | - .with_mount(Mount::bind_mount(fs::canonicalize("./otel-collector-config.yaml")?.to_string_lossy(), "/etc/otelcol/config.yaml")) |
78 | | - .with_mount(Mount::bind_mount(fs::canonicalize("./actual/logs.json")?.to_string_lossy(), "/testresults/logs.json")) |
79 | | - .with_mount(Mount::bind_mount(fs::canonicalize("./actual/metrics.json")?.to_string_lossy(), "/testresults/metrics.json")) |
80 | | - .with_mount(Mount::bind_mount(fs::canonicalize("./actual/traces.json")?.to_string_lossy(), "/testresults/traces.json")) |
| 81 | + .with_mount(Mount::bind_mount( |
| 82 | + fs::canonicalize("./otel-collector-config.yaml")?.to_string_lossy(), |
| 83 | + "/etc/otelcol/config.yaml", |
| 84 | + )) |
| 85 | + .with_mount(Mount::bind_mount( |
| 86 | + fs::canonicalize("./actual/logs.json")?.to_string_lossy(), |
| 87 | + "/testresults/logs.json", |
| 88 | + )) |
| 89 | + .with_mount(Mount::bind_mount( |
| 90 | + fs::canonicalize("./actual/metrics.json")?.to_string_lossy(), |
| 91 | + "/testresults/metrics.json", |
| 92 | + )) |
| 93 | + .with_mount(Mount::bind_mount( |
| 94 | + fs::canonicalize("./actual/traces.json")?.to_string_lossy(), |
| 95 | + "/testresults/traces.json", |
| 96 | + )) |
81 | 97 | .start() |
82 | 98 | .await?; |
83 | 99 |
|
@@ -112,12 +128,24 @@ fn upsert_empty_file(path: &str) -> File { |
112 | 128 | /// suite shutting down! |
113 | 129 | /// |
114 | 130 | pub fn stop_collector_container() { |
| 131 | + // This is a bit heinous. We get called after tokio has shutdown, but testcontainers |
| 132 | + // needs an async runtime to shutdown, so we create a temporary new one just for this |
| 133 | + // purpose. |
| 134 | + // https://github.com/testcontainers/testcontainers-rs/issues/707 |
115 | 135 | otel_debug!(name: "stop_collector_container"); |
| 136 | + tokio::runtime::Runtime::new() |
| 137 | + .unwrap() |
| 138 | + .block_on(stop_it()) |
| 139 | + .expect("Couldn't create runtime to shutdown"); |
| 140 | +} |
| 141 | + |
| 142 | +async fn stop_it() -> Result<()> { |
116 | 143 | if let Some(container) = COLLECTOR_ARC |
117 | 144 | .get() |
118 | 145 | .and_then(|arc_lock| arc_lock.lock().unwrap().take()) |
119 | 146 | { |
120 | | - container.stop(); |
| 147 | + container.stop().await?; |
121 | 148 | otel_debug!(name: "stop_collector_container::stopped"); |
122 | 149 | } |
| 150 | + Ok(()) |
123 | 151 | } |
0 commit comments