mirror of
https://github.com/containers/youki
synced 2025-04-30 13:20:17 +02:00
Add unit tests for tty module
Reuse tmpdir implementation from cgroup
This commit is contained in:
parent
1afde706fb
commit
a0da5378a1
87
Cargo.lock
generated
87
Cargo.lock
generated
@ -324,6 +324,15 @@ dependencies = [
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.7"
|
||||
@ -342,6 +351,15 @@ version = "0.2.95"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb"
|
||||
dependencies = [
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.14"
|
||||
@ -485,6 +503,31 @@ version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb"
|
||||
dependencies = [
|
||||
"instant",
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"instant",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.6"
|
||||
@ -605,6 +648,15 @@ dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.5.4"
|
||||
@ -628,6 +680,12 @@ version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.126"
|
||||
@ -659,12 +717,40 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serial_test"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0bccbcf40c8938196944a3da0e133e031a33f4d6b72db3bda3cc556e361905d"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"parking_lot",
|
||||
"serial_test_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serial_test_derive"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2acd6defeddb41eb60bb468f8825d0cfd0c2a76bc03bfd235b6a1dc4f6a1ad5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f173ac3d1a7e3b28003f40de0b5ce7fe2710f9b9dc3fc38664cebee46b3b6527"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.10.0"
|
||||
@ -818,4 +904,5 @@ dependencies = [
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serial_test",
|
||||
]
|
||||
|
@ -26,3 +26,4 @@ oci_spec = { version = "0.1.0", path = "./oci_spec" }
|
||||
[dev-dependencies]
|
||||
oci_spec = { version = "0.1.0", path = "./oci_spec", features = ["proptests"] }
|
||||
quickcheck = "1"
|
||||
serial_test = "0.5.1"
|
||||
|
@ -2,58 +2,14 @@
|
||||
|
||||
use anyhow::Result;
|
||||
use std::{
|
||||
fs,
|
||||
io::Write,
|
||||
ops::Deref,
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use oci_spec::LinuxCpu;
|
||||
|
||||
pub struct TempDir {
|
||||
path: Option<PathBuf>,
|
||||
}
|
||||
use crate::utils::{create_temp_dir, TempDir};
|
||||
|
||||
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 setup(testname: &str, cgroup_file: &str) -> (TempDir, PathBuf) {
|
||||
let tmp = create_temp_dir(testname).expect("create temp directory for test");
|
||||
@ -76,11 +32,6 @@ pub fn set_fixture(temp_dir: &Path, filename: &str, val: &str) -> Result<PathBuf
|
||||
Ok(full_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 struct LinuxCpuBuilder {
|
||||
resource: LinuxCpu,
|
||||
}
|
||||
|
@ -98,7 +98,8 @@ impl Devices {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::cgroups::test::{create_temp_dir, set_fixture};
|
||||
use crate::cgroups::test::set_fixture;
|
||||
use crate::utils::create_temp_dir;
|
||||
use oci_spec::{LinuxDeviceCgroup, LinuxDeviceType};
|
||||
use std::fs::read_to_string;
|
||||
|
||||
|
@ -116,7 +116,8 @@ impl Freezer {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::cgroups::test::{create_temp_dir, set_fixture};
|
||||
use crate::cgroups::test::set_fixture;
|
||||
use crate::utils::create_temp_dir;
|
||||
use oci_spec::FreezerState;
|
||||
|
||||
#[test]
|
||||
|
@ -58,7 +58,8 @@ impl Hugetlb {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::cgroups::test::{create_temp_dir, set_fixture};
|
||||
use crate::cgroups::test::set_fixture;
|
||||
use crate::utils::create_temp_dir;
|
||||
use oci_spec::LinuxHugepageLimit;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
|
@ -239,7 +239,8 @@ impl Memory {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::cgroups::test::{create_temp_dir, set_fixture};
|
||||
use crate::cgroups::test::set_fixture;
|
||||
use crate::utils::create_temp_dir;
|
||||
use oci_spec::LinuxMemory;
|
||||
|
||||
#[test]
|
||||
|
@ -36,8 +36,8 @@ impl NetworkClassifier {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::cgroups::test::{create_temp_dir, set_fixture};
|
||||
|
||||
use crate::cgroups::test::set_fixture;
|
||||
use crate::utils::create_temp_dir;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
|
@ -36,7 +36,8 @@ impl NetworkPriority {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::cgroups::test::{create_temp_dir, set_fixture};
|
||||
use crate::cgroups::test::set_fixture;
|
||||
use crate::utils::create_temp_dir;
|
||||
use oci_spec::LinuxInterfacePriority;
|
||||
|
||||
#[test]
|
||||
|
@ -46,8 +46,8 @@ impl Pids {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::cgroups::test::{create_temp_dir, set_fixture};
|
||||
|
||||
use crate::cgroups::test::set_fixture;
|
||||
use crate::utils::create_temp_dir;
|
||||
use super::*;
|
||||
use oci_spec::LinuxPids;
|
||||
|
||||
|
@ -86,7 +86,8 @@ impl Cpu {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::cgroups::test::{create_temp_dir, set_fixture, setup, LinuxCpuBuilder};
|
||||
use crate::cgroups::test::{set_fixture, setup, LinuxCpuBuilder};
|
||||
use crate::utils::create_temp_dir;
|
||||
use std::fs;
|
||||
|
||||
#[test]
|
||||
|
76
src/tty.rs
76
src/tty.rs
@ -68,3 +68,79 @@ pub fn setup_console(console_fd: FileDescriptor) -> Result<()> {
|
||||
close(console_fd.as_raw_fd())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use std::env;
|
||||
use std::fs::{self, File};
|
||||
use std::os::unix::net::UnixListener;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serial_test::serial;
|
||||
|
||||
use crate::utils::{create_temp_dir, TempDir};
|
||||
|
||||
|
||||
fn setup(testname: &str) -> Result<(TempDir, PathBuf, PathBuf)> {
|
||||
let testdir = create_temp_dir(testname)?;
|
||||
let rundir_path = Path::join(&testdir, "run");
|
||||
let _ = fs::create_dir(&rundir_path)?;
|
||||
let socket_path = Path::new(&rundir_path).join("socket");
|
||||
let _ = File::create(&socket_path);
|
||||
env::set_current_dir(&testdir)?;
|
||||
Ok((testdir, rundir_path, socket_path))
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_setup_console_socket() {
|
||||
let init = setup("test_setup_console_socket");
|
||||
assert!(init.is_ok());
|
||||
let (testdir, rundir_path, socket_path) = init.unwrap();
|
||||
let lis = UnixListener::bind(Path::join(&testdir, "console-socket"));
|
||||
assert!(lis.is_ok());
|
||||
let fd = setup_console_socket(&&rundir_path, &socket_path);
|
||||
assert!(fd.is_ok());
|
||||
assert_ne!(fd.unwrap().as_raw_fd(), -1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_setup_console_socket_empty() {
|
||||
let init = setup("test_setup_console_socket_empty");
|
||||
assert!(init.is_ok());
|
||||
let (_testdir, rundir_path, socket_path) = init.unwrap();
|
||||
let fd = setup_console_socket(&rundir_path, &socket_path);
|
||||
assert!(fd.is_ok());
|
||||
assert_eq!(fd.unwrap().as_raw_fd(), -1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_setup_console_socket_invalid() {
|
||||
let init = setup("test_setup_console_socket_invalid");
|
||||
assert!(init.is_ok());
|
||||
let (testdir, rundir_path, socket_path) = init.unwrap();
|
||||
let _socket = File::create(Path::join(&testdir, "console-socket"));
|
||||
assert!(_socket.is_ok());
|
||||
let fd = setup_console_socket(&rundir_path, &socket_path);
|
||||
assert!(fd.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_setup_console() {
|
||||
let init = setup("test_setup_console");
|
||||
assert!(init.is_ok());
|
||||
let (testdir, rundir_path, socket_path) = init.unwrap();
|
||||
let lis = UnixListener::bind(Path::join(&testdir, "console-socket"));
|
||||
assert!(lis.is_ok());
|
||||
let fd = setup_console_socket(&&rundir_path, &socket_path);
|
||||
let status = setup_console(fd.unwrap());
|
||||
assert!(status.is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
|
51
src/utils.rs
51
src/utils.rs
@ -3,6 +3,7 @@
|
||||
use std::env;
|
||||
use std::ffi::CString;
|
||||
use std::fs;
|
||||
use std::ops::Deref;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
@ -95,6 +96,56 @@ pub fn write_file<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Resul
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
Loading…
Reference in New Issue
Block a user