11// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22// SPDX-License-Identifier: Apache-2.0
33
4- //! Driving the `openshell` CLI the environment installed .
4+ //! Driving the installed `openshell` CLI.
55//!
66//! Some promised behavior exists only in the CLI: file upload and download,
77//! interactive connect, and port forwarding. Tests act through the CLI for
@@ -16,16 +16,18 @@ use std::time::Duration;
1616use serde:: de:: DeserializeOwned ;
1717
1818use crate :: error:: { Error , Result , ResultExt } ;
19- use crate :: target :: CliTarget ;
19+ use crate :: gateway :: { ENDPOINT_ENV , GATEWAY_ENV , Gateway , WORKSPACE_ENV } ;
2020
2121/// How long one CLI invocation may take.
2222pub const CLI_TIMEOUT : Duration = Duration :: from_mins ( 5 ) ;
23+ /// The CLI binary to run. Defaults to `openshell` on PATH.
24+ pub const BIN_ENV : & str = "OPENSHELL_BIN" ;
2325
24- /// Runs the target's CLI against the target's gateway.
26+ /// Runs the CLI against the gateway and workspace the tests use .
2527#[ derive( Clone , Debug ) ]
2628pub struct Cli {
2729 binary : PathBuf ,
28- gateway : Option < String > ,
30+ gateway : Gateway ,
2931}
3032
3133/// The buffered result of one CLI invocation.
@@ -38,13 +40,31 @@ pub struct CliOutput {
3840}
3941
4042impl Cli {
41- pub ( crate ) fn new ( target : & CliTarget , binary : PathBuf ) -> Self {
43+ /// The CLI named by [`BIN_ENV`], pointed at `gateway`.
44+ pub fn from_env ( gateway : & Gateway ) -> Self {
45+ let binary = std:: env:: var_os ( BIN_ENV )
46+ . filter ( |value| !value. is_empty ( ) )
47+ . map_or_else ( || PathBuf :: from ( "openshell" ) , PathBuf :: from) ;
4248 Self {
4349 binary,
44- gateway : target . gateway . clone ( ) ,
50+ gateway : gateway. clone ( ) ,
4551 }
4652 }
4753
54+ /// Check that the CLI reaches the gateway: `openshell status` must report
55+ /// it connected.
56+ pub async fn check_connected ( & self ) -> Result {
57+ let output = self . run ( [ "status" , "--output" , "json" ] ) . await ?. success ( ) ?;
58+ let status: serde_json:: Value = output. json ( ) ?;
59+ crate :: ensure!(
60+ status[ "status" ] == "connected" ,
61+ "`{}` reports {}" ,
62+ output. command,
63+ status
64+ ) ;
65+ Ok ( ( ) )
66+ }
67+
4868 /// Run the CLI with `args` and buffer its output. A non-zero exit is
4969 /// returned, not raised; use [`CliOutput::success`] to require success.
5070 pub async fn run < I , S > ( & self , args : I ) -> Result < CliOutput >
@@ -75,14 +95,15 @@ impl Cli {
7595 process
7696 . args ( & args)
7797 . env ( "NO_COLOR" , "1" )
78- // Select the target's gateway, never one from the caller's
79- // environment.
80- . env_remove ( "OPENSHELL_GATEWAY_ENDPOINT" )
98+ . env ( WORKSPACE_ENV , & self . gateway . workspace )
8199 . stdin ( Stdio :: null ( ) )
82100 . kill_on_drop ( true ) ;
83- match & self . gateway {
84- Some ( gateway) => process. env ( "OPENSHELL_GATEWAY" , gateway) ,
85- None => process. env_remove ( "OPENSHELL_GATEWAY" ) ,
101+ // Select exactly the gateway the SDK connected to.
102+ match & self . gateway . name {
103+ Some ( name) => process. env ( GATEWAY_ENV , name) . env_remove ( ENDPOINT_ENV ) ,
104+ None => process
105+ . env ( ENDPOINT_ENV , & self . gateway . endpoint )
106+ . env_remove ( GATEWAY_ENV ) ,
86107 } ;
87108 if let Some ( dir) = dir {
88109 process. current_dir ( dir) ;
@@ -144,22 +165,41 @@ mod tests {
144165 fn cli ( binary : & str ) -> Cli {
145166 Cli {
146167 binary : PathBuf :: from ( binary) ,
147- gateway : Some ( "tmachine" . to_string ( ) ) ,
168+ gateway : Gateway :: named_for_test ( "tmachine" ) ,
148169 }
149170 }
150171
151172 #[ tokio:: test]
152- async fn runs_with_the_target_gateway_selected ( ) {
173+ async fn runs_with_the_gateway_selected ( ) {
153174 let output = cli ( "sh" )
154- . run ( [ "-c" , "echo \" $OPENSHELL_GATEWAY\" ; exit 3" ] )
175+ . run ( [
176+ "-c" ,
177+ "echo \" $OPENSHELL_GATEWAY|${OPENSHELL_GATEWAY_ENDPOINT-unset}|$OPENSHELL_WORKSPACE\" ; exit 3" ,
178+ ] )
155179 . await
156180 . unwrap ( ) ;
157- assert_eq ! ( output. stdout, "tmachine\n " ) ;
181+ assert_eq ! ( output. stdout, "tmachine|unset|default \n " ) ;
158182 assert_eq ! ( output. exit_code, 3 ) ;
159183 let err = output. success ( ) . unwrap_err ( ) ;
160184 assert ! ( err. to_string( ) . contains( "exited with 3" ) , "{err}" ) ;
161185 }
162186
187+ #[ tokio:: test]
188+ async fn runs_with_a_raw_endpoint_selected ( ) {
189+ let cli = Cli {
190+ binary : PathBuf :: from ( "sh" ) ,
191+ gateway : Gateway :: endpoint_for_test ( "http://127.0.0.1:1" ) ,
192+ } ;
193+ let output = cli
194+ . run ( [
195+ "-c" ,
196+ "echo \" ${OPENSHELL_GATEWAY-unset}|$OPENSHELL_GATEWAY_ENDPOINT\" " ,
197+ ] )
198+ . await
199+ . unwrap ( ) ;
200+ assert_eq ! ( output. stdout, "unset|http://127.0.0.1:1\n " ) ;
201+ }
202+
163203 #[ tokio:: test]
164204 async fn parses_json_output ( ) {
165205 let output = cli ( "sh" )
0 commit comments