Skip to content

Commit c430fa9

Browse files
authored
replace test_setup* with a builder (#9446)
1 parent ebcb876 commit c430fa9

File tree

14 files changed

+190
-191
lines changed

14 files changed

+190
-191
lines changed

nexus/benches/setup_benchmark.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use omicron_test_utils::dev;
1212
// This is the default wrapper around most Nexus integration tests.
1313
// Benchmark how long an "empty test" would take.
1414
async fn do_full_setup() {
15-
let ctx =
16-
nexus_test_utils::test_setup::<omicron_nexus::Server>("full_setup", 0)
17-
.await;
15+
let ctx = nexus_test_utils::ControlPlaneBuilder::new("full_setup")
16+
.start::<omicron_nexus::Server>()
17+
.await;
1818

1919
ctx.teardown().await;
2020
}

nexus/src/app/sagas/instance_start.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,10 +1015,11 @@ mod test {
10151015

10161016
#[tokio::test]
10171017
async fn should_start_with_dead_switch() {
1018-
let cptestctx = nexus_test_utils::test_setup::<crate::Server>(
1018+
let cptestctx = nexus_test_utils::ControlPlaneBuilder::new(
10191019
"should_start_with_dead_switch",
1020-
3,
10211020
)
1021+
.with_extra_sled_agents(3)
1022+
.start::<crate::Server>()
10221023
.await;
10231024

10241025
let client = &cptestctx.external_client;

nexus/test-utils-macros/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ pub fn nexus_test(attrs: TokenStream, input: TokenStream) -> TokenStream {
126126
{
127127
#input_func
128128

129-
let ctx = ::nexus_test_utils::test_setup::<#which_nexus>(
129+
let ctx = ::nexus_test_utils::ControlPlaneBuilder::new(
130130
#func_ident_string,
131-
#extra_sled_agents,
132-
).await;
131+
)
132+
.with_extra_sled_agents(#extra_sled_agents)
133+
.start::<#which_nexus>()
134+
.await;
133135
#func_ident(&ctx).await;
134136
ctx.teardown().await;
135137
}

nexus/test-utils/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ pub mod resource_helpers;
2525
pub mod sql;
2626
mod starter;
2727

28+
pub use nexus_test::ControlPlaneBuilder;
2829
pub use nexus_test::ControlPlaneTestContext;
2930
pub use nexus_test::load_test_config;
3031
#[cfg(feature = "omicron-dev")]
3132
pub use nexus_test::omicron_dev_setup_with_config;
32-
pub use nexus_test::test_setup;
33-
pub use nexus_test::test_setup_with_config;
3433
pub use starter::ControlPlaneStarter;
3534
pub use starter::ControlPlaneTestContextSledAgent;
3635
pub use starter::register_test_producer;

nexus/test-utils/src/nexus_test.rs

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,64 @@ use std::collections::HashMap;
3838
use std::sync::{Arc, RwLock};
3939
use std::time::Duration;
4040

41+
pub struct ControlPlaneBuilder<'a> {
42+
// required
43+
test_name: &'a str,
44+
45+
// defaults provided by the builder
46+
nextra_sled_agents: u16,
47+
tls_cert: Option<Certificate>,
48+
nexus_config: NexusConfig,
49+
}
50+
51+
impl<'a> ControlPlaneBuilder<'a> {
52+
pub fn new(test_name: &'a str) -> Self {
53+
ControlPlaneBuilder {
54+
test_name,
55+
nextra_sled_agents: 0,
56+
tls_cert: None,
57+
nexus_config: load_test_config(),
58+
}
59+
}
60+
61+
pub fn with_extra_sled_agents(mut self, nextra: u16) -> Self {
62+
self.nextra_sled_agents = nextra;
63+
self
64+
}
65+
66+
pub fn with_tls_cert(mut self, tls_cert: Option<Certificate>) -> Self {
67+
self.tls_cert = tls_cert;
68+
self
69+
}
70+
71+
pub fn customize_nexus_config(
72+
mut self,
73+
f: &dyn Fn(&mut NexusConfig) -> (),
74+
) -> Self {
75+
f(&mut self.nexus_config);
76+
self
77+
}
78+
79+
pub async fn start<N: NexusServer>(self) -> ControlPlaneTestContext<N> {
80+
let mut nexus_config = self.nexus_config;
81+
let starter =
82+
ControlPlaneStarter::<N>::new(self.test_name, &mut nexus_config);
83+
setup_with_config_impl(
84+
starter,
85+
PopulateCrdb::FromEnvironmentSeed,
86+
sim::SimMode::Explicit,
87+
self.tls_cert,
88+
self.nextra_sled_agents,
89+
DEFAULT_SP_SIM_CONFIG.into(),
90+
false,
91+
)
92+
.await
93+
}
94+
}
95+
96+
/// Helper for setting up the control plane for testing and accessing its parts
97+
///
98+
/// See [`ControlPlaneBuilder`] for setting one up.
4199
pub struct ControlPlaneTestContext<N> {
42100
pub start_time: chrono::DateTime<chrono::Utc>,
43101
pub external_client: ClientTestContext,
@@ -212,45 +270,7 @@ pub fn load_test_config() -> NexusConfig {
212270
.expect("failed to load config.test.toml")
213271
}
214272

215-
pub async fn test_setup<N: NexusServer>(
216-
test_name: &str,
217-
extra_sled_agents: u16,
218-
) -> ControlPlaneTestContext<N> {
219-
let mut config = load_test_config();
220-
test_setup_with_config::<N>(
221-
test_name,
222-
&mut config,
223-
sim::SimMode::Explicit,
224-
None,
225-
extra_sled_agents,
226-
DEFAULT_SP_SIM_CONFIG.into(),
227-
)
228-
.await
229-
}
230-
231-
/// Setup routine to use for tests.
232-
pub async fn test_setup_with_config<N: NexusServer>(
233-
test_name: &str,
234-
config: &mut NexusConfig,
235-
sim_mode: sim::SimMode,
236-
initial_cert: Option<Certificate>,
237-
extra_sled_agents: u16,
238-
gateway_config_file: Utf8PathBuf,
239-
) -> ControlPlaneTestContext<N> {
240-
let starter = ControlPlaneStarter::<N>::new(test_name, config);
241-
setup_with_config_impl(
242-
starter,
243-
PopulateCrdb::FromEnvironmentSeed,
244-
sim_mode,
245-
initial_cert,
246-
extra_sled_agents,
247-
gateway_config_file,
248-
false,
249-
)
250-
.await
251-
}
252-
253-
/// Setup routine to use for `omicron-dev`. Use [`test_setup_with_config`] for
273+
/// Setup routine to use for `omicron-dev`. Use [`ControlPlaneBuilder`] for
254274
/// tests.
255275
///
256276
/// The main difference from tests is that this routine ensures the seed tarball

nexus/tests/integration_tests/certificates.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ use display_error_chain::ErrorChainExt;
88
use dropshot::HttpErrorResponseBody;
99
use dropshot::test_util::ClientTestContext;
1010
use futures::TryStreamExt;
11-
use gateway_test_utils::setup::DEFAULT_SP_SIM_CONFIG;
1211
use http::StatusCode;
1312
use http::method::Method;
1413
use internal_dns_types::names::DNS_ZONE_EXTERNAL_TESTING;
1514
use nexus_test_utils::http_testing::AuthnMode;
1615
use nexus_test_utils::http_testing::NexusRequest;
17-
use nexus_test_utils::load_test_config;
1816
use nexus_test_utils::resource_helpers::create_certificate;
1917
use nexus_test_utils::resource_helpers::delete_certificate;
2018
use nexus_test_utils_macros::nexus_test;
@@ -345,19 +343,14 @@ async fn test_silo_certificates() {
345343
let silo3 = SiloCert::new("silo3".parse().unwrap());
346344

347345
// Start Nexus with a TLS server instead of its usual HTTP server.
348-
let cptestctx = {
349-
let mut config = load_test_config();
350-
config.deployment.dropshot_external.tls = true;
351-
nexus_test_utils::test_setup_with_config::<omicron_nexus::Server>(
352-
"test_silo_certificates",
353-
&mut config,
354-
omicron_sled_agent::sim::SimMode::Explicit,
355-
Some(silo1.cert.clone()),
356-
0,
357-
DEFAULT_SP_SIM_CONFIG.into(),
358-
)
359-
.await
360-
};
346+
let cptestctx =
347+
nexus_test_utils::ControlPlaneBuilder::new("test_silo_certificates")
348+
.customize_nexus_config(&|config| {
349+
config.deployment.dropshot_external.tls = true;
350+
})
351+
.with_tls_cert(Some(silo1.cert.clone()))
352+
.start::<omicron_nexus::Server>()
353+
.await;
361354

362355
let nexus_port = cptestctx.external_client.bind_address.port();
363356

nexus/tests/integration_tests/console_api.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use anyhow::Context;
66
use camino::Utf8PathBuf;
77
use dropshot::ResultsPage;
88
use dropshot::test_util::ClientTestContext;
9-
use gateway_test_utils::setup::DEFAULT_SP_SIM_CONFIG;
109
use http::{StatusCode, header, method::Method};
1110
use nexus_auth::context::OpContext;
1211
use std::env::current_dir;
@@ -22,15 +21,13 @@ use nexus_test_utils::resource_helpers::create_console_session;
2221
use nexus_test_utils::resource_helpers::{
2322
create_silo, grant_iam, object_create,
2423
};
25-
use nexus_test_utils::{load_test_config, test_setup_with_config};
2624
use nexus_test_utils_macros::nexus_test;
2725
use nexus_types::external_api::params::{self, ProjectCreate};
2826
use nexus_types::external_api::shared::{
2927
FleetRole, SiloIdentityMode, SiloRole,
3028
};
3129
use nexus_types::external_api::{shared, views};
3230
use omicron_common::api::external::{Error, IdentityMetadataCreateParams};
33-
use omicron_sled_agent::sim;
3431
use omicron_test_utils::dev::poll::{CondCheckError, wait_for_condition};
3532

3633
type ControlPlaneTestContext =
@@ -387,20 +384,16 @@ async fn test_assets(cptestctx: &ControlPlaneTestContext) {
387384

388385
#[tokio::test]
389386
async fn test_absolute_static_dir() {
390-
let mut config = load_test_config();
391-
config.pkg.console.static_dir =
392-
Utf8PathBuf::try_from(current_dir().unwrap())
393-
.unwrap()
394-
.join("tests/static");
395-
let cptestctx = test_setup_with_config::<omicron_nexus::Server>(
396-
"test_absolute_static_dir",
397-
&mut config,
398-
sim::SimMode::Explicit,
399-
None,
400-
0,
401-
DEFAULT_SP_SIM_CONFIG.into(),
402-
)
403-
.await;
387+
let cptestctx =
388+
nexus_test_utils::ControlPlaneBuilder::new("test_absolute_static_dir")
389+
.customize_nexus_config(&|config| {
390+
config.pkg.console.static_dir =
391+
Utf8PathBuf::try_from(current_dir().unwrap())
392+
.unwrap()
393+
.join("tests/static");
394+
})
395+
.start::<omicron_nexus::Server>()
396+
.await;
404397
let testctx = &cptestctx.external_client;
405398

406399
// existing file is returned
@@ -941,17 +934,14 @@ async fn expect_redirect(testctx: &ClientTestContext, from: &str, to: &str) {
941934
/// the session was found but is expired. vs not found at all
942935
#[tokio::test]
943936
async fn test_session_idle_timeout_deletes_session() {
944-
// set idle timeout to 0 so we can test expiration
945-
let mut config = load_test_config();
946-
config.pkg.console.session_idle_timeout_minutes = 0;
947-
let cptestctx = test_setup_with_config::<omicron_nexus::Server>(
937+
let cptestctx = nexus_test_utils::ControlPlaneBuilder::new(
948938
"test_session_idle_timeout_deletes_session",
949-
&mut config,
950-
sim::SimMode::Explicit,
951-
None,
952-
0,
953-
DEFAULT_SP_SIM_CONFIG.into(),
954939
)
940+
.customize_nexus_config(&|config| {
941+
// set idle timeout to 0 so we can test expiration
942+
config.pkg.console.session_idle_timeout_minutes = 0;
943+
})
944+
.start::<omicron_nexus::Server>()
955945
.await;
956946
let testctx = &cptestctx.external_client;
957947

nexus/tests/integration_tests/device_auth.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::num::NonZeroU32;
77
use chrono::Utc;
88
use dropshot::test_util::ClientTestContext;
99
use dropshot::{HttpErrorResponseBody, ResultsPage};
10-
use gateway_test_utils::setup::DEFAULT_SP_SIM_CONFIG;
1110
use nexus_auth::authn::USER_TEST_UNPRIVILEGED;
1211
use nexus_config::NexusConfig;
1312
use nexus_db_queries::db::fixed_data::silo::DEFAULT_SILO;
@@ -21,7 +20,6 @@ use nexus_test_utils::{
2120
http_testing::{AuthnMode, NexusRequest, RequestBuilder},
2221
resource_helpers::grant_iam,
2322
};
24-
use nexus_test_utils::{load_test_config, test_setup_with_config};
2523
use nexus_test_utils_macros::nexus_test;
2624
use nexus_types::external_api::{params, views};
2725
use nexus_types::external_api::{
@@ -33,7 +31,6 @@ use nexus_types::external_api::{
3331
use omicron_uuid_kinds::SiloUserUuid;
3432

3533
use http::{StatusCode, header, method::Method};
36-
use omicron_sled_agent::sim;
3734
use oxide_client::types::{FleetRole, SiloRole};
3835
use serde::Deserialize;
3936
use tokio::time::{Duration, sleep};
@@ -994,34 +991,38 @@ async fn test_admin_logout_deletes_tokens_and_sessions(
994991
#[tokio::test]
995992
async fn test_session_list_excludes_expired() {
996993
// Test with default TTL - session should not be expired
997-
let mut config = load_test_config();
998-
test_session_list_with_config(&mut config, 1).await;
994+
test_session_list_with_config(&|_config| (), 1).await;
999995

1000996
// Test with idle TTL = 0 - session should be expired immediately
1001-
let mut config = load_test_config();
1002-
config.pkg.console.session_idle_timeout_minutes = 0;
1003-
test_session_list_with_config(&mut config, 0).await;
997+
test_session_list_with_config(
998+
&|config| {
999+
config.pkg.console.session_idle_timeout_minutes = 0;
1000+
},
1001+
0,
1002+
)
1003+
.await;
10041004

10051005
// Test with abs TTL = 0 - session should be expired immediately
1006-
let mut config = load_test_config();
1007-
config.pkg.console.session_absolute_timeout_minutes = 0;
1008-
test_session_list_with_config(&mut config, 0).await;
1006+
test_session_list_with_config(
1007+
&|config| {
1008+
config.pkg.console.session_absolute_timeout_minutes = 0;
1009+
},
1010+
0,
1011+
)
1012+
.await;
10091013
}
10101014

10111015
/// Set up a test context with the given config, create a user in the test suite
10121016
/// silo, and create a session, and assert about the length of the session list
10131017
async fn test_session_list_with_config(
1014-
config: &mut NexusConfig,
1018+
modify_config: &dyn Fn(&mut NexusConfig) -> (),
10151019
expected_sessions: usize,
10161020
) {
1017-
let cptestctx = test_setup_with_config::<omicron_nexus::Server>(
1021+
let cptestctx = nexus_test_utils::ControlPlaneBuilder::new(
10181022
"test_session_list_excludes_expired",
1019-
config,
1020-
sim::SimMode::Explicit,
1021-
None,
1022-
0,
1023-
DEFAULT_SP_SIM_CONFIG.into(),
10241023
)
1024+
.customize_nexus_config(modify_config)
1025+
.start::<omicron_nexus::Server>()
10251026
.await;
10261027
let testctx = &cptestctx.external_client;
10271028

0 commit comments

Comments
 (0)