Skip to content

Commit

Permalink
vmm: Add unit tests for restore
Browse files Browse the repository at this point in the history
The tests provide different inputs to parse and validate functions to
make sure parsing and error handling is as per expectation.

Signed-off-by: Purna Pavan Chandra <[email protected]>
  • Loading branch information
pupacha committed Apr 23, 2024
1 parent c5053d0 commit a24d877
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions vmm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3535,6 +3535,144 @@ mod tests {
Ok(())
}

#[test]
fn test_restore_parsing() -> Result<()> {
assert_eq!(
RestoreConfig::parse("source_url=/path/to/snapshot")?,
RestoreConfig {
source_url: PathBuf::from("/path/to/snapshot"),
prefault: false,
net_ids: None,
net_fds: None,
}
);
assert_eq!(
RestoreConfig::parse(
"source_url=/path/to/snapshot,prefault=off,net_ids=[net0,net1],net_fds=[3,4,5,6]"
)?,
RestoreConfig {
source_url: PathBuf::from("/path/to/snapshot"),
prefault: false,
net_ids: Some(vec!["net0".to_string(), "net1".to_string()]),
net_fds: Some(vec![3, 4, 5, 6]),
}
);
// Parsing should fail as source_url is a required field
assert!(!RestoreConfig::parse("prefault=off").is_ok());

Check failure on line 3561 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (beta, x86_64-unknown-linux-gnu, true)

this boolean expression can be simplified

Check failure on line 3561 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (stable, x86_64-unknown-linux-gnu, false)

this boolean expression can be simplified

Check failure on line 3561 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (beta, x86_64-unknown-linux-musl, true)

this boolean expression can be simplified

Check failure on line 3561 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (stable, x86_64-unknown-linux-musl, false)

this boolean expression can be simplified
Ok(())
}

#[test]
fn test_restore_config_validation() {
// interested in only VmConfig.net, so set rest to default values
let mut snapshot_vm_config = VmConfig {
cpus: CpusConfig::default(),
memory: MemoryConfig::default(),
payload: None,
rate_limit_groups: None,
disks: None,
rng: RngConfig::default(),
balloon: None,
fs: None,
pmem: None,
serial: default_serial(),
console: default_console(),
debug_console: DebugConsoleConfig::default(),

Check failure on line 3580 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (stable, aarch64-unknown-linux-gnu, false)

struct `vm_config::VmConfig` has no field named `debug_console`

Check failure on line 3580 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (stable, aarch64-unknown-linux-gnu, false)

failed to resolve: use of undeclared type `DebugConsoleConfig`

Check failure on line 3580 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (beta, aarch64-unknown-linux-musl, true)

struct `vm_config::VmConfig` has no field named `debug_console`

Check failure on line 3580 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (beta, aarch64-unknown-linux-musl, true)

failed to resolve: use of undeclared type `DebugConsoleConfig`

Check failure on line 3580 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (beta, aarch64-unknown-linux-gnu, true)

struct `vm_config::VmConfig` has no field named `debug_console`

Check failure on line 3580 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (beta, aarch64-unknown-linux-gnu, true)

failed to resolve: use of undeclared type `DebugConsoleConfig`

Check failure on line 3580 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (stable, aarch64-unknown-linux-musl, false)

struct `vm_config::VmConfig` has no field named `debug_console`

Check failure on line 3580 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (stable, aarch64-unknown-linux-musl, false)

failed to resolve: use of undeclared type `DebugConsoleConfig`
devices: None,
user_devices: None,
vdpa: None,
vsock: None,
pvpanic: false,
iommu: false,
sgx_epc: None,

Check failure on line 3587 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (stable, aarch64-unknown-linux-gnu, false)

struct `vm_config::VmConfig` has no field named `sgx_epc`

Check failure on line 3587 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (beta, aarch64-unknown-linux-musl, true)

struct `vm_config::VmConfig` has no field named `sgx_epc`

Check failure on line 3587 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (beta, aarch64-unknown-linux-gnu, true)

struct `vm_config::VmConfig` has no field named `sgx_epc`

Check failure on line 3587 in vmm/src/config.rs

View workflow job for this annotation

GitHub Actions / Quality (clippy, rustfmt) (stable, aarch64-unknown-linux-musl, false)

struct `vm_config::VmConfig` has no field named `sgx_epc`
numa: None,
watchdog: false,
platform: None,
tpm: None,
preserved_fds: None,
net: Some(vec![
NetConfig {
id: Some("net0".to_owned()),
num_queues: 2,
fds: Some(vec![-1, -1, -1, -1]),
..net_fixture()
},
NetConfig {
id: Some("net1".to_owned()),
num_queues: 1,
fds: Some(vec![-1, -1]),
..net_fixture()
},
NetConfig {
id: Some("net2".to_owned()),
fds: None,
..net_fixture()
},
]),
};

let valid_config = RestoreConfig {
source_url: PathBuf::from("/path/to/snapshot"),
prefault: false,
net_ids: Some(vec!["net0".to_string(), "net1".to_string()]),
net_fds: Some(vec![3, 4, 5, 6, 7, 8]),
};
assert!(valid_config.validate(&snapshot_vm_config).is_ok());

let mut invalid_config = valid_config.clone();
invalid_config.net_ids = Some(vec!["netx".to_string()]);
assert_eq!(
invalid_config.validate(&snapshot_vm_config),
Err(ValidationError::InvalidIdentifier("netx".to_string()))
);

invalid_config.net_ids = Some(vec!["net0".to_string(), "net0".to_string()]);
assert_eq!(
invalid_config.validate(&snapshot_vm_config),
Err(ValidationError::IdentifierNotUnique("net0".to_string()))
);

invalid_config.net_ids = Some(vec!["net1".to_string()]);
assert_eq!(
invalid_config.validate(&snapshot_vm_config),
Err(ValidationError::RestoreMissingRequiredNetId(
"net0".to_string()
))
);

invalid_config.net_ids = Some(vec![
"net0".to_string(),
"net1".to_string(),
"net2".to_string(),
]);
assert_eq!(
invalid_config.validate(&snapshot_vm_config),
Err(ValidationError::RestoreNonFdNetIdNotExpected(
"net2".to_string()
))
);

invalid_config.net_ids = Some(vec!["net0".to_string(), "net1".to_string()]);
invalid_config.net_fds = Some(vec![3, 4, 5, 6]);
assert_eq!(
invalid_config.validate(&snapshot_vm_config),
Err(ValidationError::RestoreNetFdCountMismatch(4, 6))
);

let another_valid_config = RestoreConfig {
source_url: PathBuf::from("/path/to/snapshot"),
prefault: false,
net_ids: None,
net_fds: None,
};
snapshot_vm_config.net = Some(vec![NetConfig {
id: Some("net2".to_owned()),
fds: None,
..net_fixture()
}]);
assert!(another_valid_config.validate(&snapshot_vm_config).is_ok());
}

fn platform_fixture() -> PlatformConfig {
PlatformConfig {
num_pci_segments: MAX_NUM_PCI_SEGMENTS,
Expand Down

0 comments on commit a24d877

Please sign in to comment.