team-ljyzipelo

공개
106줄3.3 KB
1use std::{
2 fs,
3 io::Write,
4 path::PathBuf,
5 process::{Command, Stdio},
6};
7use zipelo_archive_worker::{
8 protocol::*,
9 wire::{Reader, Writer},
10};
11
12fn main() {
13 let args: Vec<_> = std::env::args_os().skip(1).collect();
14 assert_eq!(
15 args.len(),
16 6,
17 "worker archive destination output_limit outcome payload_size"
18 );
19 let source = PathBuf::from(&args[1]);
20 let destination = PathBuf::from(&args[2]);
21 assert_eq!(fs::read_dir(&destination).unwrap().count(), 0);
22 let failed = args[4] == "Failed";
23 assert!(failed || args[4] == "Completed");
24 let output_limit = args[3].to_str().unwrap().parse().unwrap();
25 let payload_size: u64 = args[5].to_str().unwrap().parse().unwrap();
26 let start = Start {
27 action: Action::Extract {
28 archive: NativePath::from_path(&source),
29 destination: NativePath::from_path(&destination),
30 selection: Selection::All,
31 list_id: None,
32 gzip_output_name: None,
33 },
34 limits: Limits {
35 output: output_limit,
36 ..Limits::default()
37 },
38 };
39 let mut request = Vec::new();
40 Writer::new(&mut request, 17)
41 .send(&Message::Start(start))
42 .unwrap();
43 let mut child = Command::new(&args[0])
44 .stdin(Stdio::piped())
45 .stdout(Stdio::piped())
46 .stderr(Stdio::piped())
47 .spawn()
48 .unwrap();
49 child.stdin.take().unwrap().write_all(&request).unwrap();
50 let output = child.wait_with_output().unwrap();
51 assert!(output.status.success(), "{:?}", output.stderr);
52 assert!(output.stderr.is_empty());
53 let mut reader = Reader::new(output.stdout.as_slice());
54 let mut terminals = 0;
55 let mut outputs = 0;
56 while let Some((job, _, message)) = reader.read().unwrap() {
57 assert_eq!(job, 17);
58 assert_eq!(terminals, 0, "Terminal 뒤 이벤트가 있음");
59 match message {
60 Message::Output { .. } => outputs += 1,
61 Message::Terminal(t) => {
62 terminals += 1;
63 assert_eq!(
64 t.outcome,
65 if failed {
66 Outcome::Failed
67 } else {
68 Outcome::Completed
69 }
70 );
71 assert_eq!(
72 t.error,
73 if failed {
74 ErrorCode::Limit
75 } else {
76 ErrorCode::None
77 }
78 );
79 assert_eq!(
80 t.publication,
81 if failed {
82 Publication::None
83 } else {
84 Publication::Complete
85 }
86 );
87 assert_eq!(t.created_count, if failed { 0 } else { 1 });
88 assert_eq!(t.retained_partial_count, 0);
89 assert_eq!(t.cleanup, Cleanup::Complete);
90 }
91 _ => {}
92 }
93 }
94 assert_eq!(terminals, 1);
95 assert_eq!(outputs, if failed { 0 } else { 1 });
96 assert_eq!(
97 fs::read_dir(&destination).unwrap().count(),
98 if failed { 0 } else { 1 }
99 );
100 if !failed {
101 assert_eq!(
102 fs::metadata(destination.join("payload.bin")).unwrap().len(),
103 payload_size
104 );
105 }
106}