@@ -7,7 +7,7 @@ use test_framework::{Test, TestGroup, TestResult};
7
7
use crate :: tests:: lifecycle:: ContainerLifecycle ;
8
8
9
9
fn create_spec ( args : & [ & str ] ) -> Result < Spec > {
10
- let args_vec: Vec < String > = args. iter ( ) . map ( |& a| a. to_string ( ) ) . collect ( ) ;
10
+ let args_vec: Vec < String > = args. iter ( ) . map ( |& a| a. into ( ) ) . collect ( ) ;
11
11
let spec = SpecBuilder :: default ( )
12
12
. process (
13
13
ProcessBuilder :: default ( )
@@ -20,52 +20,72 @@ fn create_spec(args: &[&str]) -> Result<Spec> {
20
20
Ok ( spec)
21
21
}
22
22
23
+ fn failed_and_delete ( text : String , container : ContainerLifecycle ) -> TestResult {
24
+ let delete_result = container. delete ( ) ;
25
+ match delete_result {
26
+ TestResult :: Passed => TestResult :: Failed ( anyhow ! ( text) ) ,
27
+ TestResult :: Failed ( err) => TestResult :: Failed ( anyhow ! (
28
+ "{}; also container deletion failed: {:?}" ,
29
+ text,
30
+ err
31
+ ) ) ,
32
+ _ => TestResult :: Failed ( anyhow ! ( "{}; unexpected delete result" , text) ) ,
33
+ }
34
+ }
35
+
36
+ fn merge_test_results ( kill_result : TestResult , delete_result : TestResult ) -> TestResult {
37
+ match ( kill_result, delete_result) {
38
+ ( TestResult :: Failed ( err) , _) => TestResult :: Failed ( err) ,
39
+ ( TestResult :: Passed , TestResult :: Failed ( err) ) => {
40
+ TestResult :: Failed ( anyhow ! ( "Delete failed: {:?}" , err) )
41
+ }
42
+ ( TestResult :: Passed , TestResult :: Passed ) => TestResult :: Passed ,
43
+ _ => TestResult :: Failed ( anyhow ! ( "Unexpected result" ) ) ,
44
+ }
45
+ }
46
+
23
47
fn kill_with_empty_id_test ( ) -> TestResult {
24
48
let mut container = ContainerLifecycle :: new ( ) ;
25
49
26
50
// kill with empty id
27
51
container. set_id ( "" ) ;
28
- let result = match container. kill ( ) {
52
+ match container. kill ( ) {
29
53
TestResult :: Failed ( _) => TestResult :: Passed ,
30
54
TestResult :: Passed => TestResult :: Failed ( anyhow ! (
31
55
"Expected killing container with empty id to fail, but was successful"
32
56
) ) ,
33
57
_ => TestResult :: Failed ( anyhow ! (
34
58
"Unexpected killing container with empty id test result"
35
59
) ) ,
36
- } ;
37
- container. delete ( ) ;
38
- result
60
+ }
39
61
}
40
62
41
63
fn kill_non_existed_container ( ) -> TestResult {
42
- let mut container = ContainerLifecycle :: new ( ) ;
64
+ let container = ContainerLifecycle :: new ( ) ;
43
65
44
66
// kill for non existed container
45
- container. set_id ( "non-existent-container-id" ) ;
46
- let result = match container. kill ( ) {
67
+ match container. kill ( ) {
47
68
TestResult :: Failed ( _) => TestResult :: Passed ,
48
69
TestResult :: Passed => TestResult :: Failed ( anyhow ! (
49
70
"Expected killing non existed container to fail, but was successful"
50
71
) ) ,
51
72
_ => TestResult :: Failed ( anyhow ! (
52
73
"Unexpected killing non existed container test result"
53
74
) ) ,
54
- } ;
55
- container. delete ( ) ;
56
- result
75
+ }
57
76
}
77
+
58
78
fn kill_created_container_test ( ) -> TestResult {
59
79
let container = ContainerLifecycle :: new ( ) ;
60
80
61
81
// kill created container
62
82
match container. create ( ) {
63
83
TestResult :: Passed => { }
64
- _ => return TestResult :: Failed ( anyhow ! ( "Failed to create container" ) ) ,
84
+ _ => return failed_and_delete ( "Failed to create container" . to_string ( ) , container ) ,
65
85
}
66
- let result = container. kill ( ) ;
67
- container. delete ( ) ;
68
- result
86
+ let kill_result = container. kill ( ) ;
87
+ let delete_result = container. delete ( ) ;
88
+ merge_test_results ( kill_result , delete_result )
69
89
}
70
90
71
91
fn kill_stopped_container_test ( ) -> TestResult {
@@ -75,20 +95,20 @@ fn kill_stopped_container_test() -> TestResult {
75
95
// kill stopped container
76
96
match container. create_with_spec ( spec) {
77
97
TestResult :: Passed => { }
78
- _ => return TestResult :: Failed ( anyhow ! ( "Failed to create container" ) ) ,
98
+ _ => return failed_and_delete ( "Failed to create container" . to_string ( ) , container ) ,
79
99
}
80
100
match container. start ( ) {
81
101
TestResult :: Passed => { }
82
- _ => return TestResult :: Failed ( anyhow ! ( "Failed to start container" ) ) ,
102
+ _ => return failed_and_delete ( "Failed to start container" . to_string ( ) , container ) ,
83
103
}
84
104
container. waiting_for_status ( Duration :: from_secs ( 10 ) , Duration :: from_secs ( 1 ) , "stopped" ) ;
85
- let result = match container. kill ( ) {
105
+ let kill_result = match container. kill ( ) {
86
106
TestResult :: Failed ( _) => TestResult :: Passed ,
87
107
TestResult :: Passed => TestResult :: Failed ( anyhow ! ( "Expected failure but got success" ) ) ,
88
108
_ => TestResult :: Failed ( anyhow ! ( "Unexpected test result" ) ) ,
89
109
} ;
90
- container. delete ( ) ;
91
- result
110
+ let delete_result = container. delete ( ) ;
111
+ merge_test_results ( kill_result , delete_result )
92
112
}
93
113
94
114
fn kill_start_container_test ( ) -> TestResult {
@@ -98,20 +118,17 @@ fn kill_start_container_test() -> TestResult {
98
118
// kill start container
99
119
match container. create_with_spec ( spec) {
100
120
TestResult :: Passed => { }
101
- _ => return TestResult :: Failed ( anyhow ! ( "Failed to recreate container" ) ) ,
121
+ _ => return failed_and_delete ( "Failed to recreate container" . to_string ( ) , container ) ,
102
122
}
103
123
104
124
match container. start ( ) {
105
125
TestResult :: Passed => { }
106
- TestResult :: Failed ( err) => {
107
- return TestResult :: Failed ( anyhow ! ( "Failed to start container: {:?}" , err) ) ;
108
- }
109
- _ => unreachable ! ( ) ,
126
+ _ => return failed_and_delete ( ( "Failed to start container" ) . to_string ( ) , container) ,
110
127
}
111
128
container. waiting_for_status ( Duration :: from_secs ( 10 ) , Duration :: from_secs ( 1 ) , "running" ) ;
112
- let result = container. kill ( ) ;
113
- container. delete ( ) ;
114
- result
129
+ let kill_result = container. kill ( ) ;
130
+ let delete_result = container. delete ( ) ;
131
+ merge_test_results ( kill_result , delete_result )
115
132
}
116
133
117
134
pub fn get_kill_test ( ) -> TestGroup {
0 commit comments