Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions nexus/benches/setup_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use omicron_test_utils::dev;
// This is the default wrapper around most Nexus integration tests.
// Benchmark how long an "empty test" would take.
async fn do_full_setup() {
let ctx =
nexus_test_utils::test_setup::<omicron_nexus::Server>("full_setup", 0)
.await;
let ctx = nexus_test_utils::ControlPlaneBuilder::new("full_setup")
.start::<omicron_nexus::Server>()
.await;

ctx.teardown().await;
}
Expand Down
5 changes: 3 additions & 2 deletions nexus/src/app/sagas/instance_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,10 +1015,11 @@ mod test {

#[tokio::test]
async fn should_start_with_dead_switch() {
let cptestctx = nexus_test_utils::test_setup::<crate::Server>(
let cptestctx = nexus_test_utils::ControlPlaneBuilder::new(
"should_start_with_dead_switch",
3,
)
.extra_sled_agents(3)
.start::<crate::Server>()
.await;

let client = &cptestctx.external_client;
Expand Down
8 changes: 5 additions & 3 deletions nexus/test-utils-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ pub fn nexus_test(attrs: TokenStream, input: TokenStream) -> TokenStream {
{
#input_func

let ctx = ::nexus_test_utils::test_setup::<#which_nexus>(
let ctx = ::nexus_test_utils::ControlPlaneBuilder::new(
#func_ident_string,
#extra_sled_agents,
).await;
)
.extra_sled_agents(#extra_sled_agents)
.start::<#which_nexus>()
.await;
#func_ident(&ctx).await;
ctx.teardown().await;
}
Expand Down
3 changes: 1 addition & 2 deletions nexus/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ pub mod resource_helpers;
pub mod sql;
mod starter;

pub use nexus_test::ControlPlaneBuilder;
pub use nexus_test::ControlPlaneTestContext;
pub use nexus_test::load_test_config;
#[cfg(feature = "omicron-dev")]
pub use nexus_test::omicron_dev_setup_with_config;
pub use nexus_test::test_setup;
pub use nexus_test::test_setup_with_config;
pub use starter::ControlPlaneStarter;
pub use starter::ControlPlaneTestContextSledAgent;
pub use starter::register_test_producer;
Expand Down
134 changes: 95 additions & 39 deletions nexus/test-utils/src/nexus_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,100 @@ use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::Duration;

pub struct ControlPlaneBuilder<'a> {
// required
test_name: &'a str,

// fields with defaults that are cheap to construct
nextra_sled_agents: u16,
sim_mode: sim::SimMode,
tls_cert: Option<Certificate>,
gateway_config_file: Utf8PathBuf,

// fields with defaults that need to be constructed lazily
nexus_config: Option<NexusConfig>,
}

impl<'a> ControlPlaneBuilder<'a> {
pub fn new(test_name: &'a str) -> ControlPlaneBuilder<'a> {
ControlPlaneBuilder {
test_name,
nextra_sled_agents: 0,
sim_mode: sim::SimMode::Explicit,
tls_cert: None,
gateway_config_file: DEFAULT_SP_SIM_CONFIG.into(),
nexus_config: None,
}
}

pub fn extra_sled_agents(mut self, nextra: u16) -> ControlPlaneBuilder<'a> {
self.nextra_sled_agents = nextra;
self
}

pub fn sim_mode(
mut self,
sim_mode: sim::SimMode,
) -> ControlPlaneBuilder<'a> {
self.sim_mode = sim_mode;
self
}

pub fn tls_cert(
mut self,
tls_cert: Option<Certificate>,
) -> ControlPlaneBuilder<'a> {
self.tls_cert = tls_cert;
self
}

pub fn mgs_config_file(
mut self,
path: Utf8PathBuf,
) -> ControlPlaneBuilder<'a> {
self.gateway_config_file = path;
self
}

pub fn nexus_config(
mut self,
config: NexusConfig,
) -> ControlPlaneBuilder<'a> {
self.nexus_config = Some(config);
self
}

pub fn with_modified_default_config(
mut self,
f: &dyn Fn(&mut NexusConfig) -> (),
) -> ControlPlaneBuilder<'a> {
let mut config = load_test_config();
f(&mut config);
self.nexus_config = Some(config);
self
}

pub async fn start<N: NexusServer>(self) -> ControlPlaneTestContext<N> {
let mut nexus_config =
self.nexus_config.unwrap_or_else(|| load_test_config());
let starter =
ControlPlaneStarter::<N>::new(self.test_name, &mut nexus_config);
setup_with_config_impl(
starter,
PopulateCrdb::FromEnvironmentSeed,
self.sim_mode,
self.tls_cert,
self.nextra_sled_agents,
self.gateway_config_file,
false,
)
.await
}
}

/// Helper for setting up the control plane for testing and accessing its parts
///
/// See [`ControlPlaneBuilder`] for setting one up.
pub struct ControlPlaneTestContext<N> {
pub start_time: chrono::DateTime<chrono::Utc>,
pub external_client: ClientTestContext,
Expand Down Expand Up @@ -212,45 +306,7 @@ pub fn load_test_config() -> NexusConfig {
.expect("failed to load config.test.toml")
}

pub async fn test_setup<N: NexusServer>(
test_name: &str,
extra_sled_agents: u16,
) -> ControlPlaneTestContext<N> {
let mut config = load_test_config();
test_setup_with_config::<N>(
test_name,
&mut config,
sim::SimMode::Explicit,
None,
extra_sled_agents,
DEFAULT_SP_SIM_CONFIG.into(),
)
.await
}

/// Setup routine to use for tests.
pub async fn test_setup_with_config<N: NexusServer>(
test_name: &str,
config: &mut NexusConfig,
sim_mode: sim::SimMode,
initial_cert: Option<Certificate>,
extra_sled_agents: u16,
gateway_config_file: Utf8PathBuf,
) -> ControlPlaneTestContext<N> {
let starter = ControlPlaneStarter::<N>::new(test_name, config);
setup_with_config_impl(
starter,
PopulateCrdb::FromEnvironmentSeed,
sim_mode,
initial_cert,
extra_sled_agents,
gateway_config_file,
false,
)
.await
}

/// Setup routine to use for `omicron-dev`. Use [`test_setup_with_config`] for
/// Setup routine to use for `omicron-dev`. Use [`ControlPlaneBuilder`] for
/// tests.
///
/// The main difference from tests is that this routine ensures the seed tarball
Expand Down
23 changes: 8 additions & 15 deletions nexus/tests/integration_tests/certificates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ use display_error_chain::ErrorChainExt;
use dropshot::HttpErrorResponseBody;
use dropshot::test_util::ClientTestContext;
use futures::TryStreamExt;
use gateway_test_utils::setup::DEFAULT_SP_SIM_CONFIG;
use http::StatusCode;
use http::method::Method;
use internal_dns_types::names::DNS_ZONE_EXTERNAL_TESTING;
use nexus_test_utils::http_testing::AuthnMode;
use nexus_test_utils::http_testing::NexusRequest;
use nexus_test_utils::load_test_config;
use nexus_test_utils::resource_helpers::create_certificate;
use nexus_test_utils::resource_helpers::delete_certificate;
use nexus_test_utils_macros::nexus_test;
Expand Down Expand Up @@ -345,19 +343,14 @@ async fn test_silo_certificates() {
let silo3 = SiloCert::new("silo3".parse().unwrap());

// Start Nexus with a TLS server instead of its usual HTTP server.
let cptestctx = {
let mut config = load_test_config();
config.deployment.dropshot_external.tls = true;
nexus_test_utils::test_setup_with_config::<omicron_nexus::Server>(
"test_silo_certificates",
&mut config,
omicron_sled_agent::sim::SimMode::Explicit,
Some(silo1.cert.clone()),
0,
DEFAULT_SP_SIM_CONFIG.into(),
)
.await
};
let cptestctx =
nexus_test_utils::ControlPlaneBuilder::new("test_silo_certificates")
.with_modified_default_config(&|config| {
config.deployment.dropshot_external.tls = true;
})
.tls_cert(Some(silo1.cert.clone()))
.start::<omicron_nexus::Server>()
.await;

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

Expand Down
42 changes: 16 additions & 26 deletions nexus/tests/integration_tests/console_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use anyhow::Context;
use camino::Utf8PathBuf;
use dropshot::ResultsPage;
use dropshot::test_util::ClientTestContext;
use gateway_test_utils::setup::DEFAULT_SP_SIM_CONFIG;
use http::{StatusCode, header, method::Method};
use nexus_auth::context::OpContext;
use std::env::current_dir;
Expand All @@ -22,15 +21,13 @@ use nexus_test_utils::resource_helpers::create_console_session;
use nexus_test_utils::resource_helpers::{
create_silo, grant_iam, object_create,
};
use nexus_test_utils::{load_test_config, test_setup_with_config};
use nexus_test_utils_macros::nexus_test;
use nexus_types::external_api::params::{self, ProjectCreate};
use nexus_types::external_api::shared::{
FleetRole, SiloIdentityMode, SiloRole,
};
use nexus_types::external_api::{shared, views};
use omicron_common::api::external::{Error, IdentityMetadataCreateParams};
use omicron_sled_agent::sim;
use omicron_test_utils::dev::poll::{CondCheckError, wait_for_condition};

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

#[tokio::test]
async fn test_absolute_static_dir() {
let mut config = load_test_config();
config.pkg.console.static_dir =
Utf8PathBuf::try_from(current_dir().unwrap())
.unwrap()
.join("tests/static");
let cptestctx = test_setup_with_config::<omicron_nexus::Server>(
"test_absolute_static_dir",
&mut config,
sim::SimMode::Explicit,
None,
0,
DEFAULT_SP_SIM_CONFIG.into(),
)
.await;
let cptestctx =
nexus_test_utils::ControlPlaneBuilder::new("test_absolute_static_dir")
.with_modified_default_config(&|config| {
config.pkg.console.static_dir =
Utf8PathBuf::try_from(current_dir().unwrap())
.unwrap()
.join("tests/static");
})
.start::<omicron_nexus::Server>()
.await;
let testctx = &cptestctx.external_client;

// existing file is returned
Expand Down Expand Up @@ -941,17 +934,14 @@ async fn expect_redirect(testctx: &ClientTestContext, from: &str, to: &str) {
/// the session was found but is expired. vs not found at all
#[tokio::test]
async fn test_session_idle_timeout_deletes_session() {
// set idle timeout to 0 so we can test expiration
let mut config = load_test_config();
config.pkg.console.session_idle_timeout_minutes = 0;
let cptestctx = test_setup_with_config::<omicron_nexus::Server>(
let cptestctx = nexus_test_utils::ControlPlaneBuilder::new(
"test_session_idle_timeout_deletes_session",
&mut config,
sim::SimMode::Explicit,
None,
0,
DEFAULT_SP_SIM_CONFIG.into(),
)
.with_modified_default_config(&|config| {
// set idle timeout to 0 so we can test expiration
config.pkg.console.session_idle_timeout_minutes = 0;
})
.start::<omicron_nexus::Server>()
.await;
let testctx = &cptestctx.external_client;

Expand Down
Loading
Loading