Skip to content

Commit 9d13b8e

Browse files
authored
test(component-manager): table-ify the rms/nsm/psm enum-mapping tests (#2737)
1 parent 6eee006 commit 9d13b8e

6 files changed

Lines changed: 249 additions & 268 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/component-manager/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ tracing = { workspace = true }
4747
carbide-api-test-helper = { path = "../api-test-helper" }
4848
carbide-macros = { path = "../macros" }
4949
carbide-sqlx-testing = { path = "../sqlx-testing" }
50+
carbide-test-support = { path = "../test-support" }
5051
uuid = { workspace = true }
5152

5253
[build-dependencies]

crates/component-manager/src/config.rs

Lines changed: 122 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ impl BackendTlsConfig {
108108

109109
#[cfg(test)]
110110
mod tests {
111+
use carbide_test_support::{Check, check_values};
112+
111113
use super::*;
112114

113115
#[test]
@@ -118,75 +120,138 @@ mod tests {
118120
assert_eq!(cfg.compute_tray_backend, ComputeBackend::Rms);
119121
}
120122

121-
fn tls_config(
122-
cert_dir: Option<&str>,
123-
ca: Option<&str>,
124-
cert: Option<&str>,
125-
key: Option<&str>,
126-
) -> BackendTlsConfig {
123+
/// One `BackendTlsConfig` worth of path inputs for a resolver table.
124+
struct Row {
125+
cert_dir: Option<&'static str>,
126+
ca: Option<&'static str>,
127+
cert: Option<&'static str>,
128+
key: Option<&'static str>,
129+
}
130+
131+
fn tls_config(row: Row) -> BackendTlsConfig {
127132
BackendTlsConfig {
128-
cert_dir: cert_dir.map(String::from),
129-
ca_cert_path: ca.map(String::from),
130-
client_cert_path: cert.map(String::from),
131-
client_key_path: key.map(String::from),
133+
cert_dir: row.cert_dir.map(String::from),
134+
ca_cert_path: row.ca.map(String::from),
135+
client_cert_path: row.cert.map(String::from),
136+
client_key_path: row.key.map(String::from),
132137
domain: None,
133138
}
134139
}
135140

136141
#[test]
137-
fn resolve_ca_cert_explicit_path_wins() {
138-
let cfg = tls_config(Some("/dir"), Some("/explicit/ca.pem"), None, None);
139-
assert_eq!(cfg.resolve_ca_cert_path().unwrap(), "/explicit/ca.pem");
140-
}
141-
142-
#[test]
143-
fn resolve_ca_cert_falls_back_to_dir() {
144-
let cfg = tls_config(Some("/certs"), None, None, None);
145-
assert_eq!(cfg.resolve_ca_cert_path().unwrap(), "/certs/ca.crt");
146-
}
147-
148-
#[test]
149-
fn resolve_ca_cert_none_when_nothing_set() {
150-
let cfg = tls_config(None, None, None, None);
151-
assert!(cfg.resolve_ca_cert_path().is_none());
152-
}
153-
154-
#[test]
155-
fn resolve_client_cert_explicit_path_wins() {
156-
let cfg = tls_config(Some("/dir"), None, Some("/explicit/client.pem"), None);
157-
assert_eq!(
158-
cfg.resolve_client_cert_path().unwrap(),
159-
"/explicit/client.pem"
142+
fn resolve_ca_cert_path_explicit_wins_then_dir_then_none() {
143+
check_values(
144+
[
145+
Check {
146+
scenario: "explicit path wins",
147+
input: Row {
148+
cert_dir: Some("/dir"),
149+
ca: Some("/explicit/ca.pem"),
150+
cert: None,
151+
key: None,
152+
},
153+
expect: Some("/explicit/ca.pem".to_string()),
154+
},
155+
Check {
156+
scenario: "falls back to dir",
157+
input: Row {
158+
cert_dir: Some("/certs"),
159+
ca: None,
160+
cert: None,
161+
key: None,
162+
},
163+
expect: Some("/certs/ca.crt".to_string()),
164+
},
165+
Check {
166+
scenario: "none when nothing set",
167+
input: Row {
168+
cert_dir: None,
169+
ca: None,
170+
cert: None,
171+
key: None,
172+
},
173+
expect: None,
174+
},
175+
],
176+
|row| tls_config(row).resolve_ca_cert_path(),
160177
);
161178
}
162179

163180
#[test]
164-
fn resolve_client_cert_falls_back_to_dir() {
165-
let cfg = tls_config(Some("/certs"), None, None, None);
166-
assert_eq!(cfg.resolve_client_cert_path().unwrap(), "/certs/tls.crt");
167-
}
168-
169-
#[test]
170-
fn resolve_client_cert_none_when_nothing_set() {
171-
let cfg = tls_config(None, None, None, None);
172-
assert!(cfg.resolve_client_cert_path().is_none());
173-
}
174-
175-
#[test]
176-
fn resolve_client_key_explicit_path_wins() {
177-
let cfg = tls_config(Some("/dir"), None, None, Some("/explicit/key.pem"));
178-
assert_eq!(cfg.resolve_client_key_path().unwrap(), "/explicit/key.pem");
179-
}
180-
181-
#[test]
182-
fn resolve_client_key_falls_back_to_dir() {
183-
let cfg = tls_config(Some("/certs"), None, None, None);
184-
assert_eq!(cfg.resolve_client_key_path().unwrap(), "/certs/tls.key");
181+
fn resolve_client_cert_path_explicit_wins_then_dir_then_none() {
182+
check_values(
183+
[
184+
Check {
185+
scenario: "explicit path wins",
186+
input: Row {
187+
cert_dir: Some("/dir"),
188+
ca: None,
189+
cert: Some("/explicit/client.pem"),
190+
key: None,
191+
},
192+
expect: Some("/explicit/client.pem".to_string()),
193+
},
194+
Check {
195+
scenario: "falls back to dir",
196+
input: Row {
197+
cert_dir: Some("/certs"),
198+
ca: None,
199+
cert: None,
200+
key: None,
201+
},
202+
expect: Some("/certs/tls.crt".to_string()),
203+
},
204+
Check {
205+
scenario: "none when nothing set",
206+
input: Row {
207+
cert_dir: None,
208+
ca: None,
209+
cert: None,
210+
key: None,
211+
},
212+
expect: None,
213+
},
214+
],
215+
|row| tls_config(row).resolve_client_cert_path(),
216+
);
185217
}
186218

187219
#[test]
188-
fn resolve_client_key_none_when_nothing_set() {
189-
let cfg = tls_config(None, None, None, None);
190-
assert!(cfg.resolve_client_key_path().is_none());
220+
fn resolve_client_key_path_explicit_wins_then_dir_then_none() {
221+
check_values(
222+
[
223+
Check {
224+
scenario: "explicit path wins",
225+
input: Row {
226+
cert_dir: Some("/dir"),
227+
ca: None,
228+
cert: None,
229+
key: Some("/explicit/key.pem"),
230+
},
231+
expect: Some("/explicit/key.pem".to_string()),
232+
},
233+
Check {
234+
scenario: "falls back to dir",
235+
input: Row {
236+
cert_dir: Some("/certs"),
237+
ca: None,
238+
cert: None,
239+
key: None,
240+
},
241+
expect: Some("/certs/tls.key".to_string()),
242+
},
243+
Check {
244+
scenario: "none when nothing set",
245+
input: Row {
246+
cert_dir: None,
247+
ca: None,
248+
cert: None,
249+
key: None,
250+
},
251+
expect: None,
252+
},
253+
],
254+
|row| tls_config(row).resolve_client_key_path(),
255+
);
191256
}
192257
}

crates/component-manager/src/nsm.rs

Lines changed: 42 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -373,73 +373,64 @@ impl NvSwitchManager for NsmSwitchBackend {
373373
#[cfg(test)]
374374
mod tests {
375375
use carbide_secrets::credentials::Credentials;
376+
use carbide_test_support::value_scenarios;
376377

377378
use super::*;
378379

379380
#[test]
380-
fn nsm_state_queued() {
381-
assert_eq!(
382-
map_nsm_update_state(nsm::UpdateState::Queued as i32),
383-
FirmwareState::Queued,
384-
);
385-
}
381+
fn nsm_update_state_maps_each_variant() {
382+
value_scenarios!(run = |state: nsm::UpdateState| map_nsm_update_state(state as i32);
383+
"queued" {
384+
nsm::UpdateState::Queued => FirmwareState::Queued,
385+
}
386386

387-
#[test]
388-
fn nsm_state_in_progress_variants() {
389-
for state in [
390-
nsm::UpdateState::Copy,
391-
nsm::UpdateState::Upload,
392-
nsm::UpdateState::Install,
393-
nsm::UpdateState::PollCompletion,
394-
nsm::UpdateState::PowerCycle,
395-
nsm::UpdateState::WaitReachable,
396-
] {
397-
assert_eq!(
398-
map_nsm_update_state(state as i32),
399-
FirmwareState::InProgress,
400-
"expected InProgress for {state:?}",
401-
);
402-
}
403-
}
387+
"in progress" {
388+
nsm::UpdateState::Copy => FirmwareState::InProgress,
389+
nsm::UpdateState::Upload => FirmwareState::InProgress,
390+
nsm::UpdateState::Install => FirmwareState::InProgress,
391+
nsm::UpdateState::PollCompletion => FirmwareState::InProgress,
392+
nsm::UpdateState::PowerCycle => FirmwareState::InProgress,
393+
nsm::UpdateState::WaitReachable => FirmwareState::InProgress,
394+
}
404395

405-
#[test]
406-
fn nsm_state_verifying_variants() {
407-
for state in [nsm::UpdateState::Verify, nsm::UpdateState::Cleanup] {
408-
assert_eq!(
409-
map_nsm_update_state(state as i32),
410-
FirmwareState::Verifying,
411-
"expected Verifying for {state:?}",
412-
);
413-
}
414-
}
396+
"verifying" {
397+
nsm::UpdateState::Verify => FirmwareState::Verifying,
398+
nsm::UpdateState::Cleanup => FirmwareState::Verifying,
399+
}
415400

416-
#[test]
417-
fn nsm_state_completed() {
418-
assert_eq!(
419-
map_nsm_update_state(nsm::UpdateState::Completed as i32),
420-
FirmwareState::Completed,
421-
);
422-
}
401+
"completed" {
402+
nsm::UpdateState::Completed => FirmwareState::Completed,
403+
}
423404

424-
#[test]
425-
fn nsm_state_failed() {
426-
assert_eq!(
427-
map_nsm_update_state(nsm::UpdateState::Failed as i32),
428-
FirmwareState::Failed,
405+
"failed" {
406+
nsm::UpdateState::Failed => FirmwareState::Failed,
407+
}
408+
409+
"cancelled" {
410+
nsm::UpdateState::Cancelled => FirmwareState::Cancelled,
411+
}
429412
);
430413
}
431414

432415
#[test]
433-
fn nsm_state_cancelled() {
434-
assert_eq!(
435-
map_nsm_update_state(nsm::UpdateState::Cancelled as i32),
436-
FirmwareState::Cancelled,
416+
fn nsm_update_state_unknown_for_unrecognized_value() {
417+
value_scenarios!(map_nsm_update_state:
418+
"unrecognized" {
419+
9999 => FirmwareState::Unknown,
420+
}
437421
);
438422
}
439423

440424
#[test]
441-
fn nsm_state_unknown_for_unrecognized_value() {
442-
assert_eq!(map_nsm_update_state(9999), FirmwareState::Unknown);
425+
fn to_nsm_component_maps_each_variant() {
426+
value_scenarios!(run = |component: NvSwitchComponent| to_nsm_component(&component);
427+
"components" {
428+
NvSwitchComponent::Bmc => nsm::NvSwitchComponent::NvswitchComponentBmc,
429+
NvSwitchComponent::Cpld => nsm::NvSwitchComponent::NvswitchComponentCpld,
430+
NvSwitchComponent::Bios => nsm::NvSwitchComponent::NvswitchComponentBios,
431+
NvSwitchComponent::Nvos => nsm::NvSwitchComponent::NvswitchComponentNvos,
432+
}
433+
);
443434
}
444435

445436
#[test]
@@ -475,24 +466,4 @@ mod tests {
475466
assert_eq!(nvos_creds.username, "nvadmin");
476467
assert_eq!(nvos_creds.password, "nvos_pass");
477468
}
478-
479-
#[test]
480-
fn to_nsm_component_all_variants() {
481-
assert_eq!(
482-
to_nsm_component(&NvSwitchComponent::Bmc),
483-
nsm::NvSwitchComponent::NvswitchComponentBmc
484-
);
485-
assert_eq!(
486-
to_nsm_component(&NvSwitchComponent::Cpld),
487-
nsm::NvSwitchComponent::NvswitchComponentCpld
488-
);
489-
assert_eq!(
490-
to_nsm_component(&NvSwitchComponent::Bios),
491-
nsm::NvSwitchComponent::NvswitchComponentBios
492-
);
493-
assert_eq!(
494-
to_nsm_component(&NvSwitchComponent::Nvos),
495-
nsm::NvSwitchComponent::NvswitchComponentNvos
496-
);
497-
}
498469
}

0 commit comments

Comments
 (0)