@@ -7,9 +7,9 @@ use crate::grpc::test_support::{authed_request, test_server_state};
77use openshell_core:: proto:: datamodel:: v1:: ObjectMeta ;
88use openshell_core:: proto:: open_shell_server:: OpenShell ;
99use openshell_core:: proto:: {
10- CreateSandboxRequest , SandboxServiceExposure , SandboxSpec , SandboxWorkloadConfig ,
11- SandboxWorkloadTemplate , SandboxWorkloadTemplateSpec , ServiceAuthorizationMode ,
12- WorkspaceMember , WorkspaceRole ,
10+ CreateSandboxRequest , ExposeServiceRequest , SandboxServiceExposure , SandboxSpec ,
11+ SandboxWorkloadConfig , SandboxWorkloadTemplate , SandboxWorkloadTemplateSpec ,
12+ ServiceAuthorizationMode , WorkspaceMember , WorkspaceRole ,
1313} ;
1414use openshell_core:: rpc_error:: StatusExt ;
1515use std:: collections:: HashMap ;
@@ -93,8 +93,7 @@ fn canonical_payload_ignores_map_order_and_id_but_preserves_presence() {
9393 assert_ne ! ( absent, fingerprint( & template) . unwrap( ) ) ;
9494}
9595
96- #[ tokio:: test]
97- async fn service_url_replay_uses_current_origin_without_repeating_mutation ( ) {
96+ async fn url_replay_state ( ) -> ( Arc < ServerState > , tempfile:: TempDir ) {
9897 let directory = tempfile:: tempdir ( ) . unwrap ( ) ;
9998 let key = directory. path ( ) . join ( "private-key" ) ;
10099 std:: fs:: write ( & key, b"test-only stable private fingerprint material" ) . unwrap ( ) ;
@@ -107,26 +106,35 @@ async fn service_url_replay_uses_current_origin_without_repeating_mutation() {
107106 gateway_id : "test" . into ( ) ,
108107 ttl_secs : None ,
109108 } ) ;
109+ ( state, directory)
110+ }
111+
112+ #[ tokio:: test]
113+ async fn create_replay_preserves_recorded_urls_and_refreshes_explicit_origins ( ) {
114+ let ( mut state, _directory) = url_replay_state ( ) . await ;
110115 let request = CreateSandboxRequest {
111116 name : "origin-replay" . into ( ) ,
112117 spec : Some ( SandboxSpec :: default ( ) ) ,
113118 workspace_scope : Some ( openshell_core:: proto:: workspace_selector ( "default" ) ) ,
114- service_exposures : vec ! [ SandboxServiceExposure {
115- service: "web" . into( ) ,
116- target_port: 8080 ,
117- ..Default :: default ( )
118- } ] ,
119+ service_exposures : [ "" , "web" ]
120+ . into_iter ( )
121+ . map ( |service| SandboxServiceExposure {
122+ service : service. into ( ) ,
123+ target_port : 8080 ,
124+ ..Default :: default ( )
125+ } )
126+ . collect ( ) ,
119127 request_id : uuid:: Uuid :: new_v4 ( ) . to_string ( ) ,
120128 ..Default :: default ( )
121129 } ;
122130 let original = run ( & state, authed_request ( request. clone ( ) ) )
123131 . await
124132 . unwrap ( )
125133 . into_inner ( ) ;
126- Arc :: get_mut ( & mut state)
127- . unwrap ( )
128- . config
129- . supervisor_app_listener = openshell_core:: app_listener:: AppListenerConfig {
134+ let config = & mut Arc :: get_mut ( & mut state) . unwrap ( ) . config ;
135+ config . service_routing . base_domains = vec ! [ "gateway.example.com" . into ( ) ] ;
136+ config. bind_address . set_port ( 12443 ) ;
137+ config . supervisor_app_listener = openshell_core:: app_listener:: AppListenerConfig {
130138 enabled : true ,
131139 port : 9443 ,
132140 domain : "services.example.com" . into ( ) ,
@@ -138,17 +146,103 @@ async fn service_url_replay_uses_current_origin_without_repeating_mutation() {
138146 original. sandbox. as_ref( ) . unwrap( ) . metadata,
139147 replay. get_ref( ) . sandbox. as_ref( ) . unwrap( ) . metadata
140148 ) ;
149+ assert_eq ! ( replay. get_ref( ) . service_urls, original. service_urls) ;
150+ for ( service, label) in [
151+ ( "" , "default--origin-replay" ) ,
152+ ( "web" , "default--origin-replay--web" ) ,
153+ ] {
154+ assert_eq ! (
155+ replay. get_ref( ) . application_service_urls[ service] ,
156+ format!( "https://{label}.services.example.com:10443/" )
157+ ) ;
158+ let gateway = url:: Url :: parse ( & replay. get_ref ( ) . gateway_service_urls [ service] ) . unwrap ( ) ;
159+ assert_eq ! (
160+ gateway. host_str( ) ,
161+ Some ( format!( "{label}.gateway.example.com" ) . as_str( ) )
162+ ) ;
163+ assert_eq ! ( gateway. port( ) , Some ( 12443 ) ) ;
164+ assert_ne ! (
165+ replay. get_ref( ) . gateway_service_urls[ service] ,
166+ original. gateway_service_urls[ service]
167+ ) ;
168+ }
169+ }
170+
171+ #[ tokio:: test]
172+ async fn expose_replay_preserves_recorded_url_when_origins_change_or_are_disabled ( ) {
173+ let ( mut state, _directory) = url_replay_state ( ) . await ;
174+ Arc :: get_mut ( & mut state)
175+ . unwrap ( )
176+ . config
177+ . supervisor_app_listener = openshell_core:: app_listener:: AppListenerConfig {
178+ enabled : true ,
179+ port : 9443 ,
180+ domain : "old.example.com" . into ( ) ,
181+ public_url : Some ( "https://old.example.com:10443" . into ( ) ) ,
182+ } ;
183+ run (
184+ & state,
185+ authed_request ( CreateSandboxRequest {
186+ name : "expose-replay" . into ( ) ,
187+ spec : Some ( SandboxSpec :: default ( ) ) ,
188+ workspace_scope : Some ( openshell_core:: proto:: workspace_selector ( "default" ) ) ,
189+ ..Default :: default ( )
190+ } ) ,
191+ )
192+ . await
193+ . unwrap ( ) ;
194+ let request = ExposeServiceRequest {
195+ sandbox : "expose-replay" . into ( ) ,
196+ name : "web" . into ( ) ,
197+ workspace_scope : Some ( openshell_core:: proto:: workspace_selector ( "default" ) ) ,
198+ target_port : 8080 ,
199+ request_id : uuid:: Uuid :: new_v4 ( ) . to_string ( ) ,
200+ ..Default :: default ( )
201+ } ;
202+ let original = run ( & state, authed_request ( request. clone ( ) ) )
203+ . await
204+ . unwrap ( )
205+ . into_inner ( ) ;
206+ assert_eq ! (
207+ original. url,
208+ "https://default--expose-replay--web.old.example.com:10443/"
209+ ) ;
210+ let config = & mut Arc :: get_mut ( & mut state) . unwrap ( ) . config ;
211+ config. service_routing . base_domains = vec ! [ "gateway.example.com" . into( ) ] ;
212+ config. bind_address . set_port ( 12443 ) ;
213+ config. supervisor_app_listener . domain = "new.example.com" . into ( ) ;
214+ config. supervisor_app_listener . public_url = Some ( "https://new.example.com:11443" . into ( ) ) ;
215+ let replay = run ( & state, authed_request ( request. clone ( ) ) ) . await . unwrap ( ) ;
216+ assert_eq ! ( replay. metadata( ) . get( "openshell-replayed" ) . unwrap( ) , "true" ) ;
217+ assert_eq ! ( replay. get_ref( ) . endpoint, original. endpoint) ;
218+ assert_eq ! ( replay. get_ref( ) . url, original. url) ;
141219 assert_eq ! (
142- replay. get_ref( ) . service_urls [ "web" ] ,
143- "https://default--origin -replay--web.services .example.com:10443 /"
220+ replay. get_ref( ) . application_url ,
221+ "https://default--expose -replay--web.new .example.com:11443 /"
144222 ) ;
223+ let gateway = url:: Url :: parse ( & replay. get_ref ( ) . gateway_url ) . unwrap ( ) ;
145224 assert_eq ! (
146- replay. get_ref( ) . application_service_urls,
147- replay. get_ref( ) . service_urls
225+ gateway. host_str( ) ,
226+ Some ( "default--expose-replay--web.gateway.example.com" )
227+ ) ;
228+ assert_eq ! ( gateway. port( ) , Some ( 12443 ) ) ;
229+ let config = & mut Arc :: get_mut ( & mut state) . unwrap ( ) . config ;
230+ config. supervisor_app_listener . enabled = false ;
231+ config. supervisor_app_listener . public_url = None ;
232+ let without_application = run ( & state, authed_request ( request) ) . await . unwrap ( ) ;
233+ assert_eq ! (
234+ without_application
235+ . metadata( )
236+ . get( "openshell-replayed" )
237+ . unwrap( ) ,
238+ "true"
148239 ) ;
240+ assert_eq ! ( without_application. get_ref( ) . url, original. url) ;
241+ assert_eq ! ( without_application. get_ref( ) . endpoint, original. endpoint) ;
242+ assert ! ( without_application. get_ref( ) . application_url. is_empty( ) ) ;
149243 assert_eq ! (
150- replay . get_ref( ) . gateway_service_urls ,
151- original . gateway_service_urls
244+ without_application . get_ref( ) . gateway_url ,
245+ replay . get_ref ( ) . gateway_url
152246 ) ;
153247}
154248
0 commit comments