Skip to content

Commit 6426e71

Browse files
vmm: add more unit tests to lib.rs
We are now testing the functions check_health and is_instance_running. For these tests we need to alter the state InstanceState, so the test function that was creating a vmm object now has a new parameter by which you can specify the instance state. Signed-off-by: Andreea Florescu <[email protected]>
1 parent fb14c25 commit 6426e71

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

vmm/src/lib.rs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,10 +1623,8 @@ mod tests {
16231623
}
16241624
}
16251625

1626-
fn create_vmm_object() -> Vmm {
1627-
let shared_info = Arc::new(RwLock::new(InstanceInfo {
1628-
state: InstanceState::Uninitialized,
1629-
}));
1626+
fn create_vmm_object(state: InstanceState) -> Vmm {
1627+
let shared_info = Arc::new(RwLock::new(InstanceInfo { state }));
16301628

16311629
let (_to_vmm, from_api) = channel();
16321630
let vmm = Vmm::new(
@@ -1656,7 +1654,7 @@ mod tests {
16561654

16571655
#[test]
16581656
fn test_put_block_device() {
1659-
let mut vmm = create_vmm_object();
1657+
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
16601658
let f = NamedTempFile::new().unwrap();
16611659
// Test that creating a new block device returns the correct output (i.e. "Created").
16621660
let root_block_device = BlockDeviceConfig {
@@ -1699,7 +1697,7 @@ mod tests {
16991697

17001698
#[test]
17011699
fn test_put_net_device() {
1702-
let mut vmm = create_vmm_object();
1700+
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
17031701

17041702
// test create network interface
17051703
let network_interface = NetworkInterfaceBody {
@@ -1736,7 +1734,7 @@ mod tests {
17361734

17371735
#[test]
17381736
fn test_machine_configuration() {
1739-
let mut vmm = create_vmm_object();
1737+
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
17401738

17411739
// test the default values of machine config
17421740
// vcpu_count = 1
@@ -1998,8 +1996,40 @@ mod tests {
19981996
}
19991997

20001998
#[test]
2001-
pub fn test_attach_block_devices() {
2002-
let mut vmm = create_vmm_object();
1999+
fn test_check_health() {
2000+
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
2001+
assert!(vmm.check_health().is_err());
2002+
2003+
let dummy_addr = GuestAddress(0x1000);
2004+
vmm.configure_kernel(KernelConfig {
2005+
cmdline_addr: dummy_addr,
2006+
cmdline: kernel_cmdline::Cmdline::new(10),
2007+
kernel_file: tempfile::tempfile().unwrap(),
2008+
});
2009+
assert!(vmm.check_health().is_ok());
2010+
}
2011+
2012+
#[test]
2013+
fn test_is_instance_running() {
2014+
let vmm = create_vmm_object(InstanceState::Uninitialized);
2015+
assert_eq!(vmm.is_instance_running(), false);
2016+
2017+
let vmm = create_vmm_object(InstanceState::Starting);
2018+
assert_eq!(vmm.is_instance_running(), true);
2019+
2020+
let vmm = create_vmm_object(InstanceState::Halting);
2021+
assert_eq!(vmm.is_instance_running(), true);
2022+
2023+
let vmm = create_vmm_object(InstanceState::Halted);
2024+
assert_eq!(vmm.is_instance_running(), true);
2025+
2026+
let vmm = create_vmm_object(InstanceState::Running);
2027+
assert_eq!(vmm.is_instance_running(), true);
2028+
}
2029+
2030+
#[test]
2031+
fn test_attach_block_devices() {
2032+
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
20032033
let block_file = NamedTempFile::new().unwrap();
20042034
let kernel_file_temp =
20052035
NamedTempFile::new().expect("Failed to create temporary kernel file.");
@@ -2037,7 +2067,7 @@ mod tests {
20372067
assert!(vmm.get_kernel_cmdline().contains("root=/dev/vda"));
20382068

20392069
// Use Case 2: Root Block Device is specified through PARTUUID.
2040-
let mut vmm = create_vmm_object();
2070+
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
20412071
let root_block_device = BlockDeviceConfig {
20422072
drive_id: String::from("root"),
20432073
path_on_host: block_file.path().to_path_buf(),
@@ -2076,7 +2106,7 @@ mod tests {
20762106
);
20772107

20782108
// Use Case 3: Root Block Device is not added at all.
2079-
let mut vmm = create_vmm_object();
2109+
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
20802110
let non_root_block_device = BlockDeviceConfig {
20812111
drive_id: String::from("not_root"),
20822112
path_on_host: block_file.path().to_path_buf(),
@@ -2155,7 +2185,7 @@ mod tests {
21552185

21562186
#[test]
21572187
fn test_rescan() {
2158-
let mut vmm = create_vmm_object();
2188+
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
21592189
let root_file = NamedTempFile::new().unwrap();
21602190
let scratch_file = NamedTempFile::new().unwrap();
21612191
let kernel_file_temp = NamedTempFile::new().unwrap();
@@ -2203,25 +2233,25 @@ mod tests {
22032233

22042234
vmm.mmio_device_manager = Some(device_manager);
22052235

2206-
// Test valid rescan.
2236+
// Test valid rescan_block_device.
22072237
let body = serde_json::from_str::<ActionBody>(
22082238
"{\"action_type\": \"BlockDeviceRescan\", \"payload\": \"not_root\"}",
22092239
).unwrap();
22102240
assert!(vmm.rescan_block_device(body).is_ok());
22112241

2212-
// Test rescan with invalid ID.
2242+
// Test rescan_block_device with invalid ID.
22132243
let body = serde_json::from_str::<ActionBody>(
22142244
"{\"action_type\": \"BlockDeviceRescan\", \"payload\": \"foo\"}",
22152245
).unwrap();
22162246
assert!(vmm.rescan_block_device(body).is_err());
22172247

2218-
// Test rescan with invalid payload.
2248+
// Test rescan_block_device with invalid payload.
22192249
let body = serde_json::from_str::<ActionBody>(
22202250
"{\"action_type\": \"BlockDeviceRescan\", \"payload\": {}}",
22212251
).unwrap();
22222252
assert!(vmm.rescan_block_device(body).is_err());
22232253

2224-
// Test rescan with invalid device address.
2254+
// Test rescan_block_device with invalid device address.
22252255
vmm.remove_addr(&String::from("not_root"));
22262256
let body = serde_json::from_str::<ActionBody>(
22272257
"{\"action_type\": \"BlockDeviceRescan\", \"payload\": \"not_root\"}",

0 commit comments

Comments
 (0)