test_manager_lib/
offers.rs1use crate::constants::{HERMETIC_RESOLVER_REALM_NAME, TEST_ROOT_COLLECTION, WRAPPER_REALM_NAME};
6use anyhow::Error;
7use fuchsia_component_test::error::Error as RealmBuilderError;
8use fuchsia_component_test::{Capability, RealmBuilder, Ref, Route, SubRealmBuilder};
9use {fidl_fuchsia_component_decl as fdecl, fidl_fuchsia_component_test as ftest};
10
11pub(crate) fn map_offers(offers: Vec<fdecl::Offer>) -> Result<Vec<ftest::Capability>, Error> {
12 let mut capabilities = vec![];
13 for offer_decl in offers {
14 match offer_decl {
15 fdecl::Offer::Protocol(fdecl::OfferProtocol {
16 target_name: Some(target_name), ..
17 }) if target_name != "fuchsia.logger.LogSink"
18 && target_name != "fuchsia.inspect.InspectSink" =>
19 {
20 capabilities.push(
21 Capability::protocol_by_name(target_name).availability_same_as_target().into(),
22 );
23 }
24 fdecl::Offer::Directory(fdecl::OfferDirectory {
25 target_name: Some(target_name),
26 ..
27 }) => {
28 capabilities
29 .push(Capability::directory(target_name).availability_same_as_target().into());
30 }
31 fdecl::Offer::Storage(fdecl::OfferStorage {
32 target_name: Some(target_name), ..
33 }) => {
34 let use_path = format!("/{}", target_name);
35 capabilities.push(
36 Capability::storage(target_name)
37 .path(use_path)
38 .availability_same_as_target()
39 .into(),
40 );
41 }
42 fdecl::Offer::EventStream(fdecl::OfferEventStream {
43 target_name: Some(target_name),
44 ..
45 }) => {
46 capabilities.push(Capability::event_stream(target_name.clone()).into());
47 }
48 fdecl::Offer::Service(fdecl::OfferService {
49 target_name: Some(target_name), ..
50 }) => {
51 capabilities.push(
52 Capability::service_by_name(target_name).availability_same_as_target().into(),
53 );
54 }
55 fdecl::Offer::Runner(fdecl::OfferRunner { target_name: Some(target_name), .. }) => {
56 capabilities.push(Capability::runner(target_name).into());
57 }
58 fdecl::Offer::Resolver(fdecl::OfferResolver {
59 target_name: Some(target_name), ..
60 }) => {
61 capabilities.push(Capability::resolver(target_name).into());
62 }
63 fdecl::Offer::Config(fdecl::OfferConfiguration {
64 target_name: Some(target_name),
65 ..
66 }) => {
67 capabilities.push(
68 Capability::configuration(target_name).availability_same_as_target().into(),
69 );
70 }
71 fdecl::Offer::Dictionary(fdecl::OfferDictionary {
72 target_name: Some(target_name),
73 ..
74 }) if target_name != "diagnostics" => {
75 capabilities
78 .push(Capability::dictionary(target_name).availability_same_as_target().into());
79 }
80 _ => {
81 }
83 }
84 }
85 Ok(capabilities)
86}
87
88pub(crate) async fn apply_offers(
89 builder: &RealmBuilder,
90 wrapper_realm: &SubRealmBuilder,
91 offers: &Vec<ftest::Capability>,
92) -> Result<(), RealmBuilderError> {
93 for capability in offers {
94 let (capability_for_test_wrapper, capability_for_test_root) =
95 if let ftest::Capability::EventStream(event_stream) = &capability {
96 let mut test_wrapper_event_stream = event_stream.clone();
100 test_wrapper_event_stream.scope = Some(vec![Ref::child(WRAPPER_REALM_NAME).into()]);
101 let mut test_root_event_stream = event_stream.clone();
102 test_root_event_stream.scope = Some(vec![
103 Ref::collection(TEST_ROOT_COLLECTION).into(),
104 Ref::child(HERMETIC_RESOLVER_REALM_NAME).into(),
105 ]);
106 (
107 ftest::Capability::EventStream(test_wrapper_event_stream),
108 ftest::Capability::EventStream(test_root_event_stream),
109 )
110 } else {
111 (capability.clone(), capability.clone())
113 };
114 builder
115 .add_route(
116 Route::new()
117 .capability(capability_for_test_wrapper.clone())
118 .from(Ref::parent())
119 .to(wrapper_realm),
120 )
121 .await?;
122 wrapper_realm
123 .add_route(
124 Route::new()
125 .capability(capability_for_test_root.clone())
126 .from(Ref::parent())
127 .to(Ref::collection(TEST_ROOT_COLLECTION)),
128 )
129 .await?;
130 }
131 Ok(())
132}