Skip to content

Commit 2b72e74

Browse files
committed
handling capability modification in the callback function
Signed-off-by: kazuki.massaki <kazuki.massaki@gmail.com>
1 parent 098586e commit 2b72e74

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

tests/contest/contest/src/tests/process_capabilities_fail/process_capabilities_fail_test.rs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
use std::collections::HashSet;
2-
use std::str::FromStr;
1+
use std::{collections::HashSet, fs, fs::OpenOptions, io::Write};
32

4-
use anyhow::{Context, Error, Ok, Result};
3+
use anyhow::{anyhow, Context, Ok, Result};
54
use oci_spec::runtime::{Capability, LinuxCapabilitiesBuilder, ProcessBuilder, Spec, SpecBuilder};
6-
use test_framework::{Test, TestGroup, TestResult};
5+
use test_framework::{test_result, Test, TestGroup, TestResult};
76

8-
fn create_spec() -> Result<Spec> {
9-
let capability = Capability::from_str("CAP_TEST").context("invalid capability")?;
7+
use serde_json::Value;
8+
9+
use crate::utils::{test_inside_container, test_utils::CreateOptions};
1010

11+
fn create_spec() -> Result<Spec> {
1112
let linux_capability = LinuxCapabilitiesBuilder::default()
12-
.bounding(HashSet::from([capability]))
13+
.bounding(HashSet::from([Capability::Syslog]))
1314
.build()?;
1415

1516
let process = ProcessBuilder::default()
@@ -30,15 +31,39 @@ fn create_spec() -> Result<Spec> {
3031
}
3132

3233
fn process_capabilities_fail_test() -> TestResult {
33-
match create_spec() {
34-
Result::Ok(_) => TestResult::Failed(Error::msg("create_spec succeeded unexpectedly.")),
35-
Err(e) => {
36-
if e.to_string() == "invalid capability" {
37-
TestResult::Passed
38-
} else {
39-
TestResult::Failed(Error::msg(format!("unexpected error: {}", e)))
34+
let spec = test_result!(create_spec());
35+
let result = test_inside_container(spec, &CreateOptions::default(), &|bundle| {
36+
let spec_path = bundle.join("../config.json");
37+
let spec_str = fs::read_to_string(spec_path.clone()).unwrap();
38+
39+
let mut spec_json: Value = serde_json::from_str(&spec_str)?;
40+
41+
if let Some(bounding) = spec_json.pointer_mut("/process/capabilities/bounding") {
42+
if let Some(bounding_array) = bounding.as_array_mut() {
43+
for capanility in bounding_array.iter_mut() {
44+
if capanility == "CAP_SYSLOG" {
45+
*capanility = Value::String("TEST_CAP".to_string());
46+
}
47+
}
4048
}
4149
}
50+
51+
let updated_spec_str = serde_json::to_string_pretty(&spec_json)?;
52+
53+
let mut file = OpenOptions::new()
54+
.write(true)
55+
.truncate(true)
56+
.open(spec_path)?;
57+
file.write_all(updated_spec_str.as_bytes())?;
58+
59+
Ok(())
60+
});
61+
match result {
62+
TestResult::Failed(_e) => TestResult::Passed,
63+
TestResult::Skipped => TestResult::Failed(anyhow!("test was skipped unexpectedly.")),
64+
TestResult::Passed => {
65+
TestResult::Failed(anyhow!("container creation succeeded unexpectedly."))
66+
}
4267
}
4368
}
4469

tests/contest/contest/src/utils/test_utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ pub fn test_inside_container(
201201
.join("runtimetest"),
202202
)
203203
.unwrap();
204-
let create_process = create_container(&id_str, &bundle, options).unwrap();
204+
let create_process = match create_container(&id_str, &bundle, options) {
205+
Ok(p) => p,
206+
Err(e) => return TestResult::Failed(anyhow!("container create failed : {:?}", e)),
207+
};
205208
// here we do not wait for the process by calling wait() as in the test_outside_container
206209
// function because we need the output of the runtimetest. If we call wait, it will return
207210
// and we won't have an easy way of getting the stdio of the runtimetest.

0 commit comments

Comments
 (0)