David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 1 | //! Parallel testing. |
| 2 | //! |
| 3 | //! mcuboot simulator is strictly single threaded, as there is a lock around running the C startup |
| 4 | //! code, because it contains numerous global variables. |
| 5 | //! |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 6 | //! To help speed up testing, the Workflow configuration defines all of the configurations that can |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 7 | //! be run in parallel. Fortunately, cargo works well this way, and these can be run by simply |
| 8 | //! using subprocess for each particular thread. |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 9 | //! |
| 10 | //! For now, we assume all of the features are listed under |
| 11 | //! jobs->environment->strategy->matric->features |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 12 | |
| 13 | use chrono::Local; |
David Brown | 8798337 | 2024-04-11 08:58:48 -0600 | [diff] [blame] | 14 | use clap::{Parser, Subcommand}; |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 15 | use log::{debug, error, warn}; |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 16 | use std::{ |
| 17 | collections::HashSet, |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 18 | env, |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 19 | fs::{self, OpenOptions}, |
| 20 | io::{ErrorKind, stdout, Write}, |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 21 | process::Command, |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 22 | result, |
| 23 | sync::{ |
| 24 | Arc, |
| 25 | Mutex, |
| 26 | }, |
| 27 | thread, |
| 28 | time::Duration, |
| 29 | }; |
| 30 | use std_semaphore::Semaphore; |
| 31 | use yaml_rust::{ |
| 32 | Yaml, |
| 33 | YamlLoader, |
| 34 | }; |
| 35 | |
| 36 | type Result<T> = result::Result<T, failure::Error>; |
| 37 | |
| 38 | fn main() -> Result<()> { |
| 39 | env_logger::init(); |
| 40 | |
David Brown | 8798337 | 2024-04-11 08:58:48 -0600 | [diff] [blame] | 41 | let args = Cli::parse(); |
| 42 | |
| 43 | match args.command { |
| 44 | Commands::Run => (), |
| 45 | } |
| 46 | |
David Brown | c32ad20 | 2024-04-11 09:31:26 -0600 | [diff] [blame^] | 47 | let workflow_text = fs::read_to_string(&args.workflow)?; |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 48 | let workflow = YamlLoader::load_from_str(&workflow_text)?; |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 49 | |
| 50 | let ncpus = num_cpus::get(); |
| 51 | let limiter = Arc::new(Semaphore::new(ncpus as isize)); |
| 52 | |
David Brown | 91de33d | 2021-03-05 15:43:44 -0700 | [diff] [blame] | 53 | let matrix = Matrix::from_yaml(&workflow); |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 54 | |
| 55 | let mut children = vec![]; |
| 56 | let state = State::new(matrix.envs.len()); |
| 57 | let st2 = state.clone(); |
| 58 | let _status = thread::spawn(move || { |
| 59 | loop { |
| 60 | thread::sleep(Duration::new(15, 0)); |
| 61 | st2.lock().unwrap().status(); |
| 62 | } |
| 63 | }); |
| 64 | for env in matrix.envs { |
| 65 | let state = state.clone(); |
| 66 | let limiter = limiter.clone(); |
| 67 | |
| 68 | let child = thread::spawn(move || { |
| 69 | let _run = limiter.access(); |
| 70 | state.lock().unwrap().start(&env); |
| 71 | let out = env.run(); |
| 72 | state.lock().unwrap().done(&env, out); |
| 73 | }); |
| 74 | children.push(child); |
| 75 | } |
| 76 | |
| 77 | for child in children { |
| 78 | child.join().unwrap(); |
| 79 | } |
| 80 | |
David Brown | 91de33d | 2021-03-05 15:43:44 -0700 | [diff] [blame] | 81 | println!(); |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 82 | |
| 83 | Ok(()) |
| 84 | } |
| 85 | |
David Brown | 8798337 | 2024-04-11 08:58:48 -0600 | [diff] [blame] | 86 | /// The main Cli. |
| 87 | #[derive(Debug, Parser)] |
| 88 | #[command(name = "ptest")] |
| 89 | #[command(about = "Run MCUboot CI tests stand alone")] |
| 90 | struct Cli { |
David Brown | c32ad20 | 2024-04-11 09:31:26 -0600 | [diff] [blame^] | 91 | /// The workflow file to use. |
| 92 | #[arg(short, long, default_value = "../.github/workflows/sim.yaml")] |
| 93 | workflow: String, |
| 94 | |
David Brown | 8798337 | 2024-04-11 08:58:48 -0600 | [diff] [blame] | 95 | #[command(subcommand)] |
| 96 | command: Commands, |
| 97 | } |
| 98 | |
| 99 | #[derive(Debug, Subcommand)] |
| 100 | enum Commands { |
| 101 | /// Runs the tests. |
| 102 | Run, |
| 103 | } |
| 104 | |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 105 | /// State, for printing status. |
| 106 | struct State { |
| 107 | running: HashSet<String>, |
| 108 | done: HashSet<String>, |
| 109 | total: usize, |
| 110 | } |
| 111 | |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 112 | /// Result of a test run. |
| 113 | struct TestResult { |
| 114 | /// Was this run successful. |
| 115 | success: bool, |
| 116 | |
| 117 | /// The captured output. |
| 118 | output: Vec<u8>, |
| 119 | } |
| 120 | |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 121 | impl State { |
| 122 | fn new(total: usize) -> Arc<Mutex<State>> { |
| 123 | Arc::new(Mutex::new(State { |
| 124 | running: HashSet::new(), |
| 125 | done: HashSet::new(), |
David Brown | 91de33d | 2021-03-05 15:43:44 -0700 | [diff] [blame] | 126 | total, |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 127 | })) |
| 128 | } |
| 129 | |
| 130 | fn start(&mut self, fs: &FeatureSet) { |
| 131 | let key = fs.textual(); |
| 132 | if self.running.contains(&key) || self.done.contains(&key) { |
| 133 | warn!("Duplicate: {:?}", key); |
| 134 | } |
| 135 | debug!("Starting: {} ({} running)", key, self.running.len() + 1); |
| 136 | self.running.insert(key); |
| 137 | self.status(); |
| 138 | } |
| 139 | |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 140 | fn done(&mut self, fs: &FeatureSet, output: Result<TestResult>) { |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 141 | let key = fs.textual(); |
| 142 | self.running.remove(&key); |
| 143 | self.done.insert(key.clone()); |
| 144 | match output { |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 145 | Ok(output) => { |
| 146 | if !output.success || log_all() { |
| 147 | // Write the output into a file. |
| 148 | let mut count = 1; |
| 149 | let (mut fd, logname) = loop { |
| 150 | let base = if output.success { "success" } else { "failure" }; |
| 151 | let name = format!("./{}-{:04}.log", base, count); |
| 152 | count += 1; |
| 153 | match OpenOptions::new() |
| 154 | .create_new(true) |
| 155 | .write(true) |
| 156 | .open(&name) |
| 157 | { |
| 158 | Ok(file) => break (file, name), |
| 159 | Err(ref err) if err.kind() == ErrorKind::AlreadyExists => continue, |
| 160 | Err(err) => { |
| 161 | error!("Unable to write log file to current directory: {:?}", err); |
| 162 | return; |
| 163 | } |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 164 | } |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 165 | }; |
| 166 | fd.write_all(&output.output).unwrap(); |
| 167 | if !output.success { |
| 168 | error!("Failure {} log:{:?} ({} running)", key, logname, |
| 169 | self.running.len()); |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 170 | } |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 171 | } |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 172 | } |
| 173 | Err(err) => { |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 174 | error!("Unable to run test {:?} ({:?}", key, err); |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | self.status(); |
| 178 | } |
| 179 | |
| 180 | fn status(&self) { |
| 181 | let running = self.running.len(); |
| 182 | let done = self.done.len(); |
| 183 | print!(" {} running ({}/{}/{} done)\r", running, done, running + done, self.total); |
| 184 | stdout().flush().unwrap(); |
| 185 | } |
| 186 | } |
| 187 | |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 188 | /// The extracted configurations from the workflow config |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 189 | #[derive(Debug)] |
| 190 | struct Matrix { |
| 191 | envs: Vec<FeatureSet>, |
| 192 | } |
| 193 | |
| 194 | #[derive(Debug, Eq, Hash, PartialEq)] |
| 195 | struct FeatureSet { |
| 196 | // The environment variable to set. |
| 197 | env: String, |
| 198 | // The successive values to set it to. |
| 199 | values: Vec<String>, |
| 200 | } |
| 201 | |
| 202 | impl Matrix { |
David Brown | 91de33d | 2021-03-05 15:43:44 -0700 | [diff] [blame] | 203 | fn from_yaml(yaml: &[Yaml]) -> Matrix { |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 204 | let mut envs = vec![]; |
| 205 | |
| 206 | let mut all_tests = HashSet::new(); |
| 207 | |
| 208 | for y in yaml { |
| 209 | let m = match lookup_matrix(y) { |
| 210 | Some (m) => m, |
| 211 | None => continue, |
| 212 | }; |
| 213 | for elt in m { |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 214 | let elt = match elt.as_str() { |
| 215 | None => { |
| 216 | warn!("Unexpected yaml: {:?}", elt); |
| 217 | continue; |
| 218 | } |
| 219 | Some(e) => e, |
| 220 | }; |
David Brown | 91de33d | 2021-03-05 15:43:44 -0700 | [diff] [blame] | 221 | let fset = FeatureSet::decode(elt); |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 222 | |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 223 | if false { |
| 224 | // Respect the groupings in the `.workflow.yml` file. |
| 225 | envs.push(fset); |
| 226 | } else { |
| 227 | // Break each test up so we can run more in |
| 228 | // parallel. |
| 229 | let env = fset.env.clone(); |
| 230 | for val in fset.values { |
| 231 | if !all_tests.contains(&val) { |
| 232 | all_tests.insert(val.clone()); |
| 233 | envs.push(FeatureSet { |
| 234 | env: env.clone(), |
| 235 | values: vec![val], |
| 236 | }); |
| 237 | } else { |
| 238 | warn!("Duplicate: {:?}: {:?}", env, val); |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
David Brown | 91de33d | 2021-03-05 15:43:44 -0700 | [diff] [blame] | 245 | Matrix { |
| 246 | envs, |
| 247 | } |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | impl FeatureSet { |
David Brown | 91de33d | 2021-03-05 15:43:44 -0700 | [diff] [blame] | 252 | fn decode(text: &str) -> FeatureSet { |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 253 | // The github workflow is just a space separated set of values. |
| 254 | let values: Vec<_> = text |
| 255 | .split(',') |
| 256 | .map(|s| s.to_string()) |
| 257 | .collect(); |
David Brown | 91de33d | 2021-03-05 15:43:44 -0700 | [diff] [blame] | 258 | FeatureSet { |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 259 | env: "MULTI_FEATURES".to_string(), |
David Brown | 91de33d | 2021-03-05 15:43:44 -0700 | [diff] [blame] | 260 | values, |
| 261 | } |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /// Run a test for this given feature set. Output is captured and will be returned if there is |
| 265 | /// an error. Each will be run successively, and the first failure will be returned. |
| 266 | /// Otherwise, it returns None, which means everything worked. |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 267 | fn run(&self) -> Result<TestResult> { |
| 268 | let mut output = vec![]; |
| 269 | let mut success = true; |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 270 | for v in &self.values { |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 271 | let cmdout = Command::new("bash") |
David Brown | b899f79 | 2019-03-14 15:21:06 -0600 | [diff] [blame] | 272 | .arg("./ci/sim_run.sh") |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 273 | .current_dir("..") |
| 274 | .env(&self.env, v) |
| 275 | .output()?; |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 276 | // Grab the output for logging, etc. |
| 277 | writeln!(&mut output, "Test {} {}", |
| 278 | if cmdout.status.success() { "success" } else { "FAILURE" }, |
| 279 | self.textual())?; |
| 280 | writeln!(&mut output, "time: {}", Local::now().to_rfc3339())?; |
| 281 | writeln!(&mut output, "----------------------------------------")?; |
| 282 | writeln!(&mut output, "stdout:")?; |
| 283 | output.extend(&cmdout.stdout); |
| 284 | writeln!(&mut output, "----------------------------------------")?; |
| 285 | writeln!(&mut output, "stderr:")?; |
| 286 | output.extend(&cmdout.stderr); |
| 287 | if !cmdout.status.success() { |
| 288 | success = false; |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 291 | Ok(TestResult { success, output }) |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /// Convert this feature set into a textual representation |
| 295 | fn textual(&self) -> String { |
| 296 | use std::fmt::Write; |
| 297 | |
| 298 | let mut buf = String::new(); |
| 299 | |
| 300 | write!(&mut buf, "{}:", self.env).unwrap(); |
| 301 | for v in &self.values { |
| 302 | write!(&mut buf, " {}", v).unwrap(); |
| 303 | } |
| 304 | |
| 305 | buf |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | fn lookup_matrix(y: &Yaml) -> Option<&Vec<Yaml>> { |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 310 | let jobs = Yaml::String("jobs".to_string()); |
| 311 | let environment = Yaml::String("environment".to_string()); |
| 312 | let strategy = Yaml::String("strategy".to_string()); |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 313 | let matrix = Yaml::String("matrix".to_string()); |
David Brown | 48a4ec3 | 2021-01-04 17:02:27 -0700 | [diff] [blame] | 314 | let features = Yaml::String("features".to_string()); |
| 315 | y |
| 316 | .as_hash()?.get(&jobs)? |
| 317 | .as_hash()?.get(&environment)? |
| 318 | .as_hash()?.get(&strategy)? |
| 319 | .as_hash()?.get(&matrix)? |
| 320 | .as_hash()?.get(&features)? |
| 321 | .as_vec() |
David Brown | 2bc2685 | 2019-01-14 21:54:04 +0000 | [diff] [blame] | 322 | } |
David Brown | ddd390a | 2021-01-12 12:04:56 -0700 | [diff] [blame] | 323 | |
| 324 | /// Query if we should be logging all tests and not only failures. |
| 325 | fn log_all() -> bool { |
| 326 | env::var("PTEST_LOG_ALL").is_ok() |
| 327 | } |