power_manager_integration_test_lib/
lib.rs1pub mod client_connectors;
6mod mocks;
7
8use crate::mocks::activity_service::MockActivityService;
9use crate::mocks::admin::MockStateControlAdminService;
10use crate::mocks::input_settings_service::MockInputSettingsService;
11use crate::mocks::kernel_service::MockKernelService;
12use fidl::endpoints::{DiscoverableProtocolMarker, ProtocolMarker, ServiceMarker};
13use fidl::AsHandleRef as _;
14use fuchsia_component::client::Service;
15use fuchsia_component_test::{
16 Capability, ChildOptions, RealmBuilder, RealmBuilderParams, RealmInstance, Ref, Route,
17};
18use fuchsia_driver_test::{DriverTestRealmBuilder, DriverTestRealmInstance};
19use log::*;
20use std::sync::atomic::{AtomicU64, Ordering};
21use std::sync::Arc;
22use {
23 fidl_fuchsia_driver_test as fdt, fidl_fuchsia_hardware_cpu_ctrl as fcpu_ctrl,
24 fidl_fuchsia_hardware_power_statecontrol as fpower, fidl_fuchsia_io as fio,
25 fidl_fuchsia_kernel as fkernel,
26 fidl_fuchsia_powermanager_driver_temperaturecontrol as ftemperaturecontrol,
27 fidl_fuchsia_sys2 as fsys2, fidl_fuchsia_testing as ftesting,
28};
29
30const POWER_MANAGER_URL: &str = "#meta/power-manager.cm";
31const CPU_MANAGER_URL: &str = "#meta/cpu-manager.cm";
32const FAKE_COBALT_URL: &str = "#meta/fake_cobalt.cm";
33const FAKE_CLOCK_URL: &str = "#meta/fake_clock.cm";
34
35const FAKE_TIME_SCALE: u32 = 100;
37
38static UNIQUE_REALM_NUMBER: AtomicU64 = AtomicU64::new(0);
40
41pub struct TestEnvBuilder {
42 power_manager_node_config_path: Option<String>,
43 cpu_manager_node_config_path: Option<String>,
44}
45
46impl TestEnvBuilder {
47 pub fn new() -> Self {
48 Self { power_manager_node_config_path: None, cpu_manager_node_config_path: None }
49 }
50
51 pub fn power_manager_node_config_path(mut self, path: &str) -> Self {
53 self.power_manager_node_config_path = Some(path.into());
54 self
55 }
56
57 pub fn cpu_manager_node_config_path(mut self, path: &str) -> Self {
59 self.cpu_manager_node_config_path = Some(path.into());
60 self
61 }
62
63 pub async fn build(self) -> TestEnv {
64 let realm_name = format!(
67 "{}-{}",
68 fuchsia_runtime::process_self().get_koid().unwrap().raw_koid(),
69 UNIQUE_REALM_NUMBER.fetch_add(1, Ordering::Relaxed)
70 );
71
72 let realm_builder =
73 RealmBuilder::with_params(RealmBuilderParams::new().realm_name(realm_name))
74 .await
75 .expect("Failed to create RealmBuilder");
76
77 realm_builder.driver_test_realm_setup().await.expect("Failed to setup driver test realm");
78
79 let expose =
80 fuchsia_component_test::Capability::service::<fcpu_ctrl::ServiceMarker>().into();
81 let dtr_exposes = vec![expose];
82
83 realm_builder.driver_test_realm_add_dtr_exposes(&dtr_exposes).await.unwrap();
84
85 let power_manager = realm_builder
86 .add_child("power_manager", POWER_MANAGER_URL, ChildOptions::new())
87 .await
88 .expect("Failed to add child: power_manager");
89
90 let cpu_manager = realm_builder
91 .add_child("cpu_manager", CPU_MANAGER_URL, ChildOptions::new())
92 .await
93 .expect("Failed to add child: cpu_manager");
94
95 let fake_cobalt = realm_builder
96 .add_child("fake_cobalt", FAKE_COBALT_URL, ChildOptions::new())
97 .await
98 .expect("Failed to add child: fake_cobalt");
99
100 let fake_clock = realm_builder
101 .add_child("fake_clock", FAKE_CLOCK_URL, ChildOptions::new())
102 .await
103 .expect("Failed to add child: fake_clock");
104
105 let activity_service = MockActivityService::new();
106 let activity_service_clone = activity_service.clone();
107 let activity_service_child = realm_builder
108 .add_local_child(
109 "activity_service",
110 move |handles| Box::pin(activity_service_clone.clone().run(handles)),
111 ChildOptions::new(),
112 )
113 .await
114 .expect("Failed to add child: activity_service");
115
116 let input_settings_service = MockInputSettingsService::new();
117 let input_settings_service_clone = input_settings_service.clone();
118 let input_settings_service_child = realm_builder
119 .add_local_child(
120 "input_settings_service",
121 move |handles| Box::pin(input_settings_service_clone.clone().run(handles)),
122 ChildOptions::new(),
123 )
124 .await
125 .expect("Failed to add child: input_settings_service");
126
127 let admin_service = MockStateControlAdminService::new();
128 let admin_service_clone = admin_service.clone();
129 let admin_service_child = realm_builder
130 .add_local_child(
131 "admin_service",
132 move |handles| Box::pin(admin_service_clone.clone().run(handles)),
133 ChildOptions::new(),
134 )
135 .await
136 .expect("Failed to add child: admin_service");
137
138 let kernel_service = MockKernelService::new();
139 let kernel_service_clone = kernel_service.clone();
140 let kernel_service_child = realm_builder
141 .add_local_child(
142 "kernel_service",
143 move |handles| Box::pin(kernel_service_clone.clone().run(handles)),
144 ChildOptions::new(),
145 )
146 .await
147 .expect("Failed to add child: kernel_service");
148
149 let parent_to_power_manager_routes = Route::new()
151 .capability(Capability::protocol_by_name("fuchsia.logger.LogSink"))
152 .capability(Capability::protocol_by_name("fuchsia.tracing.provider.Registry"))
153 .capability(Capability::protocol_by_name("fuchsia.boot.WriteOnlyLog"));
154 realm_builder
155 .add_route(parent_to_power_manager_routes.from(Ref::parent()).to(&power_manager))
156 .await
157 .unwrap();
158
159 let parent_to_cobalt_routes =
160 Route::new().capability(Capability::protocol_by_name("fuchsia.logger.LogSink"));
161 realm_builder
162 .add_route(parent_to_cobalt_routes.from(Ref::parent()).to(&fake_cobalt))
163 .await
164 .unwrap();
165
166 let parent_to_fake_clock_routes =
167 Route::new().capability(Capability::protocol_by_name("fuchsia.logger.LogSink"));
168 realm_builder
169 .add_route(parent_to_fake_clock_routes.from(Ref::parent()).to(&fake_clock))
170 .await
171 .unwrap();
172
173 let fake_clock_to_power_manager_routes =
174 Route::new().capability(Capability::protocol_by_name("fuchsia.testing.FakeClock"));
175 realm_builder
176 .add_route(fake_clock_to_power_manager_routes.from(&fake_clock).to(&power_manager))
177 .await
178 .unwrap();
179
180 let fake_clock_to_cpu_manager_routes =
181 Route::new().capability(Capability::protocol_by_name("fuchsia.testing.FakeClock"));
182 realm_builder
183 .add_route(fake_clock_to_cpu_manager_routes.from(&fake_clock).to(&cpu_manager))
184 .await
185 .unwrap();
186
187 let fake_clock_to_parent_routes = Route::new()
188 .capability(Capability::protocol_by_name("fuchsia.testing.FakeClockControl"));
189 realm_builder
190 .add_route(fake_clock_to_parent_routes.from(&fake_clock).to(Ref::parent()))
191 .await
192 .unwrap();
193
194 let cobalt_to_power_manager_routes = Route::new()
195 .capability(Capability::protocol_by_name("fuchsia.metrics.MetricEventLoggerFactory"));
196 realm_builder
197 .add_route(cobalt_to_power_manager_routes.from(&fake_cobalt).to(&power_manager))
198 .await
199 .unwrap();
200
201 let activity_service_to_power_manager_routes =
202 Route::new().capability(Capability::protocol_by_name("fuchsia.ui.activity.Provider"));
203 realm_builder
204 .add_route(
205 activity_service_to_power_manager_routes
206 .from(&activity_service_child)
207 .to(&power_manager),
208 )
209 .await
210 .unwrap();
211
212 let input_settings_service_to_power_manager_routes =
213 Route::new().capability(Capability::protocol_by_name("fuchsia.settings.Input"));
214 realm_builder
215 .add_route(
216 input_settings_service_to_power_manager_routes
217 .from(&input_settings_service_child)
218 .to(&power_manager),
219 )
220 .await
221 .unwrap();
222
223 let shutdown_shim_to_power_manager_routes = Route::new()
224 .capability(Capability::protocol_by_name("fuchsia.hardware.power.statecontrol.Admin"));
225
226 realm_builder
227 .add_route(
228 shutdown_shim_to_power_manager_routes.from(&admin_service_child).to(&power_manager),
229 )
230 .await
231 .unwrap();
232
233 let kernel_service_to_cpu_manager_routes =
234 Route::new().capability(Capability::protocol_by_name("fuchsia.kernel.Stats"));
235 realm_builder
236 .add_route(
237 kernel_service_to_cpu_manager_routes.from(&kernel_service_child).to(&cpu_manager),
238 )
239 .await
240 .unwrap();
241
242 realm_builder
243 .add_route(
244 Route::new()
245 .capability(
246 Capability::directory("pkg")
247 .subdir("config/power_manager")
248 .as_("config")
249 .path("/config")
250 .rights(fio::R_STAR_DIR),
251 )
252 .from(Ref::framework())
253 .to(&power_manager),
254 )
255 .await
256 .unwrap();
257
258 realm_builder
259 .add_route(
260 Route::new()
261 .capability(
262 Capability::directory("pkg")
263 .subdir("config/cpu_manager")
264 .as_("config")
265 .path("/config")
266 .rights(fio::R_STAR_DIR),
267 )
268 .from(Ref::framework())
269 .to(&cpu_manager),
270 )
271 .await
272 .unwrap();
273
274 realm_builder
275 .add_route(
276 Route::new()
277 .capability(Capability::protocol::<fsys2::LifecycleControllerMarker>())
278 .from(Ref::framework())
279 .to(Ref::parent()),
280 )
281 .await
282 .unwrap();
283
284 let power_manager_to_parent_routes = Route::new()
285 .capability(Capability::protocol_by_name("fuchsia.power.profile.Watcher"))
286 .capability(Capability::protocol_by_name("fuchsia.thermal.ClientStateConnector"))
287 .capability(Capability::protocol_by_name("fuchsia.power.clientlevel.Connector"));
288
289 realm_builder
290 .add_route(power_manager_to_parent_routes.from(&power_manager).to(Ref::parent()))
291 .await
292 .unwrap();
293
294 let parent_to_cpu_manager_routes = Route::new()
296 .capability(Capability::protocol_by_name("fuchsia.tracing.provider.Registry"));
297 realm_builder
298 .add_route(parent_to_cpu_manager_routes.from(Ref::parent()).to(&cpu_manager))
299 .await
300 .unwrap();
301
302 let power_manager_to_cpu_manager_routes = Route::new()
303 .capability(Capability::protocol_by_name("fuchsia.thermal.ClientStateConnector"));
304 realm_builder
305 .add_route(power_manager_to_cpu_manager_routes.from(&power_manager).to(&cpu_manager))
306 .await
307 .unwrap();
308
309 realm_builder
310 .add_route(
311 Route::new()
312 .capability(Capability::directory("dev-topological"))
313 .from(Ref::child("driver_test_realm"))
314 .to(&power_manager),
315 )
316 .await
317 .unwrap();
318
319 realm_builder
320 .add_route(
321 Route::new()
322 .capability(Capability::service::<fcpu_ctrl::ServiceMarker>())
323 .from(Ref::child("driver_test_realm"))
324 .to(&cpu_manager),
325 )
326 .await
327 .unwrap();
328
329 realm_builder.init_mutable_config_from_package(&power_manager).await.unwrap();
331 realm_builder
332 .set_config_value(
333 &power_manager,
334 "node_config_path",
335 self.power_manager_node_config_path
336 .expect("power_manager_node_config_path not set")
337 .into(),
338 )
339 .await
340 .unwrap();
341
342 if self.cpu_manager_node_config_path.is_some() {
344 realm_builder.init_mutable_config_from_package(&cpu_manager).await.unwrap();
345 realm_builder
346 .set_config_value(
347 &cpu_manager,
348 "node_config_path",
349 self.cpu_manager_node_config_path
350 .expect("cpu_manager_node_config_path not set")
351 .into(),
352 )
353 .await
354 .unwrap();
355 }
356
357 let realm_instance = realm_builder.build().await.expect("Failed to build RealmInstance");
359
360 let args = fdt::RealmArgs {
362 root_driver: Some("#meta/root.cm".to_string()),
363 dtr_exposes: Some(dtr_exposes),
364 ..Default::default()
365 };
366
367 realm_instance
368 .driver_test_realm_start(args)
369 .await
370 .expect("Failed to start driver test realm");
371
372 set_fake_time_scale(&realm_instance, FAKE_TIME_SCALE).await;
375
376 TestEnv {
377 realm_instance: Some(realm_instance),
378 mocks: Mocks {
379 activity_service,
380 input_settings_service,
381 admin_service,
382 kernel_service,
383 },
384 }
385 }
386}
387
388pub struct TestEnv {
389 realm_instance: Option<RealmInstance>,
390 pub mocks: Mocks,
391}
392
393impl TestEnv {
394 pub fn connect_to_protocol<P: DiscoverableProtocolMarker>(&self) -> P::Proxy {
396 self.realm_instance
397 .as_ref()
398 .unwrap()
399 .root
400 .connect_to_protocol_at_exposed_dir::<P>()
401 .unwrap()
402 }
403
404 pub fn connect_to_device<P: ProtocolMarker>(&self, driver_path: &str) -> P::Proxy {
405 let dev = self.realm_instance.as_ref().unwrap().driver_test_realm_connect_to_dev().unwrap();
406 let path = driver_path.strip_prefix("/dev/").unwrap();
407
408 fuchsia_component::client::connect_to_named_protocol_at_dir_root::<P>(&dev, path).unwrap()
409 }
410
411 pub async fn connect_to_first_service_instance<S: ServiceMarker>(&self, marker: S) -> S::Proxy {
413 Service::open_from_dir(self.realm_instance.as_ref().unwrap().root.get_exposed_dir(), marker)
414 .unwrap()
415 .watch_for_any()
416 .await
417 .unwrap()
418 }
419
420 pub async fn destroy(&mut self) {
424 info!("Destroying TestEnv");
425 self.realm_instance
426 .take()
427 .expect("Missing realm instance")
428 .destroy()
429 .await
430 .expect("Failed to destroy realm instance");
431 }
432
433 pub async fn set_temperature(&self, driver_path: &str, temperature: f32) {
435 let dev = self.realm_instance.as_ref().unwrap().driver_test_realm_connect_to_dev().unwrap();
436
437 let control_path = driver_path.strip_prefix("/dev").unwrap().to_owned() + "/control";
438
439 let fake_temperature_control =
440 fuchsia_component::client::connect_to_named_protocol_at_dir_root::<
441 ftemperaturecontrol::DeviceMarker,
442 >(&dev, &control_path)
443 .unwrap();
444
445 let _status = fake_temperature_control.set_temperature_celsius(temperature).await.unwrap();
446 }
447
448 pub async fn set_cpu_stats(&self, cpu_stats: fkernel::CpuStats) {
449 self.mocks.kernel_service.set_cpu_stats(cpu_stats).await;
450 }
451
452 pub async fn wait_for_shutdown_request(&self) {
453 self.mocks.admin_service.wait_for_shutdown_request().await;
454 }
455
456 pub async fn wait_for_device(&self, driver_path: &str) {
458 let dev = self.realm_instance.as_ref().unwrap().driver_test_realm_connect_to_dev().unwrap();
459
460 let path = driver_path.strip_prefix("/dev").unwrap().to_owned();
461
462 device_watcher::recursive_wait(&dev, &path).await.unwrap();
463 }
464}
465
466impl Drop for TestEnv {
469 fn drop(&mut self) {
470 assert!(self.realm_instance.is_none(), "Must call destroy() to tear down test environment");
471 }
472}
473
474async fn set_fake_time_scale(realm_instance: &RealmInstance, scale: u32) {
476 let fake_clock_control = realm_instance
477 .root
478 .connect_to_protocol_at_exposed_dir::<ftesting::FakeClockControlMarker>()
479 .unwrap();
480
481 fake_clock_control.pause().await.expect("failed to pause fake time: FIDL error");
482 fake_clock_control
483 .resume_with_increments(
484 zx::MonotonicDuration::from_millis(1).into_nanos(),
485 &ftesting::Increment::Determined(
486 zx::MonotonicDuration::from_millis(scale.into()).into_nanos(),
487 ),
488 )
489 .await
490 .expect("failed to set fake time scale: FIDL error")
491 .expect("failed to set fake time scale: protocol error");
492}
493
494pub struct Mocks {
496 pub activity_service: Arc<MockActivityService>,
497 pub input_settings_service: Arc<MockInputSettingsService>,
498 pub admin_service: Arc<MockStateControlAdminService>,
499 pub kernel_service: Arc<MockKernelService>,
500}
501
502pub async fn test_thermal_reboot(mut env: TestEnv, sensor_path: &str, temperature: f32) {
506 let _client = client_connectors::ThermalClient::new(&env, "audio");
508
509 env.set_temperature(sensor_path, temperature).await;
512 let result = env.mocks.admin_service.wait_for_shutdown_request().await;
513 assert_eq!(result.reasons.unwrap(), vec![fpower::RebootReason2::HighTemperature]);
514
515 env.destroy().await;
516}