sl4f_lib/modular/
facade.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use crate::modular::types::{
    BasemgrResult, KillSessionResult, RestartSessionResult, StartBasemgrRequest,
};
use anyhow::{format_err, Error};
use fuchsia_component::client::connect_to_protocol;
use serde_json::{from_value, Value};
use {
    fidl_fuchsia_component_decl as fdecl, fidl_fuchsia_session as fsession,
    fidl_fuchsia_sys2 as fsys,
};

// The session runs as `./core/session-manager/session:session`. The parts are:
const SESSION_PARENT_MONIKER: &str = "./core/session-manager";
const SESSION_COLLECTION_NAME: &str = "session";
const SESSION_CHILD_NAME: &str = "session";
const SESSION_MONIKER: &str = "./core/session-manager/session:session";

/// Facade providing access to session testing interfaces.
#[derive(Debug)]
pub struct ModularFacade {
    session_launcher: fsession::LauncherProxy,
    session_restarter: fsession::RestarterProxy,
    lifecycle_controller: fsys::LifecycleControllerProxy,
    realm_query: fsys::RealmQueryProxy,
}

impl ModularFacade {
    pub fn new() -> ModularFacade {
        let session_launcher = connect_to_protocol::<fsession::LauncherMarker>()
            .expect("failed to connect to fuchsia.session.Launcher");
        let session_restarter = connect_to_protocol::<fsession::RestarterMarker>()
            .expect("failed to connect to fuchsia.session.Restarter");
        let lifecycle_controller = connect_to_protocol::<fsys::LifecycleControllerMarker>()
            .expect("failed to connect to fuchsia.sys2.LifecycleController");
        let realm_query =
            fuchsia_component::client::connect_to_protocol::<fsys::RealmQueryMarker>()
                .expect("failed to connect to fuchsia.sys2.RealmQuery");
        ModularFacade { session_launcher, session_restarter, lifecycle_controller, realm_query }
    }

    pub fn new_with_proxies(
        session_launcher: fsession::LauncherProxy,
        session_restarter: fsession::RestarterProxy,
        lifecycle_controller: fsys::LifecycleControllerProxy,
        realm_query: fsys::RealmQueryProxy,
    ) -> ModularFacade {
        ModularFacade { session_launcher, session_restarter, lifecycle_controller, realm_query }
    }

    /// Returns true if a session is currently running.
    pub async fn is_session_running(&self) -> Result<bool, Error> {
        Ok(self
            .realm_query
            .get_instance(SESSION_MONIKER)
            .await?
            .ok()
            .and_then(|instance| instance.resolved_info)
            .and_then(|info| info.execution_info)
            .is_some())
    }

    /// Restarts the currently running session.
    pub async fn restart_session(&self) -> Result<RestartSessionResult, Error> {
        if self.session_restarter.restart().await?.is_err() {
            return Ok(RestartSessionResult::Fail);
        }
        Ok(RestartSessionResult::Success)
    }

    /// Facade to stop the session.
    pub async fn stop_session(&self) -> Result<KillSessionResult, Error> {
        if !self.is_session_running().await? {
            return Ok(KillSessionResult::NoSessionRunning);
        }

        // Use a root LifecycleController to kill the session. It will send a shutdown signal to the
        // session so it can terminate gracefully.
        self.lifecycle_controller
            .destroy_instance(
                SESSION_PARENT_MONIKER,
                &fdecl::ChildRef {
                    name: SESSION_CHILD_NAME.to_string(),
                    collection: Some(SESSION_COLLECTION_NAME.to_string()),
                },
            )
            .await?
            .map_err(|err| format_err!("failed to destroy session: {:?}", err))?;

        Ok(KillSessionResult::Success)
    }

    /// Starts a session component.
    ///
    /// If a session is already running, it will be stopped first.
    ///
    /// `session_url` is required.
    ///
    /// # Arguments
    /// * `args`: A serde_json Value parsed into [`StartBasemgrRequest`]
    pub async fn start_session(&self, args: Value) -> Result<BasemgrResult, Error> {
        let req: StartBasemgrRequest = from_value(args)?;

        // If a session is running, stop it before starting a new one.
        if self.is_session_running().await? {
            self.stop_session().await?;
        }

        self.launch_session(&req.session_url).await?;

        Ok(BasemgrResult::Success)
    }

    /// Launches a session.
    ///
    /// # Arguments
    /// * `session_url`: Component URL for the session to launch.
    async fn launch_session(&self, session_url: &str) -> Result<(), Error> {
        let config = fsession::LaunchConfiguration {
            session_url: Some(session_url.to_string()),
            ..Default::default()
        };
        self.session_launcher
            .launch(&config)
            .await?
            .map_err(|err| format_err!("failed to launch session: {:?}", err))
    }
}

#[cfg(test)]
mod tests {
    use crate::modular::facade::{
        ModularFacade, SESSION_CHILD_NAME, SESSION_COLLECTION_NAME, SESSION_MONIKER,
        SESSION_PARENT_MONIKER,
    };
    use crate::modular::types::{BasemgrResult, KillSessionResult, RestartSessionResult};
    use anyhow::Error;
    use assert_matches::assert_matches;
    use fidl::endpoints::spawn_stream_handler;
    use futures::Future;
    use lazy_static::lazy_static;
    use serde_json::json;
    use test_util::Counter;
    use {fidl_fuchsia_session as fsession, fidl_fuchsia_sys2 as fsys};

    const TEST_SESSION_URL: &str = "fuchsia-pkg://fuchsia.com/test_session#meta/test_session.cm";

    // This function sets up a facade with launcher, restarter, and lifecycle_controller mocks and
    // provides counters to check how often each is called. To use it, pass in a function that
    // operates on a facade, executing the operations of the test, and pass in the expected number
    // of calls to launch, destroy, and restart.
    async fn test_facade<Fut>(
        run_facade_fns: impl Fn(ModularFacade) -> Fut,
        is_session_running: bool,
        expected_launch_count: usize,
        expected_destroy_count: usize,
        expected_restart_count: usize,
    ) -> Result<(), Error>
    where
        Fut: Future<Output = Result<(), Error>>,
    {
        lazy_static! {
            static ref SESSION_LAUNCH_CALL_COUNT: Counter = Counter::new(0);
            static ref DESTROY_CHILD_CALL_COUNT: Counter = Counter::new(0);
            static ref SESSION_RESTART_CALL_COUNT: Counter = Counter::new(0);
        }

        let session_launcher = spawn_stream_handler(move |launcher_request| async move {
            match launcher_request {
                fsession::LauncherRequest::Launch { configuration, responder } => {
                    assert!(configuration.session_url.is_some());
                    let session_url = configuration.session_url.unwrap();
                    assert!(session_url == TEST_SESSION_URL.to_string());

                    SESSION_LAUNCH_CALL_COUNT.inc();
                    let _ = responder.send(Ok(()));
                }
            }
        });

        let session_restarter = spawn_stream_handler(move |restarter_request| async move {
            match restarter_request {
                fsession::RestarterRequest::Restart { responder } => {
                    SESSION_RESTART_CALL_COUNT.inc();
                    let _ = responder.send(Ok(()));
                }
            }
        });

        let lifecycle_controller = spawn_stream_handler(|lifecycle_controller_request| async {
            match lifecycle_controller_request {
                fsys::LifecycleControllerRequest::DestroyInstance {
                    parent_moniker,
                    child,
                    responder,
                } => {
                    assert_eq!(parent_moniker, SESSION_PARENT_MONIKER.to_string());
                    assert_eq!(child.name, SESSION_CHILD_NAME);
                    assert_eq!(child.collection.unwrap(), SESSION_COLLECTION_NAME);

                    DESTROY_CHILD_CALL_COUNT.inc();
                    let _ = responder.send(Ok(()));
                }
                r => {
                    panic!("didn't expect request: {:?}", r)
                }
            }
        });

        let realm_query = spawn_stream_handler(move |realm_query_request| async move {
            match realm_query_request {
                fsys::RealmQueryRequest::GetInstance { moniker, responder } => {
                    assert_eq!(moniker, SESSION_MONIKER.to_string());
                    let instance = if is_session_running {
                        fsys::Instance {
                            moniker: Some(SESSION_MONIKER.to_string()),
                            url: Some("fake".to_string()),
                            instance_id: None,
                            resolved_info: Some(fsys::ResolvedInfo {
                                resolved_url: Some("fake".to_string()),
                                execution_info: Some(fsys::ExecutionInfo {
                                    start_reason: Some("fake".to_string()),
                                    ..Default::default()
                                }),
                                ..Default::default()
                            }),
                            ..Default::default()
                        }
                    } else {
                        fsys::Instance {
                            moniker: Some(SESSION_MONIKER.to_string()),
                            url: Some("fake".to_string()),
                            instance_id: None,
                            resolved_info: None,
                            ..Default::default()
                        }
                    };
                    let _ = responder.send(Ok(&instance));
                }
                r => {
                    panic!("didn't expect request: {:?}", r)
                }
            }
        });

        let facade = ModularFacade::new_with_proxies(
            session_launcher,
            session_restarter,
            lifecycle_controller,
            realm_query,
        );

        run_facade_fns(facade).await?;

        assert_eq!(
            SESSION_LAUNCH_CALL_COUNT.get(),
            expected_launch_count,
            "SESSION_LAUNCH_CALL_COUNT"
        );
        assert_eq!(
            DESTROY_CHILD_CALL_COUNT.get(),
            expected_destroy_count,
            "DESTROY_CHILD_CALL_COUNT"
        );
        assert_eq!(
            SESSION_RESTART_CALL_COUNT.get(),
            expected_restart_count,
            "SESSION_RESTART_CALL_COUNT"
        );

        Ok(())
    }

    #[fuchsia_async::run(2, test)]
    async fn test_stop_session() -> Result<(), Error> {
        async fn stop_session_steps(facade: ModularFacade) -> Result<(), Error> {
            assert_matches!(facade.is_session_running().await, Ok(true));
            assert_matches!(facade.stop_session().await, Ok(KillSessionResult::Success));
            Ok(())
        }

        test_facade(
            &stop_session_steps,
            true, // is_session_running
            0,    // SESSION_LAUNCH_CALL_COUNT
            1,    // DESTROY_CHILD_CALL_COUNT
            0,    // SESSION_RESTART_CALL_COUNT
        )
        .await
    }

    #[fuchsia_async::run(2, test)]
    async fn test_start_session_without_config() -> Result<(), Error> {
        async fn start_session_v2_steps(facade: ModularFacade) -> Result<(), Error> {
            let start_session_args = json!({
                "session_url": TEST_SESSION_URL,
            });
            assert_matches!(
                facade.start_session(start_session_args).await,
                Ok(BasemgrResult::Success)
            );
            Ok(())
        }

        test_facade(
            &start_session_v2_steps,
            false, // is_session_running
            1,     // SESSION_LAUNCH_CALL_COUNT
            0,     // DESTROY_CHILD_CALL_COUNT
            0,     // SESSION_RESTART_CALL_COUNT
        )
        .await
    }

    #[fuchsia_async::run(2, test)]
    async fn test_start_session_shutdown_existing() -> Result<(), Error> {
        async fn start_existing_steps(facade: ModularFacade) -> Result<(), Error> {
            let start_session_args = json!({
                "session_url": TEST_SESSION_URL,
            });

            assert_matches!(facade.is_session_running().await, Ok(true));
            // start_session() will notice that there's an existing session, destroy it, then launch
            // the new session.
            assert_matches!(
                facade.start_session(start_session_args).await,
                Ok(BasemgrResult::Success)
            );
            Ok(())
        }

        test_facade(
            &start_existing_steps,
            true, // is_session_running
            1,    // SESSION_LAUNCH_CALL_COUNT
            1,    // DESTROY_CHILD_CALL_COUNT
            0,    // SESSION_RESTART_CALL_COUNT
        )
        .await
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn test_is_session_running_not_running() -> Result<(), Error> {
        async fn not_running_steps(facade: ModularFacade) -> Result<(), Error> {
            assert_matches!(facade.is_session_running().await, Ok(false));

            Ok(())
        }

        test_facade(
            &not_running_steps,
            false, // is_session_running
            0,     // SESSION_LAUNCH_CALL_COUNT
            0,     // DESTROY_CHILD_CALL_COUNT
            0,     // SESSION_RESTART_CALL_COUNT
        )
        .await
    }

    #[fuchsia_async::run(2, test)]
    async fn test_is_session_running() -> Result<(), Error> {
        async fn is_running_steps(facade: ModularFacade) -> Result<(), Error> {
            assert_matches!(facade.is_session_running().await, Ok(true));

            Ok(())
        }

        test_facade(
            &is_running_steps,
            true, // is_session_running
            0,    // SESSION_LAUNCH_CALL_COUNT
            0,    // DESTROY_CHILD_CALL_COUNT
            0,    // SESSION_RESTART_CALL_COUNT
        )
        .await
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn test_restart_session() -> Result<(), Error> {
        async fn restart_steps(facade: ModularFacade) -> Result<(), Error> {
            assert_matches!(facade.restart_session().await, Ok(RestartSessionResult::Success));

            Ok(())
        }

        test_facade(
            &restart_steps,
            true, // is_session_running
            0,    // SESSION_LAUNCH_CALL_COUNT
            0,    // DESTROY_CHILD_CALL_COUNT
            1,    // SESSION_RESTART_CALL_COUNT
        )
        .await
    }

    #[fuchsia_async::run(2, test)]
    async fn test_restart_session_does_not_destroy() -> Result<(), Error> {
        async fn restart_steps(facade: ModularFacade) -> Result<(), Error> {
            assert_matches!(facade.is_session_running().await, Ok(true));

            assert_matches!(facade.restart_session().await, Ok(RestartSessionResult::Success));

            Ok(())
        }

        test_facade(
            &restart_steps,
            true, // is_session_running
            0,    // SESSION_LAUNCH_CALL_COUNT
            0,    // DESTROY_CHILD_CALL_COUNT
            1,    // SESSION_RESTART_CALL_COUNT
        )
        .await
    }
}