-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add e2e test for process user (#2978)
* add-test-process-user Signed-off-by: sat0ken <[email protected]> * fix format Signed-off-by: sat0ken <[email protected]> * fix error Signed-off-by: sat0ken <[email protected]> * fix err and format Signed-off-by: sat0ken <[email protected]> * fix err to use UserBuilder Signed-off-by: sat0ken <[email protected]> * fix format err Signed-off-by: sat0ken <[email protected]> * remove unnecessary return Signed-off-by: sat0ken <[email protected]> * rename module name Signed-off-by: sat0ken <[email protected]> * fix unsafe code to use nix Signed-off-by: sat0ken <[email protected]> * fix err to use nix Signed-off-by: sat0ken <[email protected]> * fix format err Signed-off-by: sat0ken <[email protected]> * update fn validate_additional_gids to check group ids length Signed-off-by: sat0ken <[email protected]> * set additional_gids to random number Signed-off-by: sat0ken <[email protected]> * set umask value to variables Signed-off-by: sat0ken <[email protected]> * fix format err Signed-off-by: sat0ken <[email protected]> * change random number range Signed-off-by: sat0ken <[email protected]> * change err msg to use bail Signed-off-by: sat0ken <[email protected]> * nit: update error message in case of test failure Signed-off-by: Yashodhan Joshi <[email protected]> * fix: make sure random gid values are unique Signed-off-by: Yashodhan Joshi <[email protected]> --------- Signed-off-by: sat0ken <[email protected]> Signed-off-by: Yashodhan Joshi <[email protected]> Co-authored-by: Yashodhan Joshi <[email protected]>
- Loading branch information
Showing
6 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod process_user_test; | ||
pub use process_user_test::get_process_user_test; |
56 changes: 56 additions & 0 deletions
56
tests/contest/contest/src/tests/process_user/process_user_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use anyhow::{Context, Ok, Result}; | ||
use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder, UserBuilder}; | ||
use rand::Rng; | ||
use test_framework::{test_result, Test, TestGroup, TestResult}; | ||
|
||
use crate::utils::test_inside_container; | ||
|
||
// Generates a Vec<u32> with a random number of elements (between 5 and 15), | ||
// where each element is a random u32 value between 0 and 65535. | ||
fn generate_unique_random_vec() -> Vec<u32> { | ||
let mut rng = rand::thread_rng(); | ||
let vec_size = rng.gen_range(5..=10); | ||
let mut ret = Vec::new(); | ||
while ret.len() < vec_size { | ||
let rand = rng.gen_range(100..=200); | ||
if !ret.contains(&rand) { | ||
ret.push(rand); | ||
} | ||
} | ||
ret | ||
} | ||
|
||
fn create_spec() -> Result<Spec> { | ||
let umask = 0o002; | ||
let user = UserBuilder::default() | ||
.uid(10u32) | ||
.gid(10u32) | ||
.additional_gids(generate_unique_random_vec()) | ||
.umask(umask as u32) | ||
.build()?; | ||
|
||
let spec = SpecBuilder::default() | ||
.process( | ||
ProcessBuilder::default() | ||
.args(vec!["runtimetest".to_string(), "process_user".to_string()]) | ||
.user(user) | ||
.build() | ||
.expect("error in creating process config"), | ||
) | ||
.build() | ||
.context("failed to build spec")?; | ||
Ok(spec) | ||
} | ||
fn process_user_test() -> TestResult { | ||
let spec = test_result!(create_spec()); | ||
test_inside_container(spec, &|_| Ok(())) | ||
} | ||
|
||
pub fn get_process_user_test() -> TestGroup { | ||
let mut process_user_test_group = TestGroup::new("process_user"); | ||
|
||
let test = Test::new("process_user_test", Box::new(process_user_test)); | ||
process_user_test_group.add(vec![Box::new(test)]); | ||
|
||
process_user_test_group | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters