@@ -16,7 +16,7 @@ use spin_app::{
1616 AppComponent , Loader ,
1717} ;
1818use spin_core:: { Module , StoreBuilder } ;
19- use spin_http:: { HttpExecutorType , HttpTrigger , HttpTriggerConfig , WagiTriggerConfig } ;
19+ use spin_http:: { HttpExecutorType , HttpTriggerConfig , WagiTriggerConfig } ;
2020use spin_trigger:: { TriggerExecutor , TriggerExecutorBuilder } ;
2121
2222pub use tokio;
@@ -37,13 +37,18 @@ macro_rules! from_json {
3737}
3838
3939#[ derive( Default ) ]
40- pub struct TestConfig {
40+ pub struct HttpTestConfig {
4141 module_path : Option < PathBuf > ,
4242 http_trigger_config : HttpTriggerConfig ,
43+ }
44+
45+ #[ derive( Default ) ]
46+ pub struct RedisTestConfig {
47+ module_path : Option < PathBuf > ,
4348 redis_channel : String ,
4449}
4550
46- impl TestConfig {
51+ impl HttpTestConfig {
4752 pub fn module_path ( & mut self , path : impl Into < PathBuf > ) -> & mut Self {
4853 init_tracing ( ) ;
4954 self . module_path = Some ( path. into ( ) ) ;
@@ -80,9 +85,22 @@ impl TestConfig {
8085 self
8186 }
8287
83- pub fn redis_trigger ( & mut self , channel : impl Into < String > ) -> & mut Self {
84- self . redis_channel = channel. into ( ) ;
85- self
88+ pub fn build_loader ( & self ) -> impl Loader {
89+ init_tracing ( ) ;
90+ TestLoader {
91+ app : self . build_locked_app ( ) ,
92+ module_path : self . module_path . clone ( ) . expect ( "module path to be set" ) ,
93+ }
94+ }
95+
96+ pub async fn build_trigger < Executor : TriggerExecutor > ( & self ) -> Executor
97+ where
98+ Executor :: TriggerConfig : DeserializeOwned ,
99+ {
100+ TriggerExecutorBuilder :: new ( self . build_loader ( ) )
101+ . build ( TEST_APP_URI . to_string ( ) )
102+ . await
103+ . unwrap ( )
86104 }
87105
88106 pub fn build_locked_app ( & self ) -> LockedApp {
@@ -100,7 +118,7 @@ impl TestConfig {
100118 "trigger_config" : self . http_trigger_config,
101119 } ,
102120 ] ) ;
103- let metadata = from_json ! ( { "name" : "test-app" , "trigger" : { "address " : "test-redis-host " , "type " : "redis " } } ) ;
121+ let metadata = from_json ! ( { "name" : "test-app" , "trigger" : { "type " : "http " , "base " : "/ " } } ) ;
104122 let variables = Default :: default ( ) ;
105123 LockedApp {
106124 spin_lock_version : spin_app:: locked:: FixedVersion ,
@@ -110,6 +128,22 @@ impl TestConfig {
110128 variables,
111129 }
112130 }
131+ }
132+
133+ impl RedisTestConfig {
134+ pub fn module_path ( & mut self , path : impl Into < PathBuf > ) -> & mut Self {
135+ init_tracing ( ) ;
136+ self . module_path = Some ( path. into ( ) ) ;
137+ self
138+ }
139+
140+ pub fn test_program ( & mut self , name : impl AsRef < Path > ) -> & mut Self {
141+ self . module_path (
142+ PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) )
143+ . join ( "../../target/test-programs" )
144+ . join ( name) ,
145+ )
146+ }
113147
114148 pub fn build_loader ( & self ) -> impl Loader {
115149 init_tracing ( ) ;
@@ -119,18 +153,42 @@ impl TestConfig {
119153 }
120154 }
121155
122- pub async fn build_trigger < Executor : TriggerExecutor > ( & self ) -> Executor
156+ pub async fn build_trigger < Executor : TriggerExecutor > ( & mut self , channel : & str ) -> Executor
123157 where
124158 Executor :: TriggerConfig : DeserializeOwned ,
125159 {
160+ self . redis_channel = channel. into ( ) ;
161+
126162 TriggerExecutorBuilder :: new ( self . build_loader ( ) )
127163 . build ( TEST_APP_URI . to_string ( ) )
128164 . await
129165 . unwrap ( )
130166 }
131167
132- pub async fn build_http_trigger ( & self ) -> HttpTrigger {
133- self . build_trigger ( ) . await
168+ pub fn build_locked_app ( & self ) -> LockedApp {
169+ let components = from_json ! ( [ {
170+ "id" : "test-component" ,
171+ "source" : {
172+ "content_type" : "application/wasm" ,
173+ "digest" : "test-source" ,
174+ } ,
175+ } ] ) ;
176+ let triggers = from_json ! ( [
177+ {
178+ "id" : "trigger--test-app" ,
179+ "trigger_type" : "redis" ,
180+ "trigger_config" : { "channel" : self . redis_channel, "component" : "test-component" } ,
181+ } ,
182+ ] ) ;
183+ let metadata = from_json ! ( { "name" : "test-app" , "trigger" : { "address" : "test-redis-host" , "type" : "redis" } } ) ;
184+ let variables = Default :: default ( ) ;
185+ LockedApp {
186+ spin_lock_version : spin_app:: locked:: FixedVersion ,
187+ components,
188+ triggers,
189+ metadata,
190+ variables,
191+ }
134192 }
135193}
136194
0 commit comments