Skip to content
Merged
Show file tree
Hide file tree
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
82 changes: 47 additions & 35 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,61 @@ name: Audit and Build

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Run Tests
run: cargo test
clippy:
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Cargo clippy
run: cargo clippy
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Cargo clippy
run: cargo clippy
fmt:
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Check rustfmt
run: cargo fmt -- --check
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Check rustfmt
run: cargo fmt -- --check
build:

runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Build with Nvidia
run: cargo build --verbose --features nvidia
if: ${{ matrix.os == 'ubuntu-latest' }}
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Build with Nvidia
run: cargo build --verbose --features nvidia
if: ${{ matrix.os == 'ubuntu-latest' }}
- name: Run tests
run: cargo test --verbose
189 changes: 189 additions & 0 deletions src/metrics/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,192 @@ pub async fn get_disk_io_metrics(disks: &mut HashMap<String, ZDisk>) {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;

#[test]
fn test_zdisk_get_perc_free_space_zero_size() {
let disk = ZDisk::new_total();
assert_eq!(disk.get_perc_free_space(), 0.0);
}

#[test]
fn test_zdisk_get_perc_free_space_normal() {
let mut disk = ZDisk::new_total();
disk.size_bytes = 1000;
disk.available_bytes = 500;

assert_eq!(disk.get_perc_free_space(), 50.0);
}

#[test]
fn test_zdisk_get_perc_free_space_full() {
let mut disk = ZDisk::new_total();
disk.size_bytes = 1000;
disk.available_bytes = 0;

assert_eq!(disk.get_perc_free_space(), 0.0);
}

#[test]
fn test_zdisk_get_perc_free_space_empty() {
let mut disk = ZDisk::new_total();
disk.size_bytes = 1000;
disk.available_bytes = 1000;

assert_eq!(disk.get_perc_free_space(), 100.0);
}

#[test]
fn test_zdisk_get_used_bytes() {
let mut disk = ZDisk::new_total();
disk.size_bytes = 1000;
disk.available_bytes = 300;

assert_eq!(disk.get_used_bytes(), 700);
}

#[test]
fn test_zdisk_get_used_bytes_underflow() {
let mut disk = ZDisk::new_total();
disk.size_bytes = 100;
disk.available_bytes = 200; // More available than size (edge case)

// saturating_sub should return 0
assert_eq!(disk.get_used_bytes(), 0);
}

#[test]
fn test_zdisk_get_perc_used_space_zero_size() {
let disk = ZDisk::new_total();
assert_eq!(disk.get_perc_used_space(), 0.0);
}

#[test]
fn test_zdisk_get_perc_used_space_normal() {
let mut disk = ZDisk::new_total();
disk.size_bytes = 1000;
disk.available_bytes = 300;

// used = 700, 700/1000 * 100 = 70%
assert!((disk.get_perc_used_space() - 70.0).abs() < 0.01);
}

#[test]
fn test_zdisk_get_read_bytes_sec() {
let mut disk = ZDisk::new_total();
disk.previous_io = IoMetrics {
read_bytes: 100,
write_bytes: 50,
};
disk.current_io = IoMetrics {
read_bytes: 600,
write_bytes: 150,
};

let tick = Duration::from_secs(1);
assert_eq!(disk.get_read_bytes_sec(&tick), 500.0);
}

#[test]
fn test_zdisk_get_read_bytes_sec_different_tick() {
let mut disk = ZDisk::new_total();
disk.previous_io = IoMetrics {
read_bytes: 100,
write_bytes: 50,
};
disk.current_io = IoMetrics {
read_bytes: 600,
write_bytes: 150,
};

let tick = Duration::from_secs(2);
assert_eq!(disk.get_read_bytes_sec(&tick), 250.0);
}

#[test]
fn test_zdisk_get_write_bytes_sec() {
let mut disk = ZDisk::new_total();
disk.previous_io = IoMetrics {
read_bytes: 100,
write_bytes: 50,
};
disk.current_io = IoMetrics {
read_bytes: 600,
write_bytes: 150,
};

let tick = Duration::from_secs(1);
assert_eq!(disk.get_write_bytes_sec(&tick), 100.0);
}

#[test]
fn test_zdisk_get_write_bytes_sec_different_tick() {
let mut disk = ZDisk::new_total();
disk.previous_io = IoMetrics {
read_bytes: 100,
write_bytes: 100,
};
disk.current_io = IoMetrics {
read_bytes: 600,
write_bytes: 500,
};

let tick = Duration::from_millis(500);
// (500 - 100) / 0.5 = 800
assert_eq!(disk.get_write_bytes_sec(&tick), 800.0);
}

// Tests for get_device_name
#[test]
fn test_get_device_name_simple() {
use std::ffi::OsStr;
let name = get_device_name(OsStr::new("/dev/sda1"));
// Since /dev/sda1 is not a symlink in test environment, it returns as-is
assert!(name.contains("sda1") || name.contains("/dev/"));
}

#[test]
fn test_get_device_name_non_existent() {
use std::ffi::OsStr;
let name = get_device_name(OsStr::new("/nonexistent/path/device"));
assert_eq!(name, "/nonexistent/path/device");
}

#[test]
fn test_get_device_name_empty() {
use std::ffi::OsStr;
let name = get_device_name(OsStr::new(""));
assert_eq!(name, "");
}

// Tests for ZDisk io operations
#[test]
fn test_zdisk_io_metrics_zero() {
let disk = ZDisk::new_total();
let tick = Duration::from_secs(1);

assert_eq!(disk.get_read_bytes_sec(&tick), 0.0);
assert_eq!(disk.get_write_bytes_sec(&tick), 0.0);
}

#[test]
fn test_zdisk_large_io_values() {
let mut disk = ZDisk::new_total();
disk.previous_io = IoMetrics {
read_bytes: 0,
write_bytes: 0,
};
disk.current_io = IoMetrics {
read_bytes: 1_000_000_000, // 1 GB
write_bytes: 500_000_000, // 500 MB
};

let tick = Duration::from_secs(1);
assert_eq!(disk.get_read_bytes_sec(&tick), 1_000_000_000.0);
assert_eq!(disk.get_write_bytes_sec(&tick), 500_000_000.0);
}
}
Loading
Loading