Skip to content

Commit 0fb020c

Browse files
test(features): port HTTP response EOF and stop/start reachability to host-gateway
The EOF check runs its three responses in one sandbox and reads the close through the shared http_raw client. The stop/start check is the restart half of podman_host_gateway, which stays in e2e/rust until its DNS relay check is decided. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent f495ae6 commit 0fb020c

5 files changed

Lines changed: 123 additions & 73 deletions

File tree

‎e2e/rust/tests/host_gateway_alias.rs‎

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -155,75 +155,3 @@ async fn sandbox_reaches_host_openshell_internal_via_host_gateway_alias() {
155155
guard.create_output
156156
);
157157
}
158-
159-
#[tokio::test]
160-
async fn sandbox_receives_eof_after_closing_http_response() {
161-
for response in [
162-
"HTTP/1.0 200 OK\r\nContent-Length: 3\r\n\r\nOK\n",
163-
"HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 3\r\n\r\nOK\n",
164-
"HTTP/1.1 200 OK\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nOK\n\r\n0\r\n\r\n",
165-
] {
166-
let listener = TcpListener::bind(("0.0.0.0", 0)).await.unwrap();
167-
let port = listener.local_addr().unwrap().port();
168-
let server = HostServer {
169-
port,
170-
task: tokio::spawn(async move {
171-
let (mut stream, _) = listener.accept().await.unwrap();
172-
stream.set_nodelay(true).expect("disable Nagle on fixture");
173-
let mut request = Vec::new();
174-
while !request.ends_with(b"\r\n\r\n") {
175-
request.push(stream.read_u8().await.unwrap());
176-
assert!(request.len() < 4096);
177-
}
178-
stream.write_all(response.as_bytes()).await.unwrap();
179-
stream.shutdown().await.unwrap();
180-
}),
181-
};
182-
let policy = write_policy(server.port).unwrap();
183-
let command = format!(
184-
r#"set -eu
185-
exec 3<>/dev/tcp/host.openshell.internal/{port}
186-
printf 'GET / HTTP/1.1\r\nHost: host.openshell.internal:{port}\r\n\r\n' >&3
187-
while true; do
188-
line=
189-
if IFS= read -r -t 5 line <&3; then
190-
printf '%s\n' "$line"
191-
else
192-
status=$?
193-
[ "$status" -eq 1 ] || {{ echo EOF_TIMEOUT; exit 1; }}
194-
[ -z "$line" ] || printf '%s\n' "$line"
195-
break
196-
fi
197-
done
198-
printf 'RESPONSE_EOF\n'
199-
"#,
200-
);
201-
let mut sandbox = SandboxGuard::create(&[
202-
"--policy",
203-
policy.path().to_str().unwrap(),
204-
"--no-auto-providers",
205-
"--",
206-
"/usr/bin/bash",
207-
"-c",
208-
&command,
209-
])
210-
.await
211-
.expect("closing response must finish without a client timeout");
212-
assert!(
213-
sandbox.create_output.lines().any(|line| line == "OK"),
214-
"{}",
215-
sandbox.create_output
216-
);
217-
assert!(
218-
sandbox.create_output.contains("RESPONSE_EOF"),
219-
"{}",
220-
sandbox.create_output
221-
);
222-
assert!(
223-
!sandbox.create_output.contains("EOF_TIMEOUT"),
224-
"{}",
225-
sandbox.create_output
226-
);
227-
sandbox.cleanup().await;
228-
}
229-
}

‎tests/suites/features/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/suites/features/host-gateway/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ publish = false
1414
[dependencies]
1515
host-server-support = { path = "../../../support/host-server" }
1616
openshell-testkit = { path = "../../../testkit/testkit" }
17+
sandbox-clients-support = { path = "../../../support/sandbox-clients" }

‎tests/suites/features/host-gateway/tests/host_gateway.rs‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
//! Python test image ([`testkit::python_image`]) that requests a page from that
1010
//! server through the name.
1111
12+
use std::time::Duration;
13+
1214
use host_server_support::{HttpRecorder, TEST_HOST};
15+
use sandbox_clients_support::{Request, http};
1316
use testkit::policy;
1417
use testkit::sdk::raw::proto;
15-
use testkit::{Context, PYTHON_BINARIES, ResultExt as _};
18+
use testkit::{Context, PYTHON_BINARIES, Poll, ResultExt as _, Sandbox};
1619

1720
/// The image's Python, which needs nothing beyond its standard library. The
1821
/// policy allows [`PYTHON_BINARIES`], the real paths behind `python3` in
@@ -70,6 +73,57 @@ async fn python_sandbox_reaches_host_server(cx: &mut Context) -> testkit::Result
7073
Ok(())
7174
}
7275

76+
/// `host.openshell.internal` still reaches the host after the sandbox is
77+
/// stopped and started, when the driver recreates its containers or pod.
78+
#[testkit::test]
79+
async fn host_server_reachable_after_stop_start(cx: &mut Context) -> testkit::Result {
80+
let marker = format!("host-gateway-restart-{}", cx.run_id());
81+
let server = HttpRecorder::start(marker.clone()).await?;
82+
let sandbox = cx
83+
.sandbox("hgss")
84+
.image(testkit::python_image())
85+
.command(["sleep", "infinity"])
86+
.policy(policy(server.port))
87+
.create_ready()
88+
.await?;
89+
90+
expect_reachable(&sandbox, server.port, &marker)
91+
.await
92+
.context("before the restart")?;
93+
sandbox.stop().await?;
94+
sandbox.start().await?;
95+
expect_reachable(&sandbox, server.port, &marker)
96+
.await
97+
.context("after the restart")
98+
}
99+
100+
/// Request a page from the host server until the reply has `marker`, for up
101+
/// to 40 seconds, as the legacy test retried.
102+
async fn expect_reachable(sandbox: &Sandbox, port: u16, marker: &str) -> testkit::Result {
103+
testkit::poll_until(
104+
&format!("the host server on port {port} from {}", sandbox.name()),
105+
Duration::from_secs(40),
106+
Duration::from_secs(1),
107+
async || {
108+
Ok(
109+
match http(sandbox, TEST_HOST, port, &Request::get("/")).await {
110+
Ok(reply)
111+
if reply
112+
.response
113+
.as_ref()
114+
.is_some_and(|response| response.body_text().contains(marker)) =>
115+
{
116+
Poll::Ready(())
117+
}
118+
Ok(reply) => Poll::Pending(format!("{reply:?}")),
119+
Err(error) => Poll::Pending(format!("{error:#}")),
120+
},
121+
)
122+
},
123+
)
124+
.await
125+
}
126+
73127
/// A sandbox policy that allows the image's Python ([`PYTHON`]) to
74128
/// connect to [`TEST_HOST`] on `port`. The endpoint names only the
75129
/// host and port, as the policy docs describe, so the driver's address for the
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! A sandbox client sees the connection close when a host server closes it
5+
//! after an HTTP response, whatever framing the response uses.
6+
7+
use host_server_support::{TEST_HOST, TcpServer};
8+
use sandbox_clients_support::{ReadEnd, http_raw};
9+
use testkit::policy;
10+
use testkit::{Context, PYTHON_BINARIES, ResultExt as _};
11+
12+
/// Responses the server sends, then closes the connection. Each ends the
13+
/// response a different way: an HTTP/1.0 body length, `Connection: close`
14+
/// with a body length, and `Connection: close` with a chunked body.
15+
const RESPONSES: [&str; 3] = [
16+
"HTTP/1.0 200 OK\r\nContent-Length: 3\r\n\r\nOK\n",
17+
"HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 3\r\n\r\nOK\n",
18+
"HTTP/1.1 200 OK\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nOK\n\r\n0\r\n\r\n",
19+
];
20+
21+
/// The client sends a request that doesn't ask the server to close, so only
22+
/// the server's close ends the read. A client that goes idle instead got the
23+
/// response but not the close.
24+
#[testkit::test]
25+
async fn sandbox_receives_eof_after_closing_http_response(cx: &mut Context) -> testkit::Result {
26+
let mut servers = Vec::new();
27+
let mut allowed = policy::base();
28+
for response in RESPONSES {
29+
let server = TcpServer::reply_when_idle(response).await?;
30+
policy::allow(
31+
&mut allowed,
32+
"host_server",
33+
TEST_HOST,
34+
server.port,
35+
PYTHON_BINARIES,
36+
);
37+
servers.push((response, server));
38+
}
39+
40+
let sandbox = cx
41+
.sandbox("eof")
42+
.image(testkit::python_image())
43+
.command(["sleep", "infinity"])
44+
.policy(allowed)
45+
.create_ready()
46+
.await?;
47+
48+
for (response, server) in &servers {
49+
let request = format!(
50+
"GET / HTTP/1.1\r\nHost: {TEST_HOST}:{}\r\n\r\n",
51+
server.port
52+
);
53+
let reply = http_raw(&sandbox, TEST_HOST, server.port, request.as_bytes())
54+
.await
55+
.context(format!("request {response:?} from the host server"))?;
56+
testkit::ensure!(
57+
reply.received_text().lines().any(|line| line == "OK"),
58+
"the client did not get the body of {response:?}: {reply:?}"
59+
);
60+
testkit::ensure!(
61+
reply.read_end == Some(ReadEnd::Closed),
62+
"the client did not see the server close after {response:?}: {reply:?}"
63+
);
64+
}
65+
Ok(())
66+
}

0 commit comments

Comments
 (0)