eggress-runtime 1.0.4

Service supervisor and composition layer for eggress
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! Integration tests for reverse proxy supervisor wiring.
//!
//! Verifies that the `ServiceSupervisor` correctly spawns reverse servers
//! and clients from TOML configuration and that they work end-to-end.

use std::io::Write;
use std::sync::atomic::Ordering;
use std::time::Duration;

use tempfile::NamedTempFile;

fn write_config(content: &str) -> NamedTempFile {
    let mut f = NamedTempFile::new().unwrap();
    f.write_all(content.as_bytes()).unwrap();
    f.flush().unwrap();
    f
}

/// Find an available port by binding to port 0.
async fn find_available_port() -> std::net::SocketAddr {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    drop(listener);
    addr
}

#[test]
fn reverse_server_config_parses_from_toml() {
    let config = r#"
version = 1

[[reverse_servers]]
id = "rev-srv-1"
control_bind = "127.0.0.1:0"
external_bind = "127.0.0.1:0"

[[reverse_clients]]
id = "rev-cli-1"
server_addr = "127.0.0.1:12345"
default_target_host = "127.0.0.1"
default_target_port = 80
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(
        result.is_ok(),
        "reverse server/client config should parse: {:?}",
        result.err()
    );
}

#[test]
fn reverse_client_parallel_connections_parses() {
    let config = r#"
version = 1

[[reverse_clients]]
id = "rev-cli-parallel"
server_addr = "127.0.0.1:12345"
parallel_connections = 3
default_target_host = "127.0.0.1"
default_target_port = 80
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(
        result.is_ok(),
        "parallel_connections config should parse: {:?}",
        result.err()
    );
}

#[test]
fn reverse_server_invalid_bind_address_fails() {
    let config = r#"
version = 1

[[reverse_servers]]
id = "rev-srv-bad"
control_bind = "not-a-valid-address"
external_bind = "127.0.0.1:0"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(result.is_err(), "invalid control_bind should fail");
}

#[test]
fn reverse_client_invalid_address_fails() {
    let config = r#"
version = 1

[[reverse_clients]]
id = "rev-cli-bad"
server_addr = "not-a-valid-address"
default_target_host = "127.0.0.1"
default_target_port = 80
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(result.is_err(), "invalid server_addr should fail");
}

#[test]
fn reverse_server_with_auth_parses() {
    let config = r#"
version = 1

[[reverse_servers]]
id = "rev-srv-auth"
control_bind = "127.0.0.1:0"
external_bind = "127.0.0.1:0"
auth_username = "admin"
auth_password = "secret123"

[[reverse_clients]]
id = "rev-cli-auth"
server_addr = "127.0.0.1:12345"
auth_username = "admin"
auth_password = "secret123"
default_target_host = "127.0.0.1"
default_target_port = 80
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(
        result.is_ok(),
        "auth config should parse: {:?}",
        result.err()
    );
}

#[test]
fn reverse_client_reconnect_config_parses() {
    let config = r#"
version = 1

[[reverse_clients]]
id = "rev-cli-reconnect"
server_addr = "127.0.0.1:12345"
reconnect_initial = "500ms"
reconnect_max = "10s"
default_target_host = "127.0.0.1"
default_target_port = 80
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(
        result.is_ok(),
        "reconnect config should parse: {:?}",
        result.err()
    );
}

#[test]
fn reverse_client_invalid_reconnect_fails() {
    let config = r#"
version = 1

[[reverse_clients]]
id = "rev-cli-bad-reconnect"
server_addr = "127.0.0.1:12345"
reconnect_initial = "not-a-duration"
default_target_host = "127.0.0.1"
default_target_port = 80
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(result.is_err(), "invalid reconnect duration should fail");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn reverse_server_spawns_and_shuts_down() {
    let control_port = find_available_port().await;

    let config = format!(
        r#"
version = 1

[[reverse_servers]]
id = "rev-srv-test"
control_bind = "127.0.0.1:{}"
external_bind = "127.0.0.1:0"
"#,
        control_port.port(),
    );

    let f = write_config(&config);
    let path = f.path().to_str().unwrap().to_string();

    let mut supervisor = eggress_runtime::ServiceSupervisor::start(&path).unwrap();
    let state = supervisor.state().clone();
    let cancel_token = supervisor.shutdown_token();

    // Run the supervisor in a background thread
    let handle = tokio::task::spawn_blocking(move || supervisor.run());

    // Give it time to start
    tokio::time::sleep(Duration::from_millis(200)).await;

    // Verify readiness
    assert!(
        state.readiness.load(Ordering::Relaxed),
        "supervisor should be ready"
    );

    // Verify reverse registry has the server registered
    let registry_snapshot = state.reverse_registry.snapshot();
    assert_eq!(
        registry_snapshot.len(),
        1,
        "reverse registry should have 1 entry"
    );
    assert_eq!(registry_snapshot[0].id, "rev-srv-test");

    // Shutdown via cancel token
    cancel_token.cancel();
    let result = tokio::time::timeout(Duration::from_secs(3), handle).await;
    assert!(result.is_ok(), "supervisor should shut down within timeout");
    let inner = result.unwrap();
    assert!(
        inner.is_ok(),
        "supervisor should shut down cleanly: {:?}",
        inner.err()
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn reverse_client_spawns_and_connects() {
    let control_port = find_available_port().await;

    let config = format!(
        r#"
version = 1

[[reverse_servers]]
id = "rev-srv"
control_bind = "127.0.0.1:{}"
external_bind = "127.0.0.1:0"

[[reverse_clients]]
id = "rev-cli"
server_addr = "127.0.0.1:{}"
default_target_host = "127.0.0.1"
default_target_port = 80
"#,
        control_port.port(),
        control_port.port(),
    );

    let f = write_config(&config);
    let path = f.path().to_str().unwrap().to_string();

    let mut supervisor = eggress_runtime::ServiceSupervisor::start(&path).unwrap();
    let state = supervisor.state().clone();
    let cancel_token = supervisor.shutdown_token();

    // Run the supervisor in a background thread
    let handle = tokio::task::spawn_blocking(move || supervisor.run());

    // Give it time to start and for client to connect
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Verify readiness
    assert!(
        state.readiness.load(Ordering::Relaxed),
        "supervisor should be ready"
    );

    // Verify reverse registry has the server registered
    let registry_snapshot = state.reverse_registry.snapshot();
    assert!(
        !registry_snapshot.is_empty(),
        "reverse registry should have entries"
    );

    // Verify reverse metrics show activity
    let metrics_snap = state.reverse_metrics.snapshot();
    // The client should have attempted at least one connection
    assert!(
        metrics_snap.control_connections_accepted_total >= 1
            || metrics_snap.control_connections_rejected_total >= 1
            || metrics_snap.control_reconnects_total >= 1,
        "expected some reverse metric activity: {:?}",
        metrics_snap,
    );

    // Shutdown
    cancel_token.cancel();
    let result = tokio::time::timeout(Duration::from_secs(3), handle).await;
    assert!(result.is_ok(), "supervisor should shut down within timeout");
    let inner = result.unwrap();
    assert!(
        inner.is_ok(),
        "supervisor should shut down cleanly: {:?}",
        inner.err()
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn reverse_config_reload_succeeds() {
    let config1 = r#"
version = 1

[[reverse_servers]]
id = "rev-srv"
control_bind = "127.0.0.1:0"
external_bind = "127.0.0.1:0"
"#;

    let f1 = write_config(config1);
    let path1 = f1.path().to_str().unwrap();
    let mut sup = eggress_runtime::ServiceSupervisor::start(path1).unwrap();

    // Reloading the same config should succeed (Applied)
    let result = sup.reload_config();
    assert!(
        matches!(
            result,
            eggress_runtime::supervisor::ReloadResult::Applied { .. }
        ),
        "reload should succeed, got: {:?}",
        result
    );
}

#[test]
fn reverse_server_missing_external_bind_fails() {
    let config = r#"
version = 1

[[reverse_servers]]
id = "rev-srv-no-external"
control_bind = "127.0.0.1:0"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(result.is_err(), "missing external_bind should fail");
}

#[test]
fn reverse_server_invalid_external_bind_fails() {
    let config = r#"
version = 1

[[reverse_servers]]
id = "rev-srv-bad-external"
control_bind = "127.0.0.1:0"
external_bind = "not-a-valid-address"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(result.is_err(), "invalid external_bind should fail");
}

#[test]
fn reverse_client_missing_target_fails() {
    let config = r#"
version = 1

[[reverse_clients]]
id = "rev-cli-no-target"
server_addr = "127.0.0.1:12345"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(
        result.is_err(),
        "missing default_target_host/port should fail"
    );
}

#[test]
fn reverse_client_partial_target_fails() {
    let config = r#"
version = 1

[[reverse_clients]]
id = "rev-cli-partial-target"
server_addr = "127.0.0.1:12345"
default_target_host = "127.0.0.1"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(
        result.is_err(),
        "only default_target_host without port should fail"
    );
}

#[test]
fn reverse_server_auth_mismatch_fails() {
    let config = r#"
version = 1

[[reverse_servers]]
id = "rev-srv-auth-mismatch"
control_bind = "127.0.0.1:0"
external_bind = "127.0.0.1:0"
auth_username = "admin"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(
        result.is_err(),
        "auth_username without password should fail"
    );
}

#[test]
fn reverse_client_invalid_server_addr_fails() {
    let config = r#"
version = 1

[[reverse_clients]]
id = "rev-cli-bad-addr"
server_addr = "not-a-valid-address"
default_target_host = "127.0.0.1"
default_target_port = 80
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    assert!(result.is_err(), "invalid server_addr should fail");
}

#[test]
fn reverse_server_password_env_var_not_set_fails() {
    let config = r#"
version = 1

[[reverse_servers]]
id = "rev-srv-missing-env"
control_bind = "127.0.0.1:0"
external_bind = "127.0.0.1:0"
auth_username = "admin"
auth_password_env = "EGGRESS_TEST_MISSING_VAR_98765"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_runtime::ServiceSupervisor::start(path);
    match result {
        Ok(_) => panic!("auth_password_env with unset var should fail at compile time"),
        Err(e) => {
            let msg = format!("{}", e);
            assert!(
                msg.contains("environment variable") && msg.contains("not set"),
                "error should mention env var not set: {}",
                msg
            );
        }
    }
}