Skip to content

Commit

Permalink
automatic generation of uid and gid for unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
utam0k committed Feb 6, 2022
1 parent d20ec86 commit 089f59e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 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 crates/libcontainer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ serde_json = "1.0"
oci-spec = { version = "0.5.3", features = ["proptests"] }
quickcheck = "1"
serial_test = "0.5.1"
rand = "0.8.4"
20 changes: 10 additions & 10 deletions crates/libcontainer/src/rootless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ mod tests {
};
use serial_test::serial;

use crate::utils::TempDir;
use crate::utils::{test_utils::gen_u32, TempDir};

use super::*;

Expand All @@ -318,12 +318,12 @@ mod tests {
.typ(LinuxNamespaceType::User)
.build()?;
let uid_mappings = vec![LinuxIdMappingBuilder::default()
.host_id(3333_u32)
.host_id(gen_u32())
.container_id(0_u32)
.size(10_u32)
.build()?];
let gid_mappings = vec![LinuxIdMappingBuilder::default()
.host_id(3333_u32)
.host_id(gen_u32())
.container_id(0_u32)
.size(10_u32)
.build()?];
Expand All @@ -343,12 +343,12 @@ mod tests {
.typ(LinuxNamespaceType::User)
.build()?;
let uid_mappings = vec![LinuxIdMappingBuilder::default()
.host_id(3333_u32)
.host_id(gen_u32())
.container_id(0_u32)
.size(10_u32)
.build()?];
let gid_mappings = vec![LinuxIdMappingBuilder::default()
.host_id(3333_u32)
.host_id(gen_u32())
.container_id(0_u32)
.size(10_u32)
.build()?];
Expand All @@ -360,7 +360,7 @@ mod tests {
.build()?;
assert!(validate_spec_for_rootless(
&SpecBuilder::default()
.linux(linux_not_include_usend)
.linux(linux_no_userns)
.build()
.unwrap()
)
Expand Down Expand Up @@ -425,8 +425,8 @@ mod tests {
let userns = LinuxNamespaceBuilder::default()
.typ(LinuxNamespaceType::User)
.build()?;
let host_uid = 3333_u32;
let host_gid = 3334_u32;
let host_uid = gen_u32();
let host_gid = gen_u32();
let container_id = 0_u32;
let size = 10_u32;
let uid_mappings = vec![LinuxIdMappingBuilder::default()
Expand Down Expand Up @@ -465,8 +465,8 @@ mod tests {
let userns = LinuxNamespaceBuilder::default()
.typ(LinuxNamespaceType::User)
.build()?;
let host_uid = 3333_u32;
let host_gid = 3334_u32;
let host_uid = gen_u32();
let host_gid = gen_u32();
let container_id = 0_u32;
let size = 10_u32;
let uid_mappings = vec![LinuxIdMappingBuilder::default()
Expand Down
5 changes: 5 additions & 0 deletions crates/libcontainer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ pub(crate) mod test_utils {
use anyhow::Context;
use anyhow::{bail, Result};
use nix::sys::wait;
use rand::Rng;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -316,6 +317,10 @@ pub(crate) mod test_utils {

Ok(())
}

pub fn gen_u32() -> u32 {
rand::thread_rng().gen()
}
}

#[cfg(test)]
Expand Down

0 comments on commit 089f59e

Please sign in to comment.