@@ -8984,307 +8984,6 @@ mod tests {
89848984 assert ! ( steps. lock( ) . unwrap( ) . is_empty( ) ) ;
89858985 }
89868986
8987- fn scripted_control_driver (
8988- steps : Vec < KubeTestStep > ,
8989- ) -> (
8990- KubernetesComputeDriver ,
8991- Arc < std:: sync:: Mutex < VecDeque < KubeTestStep > > > ,
8992- ) {
8993- let ( driver, steps, _) = scripted_driver ( KubernetesComputeConfig :: default ( ) , steps) ;
8994- ( driver, steps)
8995- }
8996-
8997- fn control_mode_object ( annotations : & [ ( & str , & str ) ] ) -> DynamicObject {
8998- let mut object = sandbox_object_for_test ( "resource-uid" , "sandbox-1" ) ;
8999- object. metadata . annotations = Some (
9000- annotations
9001- . iter ( )
9002- . map ( |( key, value) | ( ( * key) . to_string ( ) , ( * value) . to_string ( ) ) )
9003- . collect ( ) ,
9004- ) ;
9005- object
9006- }
9007-
9008- fn boundary_service_json ( role : & str , publish_not_ready : bool ) -> serde_json:: Value {
9009- let mut service = serde_json:: json!( {
9010- "apiVersion" : "v1" ,
9011- "kind" : "Service" ,
9012- "metadata" : { "name" : "os-boundary-sandbox-1" , "namespace" : "openshell" } ,
9013- "spec" : {
9014- "clusterIP" : "10.43.0.7" ,
9015- "selector" : { BOUNDARY_PAIR_LABEL : "sandbox-1" , BOUNDARY_ROLE_LABEL : role}
9016- }
9017- } ) ;
9018- if publish_not_ready {
9019- service[ "spec" ] [ "publishNotReadyAddresses" ] = true . into ( ) ;
9020- }
9021- service
9022- }
9023-
9024- /// The Service each mode renders: runtime-initiated publishes the
9025- /// supervisor before it is Ready.
9026- fn boundary_service_response ( role : & str ) -> http:: Response < kube:: client:: Body > {
9027- kube_test_response (
9028- http:: StatusCode :: OK ,
9029- boundary_service_json ( role, role == "supervisor" ) ,
9030- )
9031- }
9032-
9033- fn stored_control_policy ( generation : i64 ) -> NetworkPolicy {
9034- let names = SandboxRuntimeNames :: new ( "sandbox-1" ) ;
9035- let mut policy = control_policy (
9036- "openshell" ,
9037- & names,
9038- "sandbox-1" ,
9039- 5500 ,
9040- OwnerReference :: default ( ) ,
9041- ) ;
9042- policy. metadata . uid = Some ( "control-uid" . to_string ( ) ) ;
9043- policy. metadata . generation = Some ( generation) ;
9044- policy. metadata . resource_version = Some ( "77" . to_string ( ) ) ;
9045- policy
9046- }
9047-
9048- type Annotations < ' a > = & ' a [ ( & ' a str , & ' a str ) ] ;
9049-
9050- const SERVICE_PATH : & str = "/api/v1/namespaces/openshell/services/os-boundary-sandbox-1" ;
9051- const CONTROL_POLICY_PATH : & str =
9052- "/apis/networking.k8s.io/v1/namespaces/openshell/networkpolicies/os-control-sandbox-1" ;
9053-
9054- #[ test]
9055- fn control_connection_is_read_from_the_sandbox_and_defaults_to_legacy ( ) {
9056- assert_eq ! (
9057- sandbox_runtime_control_connection( & control_mode_object( & [ ] ) ) . unwrap( ) ,
9058- KubernetesControlConnection :: SupervisorInitiated
9059- ) ;
9060- assert_eq ! (
9061- sandbox_runtime_control_connection( & control_mode_object( & [ (
9062- ANNOTATION_SANDBOX_RUNTIME_CONTROL_CONNECTION ,
9063- "runtime-initiated"
9064- ) ] ) )
9065- . unwrap( ) ,
9066- KubernetesControlConnection :: RuntimeInitiated
9067- ) ;
9068- assert ! ( matches!(
9069- sandbox_runtime_control_connection( & control_mode_object( & [ (
9070- ANNOTATION_SANDBOX_RUNTIME_CONTROL_CONNECTION ,
9071- "Runtime-Initiated"
9072- ) ] ) ) ,
9073- Err ( KubernetesDriverError :: Precondition ( _) )
9074- ) ) ;
9075- }
9076-
9077- #[ tokio:: test]
9078- async fn reconcile_accepts_each_mode_with_its_own_endpoint ( ) {
9079- let ( driver, steps) = scripted_control_driver ( vec ! [ (
9080- http:: Method :: GET ,
9081- SERVICE_PATH ,
9082- boundary_service_response( "workload" ) ,
9083- ) ] ) ;
9084- driver
9085- . verify_sandbox_runtime_control_path (
9086- & control_mode_object ( & [ ] ) ,
9087- "sandbox-1" ,
9088- "openshell" ,
9089- true ,
9090- )
9091- . await
9092- . expect ( "legacy sandbox with workload Service" ) ;
9093- assert ! ( steps. lock( ) . unwrap( ) . is_empty( ) ) ;
9094-
9095- let ( driver, steps) = scripted_control_driver ( vec ! [
9096- (
9097- http:: Method :: GET ,
9098- SERVICE_PATH ,
9099- boundary_service_response( "supervisor" ) ,
9100- ) ,
9101- (
9102- http:: Method :: GET ,
9103- CONTROL_POLICY_PATH ,
9104- kube_test_response(
9105- http:: StatusCode :: OK ,
9106- serde_json:: to_value( stored_control_policy( 4 ) ) . unwrap( ) ,
9107- ) ,
9108- ) ,
9109- ] ) ;
9110- driver
9111- . verify_sandbox_runtime_control_path (
9112- & control_mode_object ( & [
9113- (
9114- ANNOTATION_SANDBOX_RUNTIME_CONTROL_CONNECTION ,
9115- "runtime-initiated" ,
9116- ) ,
9117- ( ANNOTATION_SANDBOX_RUNTIME_CONTROL_POLICY_UID , "control-uid" ) ,
9118- ( ANNOTATION_SANDBOX_RUNTIME_CONTROL_POLICY_GENERATION , "4" ) ,
9119- ] ) ,
9120- "sandbox-1" ,
9121- "openshell" ,
9122- true ,
9123- )
9124- . await
9125- . expect ( "runtime-initiated sandbox with bound control policy" ) ;
9126- assert ! ( steps. lock( ) . unwrap( ) . is_empty( ) ) ;
9127- }
9128-
9129- #[ tokio:: test]
9130- async fn reconcile_fails_closed_on_service_or_control_policy_drift ( ) {
9131- let runtime_annotations = [
9132- (
9133- ANNOTATION_SANDBOX_RUNTIME_CONTROL_CONNECTION ,
9134- "runtime-initiated" ,
9135- ) ,
9136- ( ANNOTATION_SANDBOX_RUNTIME_CONTROL_POLICY_UID , "control-uid" ) ,
9137- ( ANNOTATION_SANDBOX_RUNTIME_CONTROL_POLICY_GENERATION , "4" ) ,
9138- ] ;
9139- let mut broadened = stored_control_policy ( 4 ) ;
9140- broadened
9141- . spec
9142- . as_mut ( )
9143- . unwrap ( )
9144- . egress
9145- . as_mut ( )
9146- . unwrap ( )
9147- . push ( k8s_openapi:: api:: networking:: v1:: NetworkPolicyEgressRule :: default ( ) ) ;
9148- let cases: Vec < ( Annotations < ' _ > , Vec < KubeTestStep > ) > = vec ! [
9149- // A legacy sandbox's Service must keep selecting its workload.
9150- (
9151- & [ ] ,
9152- vec![ (
9153- http:: Method :: GET ,
9154- SERVICE_PATH ,
9155- boundary_service_response( "supervisor" ) ,
9156- ) ] ,
9157- ) ,
9158- // A runtime-initiated Service must keep selecting its supervisor.
9159- (
9160- & runtime_annotations,
9161- vec![ (
9162- http:: Method :: GET ,
9163- SERVICE_PATH ,
9164- boundary_service_response( "workload" ) ,
9165- ) ] ,
9166- ) ,
9167- // A runtime-initiated Service that withholds the not-Ready
9168- // supervisor would deadlock bootstrap and recovery.
9169- (
9170- & runtime_annotations,
9171- vec![ (
9172- http:: Method :: GET ,
9173- SERVICE_PATH ,
9174- kube_test_response(
9175- http:: StatusCode :: OK ,
9176- boundary_service_json( "supervisor" , false ) ,
9177- ) ,
9178- ) ] ,
9179- ) ,
9180- // A running sandbox whose Service disappeared.
9181- (
9182- & [ ] ,
9183- vec![ (
9184- http:: Method :: GET ,
9185- SERVICE_PATH ,
9186- kube_test_not_found( "services" , "os-boundary-sandbox-1" ) ,
9187- ) ] ,
9188- ) ,
9189- // The control policy was deleted.
9190- (
9191- & runtime_annotations,
9192- vec![
9193- (
9194- http:: Method :: GET ,
9195- SERVICE_PATH ,
9196- boundary_service_response( "supervisor" ) ,
9197- ) ,
9198- (
9199- http:: Method :: GET ,
9200- CONTROL_POLICY_PATH ,
9201- kube_test_not_found( "networkpolicies" , "os-control-sandbox-1" ) ,
9202- ) ,
9203- ] ,
9204- ) ,
9205- // The control policy was broadened in place.
9206- (
9207- & runtime_annotations,
9208- vec![
9209- (
9210- http:: Method :: GET ,
9211- SERVICE_PATH ,
9212- boundary_service_response( "supervisor" ) ,
9213- ) ,
9214- (
9215- http:: Method :: GET ,
9216- CONTROL_POLICY_PATH ,
9217- kube_test_response(
9218- http:: StatusCode :: OK ,
9219- serde_json:: to_value( & broadened) . unwrap( ) ,
9220- ) ,
9221- ) ,
9222- ] ,
9223- ) ,
9224- // The control policy changed after it was bound.
9225- (
9226- & runtime_annotations,
9227- vec![
9228- (
9229- http:: Method :: GET ,
9230- SERVICE_PATH ,
9231- boundary_service_response( "supervisor" ) ,
9232- ) ,
9233- (
9234- http:: Method :: GET ,
9235- CONTROL_POLICY_PATH ,
9236- kube_test_response(
9237- http:: StatusCode :: OK ,
9238- serde_json:: to_value( stored_control_policy( 5 ) ) . unwrap( ) ,
9239- ) ,
9240- ) ,
9241- ] ,
9242- ) ,
9243- // An unknown recorded mode is never reinterpreted.
9244- (
9245- & [ (
9246- ANNOTATION_SANDBOX_RUNTIME_CONTROL_CONNECTION ,
9247- "bidirectional" ,
9248- ) ] ,
9249- vec![ ] ,
9250- ) ,
9251- ] ;
9252- // A bootstrapping generation tolerates a missing Service (availability
9253- // and reaping handle it) but still rejects drift in what exists.
9254- let ( driver, remaining) = scripted_control_driver ( vec ! [ (
9255- http:: Method :: GET ,
9256- SERVICE_PATH ,
9257- kube_test_not_found( "services" , "os-boundary-sandbox-1" ) ,
9258- ) ] ) ;
9259- driver
9260- . verify_sandbox_runtime_control_path (
9261- & control_mode_object ( & [ ] ) ,
9262- "sandbox-1" ,
9263- "openshell" ,
9264- false ,
9265- )
9266- . await
9267- . expect ( "bootstrap without a Service is left to availability" ) ;
9268- assert ! ( remaining. lock( ) . unwrap( ) . is_empty( ) ) ;
9269- for ( index, ( annotations, steps) ) in cases. into_iter ( ) . enumerate ( ) {
9270- let ( driver, remaining) = scripted_control_driver ( steps) ;
9271- let result = driver
9272- . verify_sandbox_runtime_control_path (
9273- & control_mode_object ( annotations) ,
9274- "sandbox-1" ,
9275- "openshell" ,
9276- true ,
9277- )
9278- . await ;
9279- assert ! (
9280- matches!( result, Err ( KubernetesDriverError :: Precondition ( _) ) ) ,
9281- "case {index}: {:?}" ,
9282- result. err( )
9283- ) ;
9284- assert ! ( remaining. lock( ) . unwrap( ) . is_empty( ) , "case {index}" ) ;
9285- }
9286- }
9287-
92888987 #[ test]
92898988 fn bootstrap_phase_round_trips_releasing ( ) {
92908989 let phase = SandboxRuntimeBootstrapPhase :: Releasing ;
0 commit comments