|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Portable sandbox environment conformance scenarios. |
| 5 | +
|
| 6 | +use std::time::Duration; |
| 7 | + |
| 8 | +use serde::Deserialize; |
| 9 | + |
| 10 | +use crate::{OpenShellRunner, Poll, Scenario, ScenarioFuture}; |
| 11 | + |
| 12 | +const CREATE_TIMEOUT: Duration = Duration::from_mins(10); |
| 13 | +const COMMAND_TIMEOUT: Duration = Duration::from_mins(2); |
| 14 | +const TRANSITION_TIMEOUT: Duration = Duration::from_mins(4); |
| 15 | +const TRANSITION_INTERVAL: Duration = Duration::from_secs(2); |
| 16 | + |
| 17 | +#[derive(Debug, Deserialize)] |
| 18 | +struct SandboxState { |
| 19 | + name: String, |
| 20 | + phase: String, |
| 21 | +} |
| 22 | + |
| 23 | +/// Certify declared environment propagation to the canonical main and exec. |
| 24 | +pub const SANDBOX_ENVIRONMENT_SCENARIO: Scenario = Scenario { |
| 25 | + name: "sandbox-environment", |
| 26 | + description: "Verify declared environment propagation to canonical-main and exec processes.", |
| 27 | + run: run_sandbox_environment, |
| 28 | +}; |
| 29 | + |
| 30 | +fn run_sandbox_environment(runner: &mut OpenShellRunner) -> ScenarioFuture<'_> { |
| 31 | + Box::pin(async move { |
| 32 | + let sandbox_name = format!("ct-{}-env", runner.id()); |
| 33 | + let sentinel_path = "/sandbox/.openshell-declared-environment"; |
| 34 | + let main = format!( |
| 35 | + "printf '%s\\n' \"${{REPRO_SENTINEL:-missing}}\" > '{sentinel_path}'; exec sleep infinity" |
| 36 | + ); |
| 37 | + |
| 38 | + create_sandbox(runner, &sandbox_name, &main).await?; |
| 39 | + exec_expect_exact( |
| 40 | + runner, |
| 41 | + &sandbox_name, |
| 42 | + "read-main-environment", |
| 43 | + &[ |
| 44 | + "sh", |
| 45 | + "-c", |
| 46 | + &format!( |
| 47 | + "while [ ! -f '{sentinel_path}' ]; do sleep 0.05; done; cat '{sentinel_path}'" |
| 48 | + ), |
| 49 | + ], |
| 50 | + "present\n", |
| 51 | + ) |
| 52 | + .await?; |
| 53 | + let exec_environment = format!("printf '%s\\n' \"${{{}:-missing}}\"", "REPRO_SENTINEL"); |
| 54 | + exec_expect_exact( |
| 55 | + runner, |
| 56 | + &sandbox_name, |
| 57 | + "read-exec-environment", |
| 58 | + &["sh", "-c", &exec_environment], |
| 59 | + "present\n", |
| 60 | + ) |
| 61 | + .await?; |
| 62 | + delete_and_confirm_absent(runner, &sandbox_name).await |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +async fn create_sandbox( |
| 67 | + runner: &mut OpenShellRunner, |
| 68 | + sandbox_name: &str, |
| 69 | + main: &str, |
| 70 | +) -> Result<(), String> { |
| 71 | + runner.track_sandbox(sandbox_name); |
| 72 | + let create = runner |
| 73 | + .step("create") |
| 74 | + .description(format!( |
| 75 | + "sandbox '{sandbox_name}' is created with a declared environment" |
| 76 | + )) |
| 77 | + .with_timeout(CREATE_TIMEOUT) |
| 78 | + .run(&[ |
| 79 | + "sandbox", |
| 80 | + "create", |
| 81 | + "--name", |
| 82 | + sandbox_name, |
| 83 | + "--detach", |
| 84 | + "--no-tty", |
| 85 | + "--no-auto-providers", |
| 86 | + "--env", |
| 87 | + "REPRO_SENTINEL=present", |
| 88 | + "--", |
| 89 | + "sh", |
| 90 | + "-lc", |
| 91 | + main, |
| 92 | + ]) |
| 93 | + .await |
| 94 | + .map_err(|error| error.to_string())?; |
| 95 | + create.require_success()?; |
| 96 | + wait_for_phase(runner, sandbox_name, "Ready", "ready").await |
| 97 | +} |
| 98 | + |
| 99 | +async fn exec_expect_exact( |
| 100 | + runner: &OpenShellRunner, |
| 101 | + sandbox_name: &str, |
| 102 | + step: &str, |
| 103 | + command: &[&str], |
| 104 | + expected_stdout: &str, |
| 105 | +) -> Result<(), String> { |
| 106 | + let mut args = vec!["sandbox", "exec", "--name", sandbox_name, "--no-tty", "--"]; |
| 107 | + args.extend_from_slice(command); |
| 108 | + let result = runner |
| 109 | + .step(step) |
| 110 | + .description(format!("sandbox '{sandbox_name}' exec {step} succeeds")) |
| 111 | + .with_timeout(COMMAND_TIMEOUT) |
| 112 | + .run(&args) |
| 113 | + .await |
| 114 | + .map_err(|error| error.to_string())?; |
| 115 | + result.require_success()?; |
| 116 | + if result.stdout() == expected_stdout { |
| 117 | + Ok(()) |
| 118 | + } else { |
| 119 | + Err(result.failure_diagnostic(&format!("stdout is exactly {expected_stdout:?}"))) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +async fn delete_and_confirm_absent( |
| 124 | + runner: &mut OpenShellRunner, |
| 125 | + sandbox_name: &str, |
| 126 | +) -> Result<(), String> { |
| 127 | + let delete = runner |
| 128 | + .step("delete") |
| 129 | + .description(format!("sandbox '{sandbox_name}' deletion succeeds")) |
| 130 | + .with_timeout(COMMAND_TIMEOUT) |
| 131 | + .run(&["sandbox", "delete", sandbox_name]) |
| 132 | + .await |
| 133 | + .map_err(|error| error.to_string())?; |
| 134 | + delete.require_success()?; |
| 135 | + wait_for_absence(runner, sandbox_name, "deleted").await?; |
| 136 | + runner.forget_sandbox(sandbox_name); |
| 137 | + Ok(()) |
| 138 | +} |
| 139 | + |
| 140 | +async fn wait_for_phase( |
| 141 | + runner: &mut OpenShellRunner, |
| 142 | + sandbox_name: &str, |
| 143 | + expected_phase: &str, |
| 144 | + step: &str, |
| 145 | +) -> Result<(), String> { |
| 146 | + let sandbox_name = sandbox_name.to_string(); |
| 147 | + let expected_phase = expected_phase.to_string(); |
| 148 | + let step = step.to_string(); |
| 149 | + let poll_step = step.clone(); |
| 150 | + runner |
| 151 | + .poll_until( |
| 152 | + &poll_step, |
| 153 | + TRANSITION_TIMEOUT, |
| 154 | + TRANSITION_INTERVAL, |
| 155 | + async move |runner| { |
| 156 | + let result = runner |
| 157 | + .step(format!("{step}/get")) |
| 158 | + .description(format!( |
| 159 | + "sandbox '{sandbox_name}' reaches phase {expected_phase}" |
| 160 | + )) |
| 161 | + .with_timeout(COMMAND_TIMEOUT) |
| 162 | + .run(&["sandbox", "get", &sandbox_name, "--output", "json"]) |
| 163 | + .await; |
| 164 | + match result { |
| 165 | + Ok(result) if !result.success() => { |
| 166 | + Poll::Pending(result.failure_diagnostic(&format!( |
| 167 | + "sandbox '{sandbox_name}' can be retrieved" |
| 168 | + ))) |
| 169 | + } |
| 170 | + Ok(result) => match result.json::<SandboxState>() { |
| 171 | + Ok(state) if state.name != sandbox_name => Poll::Failed(format!( |
| 172 | + "sandbox get returned {:?}; expected '{sandbox_name}'", |
| 173 | + state.name |
| 174 | + )), |
| 175 | + Ok(state) if state.phase == expected_phase => Poll::Ready(()), |
| 176 | + Ok(state) => Poll::Pending(format!( |
| 177 | + "sandbox '{sandbox_name}' phase is {:?}; expected {expected_phase:?}", |
| 178 | + state.phase |
| 179 | + )), |
| 180 | + Err(error) => Poll::Failed(error.to_string()), |
| 181 | + }, |
| 182 | + Err(error) => Poll::Pending(error.to_string()), |
| 183 | + } |
| 184 | + }, |
| 185 | + ) |
| 186 | + .await |
| 187 | + .map_err(|error| error.to_string()) |
| 188 | +} |
| 189 | + |
| 190 | +async fn wait_for_absence( |
| 191 | + runner: &mut OpenShellRunner, |
| 192 | + sandbox_name: &str, |
| 193 | + step: &str, |
| 194 | +) -> Result<(), String> { |
| 195 | + let sandbox_name = sandbox_name.to_string(); |
| 196 | + let step = step.to_string(); |
| 197 | + let poll_step = step.clone(); |
| 198 | + runner |
| 199 | + .poll_until( |
| 200 | + &poll_step, |
| 201 | + TRANSITION_TIMEOUT, |
| 202 | + TRANSITION_INTERVAL, |
| 203 | + async move |runner| { |
| 204 | + let result = runner |
| 205 | + .step(format!("{step}/get")) |
| 206 | + .description(format!("sandbox '{sandbox_name}' is no longer retrievable")) |
| 207 | + .with_timeout(COMMAND_TIMEOUT) |
| 208 | + .run(&["sandbox", "get", &sandbox_name, "--output", "json"]) |
| 209 | + .await; |
| 210 | + match result { |
| 211 | + Ok(result) if !result.success() => Poll::Ready(()), |
| 212 | + Ok(_) => { |
| 213 | + Poll::Pending(format!("sandbox '{sandbox_name}' is still retrievable")) |
| 214 | + } |
| 215 | + Err(error) => Poll::Pending(error.to_string()), |
| 216 | + } |
| 217 | + }, |
| 218 | + ) |
| 219 | + .await |
| 220 | + .map_err(|error| error.to_string()) |
| 221 | +} |
0 commit comments