Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaced tempdir in libcgroup #1888

Merged
merged 1 commit into from
May 8, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/libcgroups/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
env_logger = "0.10"
serial_test = "2.0.0"
tempfile = "3"
58 changes: 29 additions & 29 deletions crates/libcgroups/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ fn parse_psi(stat_line: &str, path: &Path) -> Result<PSIData, WrappedIoError> {

#[cfg(test)]
mod tests {
use crate::test::{create_temp_dir, set_fixture};
use crate::test::set_fixture;

use super::*;

Expand All @@ -497,36 +497,36 @@ mod tests {

#[test]
fn test_parse_single_value_valid() {
let tmp = create_temp_dir("test_parse_single_value_valid").unwrap();
let file_path = set_fixture(&tmp, "single_valued_file", "1200\n").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_path = set_fixture(tmp.path(), "single_valued_file", "1200\n").unwrap();

let value = parse_single_value(&file_path).unwrap();
assert_eq!(value, 1200);
}

#[test]
fn test_parse_single_value_invalid_number() {
let tmp = create_temp_dir("test_parse_single_value_invalid_number").unwrap();
let file_path = set_fixture(&tmp, "single_invalid_file", "noop\n").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_path = set_fixture(tmp.path(), "single_invalid_file", "noop\n").unwrap();

let value = parse_single_value(&file_path);
assert!(value.is_err());
}

#[test]
fn test_parse_single_value_multiple_entries() {
let tmp = create_temp_dir("test_parse_single_value_multiple_entries").unwrap();
let file_path = set_fixture(&tmp, "multi_valued_file", "1200\n1400\n1600").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_path = set_fixture(tmp.path(), "multi_valued_file", "1200\n1400\n1600").unwrap();

let value = parse_single_value(&file_path);
assert!(value.is_err());
}

#[test]
fn test_parse_flat_keyed_data() {
let tmp = create_temp_dir("test_parse_flat_keyed_data").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = ["key1 1", "key2 2", "key3 3"].join("\n");
let file_path = set_fixture(&tmp, "flat_keyed_data", &file_content).unwrap();
let file_path = set_fixture(tmp.path(), "flat_keyed_data", &file_content).unwrap();

let actual = parse_flat_keyed_data(&file_path).unwrap();
let mut expected = HashMap::with_capacity(3);
Expand All @@ -539,59 +539,59 @@ mod tests {

#[test]
fn test_parse_flat_keyed_data_with_characters() {
let tmp = create_temp_dir("test_parse_flat_keyed_data_with_characters").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = ["key1 1", "key2 a", "key3 b"].join("\n");
let file_path = set_fixture(&tmp, "flat_keyed_data", &file_content).unwrap();
let file_path = set_fixture(tmp.path(), "flat_keyed_data", &file_content).unwrap();

let result = parse_flat_keyed_data(&file_path);
assert!(result.is_err());
}

#[test]
fn test_parse_space_separated_as_flat_keyed_data() {
let tmp = create_temp_dir("test_parse_space_separated_as_flat_keyed_data").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = ["key1", "key2", "key3", "key4"].join(" ");
let file_path = set_fixture(&tmp, "space_separated", &file_content).unwrap();
let file_path = set_fixture(tmp.path(), "space_separated", &file_content).unwrap();

let result = parse_flat_keyed_data(&file_path);
assert!(result.is_err());
}

#[test]
fn test_parse_newline_separated_as_flat_keyed_data() {
let tmp = create_temp_dir("test_parse_newline_separated_as_flat_keyed_data").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = ["key1", "key2", "key3", "key4"].join("\n");
let file_path = set_fixture(&tmp, "newline_separated", &file_content).unwrap();
let file_path = set_fixture(tmp.path(), "newline_separated", &file_content).unwrap();

let result = parse_flat_keyed_data(&file_path);
assert!(result.is_err());
}

#[test]
fn test_parse_nested_keyed_data_as_flat_keyed_data() {
let tmp = create_temp_dir("test_parse_nested_keyed_data_as_flat_keyed_data").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = [
"key1 subkey1=value1 subkey2=value2 subkey3=value3",
"key2 subkey1=value1 subkey2=value2 subkey3=value3",
"key3 subkey1=value1 subkey2=value2 subkey3=value3",
]
.join("\n");
let file_path = set_fixture(&tmp, "nested_keyed_data", &file_content).unwrap();
let file_path = set_fixture(tmp.path(), "nested_keyed_data", &file_content).unwrap();

let result = parse_flat_keyed_data(&file_path);
assert!(result.is_err());
}

#[test]
fn test_parse_nested_keyed_data() {
let tmp = create_temp_dir("test_parse_nested_keyed_data").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = [
"key1 subkey1=value1 subkey2=value2 subkey3=value3",
"key2 subkey1=value1 subkey2=value2 subkey3=value3",
"key3 subkey1=value1 subkey2=value2 subkey3=value3",
]
.join("\n");
let file_path = set_fixture(&tmp, "nested_keyed_data", &file_content).unwrap();
let file_path = set_fixture(tmp.path(), "nested_keyed_data", &file_content).unwrap();

let actual = parse_nested_keyed_data(&file_path).unwrap();
let mut expected = HashMap::with_capacity(3);
Expand Down Expand Up @@ -625,29 +625,29 @@ mod tests {

#[test]
fn test_parse_space_separated_as_nested_keyed_data() {
let tmp = create_temp_dir("test_parse_space_separated_as_nested_keyed_data").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = ["key1", "key2", "key3", "key4"].join(" ");
let file_path = set_fixture(&tmp, "space_separated", &file_content).unwrap();
let file_path = set_fixture(tmp.path(), "space_separated", &file_content).unwrap();

let result = parse_nested_keyed_data(&file_path);
assert!(result.is_err());
}

#[test]
fn test_parse_newline_separated_as_nested_keyed_data() {
let tmp = create_temp_dir("test_parse_newline_separated_as_nested_keyed_data").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = ["key1", "key2", "key3", "key4"].join("\n");
let file_path = set_fixture(&tmp, "newline_separated", &file_content).unwrap();
let file_path = set_fixture(tmp.path(), "newline_separated", &file_content).unwrap();

let result = parse_nested_keyed_data(&file_path);
assert!(result.is_err());
}

#[test]
fn test_parse_flat_keyed_as_nested_keyed_data() {
let tmp = create_temp_dir("test_parse_flat_keyed_as_nested_keyed_data").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = ["key1 1", "key2 2", "key3 3"].join("\n");
let file_path = set_fixture(&tmp, "newline_separated", &file_content).unwrap();
let file_path = set_fixture(tmp.path(), "newline_separated", &file_content).unwrap();

let result = parse_nested_keyed_data(&file_path);
assert!(result.is_err());
Expand All @@ -667,13 +667,13 @@ mod tests {

#[test]
fn test_parse_psi_full_stats() {
let tmp = create_temp_dir("test_parse_psi_full_stats").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = [
"some avg10=80.00 avg60=50.00 avg300=90.00 total=0",
"full avg10=10.00 avg60=30.00 avg300=50.00 total=0",
]
.join("\n");
let psi_file = set_fixture(&tmp, "psi.pressure", &file_content).unwrap();
let psi_file = set_fixture(tmp.path(), "psi.pressure", &file_content).unwrap();

let result = psi_stats(&psi_file).unwrap();
assert_eq!(
Expand All @@ -695,9 +695,9 @@ mod tests {

#[test]
fn test_parse_psi_only_some() {
let tmp = create_temp_dir("test_parse_psi_only_some").unwrap();
let tmp = tempfile::tempdir().unwrap();
let file_content = ["some avg10=80.00 avg60=50.00 avg300=90.00 total=0"].join("\n");
let psi_file = set_fixture(&tmp, "psi.pressure", &file_content).unwrap();
let psi_file = set_fixture(tmp.path(), "psi.pressure", &file_content).unwrap();

let result = psi_stats(&psi_file).unwrap();
assert_eq!(
Expand Down
58 changes: 3 additions & 55 deletions crates/libcgroups/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,13 @@

use anyhow::{Context, Result};
use std::{
fs,
io::Write,
ops::Deref,
path::{Path, PathBuf},
};

pub struct TempDir {
path: Option<PathBuf>,
}

impl TempDir {
pub fn new<P: Into<PathBuf>>(path: P) -> Result<Self> {
let p = path.into();
std::fs::create_dir_all(&p)?;
Ok(Self { path: Some(p) })
}

pub fn path(&self) -> &Path {
self.path
.as_ref()
.expect("temp dir has already been removed")
}

pub fn remove(&mut self) {
if let Some(p) = &self.path {
let _ = fs::remove_dir_all(p);
self.path = None;
}
}
}

impl Drop for TempDir {
fn drop(&mut self) {
self.remove();
}
}

impl AsRef<Path> for TempDir {
fn as_ref(&self) -> &Path {
self.path()
}
}

impl Deref for TempDir {
type Target = Path;

fn deref(&self) -> &Self::Target {
self.path()
}
}

pub fn create_temp_dir(test_name: &str) -> Result<TempDir> {
let dir = TempDir::new(std::env::temp_dir().join(test_name))?;
Ok(dir)
}

pub fn setup(testname: &str, cgroup_file: &str) -> (TempDir, PathBuf) {
let tmp = create_temp_dir(testname).expect("create temp directory for test");
let cgroup_file = set_fixture(&tmp, cgroup_file, "")
pub fn setup(cgroup_file: &str) -> (tempfile::TempDir, PathBuf) {
let tmp = tempfile::tempdir().expect("create temp directory for test");
let cgroup_file = set_fixture(tmp.path(), cgroup_file, "")
.unwrap_or_else(|_| panic!("set test fixture for {cgroup_file}"));

(tmp, cgroup_file)
Expand Down
30 changes: 15 additions & 15 deletions crates/libcgroups/src/v1/blkio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,28 +260,28 @@ mod tests {
use std::fs;

use super::*;
use crate::test::{create_temp_dir, set_fixture, setup};
use crate::test::{set_fixture, setup};

use oci_spec::runtime::{LinuxBlockIoBuilder, LinuxThrottleDeviceBuilder};

#[test]
fn test_set_blkio_weight() {
for cgroup_file in &[BLKIO_WEIGHT, BLKIO_BFQ_WEIGHT] {
let (tmp, weight_file) = setup("test_set_blkio_weight", cgroup_file);
let (tmp, weight_file) = setup(cgroup_file);
let blkio = LinuxBlockIoBuilder::default()
.weight(200_u16)
.build()
.unwrap();

Blkio::apply(&tmp, &blkio).expect("apply blkio");
Blkio::apply(tmp.path(), &blkio).expect("apply blkio");
let content = fs::read_to_string(weight_file).expect("read blkio weight");
assert_eq!("200", content);
}
}

#[test]
fn test_set_blkio_read_bps() {
let (tmp, throttle) = setup("test_set_blkio_read_bps", BLKIO_THROTTLE_READ_BPS);
let (tmp, throttle) = setup(BLKIO_THROTTLE_READ_BPS);

let blkio = LinuxBlockIoBuilder::default()
.throttle_read_bps_device(vec![LinuxThrottleDeviceBuilder::default()
Expand All @@ -293,7 +293,7 @@ mod tests {
.build()
.unwrap();

Blkio::apply(&tmp, &blkio).expect("apply blkio");
Blkio::apply(tmp.path(), &blkio).expect("apply blkio");
let content = fs::read_to_string(throttle)
.unwrap_or_else(|_| panic!("read {BLKIO_THROTTLE_READ_BPS} content"));

Expand All @@ -302,7 +302,7 @@ mod tests {

#[test]
fn test_set_blkio_write_bps() {
let (tmp, throttle) = setup("test_set_blkio_write_bps", BLKIO_THROTTLE_WRITE_BPS);
let (tmp, throttle) = setup(BLKIO_THROTTLE_WRITE_BPS);

let blkio = LinuxBlockIoBuilder::default()
.throttle_write_bps_device(vec![LinuxThrottleDeviceBuilder::default()
Expand All @@ -314,7 +314,7 @@ mod tests {
.build()
.unwrap();

Blkio::apply(&tmp, &blkio).expect("apply blkio");
Blkio::apply(tmp.path(), &blkio).expect("apply blkio");
let content = fs::read_to_string(throttle)
.unwrap_or_else(|_| panic!("read {BLKIO_THROTTLE_WRITE_BPS} content"));

Expand All @@ -323,7 +323,7 @@ mod tests {

#[test]
fn test_set_blkio_read_iops() {
let (tmp, throttle) = setup("test_set_blkio_read_iops", BLKIO_THROTTLE_READ_IOPS);
let (tmp, throttle) = setup(BLKIO_THROTTLE_READ_IOPS);

let blkio = LinuxBlockIoBuilder::default()
.throttle_read_iops_device(vec![LinuxThrottleDeviceBuilder::default()
Expand All @@ -335,7 +335,7 @@ mod tests {
.build()
.unwrap();

Blkio::apply(&tmp, &blkio).expect("apply blkio");
Blkio::apply(tmp.path(), &blkio).expect("apply blkio");
let content = fs::read_to_string(throttle)
.unwrap_or_else(|_| panic!("read {BLKIO_THROTTLE_READ_IOPS} content"));

Expand All @@ -344,7 +344,7 @@ mod tests {

#[test]
fn test_set_blkio_write_iops() {
let (tmp, throttle) = setup("test_set_blkio_write_iops", BLKIO_THROTTLE_WRITE_IOPS);
let (tmp, throttle) = setup(BLKIO_THROTTLE_WRITE_IOPS);

let blkio = LinuxBlockIoBuilder::default()
.throttle_write_iops_device(vec![LinuxThrottleDeviceBuilder::default()
Expand All @@ -356,7 +356,7 @@ mod tests {
.build()
.unwrap();

Blkio::apply(&tmp, &blkio).expect("apply blkio");
Blkio::apply(tmp.path(), &blkio).expect("apply blkio");
let content = fs::read_to_string(throttle)
.unwrap_or_else(|_| panic!("read {BLKIO_THROTTLE_WRITE_IOPS} content"));

Expand All @@ -365,7 +365,7 @@ mod tests {

#[test]
fn test_stat_throttling_policy() -> Result<(), Box<dyn std::error::Error>> {
let tmp = create_temp_dir("test_stat_throttling_policy").expect("create test directory");
let tmp = tempfile::tempdir().unwrap();
let content = &[
"8:0 Read 20",
"8:0 Write 20",
Expand All @@ -376,10 +376,10 @@ mod tests {
"Total 0",
]
.join("\n");
set_fixture(&tmp, BLKIO_THROTTLE_IO_SERVICE_BYTES, content).unwrap();
set_fixture(&tmp, BLKIO_THROTTLE_IO_SERVICED, content).unwrap();
set_fixture(tmp.path(), BLKIO_THROTTLE_IO_SERVICE_BYTES, content).unwrap();
set_fixture(tmp.path(), BLKIO_THROTTLE_IO_SERVICED, content).unwrap();

let actual = Blkio::stats(&tmp).expect("get cgroup stats");
let actual = Blkio::stats(tmp.path()).expect("get cgroup stats");
let mut expected = BlkioStats::default();
let devices: Vec<BlkioDeviceStat> = ["Read", "Write", "Sync", "Async", "Discard", "Total"]
.iter()
Expand Down
Loading