Skip to content

Commit

Permalink
move ipc-channel to dev dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuaf committed Sep 29, 2021
1 parent 6850ee6 commit 6d4bda6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ tabwriter = "1"
fastrand = "1.4.1"
crossbeam-channel = "0.5"
seccomp = { version = "0.1.0", path = "./seccomp" }
ipc-channel = "0.15.0"

[dev-dependencies]
# TODO: Fetch from crate.io instead of git when next release oci-spec-rs
Expand All @@ -59,6 +58,7 @@ ipc-channel = "0.15.0"
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "5018f8e5b0355a82c08962cefa5ab07a05b930c6", features = ["proptests"] }
quickcheck = "1"
serial_test = "0.5.1"
ipc-channel = "0.15.0"

[profile.release]
lto = true
8 changes: 4 additions & 4 deletions src/seccomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub fn initialize_seccomp(seccomp: &LinuxSeccomp) -> Result<Option<io::RawFd>> {
#[cfg(test)]
mod tests {
use super::*;
use crate::utils;
use crate::utils::test_utils;
use anyhow::Result;
use oci_spec::runtime::Arch;
use oci_spec::runtime::{LinuxSeccompBuilder, LinuxSyscallBuilder};
Expand Down Expand Up @@ -415,7 +415,7 @@ mod tests {
.syscalls(vec![syscall])
.build()?;

utils::test_in_child_process(|| {
test_utils::test_in_child_process(|| {
let _ = prctl::set_no_new_privileges(true);
initialize_seccomp(&seccomp_profile)?;
let ret = nix::unistd::getcwd();
Expand Down Expand Up @@ -447,7 +447,7 @@ mod tests {

// We know linux and seccomp exist, so let's just unwrap.
let seccomp_profile = spec.linux().as_ref().unwrap().seccomp().as_ref().unwrap();
utils::test_in_child_process(|| {
test_utils::test_in_child_process(|| {
let _ = prctl::set_no_new_privileges(true);
initialize_seccomp(seccomp_profile)?;

Expand All @@ -469,7 +469,7 @@ mod tests {
.architectures(vec![Arch::ScmpArchNative])
.syscalls(vec![syscall])
.build()?;
utils::test_in_child_process(|| {
test_utils::test_in_child_process(|| {
let _ = prctl::set_no_new_privileges(true);
let fd = initialize_seccomp(&seccomp_profile)?;
if fd.is_none() {
Expand Down
76 changes: 41 additions & 35 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

use anyhow::Context;
use anyhow::{bail, Result};
use ipc_channel::ipc;
use nix::sys::stat::Mode;
use nix::sys::statfs;
use nix::sys::wait;
use nix::unistd;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ffi::CString;
use std::fs::{self, DirBuilder, File};
Expand Down Expand Up @@ -218,42 +215,51 @@ pub fn create_temp_dir(test_name: &str) -> Result<TempDir> {
Ok(dir)
}

#[derive(Debug, Serialize, Deserialize)]
struct TestResult {
success: bool,
message: String,
}
#[cfg(test)]
pub(crate) mod test_utils {
use anyhow::Context;
use anyhow::{bail, Result};
use ipc_channel::ipc;
use nix::sys::wait;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct TestResult {
success: bool,
message: String,
}

pub fn test_in_child_process<F: FnOnce() -> Result<()>>(cb: F) -> Result<()> {
let (sender, receiver) = ipc::channel::<TestResult>()?;
match unsafe { nix::unistd::fork()? } {
nix::unistd::ForkResult::Parent { child } => {
let res = receiver.recv().unwrap();
wait::waitpid(child, None)?;
pub fn test_in_child_process<F: FnOnce() -> Result<()>>(cb: F) -> Result<()> {
let (sender, receiver) = ipc::channel::<TestResult>()?;
match unsafe { nix::unistd::fork()? } {
nix::unistd::ForkResult::Parent { child } => {
let res = receiver.recv().unwrap();
wait::waitpid(child, None)?;

if !res.success {
bail!("child process failed: {}", res.message);
if !res.success {
bail!("child process failed: {}", res.message);
}
}
}
nix::unistd::ForkResult::Child => {
let test_result = match cb() {
Ok(_) => TestResult {
success: true,
message: String::new(),
},
Err(err) => TestResult {
success: false,
message: err.to_string(),
},
};
sender
.send(test_result)
.context("failed to send from the child process")?;
std::process::exit(0);
}
};
nix::unistd::ForkResult::Child => {
let test_result = match cb() {
Ok(_) => TestResult {
success: true,
message: String::new(),
},
Err(err) => TestResult {
success: false,
message: err.to_string(),
},
};
sender
.send(test_result)
.context("failed to send from the child process")?;
std::process::exit(0);
}
};

Ok(())
Ok(())
}
}

#[cfg(test)]
Expand Down

0 comments on commit 6d4bda6

Please sign in to comment.