Commit e07c1ca0 authored by Stefan Schindler's avatar Stefan Schindler
Browse files

Test partitions parsing

parent e2de9d04
......@@ -69,10 +69,11 @@ fn show_disk_status(disks: &Vec<Arc<DiskInformation>>) {
fn parse_disks() -> Vec<Arc<DiskInformation>> {
let partitions = get_file_contents("/proc/partitions");
//println!("partitions: {:?}", partitions);
parse_partition_file(&partitions)
}
fn parse_partition_file(partitions: &String) -> Vec<Arc<DiskInformation>> {
let lines = partitions.split("\n").skip(2);
//println!("lines: {:?}", lines);
let mut disks = Vec::new();
let mut i = 0;
......@@ -137,6 +138,15 @@ struct DiskInformation {
state: Mutex<DiskState>,
}
impl std::cmp::PartialEq<Self> for DiskInformation {
fn eq(&self, rhs: &Self) -> bool {
self.id == rhs.id &&
self.size == rhs.size &&
self.name == rhs.name &&
*self.state.lock().unwrap() == *rhs.state.lock().unwrap()
}
}
#[derive(Debug,PartialEq,Eq)]
enum DiskState {
Detected,
......@@ -414,7 +424,7 @@ fn wipe_disk(disk: &Arc<DiskInformation>) -> std::io::Result<std::time::Duration
#[cfg(test)]
mod test {
use ::ByteUnit::*;
use ::*;
#[test]
fn unit_eq() {
......@@ -545,4 +555,38 @@ mod test {
assert_eq!(zero, zero_bps)
}
#[test]
fn parse_partition_file_sda_mmcblk0() {
let parts = "
major minor #blocks name
8 0 500107608 sda
8 1 524288 sda1
8 2 499582279 sda2
179 0 31166976 mmcblk0
179 1 31162880 mmcblk0p1
254 0 499580231 dm-0
254 1 20971520 dm-1
254 2 478605312 dm-2
".into();
let partitions = parse_partition_file(&parts);
assert_eq!(2, partitions.len());
let sda = DiskInformation {
id: 1,
name: "sda".into(),
size: KiloByte(500107608),
state: Mutex::new(Detected),
};
assert_eq!(sda, *partitions[0]);
let mmcblk0 = DiskInformation {
id: 2,
name: "mmcblk0".into(),
size: KiloByte(31166976),
state: Mutex::new(Detected),
};
assert_eq!(mmcblk0, *partitions[1]);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment