|
2 | 2 | #![feature(assert_matches)]
|
3 | 3 | #![warn(clippy::indexing_slicing)]
|
4 | 4 |
|
5 |
| -use std::{assert_matches::assert_matches, path::Path, time::Duration}; |
| 5 | +use std::{ |
| 6 | + assert_matches::assert_matches, |
| 7 | + io::Write, |
| 8 | + net::{Ipv4Addr, SocketAddr}, |
| 9 | + path::Path, |
| 10 | + time::Duration, |
| 11 | +}; |
6 | 12 |
|
7 | 13 | use mirrord_protocol::{
|
8 | 14 | tcp::{DaemonTcp, LayerTcpSteal, StealType},
|
9 | 15 | ClientMessage, DaemonMessage,
|
10 | 16 | };
|
11 | 17 | use rstest::rstest;
|
12 |
| -use tokio::io::AsyncWriteExt; |
| 18 | +use tokio::{io::AsyncWriteExt, net::TcpListener}; |
13 | 19 |
|
14 | 20 | mod common;
|
15 | 21 |
|
16 | 22 | pub use common::*;
|
17 | 23 | use tokio::net::TcpStream;
|
18 | 24 |
|
19 |
| -/// Start an application (and load the layer into it) that listens on a port that is configured to |
20 |
| -/// be ignored, and verify that no messages are sent to the agent. |
| 25 | +/// Verifies that the layer respects `feature.network.incoming.listen_ports` mapping. |
21 | 26 | #[rstest]
|
22 | 27 | #[tokio::test]
|
23 | 28 | #[timeout(Duration::from_secs(60))]
|
24 | 29 | async fn listen_ports(
|
25 | 30 | #[values(Application::RustListenPorts)] application: Application,
|
26 | 31 | dylib_path: &Path,
|
27 |
| - config_dir: &Path, |
28 | 32 | ) {
|
29 |
| - let config_path = config_dir.join("listen_ports.json"); |
| 33 | + // We need to know ports on which the application listens, |
| 34 | + // because we want to make the connections from the test code |
| 35 | + // (to verify that the layer respects `feature.network.incoming.listen_ports` mapping). |
| 36 | + // If we just use const ports, this test is flaky in the CI, as the app tends to fail with |
| 37 | + // EADDRINUSE. Here we bind to random ports, drop the sockets, and hope that the ports |
| 38 | + // remain free for the app to use. |
| 39 | + let port_1 = TcpListener::bind(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 0)) |
| 40 | + .await |
| 41 | + .unwrap() |
| 42 | + .local_addr() |
| 43 | + .unwrap() |
| 44 | + .port(); |
| 45 | + let port_2 = TcpListener::bind(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 0)) |
| 46 | + .await |
| 47 | + .unwrap() |
| 48 | + .local_addr() |
| 49 | + .unwrap() |
| 50 | + .port(); |
| 51 | + |
| 52 | + let config = serde_json::json!({ |
| 53 | + "feature": { |
| 54 | + "network": { |
| 55 | + "incoming": { |
| 56 | + "mode": "steal", |
| 57 | + "listen_ports": [[80, port_1]] |
| 58 | + } |
| 59 | + }, |
| 60 | + "fs": "local" |
| 61 | + } |
| 62 | + }); |
| 63 | + // Ports for the application code to use. |
| 64 | + // Due to the port mapping, the application should effectively listen on `port_1;port_2`. |
| 65 | + let app_ports = format!("80;{port_2}"); |
| 66 | + |
| 67 | + let mut config_file = tempfile::NamedTempFile::with_suffix(".json").unwrap(); |
| 68 | + config_file |
| 69 | + .as_file_mut() |
| 70 | + .write_all(serde_json::to_string(&config).unwrap().as_bytes()) |
| 71 | + .unwrap(); |
| 72 | + |
30 | 73 | let (mut test_process, mut intproxy) = application
|
31 | 74 | .start_process_with_layer(
|
32 | 75 | dylib_path,
|
33 |
| - vec![ |
34 |
| - ("RUST_LOG", "mirrord=trace"), |
35 |
| - ("MIRRORD_FILE_MODE", "local"), |
36 |
| - ], |
37 |
| - Some(&config_path), |
| 76 | + vec![("RUST_LOG", "mirrord=trace"), ("APP_PORTS", &app_ports)], |
| 77 | + Some(config_file.path()), |
38 | 78 | )
|
39 | 79 | .await;
|
40 | 80 |
|
41 |
| - assert_matches!( |
42 |
| - intproxy.recv().await, |
43 |
| - ClientMessage::TcpSteal(LayerTcpSteal::PortSubscribe(StealType::All(80))) |
44 |
| - ); |
45 |
| - intproxy |
46 |
| - .send(DaemonMessage::TcpSteal(DaemonTcp::SubscribeResult(Ok(80)))) |
47 |
| - .await; |
48 |
| - let mut stream = TcpStream::connect("127.0.0.1:51222").await.unwrap(); |
49 |
| - println!("connected to listener at port 51222"); |
50 |
| - stream.write_all(b"HELLO").await.unwrap(); |
51 |
| - |
52 |
| - assert_matches!( |
53 |
| - intproxy.recv().await, |
54 |
| - ClientMessage::TcpSteal(LayerTcpSteal::PortSubscribe(StealType::All(40000))) |
55 |
| - ); |
56 |
| - intproxy |
57 |
| - .send(DaemonMessage::TcpSteal(DaemonTcp::SubscribeResult(Ok( |
58 |
| - 40000, |
59 |
| - )))) |
60 |
| - .await; |
61 |
| - let mut stream = TcpStream::connect("127.0.0.1:40000").await.unwrap(); |
62 |
| - println!("connected to listener at port 40000"); |
63 |
| - stream.write_all(b"HELLO").await.unwrap(); |
64 |
| - |
65 |
| - loop { |
66 |
| - match intproxy.try_recv().await { |
67 |
| - Some(ClientMessage::TcpSteal(LayerTcpSteal::PortUnsubscribe(40000))) => {} |
68 |
| - Some(ClientMessage::TcpSteal(LayerTcpSteal::PortUnsubscribe(80))) => {} |
69 |
| - None => break, |
70 |
| - other => panic!("unexpected message: {:?}", other), |
71 |
| - } |
| 81 | + for (subscription_port, local_port) in [(80, port_1), (port_2, port_2)] { |
| 82 | + assert_matches!( |
| 83 | + intproxy.recv().await, |
| 84 | + ClientMessage::TcpSteal(LayerTcpSteal::PortSubscribe(StealType::All(port))) |
| 85 | + if port == subscription_port, |
| 86 | + ); |
| 87 | + intproxy |
| 88 | + .send(DaemonMessage::TcpSteal(DaemonTcp::SubscribeResult(Ok( |
| 89 | + subscription_port, |
| 90 | + )))) |
| 91 | + .await; |
| 92 | + let mut stream = |
| 93 | + TcpStream::connect(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), local_port)) |
| 94 | + .await |
| 95 | + .unwrap(); |
| 96 | + println!("connected to the application on port {local_port} (application code used {subscription_port})"); |
| 97 | + stream.write_all(b"HELLO").await.unwrap(); |
| 98 | + stream.shutdown().await.unwrap(); |
| 99 | + assert_matches!( |
| 100 | + intproxy.recv().await, |
| 101 | + ClientMessage::TcpSteal(LayerTcpSteal::PortUnsubscribe(port)) |
| 102 | + if port == subscription_port, |
| 103 | + ); |
72 | 104 | }
|
| 105 | + |
73 | 106 | assert_eq!(intproxy.try_recv().await, None);
|
| 107 | + |
74 | 108 | test_process.wait_assert_success().await;
|
75 | 109 | test_process.assert_no_error_in_stderr().await;
|
76 | 110 | test_process.assert_no_error_in_stdout().await;
|
|
0 commit comments