-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtest_app.rs
325 lines (289 loc) · 10.7 KB
/
test_app.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Exposes [TestApp] and [TestAppBuilder] to ease the setup of the
//! test actix server, database connection pool, and different mocking
//! components.
use std::sync::Arc;
use axum::Router;
use axum_tracing_opentelemetry::middleware::OtelAxumLayer;
use dashmap::DashMap;
use editoast_models::DbConnectionPoolV2;
use editoast_osrdyne_client::OsrdyneClient;
use serde::de::DeserializeOwned;
use tower_http::trace::TraceLayer;
use url::Url;
use crate::{
client::{TelemetryConfig, TelemetryKind},
core::{mocking::MockingClient, CoreClient},
generated_data::speed_limit_tags_config::SpeedLimitTagIds,
infra_cache::InfraCache,
init_tracing,
map::MapLayers,
valkey_utils::ValkeyConfig,
AppState, EditoastMode, ValkeyClient,
};
use axum_test::TestRequest;
use axum_test::TestServer;
use super::{authentication_middleware, CoreConfig, OsrdyneConfig, PostgresConfig, ServerConfig};
/// A builder interface for [TestApp]
///
/// It allows configuring some parameters for the app service.
/// Currently it allows setting the database connection pool (v1 or v2) and the core client.
///
/// Use [TestAppBuilder::default_app] to get a default app with a v2 database connection pool
/// and a default core client (mocking disabled).
///
/// The `db_pool_v1` parameter is only relevant while the pool migration is ongoing.
pub(crate) struct TestAppBuilder {
db_pool: Option<DbConnectionPoolV2>,
core_client: Option<CoreClient>,
osrdyne_client: Option<OsrdyneClient>,
}
impl TestAppBuilder {
pub fn new() -> Self {
Self {
db_pool: None,
core_client: None,
osrdyne_client: None,
}
}
pub fn db_pool(mut self, db_pool: DbConnectionPoolV2) -> Self {
assert!(self.db_pool.is_none());
self.db_pool = Some(db_pool);
self
}
pub fn core_client(mut self, core_client: CoreClient) -> Self {
assert!(self.core_client.is_none());
self.core_client = Some(core_client);
self
}
pub fn osrdyne_client(mut self, osrdyne_client: OsrdyneClient) -> Self {
assert!(self.osrdyne_client.is_none());
self.osrdyne_client = Some(osrdyne_client);
self
}
pub fn default_app() -> TestApp {
let pool = DbConnectionPoolV2::for_tests();
let core_client = CoreClient::Mocked(MockingClient::default());
TestAppBuilder::new()
.db_pool(pool)
.core_client(core_client)
.build()
}
pub fn build(self) -> TestApp {
// Generate test server config
let config = ServerConfig {
port: 0,
address: String::default(),
health_check_timeout: chrono::Duration::milliseconds(500),
disable_authorization: true,
map_layers_max_zoom: 18,
postgres_config: PostgresConfig {
database_url: Url::parse("postgres://osrd:password@localhost:5432/osrd").unwrap(),
pool_size: 32,
},
osrdyne_config: OsrdyneConfig {
mq_url: Url::parse("amqp://osrd:[email protected]:5672/%2f").unwrap(),
osrdyne_api_url: Url::parse("http://127.0.0.1:4242/").unwrap(),
core: CoreConfig {
timeout: chrono::Duration::seconds(180),
single_worker: false,
num_channels: 8,
},
},
valkey_config: ValkeyConfig {
no_cache: false,
is_cluster_client: false,
valkey_url: Url::parse("redis://localhost:6379").unwrap(),
},
};
// Setup tracing
let exporter = opentelemetry_stdout::SpanExporter::default();
let telemety_config = TelemetryConfig {
telemetry_kind: TelemetryKind::Opentelemetry,
..Default::default()
};
let tracing_guard =
init_tracing(EditoastMode::Webservice, &telemety_config, exporter, true).unwrap();
// Config valkey
let valkey = ValkeyClient::new(config.valkey_config.clone())
.expect("Could not build Valkey client")
.into();
// Create database pool
let db_pool_v2 = Arc::new(self.db_pool.expect(
"No database pool provided to TestAppBuilder, use Default or provide a database pool",
));
// Setup infra cache map
let infra_caches = DashMap::<i64, InfraCache>::default().into();
// Load speed limit tag config
let speed_limit_tag_ids = Arc::new(SpeedLimitTagIds::load());
// Build Core client
let core_client = Arc::new(self.core_client.expect(
"No core client provided to TestAppBuilder, use Default or provide a core client",
));
// Build Osrdyne client
let osrdyne_client = self
.osrdyne_client
.unwrap_or_else(OsrdyneClient::default_mock);
let osrdyne_client = Arc::new(osrdyne_client);
let app_state = AppState {
db_pool: db_pool_v2.clone(),
core_client: core_client.clone(),
osrdyne_client,
valkey,
infra_caches,
map_layers: Arc::new(MapLayers::default()),
speed_limit_tag_ids,
disable_authorization: true,
health_check_timeout: config.health_check_timeout,
config: Arc::new(config),
};
// Configure the axum router
let router: Router<()> = axum::Router::<AppState>::new()
.merge(crate::views::router())
.route_layer(axum::middleware::from_fn_with_state(
app_state.clone(),
authentication_middleware,
))
.layer(OtelAxumLayer::default())
.layer(TraceLayer::new_for_http())
.with_state(app_state);
// Run server
let server = TestServer::new(router).expect("test server should build properly");
TestApp {
server,
db_pool: db_pool_v2,
core_client,
tracing_guard,
}
}
}
/// Wraps an underlying, fully configured, actix service
///
/// It also holds a reference to the database connection pool and the core client,
/// which can be accessed through the [TestApp] methods.
pub(crate) struct TestApp {
server: TestServer,
db_pool: Arc<DbConnectionPoolV2>,
core_client: Arc<CoreClient>,
#[allow(unused)] // included here to extend its lifetime, not meant to be used in any way
tracing_guard: tracing::subscriber::DefaultGuard,
}
impl TestApp {
#[allow(dead_code)] // while the pool migration is ongoing
pub fn core_client(&self) -> Arc<CoreClient> {
self.core_client.clone()
}
pub fn db_pool(&self) -> Arc<DbConnectionPoolV2> {
self.db_pool.clone()
}
pub fn fetch(&self, req: TestRequest) -> TestResponse {
futures::executor::block_on(async move {
tracing::trace!(request = ?req);
let response = req.await;
TestResponse::new(response)
})
}
pub fn get(&self, path: &str) -> TestRequest {
self.server.get(&trim_path(path))
}
pub fn post(&self, path: &str) -> TestRequest {
self.server.post(&trim_path(path))
}
pub fn put(&self, path: &str) -> TestRequest {
self.server.put(&trim_path(path))
}
pub fn patch(&self, path: &str) -> TestRequest {
self.server.patch(&trim_path(path))
}
pub fn delete(&self, path: &str) -> TestRequest {
self.server.delete(&trim_path(path))
}
}
// For technical reasons, we had a hard time trying to configure the normalizing layer
// in the test server. Since we have control over the paths configured in our unit tests,
// doing this manually is probably a good enough solution for now.
fn trim_path(path: &str) -> String {
if let Some(path) = path.strip_suffix('/') {
path.to_owned()
} else if path.contains("/?") {
path.replace("/?", "?")
} else {
path.to_owned()
}
}
pub struct TestResponse {
inner: axum_test::TestResponse,
log_payload: bool,
}
impl TestResponse {
#[tracing::instrument(name = "Response", level = "debug", skip(inner), fields(status = ?inner.status_code()))]
fn new(inner: axum_test::TestResponse) -> Self {
tracing::trace!(response = ?inner);
Self {
inner,
log_payload: true,
}
}
#[allow(unused)]
pub fn log_payload(mut self, log_payload: bool) -> Self {
self.log_payload = log_payload;
self
}
fn render_response_lossy(self) -> String {
if !self.log_payload {
return "payload logging disabled".to_string();
}
let bytes = self.inner.into_bytes();
serde_json::from_slice::<serde_json::Value>(&bytes)
.ok()
.and_then(|json| serde_json::to_string_pretty(&json).ok())
.unwrap_or_else(|| "cannot render response body".to_string())
}
pub fn assert_status(self, expected_status: axum::http::StatusCode) -> Self {
let actual_status = self.inner.status_code();
if actual_status != expected_status {
let body = self.render_response_lossy();
pretty_assertions::assert_eq!(
actual_status,
expected_status,
"unexpected status code body={body}"
);
unreachable!("should have already panicked")
} else {
self
}
}
pub fn bytes(self) -> Vec<u8> {
self.inner.into_bytes().into()
}
pub fn content_type(&self) -> String {
self.inner
.header("Content-Type")
.to_str()
.expect("Content-Type header should be valid UTF-8")
.to_string()
}
#[tracing::instrument(
name = "Deserialization",
level = "debug",
skip(self),
fields(response_status = ?self.inner.status_code())
)]
pub fn json_into<T: DeserializeOwned>(self) -> T {
let body = self.bytes();
serde_json::from_slice(body.as_ref()).unwrap_or_else(|err| {
tracing::error!(error = ?err, "Error deserializing test response into the desired type");
let actual: serde_json::Value =
serde_json::from_slice(body.as_ref()).unwrap_or_else(|err| {
tracing::error!(
error = ?err,
?body,
"Failed to deserialize test response body into JSON"
);
panic!("could not deserialize test response into JSON");
});
let pretty = serde_json::to_string_pretty(&actual).unwrap();
tracing::error!(body = %pretty, "Actual JSON value");
panic!("could not deserialize test request");
})
}
}