1*c2e18aaaSAndroid Build Coastguard Worker mod common;
2*c2e18aaaSAndroid Build Coastguard Worker use adevice::adevice::Profiler;
3*c2e18aaaSAndroid Build Coastguard Worker use adevice::fingerprint::{self, FileMetadata};
4*c2e18aaaSAndroid Build Coastguard Worker use adevice::{cli, commands};
5*c2e18aaaSAndroid Build Coastguard Worker use anyhow::{Context, Result};
6*c2e18aaaSAndroid Build Coastguard Worker use clap::Parser;
7*c2e18aaaSAndroid Build Coastguard Worker use common::fakes::{FakeDevice, FakeHost, FakeMetricSender};
8*c2e18aaaSAndroid Build Coastguard Worker use std::collections::HashMap;
9*c2e18aaaSAndroid Build Coastguard Worker use std::path::PathBuf;
10*c2e18aaaSAndroid Build Coastguard Worker
11*c2e18aaaSAndroid Build Coastguard Worker const NO_LOG_FILE: Option<std::fs::File> = None;
12*c2e18aaaSAndroid Build Coastguard Worker
13*c2e18aaaSAndroid Build Coastguard Worker // Just placeholder for now to show we can call adevice.
14*c2e18aaaSAndroid Build Coastguard Worker #[test]
adevice_status() -> Result<()>15*c2e18aaaSAndroid Build Coastguard Worker fn adevice_status() -> Result<()> {
16*c2e18aaaSAndroid Build Coastguard Worker std::env::remove_var("HOME"); // Use default config, don't write one.
17*c2e18aaaSAndroid Build Coastguard Worker let device_fs = HashMap::from([
18*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/fakefs_default_file"), file_metadata("digest1")),
19*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system"), dir_metadata()),
20*c2e18aaaSAndroid Build Coastguard Worker ]);
21*c2e18aaaSAndroid Build Coastguard Worker
22*c2e18aaaSAndroid Build Coastguard Worker let host_fs = HashMap::from([
23*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/fakefs_default_file"), file_metadata("digest1")),
24*c2e18aaaSAndroid Build Coastguard Worker // NOTE: extra file on host
25*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/fakefs_new_file_file"), file_metadata("digest1")),
26*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system"), dir_metadata()),
27*c2e18aaaSAndroid Build Coastguard Worker ]);
28*c2e18aaaSAndroid Build Coastguard Worker let host_tracked_files =
29*c2e18aaaSAndroid Build Coastguard Worker vec!["system/fakefs_default_file".to_string(), "system/fakefs_new_file".to_string()];
30*c2e18aaaSAndroid Build Coastguard Worker
31*c2e18aaaSAndroid Build Coastguard Worker let fake_host = FakeHost::new(&host_fs, &host_tracked_files);
32*c2e18aaaSAndroid Build Coastguard Worker let fake_device = FakeDevice::new(&device_fs);
33*c2e18aaaSAndroid Build Coastguard Worker let mut stdout = Vec::new();
34*c2e18aaaSAndroid Build Coastguard Worker // TODO(rbraunstein): Fix argv[0]
35*c2e18aaaSAndroid Build Coastguard Worker let cli = cli::Cli::parse_from(["", "--product_out", "unused", "status"]);
36*c2e18aaaSAndroid Build Coastguard Worker
37*c2e18aaaSAndroid Build Coastguard Worker adevice::adevice::adevice(
38*c2e18aaaSAndroid Build Coastguard Worker &fake_host,
39*c2e18aaaSAndroid Build Coastguard Worker &fake_device,
40*c2e18aaaSAndroid Build Coastguard Worker &cli,
41*c2e18aaaSAndroid Build Coastguard Worker &mut stdout,
42*c2e18aaaSAndroid Build Coastguard Worker &mut FakeMetricSender::new(),
43*c2e18aaaSAndroid Build Coastguard Worker NO_LOG_FILE,
44*c2e18aaaSAndroid Build Coastguard Worker &mut Profiler::default(),
45*c2e18aaaSAndroid Build Coastguard Worker )?;
46*c2e18aaaSAndroid Build Coastguard Worker let stdout_str = String::from_utf8(stdout).unwrap();
47*c2e18aaaSAndroid Build Coastguard Worker
48*c2e18aaaSAndroid Build Coastguard Worker // TODO(rbraunstein): Check the status group it is in: (Ready to push)
49*c2e18aaaSAndroid Build Coastguard Worker assert!(
50*c2e18aaaSAndroid Build Coastguard Worker stdout_str.contains(&"system/fakefs_new_file".to_string()),
51*c2e18aaaSAndroid Build Coastguard Worker "\n\nACTUAL:\n {}",
52*c2e18aaaSAndroid Build Coastguard Worker stdout_str
53*c2e18aaaSAndroid Build Coastguard Worker );
54*c2e18aaaSAndroid Build Coastguard Worker Ok(())
55*c2e18aaaSAndroid Build Coastguard Worker }
56*c2e18aaaSAndroid Build Coastguard Worker
57*c2e18aaaSAndroid Build Coastguard Worker #[test]
lost_and_found_should_not_be_cleaned() -> Result<()>58*c2e18aaaSAndroid Build Coastguard Worker fn lost_and_found_should_not_be_cleaned() -> Result<()> {
59*c2e18aaaSAndroid Build Coastguard Worker std::env::remove_var("HOME"); // Use default config, don't write one.
60*c2e18aaaSAndroid Build Coastguard Worker let device_files = HashMap::from([
61*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system_ext/lost+found"), dir_metadata()),
62*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/some_file"), file_metadata("m1")),
63*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/lost+found"), dir_metadata()),
64*c2e18aaaSAndroid Build Coastguard Worker ]);
65*c2e18aaaSAndroid Build Coastguard Worker
66*c2e18aaaSAndroid Build Coastguard Worker // Ensure the partitions exist.
67*c2e18aaaSAndroid Build Coastguard Worker let ninja_deps = commands::split_string("system/file1 system_ext/file2");
68*c2e18aaaSAndroid Build Coastguard Worker let fake_host = FakeHost::new(&HashMap::new(), &ninja_deps);
69*c2e18aaaSAndroid Build Coastguard Worker let fake_device = FakeDevice::new(&device_files);
70*c2e18aaaSAndroid Build Coastguard Worker // TODO(rbraunstein): Fix argv[0]
71*c2e18aaaSAndroid Build Coastguard Worker let cli = cli::Cli::parse_from([
72*c2e18aaaSAndroid Build Coastguard Worker "",
73*c2e18aaaSAndroid Build Coastguard Worker "--product_out",
74*c2e18aaaSAndroid Build Coastguard Worker "unused",
75*c2e18aaaSAndroid Build Coastguard Worker "clean",
76*c2e18aaaSAndroid Build Coastguard Worker "--force",
77*c2e18aaaSAndroid Build Coastguard Worker "--restart",
78*c2e18aaaSAndroid Build Coastguard Worker "none",
79*c2e18aaaSAndroid Build Coastguard Worker ]);
80*c2e18aaaSAndroid Build Coastguard Worker
81*c2e18aaaSAndroid Build Coastguard Worker // Expect some_file, but not lost+found.
82*c2e18aaaSAndroid Build Coastguard Worker {
83*c2e18aaaSAndroid Build Coastguard Worker let mut stdout = Vec::new();
84*c2e18aaaSAndroid Build Coastguard Worker let mut metrics = FakeMetricSender::new();
85*c2e18aaaSAndroid Build Coastguard Worker adevice::adevice::adevice(
86*c2e18aaaSAndroid Build Coastguard Worker &fake_host,
87*c2e18aaaSAndroid Build Coastguard Worker &fake_device,
88*c2e18aaaSAndroid Build Coastguard Worker &cli,
89*c2e18aaaSAndroid Build Coastguard Worker &mut stdout,
90*c2e18aaaSAndroid Build Coastguard Worker &mut metrics,
91*c2e18aaaSAndroid Build Coastguard Worker NO_LOG_FILE,
92*c2e18aaaSAndroid Build Coastguard Worker &mut Profiler::default(),
93*c2e18aaaSAndroid Build Coastguard Worker )
94*c2e18aaaSAndroid Build Coastguard Worker .context("Running adevice clean")?;
95*c2e18aaaSAndroid Build Coastguard Worker let stdout_str = String::from_utf8(stdout).unwrap();
96*c2e18aaaSAndroid Build Coastguard Worker assert!(stdout_str.contains("system/some_file"), "\n\nACTUAL:\n {}", stdout_str);
97*c2e18aaaSAndroid Build Coastguard Worker assert!(!stdout_str.contains("lost+found"), "\n\nACTUAL:\n {}", stdout_str);
98*c2e18aaaSAndroid Build Coastguard Worker
99*c2e18aaaSAndroid Build Coastguard Worker assert!(fake_device.removes().contains(&PathBuf::from("system/some_file")));
100*c2e18aaaSAndroid Build Coastguard Worker assert!(!fake_device.removes().contains(&PathBuf::from("system/lost+found")));
101*c2e18aaaSAndroid Build Coastguard Worker }
102*c2e18aaaSAndroid Build Coastguard Worker
103*c2e18aaaSAndroid Build Coastguard Worker Ok(())
104*c2e18aaaSAndroid Build Coastguard Worker }
105*c2e18aaaSAndroid Build Coastguard Worker
106*c2e18aaaSAndroid Build Coastguard Worker #[test]
update_should_clean_stale_files() -> Result<()>107*c2e18aaaSAndroid Build Coastguard Worker fn update_should_clean_stale_files() -> Result<()> {
108*c2e18aaaSAndroid Build Coastguard Worker std::env::remove_var("HOME");
109*c2e18aaaSAndroid Build Coastguard Worker let device_files = HashMap::from([(PathBuf::from("system/STALE_FILE"), file_metadata("m1"))]);
110*c2e18aaaSAndroid Build Coastguard Worker
111*c2e18aaaSAndroid Build Coastguard Worker // Ensure the partitions exist.
112*c2e18aaaSAndroid Build Coastguard Worker let ninja_deps = commands::split_string("system/other_file");
113*c2e18aaaSAndroid Build Coastguard Worker let fake_host = FakeHost::new(&HashMap::new(), &ninja_deps);
114*c2e18aaaSAndroid Build Coastguard Worker let fake_device = FakeDevice::new(&device_files);
115*c2e18aaaSAndroid Build Coastguard Worker let cli = cli::Cli::parse_from(["", "--product_out", "unused", "update", "--restart", "none"]);
116*c2e18aaaSAndroid Build Coastguard Worker
117*c2e18aaaSAndroid Build Coastguard Worker // Expect some_file
118*c2e18aaaSAndroid Build Coastguard Worker {
119*c2e18aaaSAndroid Build Coastguard Worker let mut stdout = Vec::new();
120*c2e18aaaSAndroid Build Coastguard Worker let mut metrics = FakeMetricSender::new();
121*c2e18aaaSAndroid Build Coastguard Worker adevice::adevice::adevice(
122*c2e18aaaSAndroid Build Coastguard Worker &fake_host,
123*c2e18aaaSAndroid Build Coastguard Worker &fake_device,
124*c2e18aaaSAndroid Build Coastguard Worker &cli,
125*c2e18aaaSAndroid Build Coastguard Worker &mut stdout,
126*c2e18aaaSAndroid Build Coastguard Worker &mut metrics,
127*c2e18aaaSAndroid Build Coastguard Worker NO_LOG_FILE,
128*c2e18aaaSAndroid Build Coastguard Worker &mut Profiler::default(),
129*c2e18aaaSAndroid Build Coastguard Worker )
130*c2e18aaaSAndroid Build Coastguard Worker .context("Running adevice clean")?;
131*c2e18aaaSAndroid Build Coastguard Worker
132*c2e18aaaSAndroid Build Coastguard Worker assert!(fake_device.removes().contains(&PathBuf::from("system/STALE_FILE")));
133*c2e18aaaSAndroid Build Coastguard Worker }
134*c2e18aaaSAndroid Build Coastguard Worker
135*c2e18aaaSAndroid Build Coastguard Worker Ok(())
136*c2e18aaaSAndroid Build Coastguard Worker }
137*c2e18aaaSAndroid Build Coastguard Worker
138*c2e18aaaSAndroid Build Coastguard Worker #[test]
update_big_fs_change() -> Result<()>139*c2e18aaaSAndroid Build Coastguard Worker fn update_big_fs_change() -> Result<()> {
140*c2e18aaaSAndroid Build Coastguard Worker std::env::remove_var("HOME"); // Use default config, don't write one.
141*c2e18aaaSAndroid Build Coastguard Worker let device_files = HashMap::from([
142*c2e18aaaSAndroid Build Coastguard Worker // <-- STALE_FILE not on host
143*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/STALE_FILE"), file_metadata("m1")),
144*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin"), dir_metadata()),
145*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/f1"), file_metadata("m1")),
146*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/f2"), file_metadata("m1")),
147*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/dir1"), dir_metadata()),
148*c2e18aaaSAndroid Build Coastguard Worker // <-- STALE_DIR not on host
149*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/dir1/STALE_DIR"), dir_metadata()),
150*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/dir1/STALE_DIR/stalefile1"), file_metadata("m1")),
151*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/dir1/STALE_DIR/stalefile2"), file_metadata("m1")),
152*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/dir1/f1"), file_metadata("m1")),
153*c2e18aaaSAndroid Build Coastguard Worker ]);
154*c2e18aaaSAndroid Build Coastguard Worker
155*c2e18aaaSAndroid Build Coastguard Worker let host_files = HashMap::from([
156*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin"), dir_metadata()),
157*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/f1"), file_metadata("m1")),
158*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/f2"), file_metadata("m1")),
159*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/dir1"), dir_metadata()),
160*c2e18aaaSAndroid Build Coastguard Worker (PathBuf::from("system/bin/dir1/f1"), file_metadata("m1")),
161*c2e18aaaSAndroid Build Coastguard Worker ]);
162*c2e18aaaSAndroid Build Coastguard Worker
163*c2e18aaaSAndroid Build Coastguard Worker // Ensure the partitions exist.
164*c2e18aaaSAndroid Build Coastguard Worker let ninja_deps = commands::split_string("system/bin/f1 system/bin/f2 system/bin/dir1/f1");
165*c2e18aaaSAndroid Build Coastguard Worker let fake_host = FakeHost::new(&host_files, &ninja_deps);
166*c2e18aaaSAndroid Build Coastguard Worker let fake_device = FakeDevice::new(&device_files);
167*c2e18aaaSAndroid Build Coastguard Worker let cli = cli::Cli::parse_from(["", "--product_out", "unused", "update", "--restart", "none"]);
168*c2e18aaaSAndroid Build Coastguard Worker
169*c2e18aaaSAndroid Build Coastguard Worker // Expect some_file
170*c2e18aaaSAndroid Build Coastguard Worker {
171*c2e18aaaSAndroid Build Coastguard Worker let mut stdout = Vec::new();
172*c2e18aaaSAndroid Build Coastguard Worker let mut metrics = FakeMetricSender::new();
173*c2e18aaaSAndroid Build Coastguard Worker adevice::adevice::adevice(
174*c2e18aaaSAndroid Build Coastguard Worker &fake_host,
175*c2e18aaaSAndroid Build Coastguard Worker &fake_device,
176*c2e18aaaSAndroid Build Coastguard Worker &cli,
177*c2e18aaaSAndroid Build Coastguard Worker &mut stdout,
178*c2e18aaaSAndroid Build Coastguard Worker &mut metrics,
179*c2e18aaaSAndroid Build Coastguard Worker NO_LOG_FILE,
180*c2e18aaaSAndroid Build Coastguard Worker &mut Profiler::default(),
181*c2e18aaaSAndroid Build Coastguard Worker )
182*c2e18aaaSAndroid Build Coastguard Worker .context("Running adevice update")?;
183*c2e18aaaSAndroid Build Coastguard Worker
184*c2e18aaaSAndroid Build Coastguard Worker assert_eq!(
185*c2e18aaaSAndroid Build Coastguard Worker vec![
186*c2e18aaaSAndroid Build Coastguard Worker // expected to be ordered dfs
187*c2e18aaaSAndroid Build Coastguard Worker PathBuf::from("system/bin/dir1/STALE_DIR/stalefile2"),
188*c2e18aaaSAndroid Build Coastguard Worker PathBuf::from("system/bin/dir1/STALE_DIR/stalefile1"),
189*c2e18aaaSAndroid Build Coastguard Worker PathBuf::from("system/STALE_FILE"),
190*c2e18aaaSAndroid Build Coastguard Worker PathBuf::from("system/bin/dir1/STALE_DIR"),
191*c2e18aaaSAndroid Build Coastguard Worker ],
192*c2e18aaaSAndroid Build Coastguard Worker fake_device.removes(),
193*c2e18aaaSAndroid Build Coastguard Worker );
194*c2e18aaaSAndroid Build Coastguard Worker }
195*c2e18aaaSAndroid Build Coastguard Worker
196*c2e18aaaSAndroid Build Coastguard Worker Ok(())
197*c2e18aaaSAndroid Build Coastguard Worker }
198*c2e18aaaSAndroid Build Coastguard Worker
file_metadata(digest: &str) -> FileMetadata199*c2e18aaaSAndroid Build Coastguard Worker pub fn file_metadata(digest: &str) -> FileMetadata {
200*c2e18aaaSAndroid Build Coastguard Worker FileMetadata {
201*c2e18aaaSAndroid Build Coastguard Worker file_type: fingerprint::FileType::File,
202*c2e18aaaSAndroid Build Coastguard Worker digest: digest.to_string(),
203*c2e18aaaSAndroid Build Coastguard Worker ..Default::default()
204*c2e18aaaSAndroid Build Coastguard Worker }
205*c2e18aaaSAndroid Build Coastguard Worker }
206*c2e18aaaSAndroid Build Coastguard Worker
dir_metadata() -> FileMetadata207*c2e18aaaSAndroid Build Coastguard Worker pub fn dir_metadata() -> FileMetadata {
208*c2e18aaaSAndroid Build Coastguard Worker FileMetadata { file_type: fingerprint::FileType::Directory, ..Default::default() }
209*c2e18aaaSAndroid Build Coastguard Worker }
210