Skip to content

Commit

Permalink
Add some more resource helpers (oxidecomputer#6602)
Browse files Browse the repository at this point in the history
Add `create_disk_from_snapshot` helper.

Also, when writing unit tests that include expunging a disk, it's still
useful to ask if all Crucible resources were expunged during clean up.
However Nexus will not perform this clean up if a disk is expunged, so
add a few methods to DiskTest to drop a given zpool from the
PerSledDiskState.
  • Loading branch information
jmpesp authored Sep 19, 2024
1 parent dc614fd commit 017d6e8
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions nexus/test-utils/src/resource_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,28 @@ pub async fn create_disk(
.await
}

pub async fn create_disk_from_snapshot(
client: &ClientTestContext,
project_name: &str,
disk_name: &str,
snapshot_id: Uuid,
) -> Disk {
let url = format!("/v1/disks?project={}", project_name);
object_create(
client,
&url,
&params::DiskCreate {
identity: IdentityMetadataCreateParams {
name: disk_name.parse().unwrap(),
description: String::from("sells rainsticks"),
},
disk_source: params::DiskSource::Snapshot { snapshot_id },
size: ByteCount::from_gibibytes_u32(1),
},
)
.await
}

pub async fn create_snapshot(
client: &ClientTestContext,
project_name: &str,
Expand Down Expand Up @@ -1214,6 +1236,10 @@ impl<'a, N: NexusServer> DiskTest<'a, N> {
}

/// Returns true if all Crucible resources were cleaned up, false otherwise.
///
/// Note: be careful performing this test when also peforming physical disk
/// expungement, as Nexus will consider resources on those physical disks
/// gone and will not attempt to clean them up!
pub async fn crucible_resources_deleted(&self) -> bool {
for (sled_id, state) in &self.sleds {
for zpool in &state.zpools {
Expand All @@ -1231,4 +1257,17 @@ impl<'a, N: NexusServer> DiskTest<'a, N> {

true
}

/// Drop all of a zpool's resources
///
/// Call this before checking `crucible_resources_deleted` if the test has
/// also performed a physical disk policy change to "expunged". Nexus will
/// _not_ clean up crucible resources on an expunged disk (due to the "gone"
/// check that it performs), but it's useful for tests to be able to assert
/// all crucible resources are cleaned up.
pub async fn remove_zpool(&mut self, zpool_id: Uuid) {
for sled in self.sleds.values_mut() {
sled.zpools.retain(|zpool| *zpool.id.as_untyped_uuid() != zpool_id);
}
}
}

0 comments on commit 017d6e8

Please sign in to comment.