Skip to content

Commit c647f40

Browse files
SattvikSattvik
Sattvik
authored and
Sattvik
committed
fix: add mongo service
1 parent 24fcf67 commit c647f40

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/test/java/io/supertokens/test/TestServiceUtils.java

+56
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static void startServices() {
3535
OAuthProviderService.startService();
3636
PostgresqlService.startService();
3737
MysqlService.startService();
38+
MongodbService.startService();
3839
} catch (Exception e) {
3940
throw new RuntimeException(e);
4041
}
@@ -44,6 +45,7 @@ public static void killServices() throws InterruptedException, IOException {
4445
OAuthProviderService.killService();
4546
PostgresqlService.killService();
4647
MysqlService.killService();
48+
MongodbService.killService();
4749
}
4850

4951
private static class CmdHelper {
@@ -173,6 +175,60 @@ public static void killService() throws IOException, InterruptedException {
173175
}
174176
}
175177

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+
176232
private static class OAuthProviderService {
177233
private static final int SVC_PORT1 = new Random().nextInt(2500) * 2 + 25000;
178234
private static final int SVC_PORT2 = SVC_PORT1 + 1;

0 commit comments

Comments
 (0)