From 5407d4a9a1885806a0f74abfc8cfe17baf13e124 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 13 Jul 2023 10:26:56 -0400 Subject: [PATCH] Don't use env::tempdir This can cause test pollution. Create a new temp directory on each run. --- aya/Cargo.toml | 1 + aya/src/programs/links.rs | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/aya/Cargo.toml b/aya/Cargo.toml index dd4392d9..28054e44 100644 --- a/aya/Cargo.toml +++ b/aya/Cargo.toml @@ -31,6 +31,7 @@ tokio = { version = "1.24.0", features = ["rt"], optional = true } [dev-dependencies] futures = { version = "0.3.12", default-features = false, features = ["std"] } matches = "0.1.8" +tempfile = "3" [features] default = [] diff --git a/aya/src/programs/links.rs b/aya/src/programs/links.rs index 649d1f9b..1bfc6d0b 100644 --- a/aya/src/programs/links.rs +++ b/aya/src/programs/links.rs @@ -354,7 +354,8 @@ pub enum LinkError { #[cfg(test)] mod tests { use matches::assert_matches; - use std::{cell::RefCell, env, fs::File, mem, os::unix::io::AsRawFd, rc::Rc}; + use std::{cell::RefCell, fs::File, mem, os::unix::io::AsRawFd, rc::Rc}; + use tempfile::tempdir; use crate::{programs::ProgramError, sys::override_syscall}; @@ -489,8 +490,8 @@ mod tests { #[test] #[cfg_attr(miri, ignore)] fn test_pin() { - let dir = env::temp_dir(); - let f1 = File::create(dir.join("f1")).expect("unable to create file in tmpdir"); + let dir = tempdir().unwrap(); + let f1 = File::create(dir.path().join("f1")).expect("unable to create file in tmpdir"); let fd_link = FdLink::new(f1.as_raw_fd()); // leak the fd, it will get closed when our pinned link is dropped @@ -499,11 +500,12 @@ mod tests { // override syscall to allow for pin to happen in our tmpdir override_syscall(|_| Ok(0)); // create the file that would have happened as a side-effect of a real pin operation - File::create(dir.join("f1-pin")).expect("unable to create file in tmpdir"); - assert!(dir.join("f1-pin").exists()); + let pin = dir.path().join("f1-pin"); + File::create(&pin).expect("unable to create file in tmpdir"); + assert!(pin.exists()); - let pinned_link = fd_link.pin(dir.join("f1-pin")).expect("pin failed"); + let pinned_link = fd_link.pin(&pin).expect("pin failed"); pinned_link.unpin().expect("unpin failed"); - assert!(!dir.join("f1-pin").exists()); + assert!(!pin.exists()); } }