Skip to content

Commit

Permalink
Merge pull request #102 from constreference/tty
Browse files Browse the repository at this point in the history
Add unit tests for tty module
  • Loading branch information
utam0k authored Jun 20, 2021
2 parents 1afde70 + a0da537 commit 4f6e8e9
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 61 deletions.
87 changes: 87 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
53 changes: 2 additions & 51 deletions src/cgroups/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

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()
}
}
use crate::utils::{create_temp_dir, TempDir};

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");
Expand All @@ -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,
}
Expand Down
3 changes: 2 additions & 1 deletion src/cgroups/v1/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/cgroups/v1/freezer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion src/cgroups/v1/hugetlb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/cgroups/v1/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/cgroups/v1/network_classifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion src/cgroups/v1/network_priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/cgroups/v1/pids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/cgroups/v2/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
76 changes: 76 additions & 0 deletions src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Loading

0 comments on commit 4f6e8e9

Please sign in to comment.