55//! compute driver.
66//!
77//! The test starts an HTTP server where the tests run, which is the host the
8- //! gateway runs its sandboxes on, and runs a client in a sandbox from the
9- //! Python test image ([`testkit::python_image`]) that requests a page from that
10- //! server through the name .
8+ //! gateway runs its sandboxes on, and requests a page from it through the name
9+ //! with [`sandbox_clients_support::http`], from a sandbox created from the
10+ //! Python test image ([`testkit::python_image`]) .
1111
12- use host_server_support:: HttpRecorder ;
12+ use host_server_support:: { HttpRecorder , TEST_HOST } ;
13+ use sandbox_clients_support:: { Request , http} ;
1314use testkit:: policy;
1415use testkit:: sdk:: raw:: proto;
1516use testkit:: { Context , PYTHON_BINARIES , ResultExt as _} ;
1617
17- /// The image's Python, which needs nothing beyond its standard library. The
18- /// policy allows [`PYTHON_BINARIES`], the real paths behind `python3` in
19- /// `testkit::python_image()` images, because a rule matches the executable
20- /// behind a symlink.
21- const PYTHON : & str = "python3" ;
22-
23- /// Requests `/` from `host.openshell.internal` on the port in `argv[1]` and
24- /// prints the response. The server listens before the sandbox is created,
25- /// and a Ready sandbox's network is up, so the client doesn't retry.
26- const CLIENT : & str = r#"
27- import socket, sys
28-
29- host, port = "host.openshell.internal", int(sys.argv[1])
30- with socket.create_connection((host, port), timeout=10) as conn:
31- conn.sendall(f"GET / HTTP/1.1\r\nHost: {host}:{port}\r\nConnection: close\r\n\r\n".encode())
32- response = b""
33- while chunk := conn.recv(4096):
34- response += chunk
35- print(response.decode(errors="replace"))
36- "# ;
37-
3818/// A sandbox from the Python test image resolves `host.openshell.internal`
3919/// and gets a response from a server the test runs on the host. The default
4020/// policy denies all network access, so the test passes one that lets only the
@@ -53,38 +33,35 @@ async fn python_sandbox_reaches_host_server(cx: &mut Context) -> testkit::Result
5333 let sandbox = cx
5434 . sandbox ( "hg" )
5535 . image ( image. clone ( ) )
36+ . command ( [ "sleep" , "infinity" ] )
5637 . policy ( policy ( server. port ) )
57- . command ( [ "sh" , "-c" , "exec sleep infinity" ] )
5838 . create_ready ( )
5939 . await
6040 . context ( format ! ( "create a sandbox from {image}" ) ) ?;
6141
62- let client = sandbox
63- . exec ( [ PYTHON , "-c" , CLIENT , & server . port . to_string ( ) ] )
64- . await ?
65- . success ( )
66- . context ( "run the client in the sandbox " ) ?;
42+ // The server listens before the sandbox is created, and a Ready
43+ // sandbox's network is up, so the request isn't retried.
44+ let reply = http ( & sandbox , TEST_HOST , server . port , & Request :: get ( "/" ) )
45+ . await
46+ . context ( "request a page from the host server " ) ?;
6747 testkit:: ensure!(
68- client. stdout. contains( & marker) ,
69- "the client did not get the host server's response:\n {}{}" ,
70- client. stdout,
71- client. stderr
48+ reply
49+ . response
50+ . as_ref( )
51+ . is_some_and( |response| response. body_text( ) . contains( & marker) ) ,
52+ "the client did not get the host server's response: {reply:?}"
7253 ) ;
7354 Ok ( ( ) )
7455}
7556
76- /// A sandbox policy that allows the image's Python ([`PYTHON`]) to
77- /// connect to `host.openshell.internal` on `port`. The endpoint names only the
57+ /// A sandbox policy that allows the image's Python to connect to
58+ /// `host.openshell.internal` on `port`. It allows [`PYTHON_BINARIES`], the
59+ /// real paths behind `python3` in `testkit::python_image()` images, because a
60+ /// rule matches the executable behind a symlink. The endpoint names only the
7861/// host and port, as the policy docs describe, so the driver's address for the
7962/// name is what makes it reachable, not an `allowed_ips` range.
8063fn policy ( port : u16 ) -> proto:: SandboxPolicy {
8164 let mut policy = policy:: base ( ) ;
82- policy:: allow (
83- & mut policy,
84- "host_server" ,
85- "host.openshell.internal" ,
86- port,
87- PYTHON_BINARIES ,
88- ) ;
65+ policy:: allow ( & mut policy, "host_server" , TEST_HOST , port, PYTHON_BINARIES ) ;
8966 policy
9067}
0 commit comments