| | @@ -0,0 +1,318 @@ |
| 1 | +추가: use super::*; |
| 2 | +추가: use std::os::unix::fs::DirBuilderExt; |
| 3 | +추가: |
| 4 | +추가: const SOURCE: &[u8] = b"unchanged source"; |
| 5 | +추가: const SENTINEL: &[u8] = b"unrelated user file"; |
| 6 | +추가: |
| 7 | +추가: fn prepare(s: &Scratch, operation: &str) -> (Start, PathBuf) { |
| 8 | +추가: fs::write(s.0.join("source"), SOURCE).unwrap(); |
| 9 | +추가: fs::DirBuilder::new() |
| 10 | +추가: .mode(0o700) |
| 11 | +추가: .create(s.0.join("tmp")) |
| 12 | +추가: .unwrap(); |
| 13 | +추가: let (action, output) = match operation { |
| 14 | +추가: "extract" | "extract-nested" => { |
| 15 | +추가: let created = execute(Start { |
| 16 | +추가: action: Action::CreateZip { |
| 17 | +추가: sources: vec![Source { |
| 18 | +추가: path: s.path("source"), |
| 19 | +추가: archive_name: if operation == "extract-nested" { |
| 20 | +추가: "folder/payload" |
| 21 | +추가: } else { |
| 22 | +추가: "payload" |
| 23 | +추가: } |
| 24 | +추가: .into(), |
| 25 | +추가: }], |
| 26 | +추가: output_file: s.path("archive"), |
| 27 | +추가: method: Method::Store, |
| 28 | +추가: encryption: Encryption::None, |
| 29 | +추가: }, |
| 30 | +추가: limits: Limits::default(), |
| 31 | +추가: }); |
| 32 | +추가: assert_eq!(terminal(&created).outcome, Outcome::Completed); |
| 33 | +추가: fs::DirBuilder::new() |
| 34 | +추가: .mode(0o700) |
| 35 | +추가: .create(s.0.join("output")) |
| 36 | +추가: .unwrap(); |
| 37 | +추가: if operation == "extract-nested" { |
| 38 | +추가: fs::DirBuilder::new() |
| 39 | +추가: .mode(0o700) |
| 40 | +추가: .create(s.0.join("output/folder")) |
| 41 | +추가: .unwrap(); |
| 42 | +추가: } |
| 43 | +추가: ( |
| 44 | +추가: Action::Extract { |
| 45 | +추가: archive: s.path("archive"), |
| 46 | +추가: destination: s.path("output"), |
| 47 | +추가: selection: Selection::All, |
| 48 | +추가: list_id: None, |
| 49 | +추가: gzip_output_name: None, |
| 50 | +추가: }, |
| 51 | +추가: s.0.join(if operation == "extract-nested" { |
| 52 | +추가: "output/folder/payload" |
| 53 | +추가: } else { |
| 54 | +추가: "output/payload" |
| 55 | +추가: }), |
| 56 | +추가: ) |
| 57 | +추가: } |
| 58 | +추가: "seven-zip" => ( |
| 59 | +추가: Action::CreateSevenZip { |
| 60 | +추가: sources: vec![Source { |
| 61 | +추가: path: s.path("source"), |
| 62 | +추가: archive_name: "payload".into(), |
| 63 | +추가: }], |
| 64 | +추가: output_file: s.path("result.7z"), |
| 65 | +추가: method: SevenZipMethod::Lzma2, |
| 66 | +추가: solid: false, |
| 67 | +추가: encryption: Encryption::None, |
| 68 | +추가: }, |
| 69 | +추가: s.0.join("result.7z"), |
| 70 | +추가: ), |
| 71 | +추가: "recovery" => { |
| 72 | +추가: fs::write( |
| 73 | +추가: s.0.join("archive"), |
| 74 | +추가: include_bytes!("../zip-recovery-fixtures/broken-local-header.zip"), |
| 75 | +추가: ) |
| 76 | +추가: .unwrap(); |
| 77 | +추가: ( |
| 78 | +추가: Action::RecoverZip { |
| 79 | +추가: archive: s.path("archive"), |
| 80 | +추가: output_file: s.path("recovered.zip"), |
| 81 | +추가: }, |
| 82 | +추가: s.0.join("recovered.zip"), |
| 83 | +추가: ) |
| 84 | +추가: } |
| 85 | +추가: _ => unreachable!(), |
| 86 | +추가: }; |
| 87 | +추가: ( |
| 88 | +추가: Start { |
| 89 | +추가: action, |
| 90 | +추가: limits: Limits::default(), |
| 91 | +추가: }, |
| 92 | +추가: output, |
| 93 | +추가: ) |
| 94 | +추가: } |
| 95 | +추가: |
| 96 | +추가: fn run(s: &Scratch, start: Start, fault: &str) -> Vec<Message> { |
| 97 | +추가: let library = s.0.join("fault.so"); |
| 98 | +추가: assert!( |
| 99 | +추가: Command::new("cc") |
| 100 | +추가: .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) |
| 101 | +추가: .arg( |
| 102 | +추가: PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 103 | +추가: .join("../../tests/rust-archive-worker/io_fault.c"), |
| 104 | +추가: ) |
| 105 | +추가: .args(["-ldl", "-o"]) |
| 106 | +추가: .arg(&library) |
| 107 | +추가: .status() |
| 108 | +추가: .unwrap() |
| 109 | +추가: .success() |
| 110 | +추가: ); |
| 111 | +추가: let mut child = s |
| 112 | +추가: .worker() |
| 113 | +추가: .env("TMPDIR", s.0.join("tmp")) |
| 114 | +추가: .env("LD_PRELOAD", library) |
| 115 | +추가: .env("ZIPELO_TEST_FAULT", fault) |
| 116 | +추가: .env("ZIPELO_TEST_MARKER", s.0.join("marker")) |
| 117 | +추가: .stdin(Stdio::piped()) |
| 118 | +추가: .stdout(Stdio::piped()) |
| 119 | +추가: .stderr(Stdio::piped()) |
| 120 | +추가: .spawn() |
| 121 | +추가: .unwrap(); |
| 122 | +추가: Writer::new(child.stdin.take().unwrap(), 17) |
| 123 | +추가: .send(&Message::Start(start)) |
| 124 | +추가: .unwrap(); |
| 125 | +추가: let output = child.wait_with_output().unwrap(); |
| 126 | +추가: assert!(output.status.success()); |
| 127 | +추가: assert!(output.stderr.is_empty(), "{:?}", output.stderr); |
| 128 | +추가: let mut reader = Reader::new(output.stdout.as_slice()); |
| 129 | +추가: let mut events = Vec::new(); |
| 130 | +추가: while let Some((_, _, event)) = reader.read().unwrap() { |
| 131 | +추가: events.push(event); |
| 132 | +추가: } |
| 133 | +추가: assert_eq!( |
| 134 | +추가: events |
| 135 | +추가: .iter() |
| 136 | +추가: .filter(|e| matches!(e, Message::Terminal(_))) |
| 137 | +추가: .count(), |
| 138 | +추가: 1 |
| 139 | +추가: ); |
| 140 | +추가: assert_eq!(fs::read_dir(s.0.join("tmp")).unwrap().count(), 0); |
| 141 | +추가: events |
| 142 | +추가: } |
| 143 | +추가: |
| 144 | +추가: #[test] |
| 145 | +추가: fn storage_failure_matrix_preserves_outputs_and_reports_remaining_files() { |
| 146 | +추가: for operation in ["extract", "extract-nested", "seven-zip", "recovery"] { |
| 147 | +추가: for fault in [ |
| 148 | +추가: "none", |
| 149 | +추가: "temp-full", |
| 150 | +추가: "create-full", |
| 151 | +추가: "full", |
| 152 | +추가: "sync-full", |
| 153 | +추가: "sync-io", |
| 154 | +추가: "dir-sync-full", |
| 155 | +추가: "dir-sync-io", |
| 156 | +추가: "cleanup-fail", |
| 157 | +추가: "full-cleanup-fail", |
| 158 | +추가: "sync-cleanup-fail", |
| 159 | +추가: ] { |
| 160 | +추가: let s = Scratch::new(); |
| 161 | +추가: let (request, output) = prepare(&s, operation); |
| 162 | +추가: let parent = output.parent().unwrap(); |
| 163 | +추가: let root = if operation.starts_with("extract") { |
| 164 | +추가: s.0.join("output") |
| 165 | +추가: } else { |
| 166 | +추가: s.0.clone() |
| 167 | +추가: }; |
| 168 | +추가: let unrelated = parent.join(".zipelo-unrelated.tmp"); |
| 169 | +추가: fs::write(&unrelated, SENTINEL).unwrap(); |
| 170 | +추가: let archive = fs::read(s.0.join("archive")).ok(); |
| 171 | +추가: let events = run(&s, request, fault); |
| 172 | +추가: let end = terminal(&events); |
| 173 | +추가: let context = format!("{operation}/{fault}: {events:?}"); |
| 174 | +추가: let published = matches!( |
| 175 | +추가: fault, |
| 176 | +추가: "none" | "cleanup-fail" | "dir-sync-full" | "dir-sync-io" |
| 177 | +추가: ); |
| 178 | +추가: assert_eq!(output.exists(), published, "{context}"); |
| 179 | +추가: assert_eq!(end.created_count, u64::from(published), "{context}"); |
| 180 | +추가: assert_eq!( |
| 181 | +추가: end.outcome, |
| 182 | +추가: if fault == "none" { |
| 183 | +추가: Outcome::Completed |
| 184 | +추가: } else { |
| 185 | +추가: Outcome::Failed |
| 186 | +추가: }, |
| 187 | +추가: "{context}" |
| 188 | +추가: ); |
| 189 | +추가: let expected_error = if fault == "none" { |
| 190 | +추가: ErrorCode::None |
| 191 | +추가: } else if matches!( |
| 192 | +추가: fault, |
| 193 | +추가: "sync-io" | "dir-sync-io" | "cleanup-fail" | "sync-cleanup-fail" |
| 194 | +추가: ) { |
| 195 | +추가: ErrorCode::Io |
| 196 | +추가: } else { |
| 197 | +추가: ErrorCode::DiskFull |
| 198 | +추가: }; |
| 199 | +추가: assert_eq!(end.error, expected_error, "{context}"); |
| 200 | +추가: assert_eq!( |
| 201 | +추가: end.publication, |
| 202 | +추가: if fault == "none" { |
| 203 | +추가: Publication::Complete |
| 204 | +추가: } else if published { |
| 205 | +추가: Publication::Partial |
| 206 | +추가: } else { |
| 207 | +추가: Publication::None |
| 208 | +추가: }, |
| 209 | +추가: "{context}" |
| 210 | +추가: ); |
| 211 | +추가: assert_eq!( |
| 212 | +추가: end.durability, |
| 213 | +추가: if fault.starts_with("dir-sync") { |
| 214 | +추가: Durability::Failed |
| 215 | +추가: } else if published { |
| 216 | +추가: Durability::Synced |
| 217 | +추가: } else { |
| 218 | +추가: Durability::NotApplicable |
| 219 | +추가: }, |
| 220 | +추가: "{context}" |
| 221 | +추가: ); |
| 222 | +추가: let retained: Vec<_> = events |
| 223 | +추가: .iter() |
| 224 | +추가: .filter_map(|e| match e { |
| 225 | +추가: Message::Output { |
| 226 | +추가: relative_name, |
| 227 | +추가: disposition: Disposition::PartialRetained, |
| 228 | +추가: .. |
| 229 | +추가: } => Some(relative_name), |
| 230 | +추가: _ => None, |
| 231 | +추가: }) |
| 232 | +추가: .collect(); |
| 233 | +추가: let cleanup_failed = fault.ends_with("cleanup-fail"); |
| 234 | +추가: assert_eq!( |
| 235 | +추가: end.cleanup, |
| 236 | +추가: if cleanup_failed { |
| 237 | +추가: Cleanup::Failed |
| 238 | +추가: } else { |
| 239 | +추가: Cleanup::Complete |
| 240 | +추가: }, |
| 241 | +추가: "{context}" |
| 242 | +추가: ); |
| 243 | +추가: assert_eq!( |
| 244 | +추가: end.retained_partial_count, |
| 245 | +추가: u64::from(cleanup_failed), |
| 246 | +추가: "{context}" |
| 247 | +추가: ); |
| 248 | +추가: assert_eq!(retained.len(), usize::from(cleanup_failed), "{context}"); |
| 249 | +추가: for name in retained { |
| 250 | +추가: let path = root.join(name); |
| 251 | +추가: assert!(path.is_file(), "{context}: {path:?}"); |
| 252 | +추가: if published { |
| 253 | +추가: assert_eq!(fs::read(path).unwrap(), fs::read(&output).unwrap()); |
| 254 | +추가: } |
| 255 | +추가: } |
| 256 | +추가: assert_eq!(fs::read(&unrelated).unwrap(), SENTINEL, "{context}"); |
| 257 | +추가: assert_eq!(fs::read(s.0.join("source")).unwrap(), SOURCE, "{context}"); |
| 258 | +추가: assert_eq!(fs::read(s.0.join("archive")).ok(), archive, "{context}"); |
| 259 | +추가: assert_eq!( |
| 260 | +추가: fs::read_dir(parent) |
| 261 | +추가: .unwrap() |
| 262 | +추가: .filter(|e| { |
| 263 | +추가: let name = e.as_ref().unwrap().file_name(); |
| 264 | +추가: let name = name.to_string_lossy(); |
| 265 | +추가: name.starts_with(".zipelo-") && name != ".zipelo-unrelated.tmp" |
| 266 | +추가: }) |
| 267 | +추가: .count(), |
| 268 | +추가: usize::from(cleanup_failed), |
| 269 | +추가: "{context}" |
| 270 | +추가: ); |
| 271 | +추가: if published && operation.starts_with("extract") { |
| 272 | +추가: assert_eq!(fs::read(output).unwrap(), SOURCE, "{context}"); |
| 273 | +추가: } |
| 274 | +추가: } |
| 275 | +추가: } |
| 276 | +추가: } |
| 277 | +추가: |
| 278 | +추가: #[test] |
| 279 | +추가: fn cleanup_does_not_delete_a_replacement_at_the_temporary_name() { |
| 280 | +추가: for operation in ["extract", "extract-nested", "seven-zip", "recovery"] { |
| 281 | +추가: for fault in ["swap-sync", "swap-publish"] { |
| 282 | +추가: let s = Scratch::new(); |
| 283 | +추가: let (request, output) = prepare(&s, operation); |
| 284 | +추가: let events = run(&s, request, fault); |
| 285 | +추가: let end = terminal(&events); |
| 286 | +추가: assert_eq!(end.outcome, Outcome::Failed, "{operation}: {events:?}"); |
| 287 | +추가: assert_eq!( |
| 288 | +추가: end.error, |
| 289 | +추가: if fault == "swap-publish" { |
| 290 | +추가: ErrorCode::Conflict |
| 291 | +추가: } else { |
| 292 | +추가: ErrorCode::Io |
| 293 | +추가: } |
| 294 | +추가: ); |
| 295 | +추가: assert!(!output.exists()); |
| 296 | +추가: let replacements: Vec<_> = fs::read_dir(output.parent().unwrap()) |
| 297 | +추가: .unwrap() |
| 298 | +추가: .map(|e| e.unwrap().path()) |
| 299 | +추가: .filter(|p| p.file_name().unwrap().to_string_lossy().ends_with(".tmp")) |
| 300 | +추가: .collect(); |
| 301 | +추가: assert_eq!(replacements.len(), 1, "{operation}: {events:?}"); |
| 302 | +추가: assert_eq!( |
| 303 | +추가: fs::read(&replacements[0]).unwrap(), |
| 304 | +추가: b"unrelated replacement" |
| 305 | +추가: ); |
| 306 | +추가: assert_eq!(end.cleanup, Cleanup::Unconfirmed); |
| 307 | +추가: assert_eq!(end.retained_partial_count, 0); |
| 308 | +추가: assert!(!events.iter().any(|e| matches!( |
| 309 | +추가: e, |
| 310 | +추가: Message::Output { |
| 311 | +추가: disposition: Disposition::PartialRetained, |
| 312 | +추가: .. |
| 313 | +추가: } |
| 314 | +추가: ))); |
| 315 | +추가: assert_eq!(fs::read(s.0.join("source")).unwrap(), SOURCE); |
| 316 | +추가: } |
| 317 | +추가: } |
| 318 | +추가: } |