component_debug/storage/
delete_all.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::{anyhow, Result};
6use fidl_fuchsia_sys2::StorageAdminProxy;
7
8/// Delete the contents of all the storage of this component.
9///
10/// # Arguments
11/// * `storage_admin`: The StorageAdminProxy
12/// * `moniker`: The moniker for the target component
13pub async fn delete_all(storage_admin: StorageAdminProxy, moniker: String) -> Result<()> {
14    storage_admin
15        .delete_component_storage(&moniker)
16        .await?
17        .map_err(|e| anyhow!("Could not delete storage contents of this component: {:?}", e))?;
18
19    println!("Deleted storage contents of component");
20    Ok(())
21}
22
23////////////////////////////////////////////////////////////////////////////////
24// tests
25
26#[cfg(test)]
27mod test {
28    use super::*;
29    use fidl::endpoints::{create_proxy_and_stream, Proxy};
30    use fidl_fuchsia_sys2 as fsys;
31    use futures::TryStreamExt;
32
33    #[fuchsia_async::run_singlethreaded(test)]
34    async fn test_delete_all() -> Result<()> {
35        let (storage_admin_proxy, mut stream) =
36            create_proxy_and_stream::<<StorageAdminProxy as Proxy>::Protocol>();
37        // Setup fake admin
38        fuchsia_async::Task::local(async move {
39            let request = stream.try_next().await;
40            if let Ok(Some(fsys::StorageAdminRequest::DeleteComponentStorage {
41                relative_moniker,
42                responder,
43                ..
44            })) = request
45            {
46                if relative_moniker == "foo" {
47                    responder.send(Ok(())).unwrap();
48                } else {
49                    panic!(
50                        "couldn't parse string as moniker for storage admin protocol: {:?}",
51                        relative_moniker
52                    );
53                }
54            } else {
55                panic!("did not get delete component storage request: {:?}", request)
56            }
57        })
58        .detach();
59        delete_all(storage_admin_proxy, "foo".to_string())
60            .await
61            .expect("delete component storage failed");
62        Ok(())
63    }
64}