Skip to content
Merged
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
62 changes: 59 additions & 3 deletions bin/propolis-server/src/lib/spec/api_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ pub(super) fn parse_disk_from_request(
disk: &DiskRequest,
) -> Result<ParsedStorageDevice, DeviceRequestError> {
let pci_path = slot_to_pci_path(disk.slot, SlotType::Disk)?;
let backend_name = format!("{}-backend", disk.name);
let device_spec = match disk.device.as_ref() {
"virtio" => StorageDeviceV0::VirtioDisk(VirtioDisk {
backend_name: disk.name.to_string(),
backend_name: backend_name.clone(),
pci_path,
}),
"nvme" => StorageDeviceV0::NvmeDisk(NvmeDisk {
backend_name: disk.name.to_string(),
backend_name: backend_name.clone(),
pci_path,
}),
_ => {
Expand All @@ -91,7 +92,6 @@ pub(super) fn parse_disk_from_request(
};

let device_name = disk.name.clone();
let backend_name = format!("{}-backend", disk.name);
let backend_spec = StorageBackendV0::Crucible(CrucibleStorageBackend {
request_json: serde_json::to_string(&disk.volume_construction_request)
.map_err(|e| {
Expand Down Expand Up @@ -152,3 +152,59 @@ pub(super) fn parse_nic_from_request(
backend_spec,
})
}

#[cfg(test)]
mod test {
use propolis_api_types::VolumeConstructionRequest;
use uuid::Uuid;

use super::*;

fn check_parsed_storage_device_backend_pointer(
parsed: &ParsedStorageDevice,
) {
let device_to_backend = match &parsed.device_spec {
StorageDeviceV0::VirtioDisk(d) => d.backend_name.clone(),
StorageDeviceV0::NvmeDisk(d) => d.backend_name.clone(),
};

assert_eq!(device_to_backend, parsed.backend_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like instead of having ParsedStorageDevice.backend_name and requiring it always matches what happens to also be in the device spec, that we'd be better off with a StorageDeviceV0::backend_name() helper that we use in places like add_storage_device(). something like .insert(device_spec.backend_name(), backend_spec)?

do we expect to have storage devices that don't know their backend name? (that seems unlikely, but it's an obvious reason my thought wouldn't stick!)

(i also understand this change and why it fixes things, so +1 for landing to make things fixed and adjusting after if you're so inclined)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately my plan is to do away with ParsedStorageDevice entirely and replace it with a Disk type that contains both the device and backend halves of each of a VM's storage components. In this bug I got caught looking the wrong way halfway through the series of refactoring PRs that's supposed to make that happen ;)

I have the result of this wave of refactoring in a branch in my fork; here's what the routine this PR fixes will look like when that's done. This will (hopefully) foreclose on the sort of bug we hit here, since machine init won't be looking up named backends in a map anymore. (I think the code can be improved even more beyond what I have in the fork at this point, but am trying to stick to one renovation project at a time--insert the beaver-big-plans meme here...)

There's even more context in #735 if you're looking for some light bedtime reading.

}

#[test]
fn parsed_disk_devices_point_to_backends() {
let vcr = VolumeConstructionRequest::File {
id: Uuid::nil(),
block_size: 512,
path: "".to_string(),
};

let req = DiskRequest {
name: "my-disk".to_string(),
slot: Slot(0),
read_only: false,
device: "nvme".to_string(),
volume_construction_request: vcr,
};

let parsed = parse_disk_from_request(&req).unwrap();
check_parsed_storage_device_backend_pointer(&parsed);
}

#[test]
fn parsed_network_devices_point_to_backends() {
let req =
NetworkInterfaceRequest { name: "vnic".to_string(), slot: Slot(0) };

let parsed = parse_nic_from_request(&req).unwrap();
let NetworkDeviceV0::VirtioNic(nic) = &parsed.device_spec;
assert_eq!(nic.backend_name, parsed.backend_name);
}

#[test]
fn parsed_cloud_init_devices_point_to_backends() {
let base64 = "AAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string();
let parsed = parse_cloud_init_from_request(base64).unwrap();
check_parsed_storage_device_backend_pointer(&parsed);
}
}