team-ljyzipelo

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