@@ -35,6 +35,7 @@ public static void startServices() {
35
35
OAuthProviderService .startService ();
36
36
PostgresqlService .startService ();
37
37
MysqlService .startService ();
38
+ MongodbService .startService ();
38
39
} catch (Exception e ) {
39
40
throw new RuntimeException (e );
40
41
}
@@ -44,6 +45,7 @@ public static void killServices() throws InterruptedException, IOException {
44
45
OAuthProviderService .killService ();
45
46
PostgresqlService .killService ();
46
47
MysqlService .killService ();
48
+ MongodbService .killService ();
47
49
}
48
50
49
51
private static class CmdHelper {
@@ -173,6 +175,60 @@ public static void killService() throws IOException, InterruptedException {
173
175
}
174
176
}
175
177
178
+ private static class MongodbService {
179
+ private static final String MONGODB_SERVICE_NAME = "mongodb_" + System .getProperty ("org.gradle.test.worker" , "base" );
180
+ private static final int MONGODB_PORT = new Random ().nextInt (5000 ) + 30000 ;
181
+
182
+ static {
183
+ System .setProperty ("ST_MONGODB_PLUGIN_SERVER_PORT" , "" + MONGODB_PORT );
184
+ }
185
+
186
+ private static int runCommand (String command ) throws InterruptedException , IOException {
187
+ System .out .println ("Running command: " + command );
188
+ return CmdHelper .runCommand (new String [] {
189
+ "docker" , "exec" , MONGODB_SERVICE_NAME , "mongosh" , "--eval" , command
190
+ });
191
+ }
192
+
193
+ public static void startService () throws IOException , InterruptedException {
194
+ if (System .getProperty ("ST_PLUGIN_NAME" , "" ).isBlank () || System .getProperty ("ST_PLUGIN_NAME" , "" ).equals ("mongodb" )) {
195
+ int exitCode = CmdHelper .runCommand (new String [] {
196
+ "docker" , "run" , "--rm" , "--name" , MONGODB_SERVICE_NAME ,
197
+ "-d" , "-p" , MONGODB_PORT + ":27017" ,
198
+ "-e" , "MONGO_INITDB_ROOT_USERNAME=root" ,
199
+ "-e" , "MONGO_INITDB_ROOT_PASSWORD=root" ,
200
+ "mongo:latest"
201
+ });
202
+
203
+ if (exitCode != 0 ) {
204
+ throw new RuntimeException ("Failed to start MongoDB service" );
205
+ }
206
+
207
+ // Wait for MongoDB to be ready
208
+ for (int i = 0 ; i < 1000 ; i ++) {
209
+ exitCode = CmdHelper .runCommand (new String [] {
210
+ "docker" , "exec" , MONGODB_SERVICE_NAME , "mongosh" , "--eval" , "db.version()"
211
+ });
212
+ if (exitCode == 0 ) {
213
+ break ;
214
+ }
215
+ Thread .sleep (200 );
216
+ }
217
+
218
+ // Create databases
219
+ while (runCommand ("use supertokens" ) != 0 ) {
220
+ Thread .sleep (200 );
221
+ }
222
+ }
223
+ }
224
+
225
+ public static void killService () throws IOException , InterruptedException {
226
+ CmdHelper .runCommand (new String [] {
227
+ "docker" , "stop" , MONGODB_SERVICE_NAME
228
+ });
229
+ }
230
+ }
231
+
176
232
private static class OAuthProviderService {
177
233
private static final int SVC_PORT1 = new Random ().nextInt (2500 ) * 2 + 25000 ;
178
234
private static final int SVC_PORT2 = SVC_PORT1 + 1 ;
0 commit comments