Skip to content

Commit

Permalink
fix test_{uid,gid}_mapping_should_succeed().
Browse files Browse the repository at this point in the history
  • Loading branch information
utam0k committed Feb 5, 2022
1 parent 7e71ab3 commit 2597291
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
25 changes: 23 additions & 2 deletions crates/libcontainer/src/process/container_main_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ fn setup_mapping(rootless: &Rootless, pid: Pid) -> Result<()> {
mod tests {
use super::*;
use crate::process::channel::{intermediate_channel, main_channel};
use crate::rootless::{get_gid_path, get_uid_path};
use nix::{
sched::{unshare, CloneFlags},
unistd::{self, getgid, getuid},
Expand All @@ -184,6 +185,8 @@ mod tests {
use serial_test::serial;
use std::fs;

use crate::utils::TempDir;

#[test]
#[serial]
fn setup_uid_mapping_should_succeed() -> Result<()> {
Expand All @@ -204,8 +207,17 @@ mod tests {
unistd::ForkResult::Parent { child } => {
parent_receiver.wait_for_mapping_request()?;
parent_receiver.close()?;

let tempdir = TempDir::new(get_uid_path(&child).parent().unwrap())?;
let uid_map_path = tempdir.join("uid_map");
let _ = fs::File::create(&uid_map_path)?;

let tempdir = TempDir::new(get_gid_path(&child).parent().unwrap())?;
let gid_map_path = tempdir.join("gid_map");
let _ = fs::File::create(&gid_map_path)?;

setup_mapping(&rootless, child)?;
let line = fs::read_to_string(format!("/proc/{}/uid_map", child.as_raw()))?;
let line = fs::read_to_string(uid_map_path)?;
let line_splited = line.split_whitespace();
for (act, expect) in line_splited.zip([
uid_mapping.container_id().to_string(),
Expand Down Expand Up @@ -249,8 +261,17 @@ mod tests {
unistd::ForkResult::Parent { child } => {
parent_receiver.wait_for_mapping_request()?;
parent_receiver.close()?;

let tempdir = TempDir::new(get_uid_path(&child).parent().unwrap())?;
let uid_map_path = tempdir.join("uid_map");
let _ = fs::File::create(&uid_map_path)?;

let tempdir = TempDir::new(get_gid_path(&child).parent().unwrap())?;
let gid_map_path = tempdir.join("gid_map");
let _ = fs::File::create(&gid_map_path)?;

setup_mapping(&rootless, child)?;
let line = fs::read_to_string(format!("/proc/{}/gid_map", child.as_raw()))?;
let line = fs::read_to_string(gid_map_path)?;
let line_splited = line.split_whitespace();
for (act, expect) in line_splited.zip([
gid_mapping.container_id().to_string(),
Expand Down
4 changes: 2 additions & 2 deletions crates/libcontainer/src/rootless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn get_uid_path(pid: &Pid) -> PathBuf {
}

#[cfg(test)]
fn get_uid_path(pid: &Pid) -> PathBuf {
pub fn get_uid_path(pid: &Pid) -> PathBuf {
utils::get_temp_dir_path(format!("{pid}_mapping_path").as_str()).join("uid_map")
}

Expand All @@ -113,7 +113,7 @@ fn get_gid_path(pid: &Pid) -> PathBuf {
}

#[cfg(test)]
fn get_gid_path(pid: &Pid) -> PathBuf {
pub fn get_gid_path(pid: &Pid) -> PathBuf {
utils::get_temp_dir_path(format!("{pid}_mapping_path").as_str()).join("gid_map")
}

Expand Down

0 comments on commit 2597291

Please sign in to comment.