1
- use std:: collections:: HashSet ;
2
- use std:: str:: FromStr ;
1
+ use std:: { collections:: HashSet , fs, fs:: OpenOptions , io:: Write } ;
3
2
4
- use anyhow:: { Context , Error , Ok , Result } ;
3
+ use anyhow:: { anyhow , Context , Ok , Result } ;
5
4
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 } ;
7
6
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 } ;
10
10
11
+ fn create_spec ( ) -> Result < Spec > {
11
12
let linux_capability = LinuxCapabilitiesBuilder :: default ( )
12
- . bounding ( HashSet :: from ( [ capability ] ) )
13
+ . bounding ( HashSet :: from ( [ Capability :: Syslog ] ) )
13
14
. build ( ) ?;
14
15
15
16
let process = ProcessBuilder :: default ( )
@@ -30,15 +31,39 @@ fn create_spec() -> Result<Spec> {
30
31
}
31
32
32
33
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
+ }
40
48
}
41
49
}
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
+ }
42
67
}
43
68
}
44
69
0 commit comments