Skip to content

Commit 39f5f14

Browse files
sat0kenYamasouA
authored andcommitted
e2e tests: Add test root readonly (youki-dev#2976)
* add test root readonly true Signed-off-by: sat0ken <[email protected]> * fix test group name Signed-off-by: sat0ken <[email protected]> * fix format Signed-off-by: sat0ken <[email protected]> * remove blank line Signed-off-by: sat0ken <[email protected]> * remove unused import Signed-off-by: sat0ken <[email protected]> * fix format err Signed-off-by: sat0ken <[email protected]> * remove unnecessary return Signed-off-by: sat0ken <[email protected]> * separate test root readonly true and false Signed-off-by: sat0ken <[email protected]> * fix format err Signed-off-by: sat0ken <[email protected]> * change test_dir_write_access to pub fn to use test Signed-off-by: sat0ken <[email protected]> * check root readonly to use test_dir_write_access Signed-off-by: sat0ken <[email protected]> * fix format err Signed-off-by: sat0ken <[email protected]> * fix format err Signed-off-by: sat0ken <[email protected]> * remove blank line Signed-off-by: sat0ken <[email protected]> * separate two tests to root_readonly_true and root_readonly_false Signed-off-by: sat0ken <[email protected]> * change test_dir_read_access to pub fn to use test Signed-off-by: sat0ken <[email protected]> * fix debug message and add check read access Signed-off-by: sat0ken <[email protected]> * fix format err Signed-off-by: sat0ken <[email protected]> * add root_readonly test to main Signed-off-by: sat0ken <[email protected]> * add read access test when root readonly is false Signed-off-by: sat0ken <[email protected]> * fox type err Signed-off-by: sat0ken <[email protected]> * remove code err to raw os err Signed-off-by: sat0ken <[email protected]> * add CreateOptions Signed-off-by: sat0ken <[email protected]> --------- Signed-off-by: sat0ken <[email protected]> Signed-off-by: Akiyama <[email protected]>
1 parent 1f8bc23 commit 39f5f14

File tree

7 files changed

+92
-3
lines changed

7 files changed

+92
-3
lines changed

tests/contest/contest/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::tests::process_oom_score_adj::get_process_oom_score_adj_test;
2727
use crate::tests::process_rlimits::get_process_rlimits_test;
2828
use crate::tests::process_user::get_process_user_test;
2929
use crate::tests::readonly_paths::get_ro_paths_test;
30+
use crate::tests::root_readonly_true::get_root_readonly_test;
3031
use crate::tests::scheduler::get_scheduler_test;
3132
use crate::tests::seccomp::get_seccomp_test;
3233
use crate::tests::seccomp_notify::get_seccomp_notify_test;
@@ -119,6 +120,7 @@ fn main() -> Result<()> {
119120
let scheduler = get_scheduler_test();
120121
let io_priority_test = get_io_priority_test();
121122
let devices = get_devices_test();
123+
let root_readonly = get_root_readonly_test();
122124
let process = get_process_test();
123125
let process_user = get_process_user_test();
124126
let process_rlimtis = get_process_rlimits_test();
@@ -148,6 +150,7 @@ fn main() -> Result<()> {
148150
tm.add_test_group(Box::new(sysctl));
149151
tm.add_test_group(Box::new(scheduler));
150152
tm.add_test_group(Box::new(devices));
153+
tm.add_test_group(Box::new(root_readonly));
151154
tm.add_test_group(Box::new(process));
152155
tm.add_test_group(Box::new(process_user));
153156
tm.add_test_group(Box::new(process_rlimtis));

tests/contest/contest/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod process_oom_score_adj;
1717
pub mod process_rlimits;
1818
pub mod process_user;
1919
pub mod readonly_paths;
20+
pub mod root_readonly_true;
2021
pub mod scheduler;
2122
pub mod seccomp;
2223
pub mod seccomp_notify;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod root_readonly_tests;
2+
pub use root_readonly_tests::get_root_readonly_test;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use anyhow::{Context, Ok, Result};
2+
use oci_spec::runtime::{ProcessBuilder, RootBuilder, Spec, SpecBuilder};
3+
use test_framework::{test_result, Test, TestGroup, TestResult};
4+
5+
use crate::utils::test_inside_container;
6+
use crate::utils::test_utils::CreateOptions;
7+
8+
fn create_spec(readonly: bool) -> Result<Spec> {
9+
let spec = SpecBuilder::default()
10+
.root(RootBuilder::default().readonly(readonly).build().unwrap())
11+
.process(
12+
ProcessBuilder::default()
13+
.args(vec!["runtimetest".to_string(), "root_readonly".to_string()])
14+
.build()
15+
.expect("error in creating config"),
16+
)
17+
.build()
18+
.context("failed to build spec")?;
19+
20+
Ok(spec)
21+
}
22+
23+
fn root_readonly_true_test() -> TestResult {
24+
let spec_true = test_result!(create_spec(true));
25+
test_inside_container(spec_true, &CreateOptions::default(), &|_| Ok(()))
26+
}
27+
28+
fn root_readonly_false_test() -> TestResult {
29+
let spec_false = test_result!(create_spec(false));
30+
test_inside_container(spec_false, &CreateOptions::default(), &|_| Ok(()))
31+
}
32+
33+
pub fn get_root_readonly_test() -> TestGroup {
34+
let mut root_readonly_test_group = TestGroup::new("root_readonly");
35+
36+
let test_true = Test::new("root_readonly_true_test", Box::new(root_readonly_true_test));
37+
let test_false = Test::new(
38+
"root_readonly_false_test",
39+
Box::new(root_readonly_false_test),
40+
);
41+
root_readonly_test_group.add(vec![Box::new(test_true), Box::new(test_false)]);
42+
43+
root_readonly_test_group
44+
}

tests/contest/runtimetest/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fn main() {
4444
"io_priority_class_be" => tests::test_io_priority_class(&spec, IoprioClassBe),
4545
"io_priority_class_idle" => tests::test_io_priority_class(&spec, IoprioClassIdle),
4646
"devices" => tests::validate_devices(&spec),
47+
"root_readonly" => tests::test_validate_root_readonly(&spec),
4748
"process" => tests::validate_process(&spec),
4849
"process_user" => tests::validate_process_user(&spec),
4950
"process_rlimits" => tests::validate_process_rlimits(&spec),

tests/contest/runtimetest/src/tests.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use oci_spec::runtime::{
1616
LinuxDevice, LinuxDeviceType, LinuxSchedulerPolicy, PosixRlimit, PosixRlimitType, Spec,
1717
};
1818

19-
use crate::utils::{self, test_read_access, test_write_access};
19+
use crate::utils::{
20+
self, test_dir_read_access, test_dir_write_access, test_read_access, test_write_access,
21+
};
2022

2123
////////// ANCHOR: example_hello_world
2224
pub fn hello_world(_spec: &Spec) {
@@ -551,6 +553,42 @@ pub fn test_io_priority_class(spec: &Spec, io_priority_class: IOPriorityClass) {
551553
}
552554
}
553555

556+
pub fn test_validate_root_readonly(spec: &Spec) {
557+
let root = spec.root().as_ref().unwrap();
558+
if root.readonly().unwrap() {
559+
if let Err(e) = test_dir_write_access("/") {
560+
let errno = Errno::from_raw(e.raw_os_error().unwrap());
561+
if errno == Errno::EROFS {
562+
/* This is expected */
563+
} else {
564+
eprintln!(
565+
"readonly root filesystem, error in testing write access for path /, error: {}",
566+
errno
567+
);
568+
}
569+
}
570+
if let Err(e) = test_dir_read_access("/") {
571+
eprintln!(
572+
"readonly root filesystem, but error in testing read access for path /, error: {}",
573+
e
574+
);
575+
}
576+
} else {
577+
if let Err(e) = test_dir_write_access("/") {
578+
eprintln!(
579+
"readonly root filesystem is false, but error in testing write access for path /, error: {}",
580+
e
581+
);
582+
}
583+
if let Err(e) = test_dir_read_access("/") {
584+
eprintln!(
585+
"readonly root filesystem is false, but error in testing read access for path /, error: {}",
586+
e
587+
);
588+
}
589+
}
590+
}
591+
554592
pub fn validate_process(spec: &Spec) {
555593
let process = spec.process().as_ref().unwrap();
556594
let expected_cwd = process.cwd();

tests/contest/runtimetest/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn test_file_read_access(path: &str) -> Result<(), std::io::Error> {
1414
Ok(())
1515
}
1616

17-
fn test_dir_read_access(path: &str) -> Result<(), std::io::Error> {
17+
pub fn test_dir_read_access(path: &str) -> Result<(), std::io::Error> {
1818
let _ = std::fs::read_dir(path)?;
1919
Ok(())
2020
}
@@ -51,7 +51,7 @@ fn test_file_write_access(path: &str) -> Result<(), std::io::Error> {
5151
Ok(())
5252
}
5353

54-
fn test_dir_write_access(path: &str) -> Result<(), std::io::Error> {
54+
pub fn test_dir_write_access(path: &str) -> Result<(), std::io::Error> {
5555
let _ = std::fs::OpenOptions::new()
5656
.create(true)
5757
.truncate(true)

0 commit comments

Comments
 (0)