1
1
use anyhow:: Context ;
2
2
use std:: path:: { Path , PathBuf } ;
3
3
use testing_framework:: {
4
- EnvTemplate , InMemorySpin , OnTestError , Runtime , ServicesConfig , Spin , SpinConfig ,
5
- TestEnvironment , TestEnvironmentConfig , TestError , TestResult ,
4
+ http:: { Method , Request } ,
5
+ runtimes:: {
6
+ in_process_spin:: InProcessSpin ,
7
+ spin_cli:: { SpinCli , SpinConfig } ,
8
+ } ,
9
+ EnvTemplate , OnTestError , Runtime , ServicesConfig , TestEnvironment , TestEnvironmentConfig ,
10
+ TestError , TestResult ,
6
11
} ;
7
12
8
13
/// Configuration for a runtime test
@@ -19,45 +24,34 @@ pub struct RuntimeTest<R> {
19
24
env : TestEnvironment < R > ,
20
25
}
21
26
22
- impl RuntimeTest < Spin > {
27
+ impl RuntimeTest < SpinCli > {
23
28
/// Run the runtime tests suite.
24
29
///
25
30
/// Error represents an error in bootstrapping the tests. What happens on individual test failures
26
31
/// is controlled by `on_error`.
27
32
pub fn run_all (
28
- tests_path : & Path ,
33
+ tests_dir_path : & Path ,
29
34
spin_binary : PathBuf ,
30
35
on_error : OnTestError ,
31
36
) -> anyhow:: Result < ( ) > {
32
- for test in std:: fs:: read_dir ( tests_path)
33
- . with_context ( || format ! ( "failed to read test directory '{}'" , tests_path. display( ) ) ) ?
34
- {
35
- let test = test. context ( "I/O error reading entry from test directory" ) ?;
36
- if !test. file_type ( ) ?. is_dir ( ) {
37
- log:: debug!(
38
- "Ignoring non-sub-directory in test directory: {}" ,
39
- test. path( ) . display( )
40
- ) ;
41
- continue ;
42
- }
43
-
37
+ Self :: run_on_all ( tests_dir_path, |test_path| {
44
38
let config = RuntimeTestConfig {
45
- test_path : test . path ( ) ,
39
+ test_path,
46
40
runtime_config : SpinConfig {
47
41
binary_path : spin_binary. clone ( ) ,
48
42
} ,
49
43
on_error,
50
44
} ;
51
45
Self :: bootstrap ( config) ?. run ( ) ;
52
- }
53
- Ok ( ( ) )
46
+ Ok ( ( ) )
47
+ } )
54
48
}
55
49
56
- pub fn bootstrap ( config : RuntimeTestConfig < Spin > ) -> anyhow:: Result < Self > {
50
+ pub fn bootstrap ( config : RuntimeTestConfig < SpinCli > ) -> anyhow:: Result < Self > {
57
51
log:: info!( "Testing: {}" , config. test_path. display( ) ) ;
58
52
let test_path_clone = config. test_path . to_owned ( ) ;
59
53
let spin_binary = config. runtime_config . binary_path . clone ( ) ;
60
- let preboot = move |env : & mut TestEnvironment < Spin > | {
54
+ let preboot = move |env : & mut TestEnvironment < SpinCli > | {
61
55
copy_manifest ( & test_path_clone, env) ?;
62
56
Ok ( ( ) )
63
57
} ;
@@ -67,7 +61,7 @@ impl RuntimeTest<Spin> {
67
61
[ ] ,
68
62
preboot,
69
63
services_config,
70
- testing_framework:: SpinMode :: Http ,
64
+ testing_framework:: runtimes :: SpinAppType :: Http ,
71
65
) ;
72
66
let env = TestEnvironment :: up ( env_config) ?;
73
67
Ok ( Self {
@@ -81,7 +75,7 @@ impl RuntimeTest<Spin> {
81
75
pub fn run ( & mut self ) {
82
76
self . run_test ( |env| {
83
77
let runtime = env. runtime_mut ( ) ;
84
- let request = testing_framework :: Request :: new ( reqwest :: Method :: GET , "/" ) ;
78
+ let request = Request :: new ( Method :: GET , "/" ) ;
85
79
let response = runtime. make_http_request ( request) ?;
86
80
if response. status ( ) == 200 {
87
81
return Ok ( ( ) ) ;
@@ -99,44 +93,33 @@ impl RuntimeTest<Spin> {
99
93
100
94
Err ( TestError :: Failure ( RuntimeTestFailure {
101
95
error : text,
102
- stderr : runtime. stderr ( ) . to_owned ( ) ,
96
+ stderr : Some ( runtime. stderr ( ) . to_owned ( ) ) ,
103
97
} ) )
104
98
} )
105
99
}
106
100
}
107
101
108
- impl RuntimeTest < InMemorySpin > {
102
+ impl RuntimeTest < InProcessSpin > {
109
103
/// Run the runtime tests suite.
110
104
///
111
105
/// Error represents an error in bootstrapping the tests. What happens on individual test failures
112
106
/// is controlled by `on_error`.
113
- pub fn run_all ( tests_path : & Path , on_error : OnTestError ) -> anyhow:: Result < ( ) > {
114
- for test in std:: fs:: read_dir ( tests_path)
115
- . with_context ( || format ! ( "failed to read test directory '{}'" , tests_path. display( ) ) ) ?
116
- {
117
- let test = test. context ( "I/O error reading entry from test directory" ) ?;
118
- if !test. file_type ( ) ?. is_dir ( ) {
119
- log:: debug!(
120
- "Ignoring non-sub-directory in test directory: {}" ,
121
- test. path( ) . display( )
122
- ) ;
123
- continue ;
124
- }
125
-
107
+ pub fn run_all ( tests_dir_path : & Path , on_error : OnTestError ) -> anyhow:: Result < ( ) > {
108
+ Self :: run_on_all ( tests_dir_path, |test_path| {
126
109
let config = RuntimeTestConfig {
127
- test_path : test . path ( ) ,
110
+ test_path,
128
111
runtime_config : ( ) ,
129
112
on_error,
130
113
} ;
131
114
Self :: bootstrap ( config) ?. run ( ) ;
132
- }
133
- Ok ( ( ) )
115
+ Ok ( ( ) )
116
+ } )
134
117
}
135
118
136
- pub fn bootstrap ( config : RuntimeTestConfig < InMemorySpin > ) -> anyhow:: Result < Self > {
119
+ pub fn bootstrap ( config : RuntimeTestConfig < InProcessSpin > ) -> anyhow:: Result < Self > {
137
120
log:: info!( "Testing: {}" , config. test_path. display( ) ) ;
138
121
let test_path_clone = config. test_path . to_owned ( ) ;
139
- let preboot = move |env : & mut TestEnvironment < InMemorySpin > | {
122
+ let preboot = move |env : & mut TestEnvironment < InProcessSpin > | {
140
123
copy_manifest ( & test_path_clone, env) ?;
141
124
Ok ( ( ) )
142
125
} ;
@@ -153,7 +136,7 @@ impl RuntimeTest<InMemorySpin> {
153
136
pub fn run ( & mut self ) {
154
137
self . run_test ( |env| {
155
138
let runtime = env. runtime_mut ( ) ;
156
- let response = runtime. make_http_request ( testing_framework :: Request :: new ( reqwest :: Method :: GET , "/" ) ) ?;
139
+ let response = runtime. make_http_request ( Request :: new ( Method :: GET , "/" ) ) ?;
157
140
if response. status ( ) == 200 {
158
141
return Ok ( ( ) ) ;
159
142
}
@@ -169,13 +152,37 @@ impl RuntimeTest<InMemorySpin> {
169
152
170
153
Err ( TestError :: Failure ( RuntimeTestFailure {
171
154
error : text,
172
- stderr : String :: new ( )
155
+ stderr : None
173
156
} ) )
174
157
} )
175
158
}
176
159
}
177
160
178
161
impl < R > RuntimeTest < R > {
162
+ /// Run a closure against all tests in the given tests directory
163
+ pub fn run_on_all (
164
+ tests_dir_path : & Path ,
165
+ run : impl Fn ( PathBuf ) -> anyhow:: Result < ( ) > ,
166
+ ) -> anyhow:: Result < ( ) > {
167
+ for test in std:: fs:: read_dir ( tests_dir_path) . with_context ( || {
168
+ format ! (
169
+ "failed to read test directory '{}'" ,
170
+ tests_dir_path. display( )
171
+ )
172
+ } ) ? {
173
+ let test = test. context ( "I/O error reading entry from test directory" ) ?;
174
+ if !test. file_type ( ) ?. is_dir ( ) {
175
+ log:: debug!(
176
+ "Ignoring non-sub-directory in test directory: {}" ,
177
+ test. path( ) . display( )
178
+ ) ;
179
+ continue ;
180
+ }
181
+ run ( test. path ( ) ) ?;
182
+ }
183
+ Ok ( ( ) )
184
+ }
185
+
179
186
/// Run an individual test
180
187
pub ( crate ) fn run_test (
181
188
& mut self ,
@@ -220,15 +227,21 @@ impl<R> RuntimeTest<R> {
220
227
} else {
221
228
error ! (
222
229
on_error,
223
- "Test errored but not in the expected way.\n \t expected: {expected}\n \t got: {error}\n \n stderr:\n {stderr}" ,
230
+ "Test errored but not in the expected way.\n \t expected: {expected}\n \t got: {error}{}" ,
231
+ stderr
232
+ . map( |e| format!( "\n \n stderr:\n {e}" ) )
233
+ . unwrap_or_default( )
224
234
)
225
235
}
226
236
}
227
237
Err ( TestError :: Failure ( RuntimeTestFailure { error, stderr } ) ) => {
228
238
error ! (
229
239
on_error,
230
- "Test '{}' errored: {error}\n stderr:\n {stderr}" ,
231
- self . test_path. display( )
240
+ "Test '{}' errored: {error}{}" ,
241
+ self . test_path. display( ) ,
242
+ stderr
243
+ . map( |e| format!( "\n stderr:\n {e}" ) )
244
+ . unwrap_or_default( )
232
245
) ;
233
246
}
234
247
Err ( TestError :: Fatal ( extra) ) => {
@@ -287,6 +300,6 @@ fn copy_manifest<R>(test_dir: &Path, env: &mut TestEnvironment<R>) -> anyhow::Re
287
300
struct RuntimeTestFailure {
288
301
/// The error message returned by the runtime
289
302
error : String ,
290
- /// The runtime's stderr
291
- stderr : String ,
303
+ /// The runtime's stderr if there is one
304
+ stderr : Option < String > ,
292
305
}
0 commit comments