Skip to content

Commit 5250fc2

Browse files
committed
initial integration test for time namespace
Signed-off-by: Carson Weeks <[email protected]>
1 parent cfb7b3d commit 5250fc2

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

crates/libcontainer/src/container/builder_impl.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,29 @@ impl ContainerBuilderImpl {
145145
}
146146

147147
// Extract time namespace offsets from spec
148+
// Only set time_offsets if we're creating a NEW time namespace (not joining an existing one)
148149
let time_offsets = self
149150
.spec
150151
.linux()
151152
.as_ref()
152-
.and_then(|linux| linux.time_offsets().as_ref())
153+
.and_then(|linux| {
154+
let creating_new_time_ns = linux
155+
.namespaces()
156+
.as_ref()
157+
.and_then(|namespaces| {
158+
namespaces
159+
.iter()
160+
.find(|ns| ns.typ() == oci_spec::runtime::LinuxNamespaceType::Time)
161+
})
162+
.map(|time_ns| time_ns.path().is_none())
163+
.unwrap_or(false);
164+
165+
if creating_new_time_ns {
166+
linux.time_offsets().as_ref()
167+
} else {
168+
None
169+
}
170+
})
153171
.map(|offsets| {
154172
offsets
155173
.iter()

tests/contest/contest/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use crate::tests::scheduler::get_scheduler_test;
4141
use crate::tests::seccomp::get_seccomp_test;
4242
use crate::tests::seccomp_notify::get_seccomp_notify_test;
4343
use crate::tests::sysctl::get_sysctl_test;
44+
use crate::tests::time_ns::get_time_ns_test;
4445
use crate::tests::tlb::get_tlb_test;
4546
use crate::tests::uid_mappings::get_uid_mappings_test;
4647
use crate::utils::support::{set_runtime_path, set_runtimetest_path};
@@ -148,6 +149,7 @@ fn main() -> Result<()> {
148149
let exec_cpu_affinity = get_exec_cpu_affinity_test();
149150
let personality = get_personality_test();
150151
let prohibit_symlink = get_prohibit_symlink_test();
152+
let time_ns = get_time_ns_test();
151153

152154
tm.add_test_group(Box::new(cl));
153155
tm.add_test_group(Box::new(cc));
@@ -190,6 +192,7 @@ fn main() -> Result<()> {
190192
tm.add_test_group(Box::new(personality));
191193
tm.add_test_group(Box::new(prohibit_symlink));
192194
tm.add_test_group(Box::new(io_priority_test));
195+
tm.add_test_group(Box::new(time_ns));
193196
tm.add_cleanup(Box::new(cgroups::cleanup_v1));
194197
tm.add_cleanup(Box::new(cgroups::cleanup_v2));
195198

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ pub mod scheduler;
3131
pub mod seccomp;
3232
pub mod seccomp_notify;
3333
pub mod sysctl;
34+
pub mod time_ns;
3435
pub mod tlb;
3536
pub mod uid_mappings;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::collections::HashMap;
2+
3+
use anyhow::{Context, Ok, Result};
4+
use oci_spec::runtime::{
5+
LinuxBuilder, LinuxNamespace, LinuxNamespaceType, LinuxTimeOffset, ProcessBuilder, Spec,
6+
SpecBuilder,
7+
};
8+
use test_framework::{Test, TestGroup, TestResult, test_result};
9+
10+
use crate::utils::test_inside_container;
11+
use crate::utils::test_utils::CreateOptions;
12+
13+
fn create_spec() -> Result<Spec> {
14+
let mut default_namespaces: Vec<LinuxNamespace> = oci_spec::runtime::get_default_namespaces();
15+
default_namespaces.push(
16+
LinuxNamespace::default()
17+
.set_typ(LinuxNamespaceType::Time)
18+
.to_owned(),
19+
);
20+
21+
let boottime: HashMap<_, _> = [(
22+
"boottime".to_owned(),
23+
LinuxTimeOffset::default()
24+
.set_secs(Some(9999999))
25+
.to_owned(),
26+
)]
27+
.into_iter()
28+
.collect();
29+
30+
SpecBuilder::default()
31+
.process(
32+
ProcessBuilder::default()
33+
.args(
34+
["runtimetest", "hello_world"]
35+
.iter()
36+
.map(|s| s.to_string())
37+
.collect::<Vec<String>>(),
38+
)
39+
.build()?,
40+
)
41+
.linux(
42+
LinuxBuilder::default()
43+
.time_offsets(boottime)
44+
.namespaces(default_namespaces)
45+
.build()?,
46+
)
47+
.build()
48+
.context("failed to create spec")
49+
}
50+
51+
fn time_ns_test() -> TestResult {
52+
let spec = test_result!(create_spec());
53+
test_inside_container(&spec, &CreateOptions::default(), &|_| Ok(()))
54+
}
55+
56+
pub fn get_time_ns_test() -> TestGroup {
57+
let mut test_group = TestGroup::new("time_ns");
58+
let test1 = Test::new("set boottime to 9999999", Box::new(time_ns_test));
59+
test_group.add(vec![Box::new(test1)]);
60+
61+
test_group
62+
}

0 commit comments

Comments
 (0)