1
- use std:: {
2
- collections:: HashMap ,
3
- path:: { Path , PathBuf } ,
4
- } ;
1
+ use std:: { collections:: HashMap , path:: Path } ;
5
2
6
3
use crate :: {
7
- runtimes:: { in_process_spin:: InProcessSpin , spin_cli:: SpinCli , SpinAppType } ,
8
4
services:: { Services , ServicesConfig } ,
9
5
Runtime ,
10
6
} ;
@@ -23,6 +19,8 @@ pub struct TestEnvironment<R> {
23
19
24
20
impl < R : Runtime > TestEnvironment < R > {
25
21
/// Spin up a test environment with a runtime
22
+ ///
23
+ /// `config` specifies the services to run and how to create the runtime.
26
24
pub fn up (
27
25
config : TestEnvironmentConfig < R > ,
28
26
init_env : impl FnOnce ( & mut Self ) -> crate :: TestResult < anyhow:: Error > + ' static ,
@@ -33,7 +31,9 @@ impl<R: Runtime> TestEnvironment<R> {
33
31
env. start_runtime ( runtime)
34
32
}
35
33
36
- /// Whether an error has occurred
34
+ /// Returns an error if the environment is not healthy.
35
+ ///
36
+ /// If a runtime is present, it will also be checked for errors.
37
37
fn error ( & mut self ) -> anyhow:: Result < ( ) > {
38
38
self . services . healthy ( ) ?;
39
39
if let Some ( runtime) = & mut self . runtime {
@@ -45,6 +45,8 @@ impl<R: Runtime> TestEnvironment<R> {
45
45
46
46
impl < R > TestEnvironment < R > {
47
47
/// Spin up a test environment without a runtime
48
+ ///
49
+ /// `services` specifies the services to run.
48
50
pub fn boot ( services : & ServicesConfig ) -> anyhow:: Result < Self > {
49
51
let temp = temp_dir:: TempDir :: new ( )
50
52
. context ( "failed to produce a temporary directory to run the test in" ) ?;
@@ -61,6 +63,8 @@ impl<R> TestEnvironment<R> {
61
63
}
62
64
63
65
/// Start the runtime
66
+ ///
67
+ /// Will error if the environment is not healthy.
64
68
pub fn start_runtime < N : Runtime > ( self , runtime : N ) -> anyhow:: Result < TestEnvironment < N > > {
65
69
let mut this = TestEnvironment {
66
70
temp : self . temp ,
@@ -182,77 +186,7 @@ impl<R> TestEnvironment<R> {
182
186
/// Configuration for a test environment
183
187
pub struct TestEnvironmentConfig < R > {
184
188
/// A callback to create a runtime given a path to a temporary directory
185
- create_runtime : Box < RuntimeCreator < R > > ,
189
+ pub create_runtime : Box < RuntimeCreator < R > > ,
186
190
/// The services that the test requires
187
- services_config : ServicesConfig ,
188
- }
189
-
190
- impl TestEnvironmentConfig < SpinCli > {
191
- /// Configure a test environment that uses a local Spin binary as a runtime
192
- ///
193
- /// * `spin_binary` - the path to the Spin binary
194
- /// * `preboot` - a callback that happens after the services have started but before the runtime is
195
- /// * `test` - a callback that runs the test against the runtime
196
- /// * `services_config` - the services that the test requires
197
- pub fn spin (
198
- spin_binary : PathBuf ,
199
- spin_up_args : impl IntoIterator < Item = String > ,
200
- preboot : impl FnOnce ( & mut TestEnvironment < SpinCli > ) -> anyhow:: Result < ( ) > + ' static ,
201
- services_config : ServicesConfig ,
202
- app_type : SpinAppType ,
203
- ) -> Self {
204
- let spin_up_args = spin_up_args. into_iter ( ) . collect ( ) ;
205
- Self {
206
- services_config,
207
- create_runtime : Box :: new ( move |env| {
208
- preboot ( env) ?;
209
- SpinCli :: start ( & spin_binary, env, spin_up_args, app_type)
210
- } ) ,
211
- }
212
- }
213
- }
214
-
215
- impl TestEnvironmentConfig < InProcessSpin > {
216
- pub fn in_process (
217
- services_config : ServicesConfig ,
218
- preboot : impl FnOnce ( & mut TestEnvironment < InProcessSpin > ) -> anyhow:: Result < ( ) > + ' static ,
219
- ) -> Self {
220
- Self {
221
- services_config,
222
- create_runtime : Box :: new ( |env| {
223
- preboot ( env) ?;
224
- tokio:: runtime:: Runtime :: new ( )
225
- . context ( "failed to start tokio runtime" ) ?
226
- . block_on ( async {
227
- use spin_trigger:: {
228
- loader:: TriggerLoader , HostComponentInitData , RuntimeConfig ,
229
- TriggerExecutorBuilder ,
230
- } ;
231
- use spin_trigger_http:: HttpTrigger ;
232
- let locked_app = spin_loader:: from_file (
233
- env. path ( ) . join ( "spin.toml" ) ,
234
- spin_loader:: FilesMountStrategy :: Direct ,
235
- None ,
236
- )
237
- . await ?;
238
- let json = locked_app. to_json ( ) ?;
239
- std:: fs:: write ( env. path ( ) . join ( "locked.json" ) , json) ?;
240
-
241
- let loader = TriggerLoader :: new ( env. path ( ) . join ( ".working_dir" ) , false ) ;
242
- let mut builder = TriggerExecutorBuilder :: < HttpTrigger > :: new ( loader) ;
243
- // TODO(rylev): see if we can reuse the builder from spin_trigger instead of duplicating it here
244
- builder. hooks ( spin_trigger:: network:: Network :: default ( ) ) ;
245
- let trigger = builder
246
- . build (
247
- format ! ( "file:{}" , env. path( ) . join( "locked.json" ) . display( ) ) ,
248
- RuntimeConfig :: default ( ) ,
249
- HostComponentInitData :: default ( ) ,
250
- )
251
- . await ?;
252
-
253
- Result :: < _ , anyhow:: Error > :: Ok ( InProcessSpin :: new ( trigger) )
254
- } )
255
- } ) ,
256
- }
257
- }
191
+ pub services_config : ServicesConfig ,
258
192
}
0 commit comments