-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathintegration.rs
More file actions
363 lines (332 loc) · 10.4 KB
/
Copy pathintegration.rs
File metadata and controls
363 lines (332 loc) · 10.4 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Integration tests for the codag-drain template host, driving the axum
//! `Router` directly via `tower::ServiceExt::oneshot` (no real network).
use axum::body::Body;
use axum::http::{HeaderValue, Request, StatusCode};
use http_body_util::BodyExt;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Semaphore;
use tower::ServiceExt;
use dashmap::DashMap;
use codag_drain::{template_logs, LogLine, TemplaterConfig};
use codag_drain_server::routes;
use codag_drain_server::session::{self, AppState, Limits};
const TEST_TOKEN: &str = "test-drain-token";
fn sample_text() -> String {
[
"worker ready shard=1",
"worker ready shard=2",
"worker ready shard=3",
"node phase changed to Succeeded now",
"node phase changed to Failed now",
"node phase changed to Skipped now",
]
.join("\n")
}
fn parsed_lines() -> Vec<LogLine> {
sample_text().lines().map(session::parse_line).collect()
}
async fn body_string(resp: axum::response::Response) -> String {
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
String::from_utf8(bytes.to_vec()).unwrap()
}
fn app() -> axum::Router {
routes::router(state_with_limits(Limits::from_env()))
}
fn limited_app(limits: Limits) -> axum::Router {
routes::router(state_with_limits(limits))
}
fn state_with_limits(limits: Limits) -> AppState {
let max_inflight = limits.max_inflight;
AppState {
sessions: Arc::new(DashMap::new()),
limits: Arc::new(limits),
template_slots: Arc::new(Semaphore::new(max_inflight)),
auth_token: Some(Arc::<str>::from(TEST_TOKEN)),
}
}
fn authed(req: Request<Body>) -> Request<Body> {
let (mut parts, body) = req.into_parts();
parts.headers.insert(
"authorization",
HeaderValue::from_static("Bearer test-drain-token"),
);
Request::from_parts(parts, body)
}
#[tokio::test]
async fn health_returns_ok() {
let resp = app()
.oneshot(Request::get("/health").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(body_string(resp).await, "ok");
}
#[tokio::test]
async fn templates_404_for_unknown_session() {
let resp = app()
.oneshot(authed(
Request::get("/v1/session/nope/templates")
.body(Body::empty())
.unwrap(),
))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn v1_routes_require_bearer_auth() {
let resp = app()
.oneshot(
Request::post("/v1/template")
.body(Body::from("one\ntwo\n"))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn v1_routes_fail_closed_without_configured_auth_token() {
let limits = Limits::from_env();
let max_inflight = limits.max_inflight;
let state = AppState {
sessions: Arc::new(DashMap::new()),
limits: Arc::new(limits),
template_slots: Arc::new(Semaphore::new(max_inflight)),
auth_token: None,
};
let resp = routes::router(state)
.oneshot(authed(
Request::post("/v1/template")
.body(Body::from("one\ntwo\n"))
.unwrap(),
))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE);
}
#[tokio::test]
async fn ingest_then_templates_text_parity() {
let app = app();
let body = sample_text();
let resp = app
.clone()
.oneshot(authed(
Request::post("/v1/session/s1/ingest")
.body(Body::from(body.clone()))
.unwrap(),
))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let ingest_json: serde_json::Value = serde_json::from_str(&body_string(resp).await).unwrap();
let expected_n = parsed_lines().len();
assert_eq!(
ingest_json["ingested"].as_u64().unwrap() as usize,
expected_n
);
assert_eq!(ingest_json["total"].as_u64().unwrap() as usize, expected_n);
let resp = app
.oneshot(authed(
Request::get("/v1/session/s1/templates?format=text&grouper=drain-stock")
.body(Body::empty())
.unwrap(),
))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let got = body_string(resp).await;
let cfg = TemplaterConfig {
grouper: codag_drain::GrouperKind::DrainStock,
..TemplaterConfig::default()
};
let expected = template_logs(&parsed_lines(), &cfg).render();
assert_eq!(got, expected, "templates text != library render");
}
#[tokio::test]
async fn template_oneshot_parity() {
let body = sample_text();
let resp = app()
.oneshot(authed(
Request::post("/v1/template?format=text")
.body(Body::from(body))
.unwrap(),
))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let got = body_string(resp).await;
let cfg = TemplaterConfig::default();
let expected = template_logs(&parsed_lines(), &cfg).render();
assert_eq!(got, expected, "/v1/template != library render");
}
#[tokio::test]
async fn template_oneshot_json_can_parse_text_body() {
let body = sample_text();
let resp = app()
.oneshot(authed(
Request::post("/v1/template?format=json")
.header("content-type", "text/plain")
.body(Body::from(body))
.unwrap(),
))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let json: serde_json::Value = serde_json::from_str(&body_string(resp).await).unwrap();
assert_eq!(
json["original_count"].as_u64().unwrap() as usize,
parsed_lines().len()
);
assert!(!json.get("groups").unwrap().as_array().unwrap().is_empty());
}
#[tokio::test]
async fn templates_json_shape() {
let app = app();
let body = sample_text();
app.clone()
.oneshot(authed(
Request::post("/v1/session/sj/ingest")
.body(Body::from(body))
.unwrap(),
))
.await
.unwrap();
let resp = app
.oneshot(authed(
Request::get("/v1/session/sj/templates?format=json")
.body(Body::empty())
.unwrap(),
))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let json: serde_json::Value = serde_json::from_str(&body_string(resp).await).unwrap();
assert!(json.get("groups").unwrap().is_array());
assert!(json.get("original_count").is_some());
assert!(json.get("template_count").is_some());
assert!(json.get("line_compression").is_some());
}
#[tokio::test]
async fn ingest_counts_accumulate_across_two_calls() {
let app = app();
let r1 = app
.clone()
.oneshot(authed(
Request::post("/v1/session/acc/ingest")
.body(Body::from("alpha one\nbeta two\n"))
.unwrap(),
))
.await
.unwrap();
let j1: serde_json::Value = serde_json::from_str(&body_string(r1).await).unwrap();
assert_eq!(j1["ingested"].as_u64().unwrap(), 2);
assert_eq!(j1["total"].as_u64().unwrap(), 2);
let r2 = app
.clone()
.oneshot(authed(
Request::post("/v1/session/acc/ingest")
.body(Body::from("gamma three\n"))
.unwrap(),
))
.await
.unwrap();
let j2: serde_json::Value = serde_json::from_str(&body_string(r2).await).unwrap();
assert_eq!(j2["ingested"].as_u64().unwrap(), 1);
assert_eq!(j2["total"].as_u64().unwrap(), 3);
}
#[tokio::test]
async fn ingest_ndjson_format() {
let app = app();
let body =
"{\"message\":\"boom\",\"level\":\"error\"}\n{\"message\":\"again\",\"level\":\"error\"}\n";
let r = app
.oneshot(authed(
Request::post("/v1/session/nd/ingest?body=ndjson")
.body(Body::from(body))
.unwrap(),
))
.await
.unwrap();
assert_eq!(r.status(), StatusCode::OK);
let j: serde_json::Value = serde_json::from_str(&body_string(r).await).unwrap();
assert_eq!(j["ingested"].as_u64().unwrap(), 2);
}
#[tokio::test]
async fn invalid_ndjson_returns_400() {
let r = app()
.oneshot(authed(
Request::post("/v1/template?body=ndjson")
.body(Body::from("{\"message\":\"ok\"}\nnot json\n"))
.unwrap(),
))
.await
.unwrap();
assert_eq!(r.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn line_limit_returns_413() {
let r = limited_app(Limits {
max_body_bytes: 1024,
max_lines: 1,
max_line_chars: 100,
max_samples: 3,
max_inflight: 1,
template_timeout: Duration::from_secs(1),
})
.oneshot(authed(
Request::post("/v1/template")
.body(Body::from("one\ntwo\n"))
.unwrap(),
))
.await
.unwrap();
assert_eq!(r.status(), StatusCode::PAYLOAD_TOO_LARGE);
}
#[tokio::test]
async fn samples_limit_returns_413() {
let r = limited_app(Limits {
max_body_bytes: 1024,
max_lines: 10,
max_line_chars: 100,
max_samples: 1,
max_inflight: 1,
template_timeout: Duration::from_secs(1),
})
.oneshot(authed(
Request::post("/v1/template?samples=2")
.body(Body::from("one\ntwo\n"))
.unwrap(),
))
.await
.unwrap();
assert_eq!(r.status(), StatusCode::PAYLOAD_TOO_LARGE);
}
#[tokio::test]
async fn concurrency_overflow_returns_429() {
let limits = Limits {
max_body_bytes: 1024,
max_lines: 10,
max_line_chars: 100,
max_samples: 3,
max_inflight: 1,
template_timeout: Duration::from_secs(1),
};
let state = AppState {
sessions: Arc::new(DashMap::new()),
limits: Arc::new(limits),
template_slots: Arc::new(Semaphore::new(1)),
auth_token: Some(Arc::<str>::from(TEST_TOKEN)),
};
let _held = state.template_slots.clone().try_acquire_owned().unwrap();
let r = routes::router(state)
.oneshot(authed(
Request::post("/v1/template")
.body(Body::from("one\ntwo\n"))
.unwrap(),
))
.await
.unwrap();
assert_eq!(r.status(), StatusCode::TOO_MANY_REQUESTS);
}