snix_castore/blobservice/
from_addr.rs

1use std::sync::Arc;
2
3use url::Url;
4
5use crate::composition::{
6    CompositionContext, DeserializeWithRegistry, REG, ServiceBuilder, with_registry,
7};
8
9use super::BlobService;
10
11/// Constructs a new instance of a [BlobService] from an URI.
12///
13/// The following schemes are supported by the following services:
14/// - `memory://` ([super::MemoryBlobService])
15/// - `grpc+*://` ([super::GRPCBlobService])
16/// - `objectstore+*://` ([super::ObjectStoreBlobService])
17///
18/// See their `from_url` methods for more details about their syntax.
19pub async fn from_addr(
20    uri: &str,
21) -> Result<Arc<dyn BlobService>, Box<dyn std::error::Error + Send + Sync>> {
22    let url = Url::parse(uri)
23        .map_err(|e| crate::Error::StorageError(format!("unable to parse url: {}", e)))?;
24
25    let blob_service_config = with_registry(&REG, || {
26        <DeserializeWithRegistry<Box<dyn ServiceBuilder<Output = dyn BlobService>>>>::try_from(url)
27    })?
28    .0;
29    let blob_service = blob_service_config
30        .build("anonymous", &CompositionContext::blank(&REG))
31        .await?;
32
33    Ok(blob_service)
34}
35
36#[cfg(test)]
37mod tests {
38    use super::from_addr;
39    use rstest::rstest;
40
41    #[rstest]
42    /// This uses an unsupported scheme.
43    #[case::unsupported_scheme("http://foo.example/test", false)]
44    /// This correctly sets the scheme, and doesn't set a path.
45    #[case::memory_valid("memory://", true)]
46    /// This sets a memory url host to `foo`
47    #[case::memory_invalid_host("memory://foo", false)]
48    /// This sets a memory url path to "/", which is invalid.
49    #[case::memory_invalid_root_path("memory:///", false)]
50    /// This sets a memory url path to "/foo", which is invalid.
51    #[case::memory_invalid_root_path_foo("memory:///foo", false)]
52    /// Correct scheme to connect to a unix socket.
53    #[case::grpc_valid_unix_socket("grpc+unix:///path/to/somewhere", true)]
54    /// Correct scheme for unix socket, but setting a host too, which is invalid.
55    #[case::grpc_invalid_unix_socket_and_host("grpc+unix://host.example/path/to/somewhere", false)]
56    /// Correct scheme to connect to localhost, with port 12345
57    #[case::grpc_valid_ipv6_localhost_port_12345("grpc+http://[::1]:12345", true)]
58    /// Correct scheme to connect to localhost over http, without specifying a port.
59    #[case::grpc_valid_http_host_without_port("grpc+http://localhost", true)]
60    /// Correct scheme to connect to localhost over http, without specifying a port.
61    #[case::grpc_valid_https_host_without_port("grpc+https://localhost", true)]
62    /// Correct scheme to connect to localhost over http, but with additional path, which is invalid.
63    #[case::grpc_invalid_has_path("grpc+http://localhost/some-path", false)]
64    /// An example for object store (InMemory)
65    #[case::objectstore_valid_memory("objectstore+memory:///", true)]
66    /// An example for object store (LocalFileSystem)
67    #[case::objectstore_valid_file("objectstore+file:///foo/bar", true)]
68    // An example for object store (HTTP / WebDAV)
69    #[case::objectstore_valid_http_url("objectstore+https://localhost:8080/some-path", true)]
70    /// An example for object store (S3)
71    #[cfg_attr(
72        feature = "cloud",
73        case::objectstore_valid_s3_url("objectstore+s3://bucket/path", true)
74    )]
75    /// An example for object store (GCS)
76    #[cfg_attr(
77        feature = "cloud",
78        case::objectstore_valid_gcs_url("objectstore+gs://bucket/path", true)
79    )]
80    #[tokio::test]
81    async fn test_from_addr_tokio(#[case] uri_str: &str, #[case] exp_succeed: bool) {
82        if exp_succeed {
83            from_addr(uri_str).await.expect("should succeed");
84        } else {
85            assert!(from_addr(uri_str).await.is_err(), "should fail");
86        }
87    }
88}