Skip to content

Commit e1f5889

Browse files
authored
minor: refactor faas tests to validate metadata (#1228)
1 parent 29ba150 commit e1f5889

File tree

3 files changed

+153
-63
lines changed

3 files changed

+153
-63
lines changed

src/cmap/establish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub(super) mod handshake;
1+
pub(crate) mod handshake;
22

33
use std::time::Duration;
44

src/cmap/establish/handshake.rs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,43 @@ const RUNTIME_NAME: &str = "tokio";
2626
#[cfg(feature = "sync")]
2727
const RUNTIME_NAME: &str = "sync (with tokio)";
2828

29-
#[derive(Clone, Debug)]
30-
struct ClientMetadata {
31-
application: Option<AppMetadata>,
32-
driver: DriverMetadata,
33-
os: OsMetadata,
34-
platform: String,
35-
env: Option<RuntimeEnvironment>,
29+
#[derive(Clone, Debug, PartialEq)]
30+
pub(crate) struct ClientMetadata {
31+
pub(crate) application: Option<AppMetadata>,
32+
pub(crate) driver: DriverMetadata,
33+
pub(crate) os: OsMetadata,
34+
pub(crate) platform: String,
35+
pub(crate) env: Option<RuntimeEnvironment>,
3636
}
3737

38-
#[derive(Clone, Debug)]
39-
struct AppMetadata {
40-
name: String,
38+
#[derive(Clone, Debug, PartialEq)]
39+
pub(crate) struct AppMetadata {
40+
pub(crate) name: String,
4141
}
4242

43-
#[derive(Clone, Debug)]
44-
struct DriverMetadata {
45-
name: String,
46-
version: String,
43+
#[derive(Clone, Debug, PartialEq)]
44+
pub(crate) struct DriverMetadata {
45+
pub(crate) name: String,
46+
pub(crate) version: String,
4747
}
4848

49-
#[derive(Clone, Debug)]
50-
struct OsMetadata {
51-
os_type: String,
52-
name: Option<String>,
53-
architecture: Option<String>,
54-
version: Option<String>,
49+
#[derive(Clone, Debug, PartialEq)]
50+
pub(crate) struct OsMetadata {
51+
pub(crate) os_type: String,
52+
pub(crate) name: Option<String>,
53+
pub(crate) architecture: Option<String>,
54+
pub(crate) version: Option<String>,
5555
}
5656

5757
#[derive(Clone, Debug, PartialEq)]
58-
struct RuntimeEnvironment {
59-
name: Option<FaasEnvironmentName>,
60-
runtime: Option<String>,
61-
timeout_sec: Option<i32>,
62-
memory_mb: Option<i32>,
63-
region: Option<String>,
64-
url: Option<String>,
65-
container: Option<RawDocumentBuf>,
58+
pub(crate) struct RuntimeEnvironment {
59+
pub(crate) name: Option<FaasEnvironmentName>,
60+
pub(crate) runtime: Option<String>,
61+
pub(crate) timeout_sec: Option<i32>,
62+
pub(crate) memory_mb: Option<i32>,
63+
pub(crate) region: Option<String>,
64+
pub(crate) url: Option<String>,
65+
pub(crate) container: Option<RawDocumentBuf>,
6666
}
6767

6868
#[derive(Copy, Clone, Debug, PartialEq)]
@@ -158,7 +158,7 @@ impl From<&RuntimeEnvironment> for RawBson {
158158
}
159159

160160
impl RuntimeEnvironment {
161-
const UNSET: Self = RuntimeEnvironment {
161+
pub(crate) const UNSET: Self = RuntimeEnvironment {
162162
name: None,
163163
runtime: None,
164164
timeout_sec: None,
@@ -262,7 +262,7 @@ impl FaasEnvironmentName {
262262
/// Contains the basic handshake information that can be statically determined. This document
263263
/// (potentially with additional fields added) can be cloned and put in the `client` field of
264264
/// the `hello` or legacy hello command.
265-
static BASE_CLIENT_METADATA: Lazy<ClientMetadata> = Lazy::new(|| ClientMetadata {
265+
pub(crate) static BASE_CLIENT_METADATA: Lazy<ClientMetadata> = Lazy::new(|| ClientMetadata {
266266
application: None,
267267
driver: DriverMetadata {
268268
name: "mongo-rust-driver".into(),
@@ -335,6 +335,10 @@ pub(crate) struct Handshaker {
335335
http_client: crate::runtime::HttpClient,
336336
}
337337

338+
#[cfg(test)]
339+
#[allow(clippy::incompatible_msrv)]
340+
pub(crate) static TEST_METADATA: std::sync::OnceLock<ClientMetadata> = std::sync::OnceLock::new();
341+
338342
impl Handshaker {
339343
/// Creates a new Handshaker.
340344
pub(crate) fn new(options: HandshakerOptions) -> Self {
@@ -427,6 +431,9 @@ impl Handshaker {
427431
trunc_fn(&mut metadata);
428432
meta_doc = (&metadata).into();
429433
}
434+
#[cfg(test)]
435+
#[allow(clippy::incompatible_msrv)]
436+
let _ = TEST_METADATA.set(metadata);
430437
body.append("client", meta_doc);
431438

432439
Ok((command, client_first))

src/test/spec/faas.rs

Lines changed: 115 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
use std::env;
22

3-
use crate::Client;
3+
use crate::{
4+
cmap::establish::handshake::{
5+
ClientMetadata,
6+
FaasEnvironmentName,
7+
RuntimeEnvironment,
8+
BASE_CLIENT_METADATA,
9+
},
10+
Client,
11+
};
412

513
type Result<T> = anyhow::Result<T>;
614

@@ -33,79 +41,154 @@ impl Drop for TempVars {
3341
}
3442
}
3543

36-
async fn check_faas_handshake(vars: &[(&'static str, &str)]) -> Result<()> {
44+
async fn check_faas_handshake(
45+
vars: &[(&'static str, &str)],
46+
expected: &ClientMetadata,
47+
) -> Result<()> {
3748
let _tv = TempVars::set(vars);
3849

3950
let client = Client::for_test().await;
4051
client.list_database_names().await?;
52+
#[allow(clippy::incompatible_msrv)]
53+
let metadata = crate::cmap::establish::handshake::TEST_METADATA
54+
.get()
55+
.unwrap();
56+
assert_eq!(expected, metadata);
4157

4258
Ok(())
4359
}
4460

4561
#[tokio::test]
4662
async fn valid_aws() -> Result<()> {
47-
check_faas_handshake(&[
48-
("AWS_EXECUTION_ENV", "AWS_Lambda_java8"),
49-
("AWS_REGION", "us-east-2"),
50-
("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "1024"),
51-
])
63+
check_faas_handshake(
64+
&[
65+
("AWS_EXECUTION_ENV", "AWS_Lambda_java8"),
66+
("AWS_REGION", "us-east-2"),
67+
("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "1024"),
68+
],
69+
&ClientMetadata {
70+
env: Some(RuntimeEnvironment {
71+
name: Some(FaasEnvironmentName::AwsLambda),
72+
runtime: Some("AWS_Lambda_java8".to_string()),
73+
memory_mb: Some(1024),
74+
region: Some("us-east-2".to_string()),
75+
..RuntimeEnvironment::UNSET
76+
}),
77+
..BASE_CLIENT_METADATA.clone()
78+
},
79+
)
5280
.await
5381
}
5482

5583
#[tokio::test]
5684
async fn valid_azure() -> Result<()> {
57-
check_faas_handshake(&[("FUNCTIONS_WORKER_RUNTIME", "node")]).await
85+
check_faas_handshake(
86+
&[("FUNCTIONS_WORKER_RUNTIME", "node")],
87+
&ClientMetadata {
88+
env: Some(RuntimeEnvironment {
89+
name: Some(FaasEnvironmentName::AzureFunc),
90+
runtime: Some("node".to_string()),
91+
..RuntimeEnvironment::UNSET
92+
}),
93+
..BASE_CLIENT_METADATA.clone()
94+
},
95+
)
96+
.await
5897
}
5998

6099
#[tokio::test]
61100
async fn valid_gcp() -> Result<()> {
62-
check_faas_handshake(&[
63-
("K_SERVICE", "servicename"),
64-
("FUNCTION_MEMORY_MB", "1024"),
65-
("FUNCTION_TIMEOUT_SEC", "60"),
66-
("FUNCTION_REGION", "us-central1"),
67-
])
101+
check_faas_handshake(
102+
&[
103+
("K_SERVICE", "servicename"),
104+
("FUNCTION_MEMORY_MB", "1024"),
105+
("FUNCTION_TIMEOUT_SEC", "60"),
106+
("FUNCTION_REGION", "us-central1"),
107+
],
108+
&ClientMetadata {
109+
env: Some(RuntimeEnvironment {
110+
name: Some(FaasEnvironmentName::GcpFunc),
111+
memory_mb: Some(1024),
112+
timeout_sec: Some(60),
113+
region: Some("us-central1".to_string()),
114+
..RuntimeEnvironment::UNSET
115+
}),
116+
..BASE_CLIENT_METADATA.clone()
117+
},
118+
)
68119
.await
69120
}
70121

71122
#[tokio::test]
72123
async fn valid_vercel() -> Result<()> {
73-
check_faas_handshake(&[
74-
("VERCEL", "1"),
75-
("VERCEL_URL", "*.vercel.app"),
76-
("VERCEL_REGION", "cdg1"),
77-
])
124+
check_faas_handshake(
125+
&[
126+
("VERCEL", "1"),
127+
("VERCEL_URL", "*.vercel.app"),
128+
("VERCEL_REGION", "cdg1"),
129+
],
130+
&ClientMetadata {
131+
env: Some(RuntimeEnvironment {
132+
name: Some(FaasEnvironmentName::Vercel),
133+
region: Some("cdg1".to_string()),
134+
..RuntimeEnvironment::UNSET
135+
}),
136+
..BASE_CLIENT_METADATA.clone()
137+
},
138+
)
78139
.await
79140
}
80141

81142
#[tokio::test]
82143
async fn invalid_multiple_providers() -> Result<()> {
83-
check_faas_handshake(&[
84-
("AWS_EXECUTION_ENV", "AWS_Lambda_java8"),
85-
("FUNCTIONS_WORKER_RUNTIME", "node"),
86-
])
144+
check_faas_handshake(
145+
&[
146+
("AWS_EXECUTION_ENV", "AWS_Lambda_java8"),
147+
("FUNCTIONS_WORKER_RUNTIME", "node"),
148+
],
149+
&BASE_CLIENT_METADATA,
150+
)
87151
.await
88152
}
89153

90154
#[tokio::test]
91155
async fn invalid_long_string() -> Result<()> {
92-
check_faas_handshake(&[
93-
("AWS_EXECUTION_ENV", "AWS_Lambda_java8"),
94-
("AWS_REGION", &"a".repeat(512)),
95-
])
156+
check_faas_handshake(
157+
&[
158+
("AWS_EXECUTION_ENV", "AWS_Lambda_java8"),
159+
("AWS_REGION", &"a".repeat(512)),
160+
],
161+
&ClientMetadata {
162+
env: Some(RuntimeEnvironment {
163+
name: Some(FaasEnvironmentName::AwsLambda),
164+
..RuntimeEnvironment::UNSET
165+
}),
166+
..BASE_CLIENT_METADATA.clone()
167+
},
168+
)
96169
.await
97170
}
98171

99172
#[tokio::test]
100173
async fn invalid_wrong_type() -> Result<()> {
101-
check_faas_handshake(&[
102-
("AWS_EXECUTION_ENV", "AWS_Lambda_java8"),
103-
("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "big"),
104-
])
174+
check_faas_handshake(
175+
&[
176+
("AWS_EXECUTION_ENV", "AWS_Lambda_java8"),
177+
("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "big"),
178+
],
179+
&ClientMetadata {
180+
env: Some(RuntimeEnvironment {
181+
name: Some(FaasEnvironmentName::AwsLambda),
182+
runtime: Some("AWS_Lambda_java8".to_string()),
183+
..RuntimeEnvironment::UNSET
184+
}),
185+
..BASE_CLIENT_METADATA.clone()
186+
},
187+
)
105188
.await
106189
}
107190

108191
#[tokio::test]
109192
async fn invalid_aws_not_lambda() -> Result<()> {
110-
check_faas_handshake(&[("AWS_EXECUTION_ENV", "EC2")]).await
193+
check_faas_handshake(&[("AWS_EXECUTION_ENV", "EC2")], &BASE_CLIENT_METADATA).await
111194
}

0 commit comments

Comments
 (0)