Skip to content

Commit 9513575

Browse files
vmm: added unit tests in src/lib.rs
Tested error paths for existing configurations. Added test for configure_boot_source method. Signed-off-by: Andreea Florescu <[email protected]>
1 parent 0565c3e commit 9513575

File tree

1 file changed

+140
-17
lines changed

1 file changed

+140
-17
lines changed

vmm/src/lib.rs

Lines changed: 140 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,10 +1706,10 @@ mod tests {
17061706
}
17071707

17081708
#[test]
1709-
fn test_put_block_device() {
1709+
fn test_insert_block_device() {
17101710
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
17111711
let f = NamedTempFile::new().unwrap();
1712-
// Test that creating a new block device returns the correct output (i.e. "Created").
1712+
// Test that creating a new block device returns the correct output.
17131713
let root_block_device = BlockDeviceConfig {
17141714
drive_id: String::from("root"),
17151715
path_on_host: f.path().to_path_buf(),
@@ -1725,7 +1725,7 @@ mod tests {
17251725
.contains(&root_block_device)
17261726
);
17271727

1728-
// Test that updating a block device returns the correct output (i.e. "Updated").
1728+
// Test that updating a block device returns the correct output.
17291729
let root_block_device = BlockDeviceConfig {
17301730
drive_id: String::from("root"),
17311731
path_on_host: f.path().to_path_buf(),
@@ -1740,10 +1740,58 @@ mod tests {
17401740
.config_list
17411741
.contains(&root_block_device)
17421742
);
1743+
1744+
// Test insert second drive with the same path fails.
1745+
let root_block_device = BlockDeviceConfig {
1746+
drive_id: String::from("dummy_dev"),
1747+
path_on_host: f.path().to_path_buf(),
1748+
is_root_device: false,
1749+
partuuid: None,
1750+
is_read_only: true,
1751+
rate_limiter: None,
1752+
};
1753+
assert!(vmm.insert_block_device(root_block_device.clone()).is_err());
1754+
1755+
// Test inserting a second drive is ok.
1756+
let f = NamedTempFile::new().unwrap();
1757+
// Test that creating a new block device returns the correct output.
1758+
let non_root = BlockDeviceConfig {
1759+
drive_id: String::from("non_root"),
1760+
path_on_host: f.path().to_path_buf(),
1761+
is_root_device: false,
1762+
partuuid: None,
1763+
is_read_only: false,
1764+
rate_limiter: None,
1765+
};
1766+
assert!(vmm.insert_block_device(non_root).is_ok());
1767+
1768+
// Test that making the second device root fails (it would result in 2 root block
1769+
// devices.
1770+
let non_root = BlockDeviceConfig {
1771+
drive_id: String::from("non_root"),
1772+
path_on_host: f.path().to_path_buf(),
1773+
is_root_device: true,
1774+
partuuid: None,
1775+
is_read_only: false,
1776+
rate_limiter: None,
1777+
};
1778+
assert!(vmm.insert_block_device(non_root).is_err());
1779+
1780+
// Test update after boot.
1781+
vmm.set_instance_state(InstanceState::Running);
1782+
let root_block_device = BlockDeviceConfig {
1783+
drive_id: String::from("root"),
1784+
path_on_host: f.path().to_path_buf(),
1785+
is_root_device: false,
1786+
partuuid: None,
1787+
is_read_only: true,
1788+
rate_limiter: None,
1789+
};
1790+
assert!(vmm.insert_block_device(root_block_device).is_err())
17431791
}
17441792

17451793
#[test]
1746-
fn test_put_net_device() {
1794+
fn test_insert_net_device() {
17471795
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
17481796

17491797
// test create network interface
@@ -1758,19 +1806,43 @@ mod tests {
17581806
};
17591807
assert!(vmm.insert_net_device(network_interface).is_ok());
17601808

1761-
if let Ok(mac) = MacAddr::parse_str("01:23:45:67:89:0A") {
1762-
// test update network interface
1763-
let network_interface = NetworkInterfaceBody {
1764-
iface_id: String::from("netif"),
1765-
state: DeviceState::Attached,
1766-
host_dev_name: String::from("hostname2"),
1767-
guest_mac: Some(mac),
1768-
rx_rate_limiter: None,
1769-
tx_rate_limiter: None,
1770-
allow_mmds_requests: false,
1771-
};
1772-
assert!(vmm.insert_net_device(network_interface).is_ok());
1773-
}
1809+
let mac = MacAddr::parse_str("01:23:45:67:89:0A").unwrap();
1810+
// test update network interface
1811+
let network_interface = NetworkInterfaceBody {
1812+
iface_id: String::from("netif"),
1813+
state: DeviceState::Attached,
1814+
host_dev_name: String::from("hostname2"),
1815+
guest_mac: Some(mac.clone()),
1816+
rx_rate_limiter: None,
1817+
tx_rate_limiter: None,
1818+
allow_mmds_requests: false,
1819+
};
1820+
assert!(vmm.insert_net_device(network_interface).is_ok());
1821+
1822+
// Test insert new net device with same mac fails.
1823+
let network_interface = NetworkInterfaceBody {
1824+
iface_id: String::from("netif2"),
1825+
state: DeviceState::Attached,
1826+
host_dev_name: String::from("hostname3"),
1827+
guest_mac: Some(mac),
1828+
rx_rate_limiter: None,
1829+
tx_rate_limiter: None,
1830+
allow_mmds_requests: false,
1831+
};
1832+
assert!(vmm.insert_net_device(network_interface).is_err());
1833+
1834+
// Test that update post-boot fails.
1835+
vmm.set_instance_state(InstanceState::Running);
1836+
let network_interface = NetworkInterfaceBody {
1837+
iface_id: String::from("netif"),
1838+
state: DeviceState::Attached,
1839+
host_dev_name: String::from("hostname2"),
1840+
guest_mac: None,
1841+
rx_rate_limiter: None,
1842+
tx_rate_limiter: None,
1843+
allow_mmds_requests: false,
1844+
};
1845+
assert!(vmm.insert_net_device(network_interface).is_err());
17741846
}
17751847

17761848
#[test]
@@ -1860,6 +1932,16 @@ mod tests {
18601932
assert_eq!(vmm.vm_config.vcpu_count, Some(2));
18611933
assert_eq!(vmm.vm_config.ht_enabled, Some(true));
18621934
assert_eq!(vmm.vm_config.cpu_template, Some(CpuFeaturesTemplate::T2));
1935+
1936+
// 3. Test update vm configuration after boot.
1937+
vmm.set_instance_state(InstanceState::Running);
1938+
let machine_config = VmConfig {
1939+
vcpu_count: Some(2),
1940+
mem_size_mib: None,
1941+
ht_enabled: Some(true),
1942+
cpu_template: Some(CpuFeaturesTemplate::T2),
1943+
};
1944+
assert!(vmm.set_vm_configuration(machine_config).is_err());
18631945
}
18641946

18651947
#[test]
@@ -2144,6 +2226,12 @@ mod tests {
21442226
vmm.set_block_device_path("not_root".to_string(), path)
21452227
.is_ok()
21462228
);
2229+
2230+
// Test partial update of block device fails due to invalid file.
2231+
assert!(
2232+
vmm.set_block_device_path("not_root".to_string(), String::from("dummy_path"))
2233+
.is_err()
2234+
);
21472235
}
21482236

21492237
#[test]
@@ -2186,6 +2274,41 @@ mod tests {
21862274
assert!(vmm.init_devices().is_ok());
21872275
}
21882276

2277+
#[test]
2278+
fn test_configure_boot_source() {
2279+
let mut vmm = create_vmm_object(InstanceState::Uninitialized);
2280+
2281+
// Test invalid kernel path.
2282+
assert!(
2283+
vmm.configure_boot_source(String::from("dummy-path"), None)
2284+
.is_err()
2285+
);
2286+
2287+
// Test valid kernel path and invalid cmdline.
2288+
let kernel_file = NamedTempFile::new().expect("Failed to create temporary kernel file.");
2289+
let kernel_path = String::from(kernel_file.path().to_path_buf().to_str().unwrap());
2290+
let invalid_cmdline =
2291+
String::from_utf8(vec![b'X'; x86_64::layout::CMDLINE_MAX_SIZE + 1]).unwrap();
2292+
assert!(
2293+
vmm.configure_boot_source(kernel_path.clone(), Some(invalid_cmdline))
2294+
.is_err()
2295+
);
2296+
2297+
// Test valid configuration.
2298+
assert!(vmm.configure_boot_source(kernel_path.clone(), None).is_ok());
2299+
assert!(
2300+
vmm.configure_boot_source(kernel_path.clone(), Some(String::from("reboot=k")))
2301+
.is_ok()
2302+
);
2303+
2304+
// Test valid configuration after boot (should fail).
2305+
vmm.set_instance_state(InstanceState::Running);
2306+
assert!(
2307+
vmm.configure_boot_source(kernel_path.clone(), None)
2308+
.is_err()
2309+
);
2310+
}
2311+
21892312
#[test]
21902313
fn test_rescan() {
21912314
let mut vmm = create_vmm_object(InstanceState::Uninitialized);

0 commit comments

Comments
 (0)