Skip to content

Commit ce41fc0

Browse files
test(features): use the system Python for the host-gateway client and trim exports
The client needs only the standard library, so run /usr/bin/python3 instead of the image's uv virtual environment and let the policy name its real path. Keep the new image constants private to testkit, and fail the k3s installer if the cluster ever has more than one node, since the host gateway address is the node the tests run on. Refs #5275 Signed-off-by: Matthew Grossman <[email protected]>
1 parent a5fef8b commit ce41fc0

4 files changed

Lines changed: 32 additions & 41 deletions

File tree

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

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
1414
use tokio::net::{TcpListener, TcpStream};
1515
use tokio::task::JoinHandle;
1616

17-
/// The Python test image's virtual environment, which links to a uv-managed
18-
/// Python under `/sandbox/.uv/python`.
19-
const VENV_PYTHON: &str = "/sandbox/.venv/bin/python3";
20-
21-
/// Prints the interpreter's prefix, then requests `/` from
22-
/// `host.openshell.internal` on the port in `argv[1]` and prints the
23-
/// response. A failed connection is retried for 20 seconds, so a slow
24-
/// start doesn't fail the test, and reported on stderr.
17+
/// The image's system Python, which needs nothing beyond its standard library.
18+
/// The policy names its real path, `/usr/bin/python3.*`, because a rule matches
19+
/// the executable behind a symlink.
20+
const PYTHON: &str = "/usr/bin/python3";
21+
22+
/// Requests `/` from `host.openshell.internal` on the port in `argv[1]` and
23+
/// prints the response. A failed connection is retried for 20 seconds, so a
24+
/// slow start doesn't fail the test, and reported on stderr.
2525
const CLIENT: &str = r#"
2626
import socket, sys, time
2727
28-
print(f"prefix={sys.prefix}", flush=True)
2928
host, port = "host.openshell.internal", int(sys.argv[1])
3029
deadline = time.monotonic() + 20
3130
while True:
@@ -46,9 +45,9 @@ print(response.decode(errors="replace"))
4645
"#;
4746

4847
/// A sandbox from the Python test image resolves `host.openshell.internal`
49-
/// and gets a response from a server the test runs on the host. The image's
50-
/// virtual environment runs the client, so its workspace content reached the
51-
/// sandbox, and the policy lets only that Python reach the server's port.
48+
/// and gets a response from a server the test runs on the host. The default
49+
/// policy denies all network access, so the test passes one that lets only the
50+
/// image's Python reach the server's port.
5251
#[testkit::test]
5352
async fn python_sandbox_reaches_host_server(cx: &mut Context) -> testkit::Result {
5453
let marker = format!("host-gateway-{}", cx.run_id());
@@ -87,25 +86,11 @@ async fn python_sandbox_reaches_host_server(cx: &mut Context) -> testkit::Result
8786

8887
let client = cx
8988
.cli([
90-
"sandbox",
91-
"exec",
92-
"--name",
93-
&name,
94-
"--no-tty",
95-
"--",
96-
VENV_PYTHON,
97-
"-c",
98-
CLIENT,
99-
&port,
89+
"sandbox", "exec", "--name", &name, "--no-tty", "--", PYTHON, "-c", CLIENT, &port,
10090
])
10191
.await?
10292
.success()
10393
.context("run the client in the sandbox")?;
104-
testkit::ensure!(
105-
client.stdout.contains("prefix=/sandbox/.venv"),
106-
"the client did not run in the image's virtual environment:\n{}",
107-
client.stdout
108-
);
10994
testkit::ensure!(
11095
client.stdout.contains(&marker),
11196
"the client did not get the host server's response:\n{}{}",
@@ -115,11 +100,10 @@ async fn python_sandbox_reaches_host_server(cx: &mut Context) -> testkit::Result
115100
Ok(())
116101
}
117102

118-
/// A sandbox policy for the Python test image that allows its uv-managed
119-
/// Python, which [`VENV_PYTHON`] links to, to connect to
120-
/// `host.openshell.internal` on `port`. The endpoint names only the host and
121-
/// port, as the policy docs describe, so the driver's address for the name
122-
/// is what makes it reachable, not an `allowed_ips` range.
103+
/// A sandbox policy that allows the image's system Python ([`PYTHON`]) to
104+
/// connect to `host.openshell.internal` on `port`. The endpoint names only the
105+
/// host and port, as the policy docs describe, so the driver's address for the
106+
/// name is what makes it reachable, not an `allowed_ips` range.
123107
fn policy(port: u16) -> String {
124108
format!(
125109
r#"version: 1
@@ -142,7 +126,7 @@ network_policies:
142126
- host: host.openshell.internal
143127
port: {port}
144128
binaries:
145-
- path: /sandbox/.uv/python/**/python*
129+
- path: /usr/bin/python3.*
146130
"#
147131
)
148132
}

‎tests/testkit/testkit/src/image.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ pub const BASE_IMAGE_ENV: &str = "OPENSHELL_TEST_BASE_IMAGE";
1010
pub const DEFAULT_BASE_IMAGE: &str = "nvcr.io/nvidia/base/ubuntu:24.04";
1111

1212
/// Overrides [`DEFAULT_PYTHON_IMAGE`].
13-
pub const PYTHON_IMAGE_ENV: &str = "OPENSHELL_TEST_PYTHON_IMAGE";
13+
const PYTHON_IMAGE_ENV: &str = "OPENSHELL_TEST_PYTHON_IMAGE";
1414
/// The workload image for tests that run `python3`, curl, git or openssl in a
1515
/// sandbox, built from `e2e/python/Dockerfile.workload`. It isn't published:
1616
/// the tmachine installers load it into every environment, and
1717
/// `mise run e2e:workload:build` builds it locally.
18-
pub const DEFAULT_PYTHON_IMAGE: &str = "openshell/e2e-python:dev";
18+
const DEFAULT_PYTHON_IMAGE: &str = "openshell/e2e-python:dev";
1919

2020
/// The image named by [`BASE_IMAGE_ENV`], or [`DEFAULT_BASE_IMAGE`].
2121
pub fn base_image() -> String {

‎tests/testkit/testkit/src/lib.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ pub use context::{
3939
};
4040
pub use error::{Error, Result, ResultExt};
4141
pub use gateway::Gateway;
42-
pub use image::{
43-
BASE_IMAGE_ENV, DEFAULT_BASE_IMAGE, DEFAULT_PYTHON_IMAGE, PYTHON_IMAGE_ENV, base_image,
44-
python_image,
45-
};
42+
pub use image::{BASE_IMAGE_ENV, DEFAULT_BASE_IMAGE, base_image, python_image};
4643
pub use openshell_sdk as sdk;
4744
pub use poll::{Poll, poll_until};
4845
pub use run::{TestInfo, run};

‎tests/tmachine/ansible/playbooks/openshell-k3s.yaml‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@
6161
mode: "0600"
6262

6363
# Tests run in the guest, so host.openshell.internal must resolve to the
64-
# node itself. QEMU user networking gives every boot the same guest
65-
# address (10.0.2.15), so the value stays valid when the disk is reused.
64+
# node itself. A tmachine environment is one VM, so the cluster has one
65+
# node; the check below fails if that stops being true, because then the
66+
# address must be the one of the node the tests run on. QEMU user
67+
# networking gives every boot the same guest address (10.0.2.15), so the
68+
# value stays valid when the disk is reused.
6669
# The node can also list an IPv6 address after it, so take the IPv4 one.
6770
- name: Discover the K3s node address
6871
ansible.builtin.command:
@@ -81,6 +84,13 @@
8184
retries: 30
8285
delay: 2
8386

87+
- name: Check the K3s cluster has a single node
88+
ansible.builtin.command:
89+
argv: [/usr/local/bin/k3s, kubectl, --request-timeout=10s, get, nodes, --output=name]
90+
register: k3s_nodes
91+
changed_when: false
92+
failed_when: k3s_nodes.rc != 0 or k3s_nodes.stdout_lines | length != 1
93+
8494
- name: Select the K3s node IPv4 address
8595
ansible.builtin.set_fact:
8696
k3s_node_ipv4: "{{ k3s_node_address.stdout.split() | select('match', '^[0-9.]+$') | first }}"

0 commit comments

Comments
 (0)