-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade to Mongo 7.0.4
- Loading branch information
Showing
9 changed files
with
257 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package us.kbase.test.auth2; | ||
|
||
import static us.kbase.common.test.controllers.ControllerCommon.checkExe; | ||
import static us.kbase.common.test.controllers.ControllerCommon.findFreePort; | ||
import static us.kbase.common.test.controllers.ControllerCommon.makeTempDirs; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.util.stream.Collectors; | ||
|
||
import com.github.zafarkhaja.semver.Version; | ||
import org.apache.commons.io.FileUtils; | ||
|
||
|
||
/** Q&D Utility to run a Mongo server for the purposes of testing from | ||
* Java. | ||
* @author [email protected], [email protected] | ||
* | ||
*/ | ||
public class MongoController { | ||
|
||
private final static String DATA_DIR = "data"; | ||
|
||
private final static List<String> tempDirectories = | ||
new LinkedList<String>(); | ||
static { | ||
tempDirectories.add(DATA_DIR); | ||
} | ||
|
||
private final static Version MONGO_DB_6_1 = | ||
Version.forIntegers(6,1); | ||
|
||
private final Path tempDir; | ||
|
||
private final Process mongo; | ||
|
||
private final int port; | ||
|
||
public MongoController( | ||
final String mongoExe, | ||
final Path rootTempDir) | ||
throws Exception { | ||
this(mongoExe, rootTempDir, false); | ||
} | ||
|
||
public MongoController( | ||
final String mongoExe, | ||
final Path rootTempDir, | ||
final boolean useWiredTiger) | ||
throws Exception { | ||
checkExe(mongoExe, "mongod server"); | ||
tempDir = makeTempDirs(rootTempDir, "MongoController-", tempDirectories); | ||
port = findFreePort(); | ||
Version dbVer = getMongoDBVer(mongoExe); | ||
List<String> command = getMongoServerStartCommand(mongoExe, useWiredTiger, dbVer); | ||
mongo = startProcess(command); | ||
} | ||
|
||
public int getServerPort() { | ||
return port; | ||
} | ||
|
||
public Path getTempDir() { | ||
return tempDir; | ||
} | ||
|
||
public void destroy(boolean deleteTempFiles) throws IOException { | ||
if (mongo != null) { | ||
mongo.destroy(); | ||
} | ||
if (tempDir != null && deleteTempFiles) { | ||
FileUtils.deleteDirectory(tempDir.toFile()); | ||
} | ||
} | ||
|
||
private static Version getMongoDBVer(final String mongoExe) throws IOException { | ||
|
||
// build MongoDB version check command | ||
List<String> command = new LinkedList<String>(); | ||
command.addAll(Arrays.asList(mongoExe, "--version")); | ||
|
||
// start MongoDB version check process | ||
ProcessBuilder checkVerPb = new ProcessBuilder(command); | ||
Process checkVerProcess = checkVerPb.start(); | ||
|
||
// parse mongod --version output string | ||
String dbVer = new BufferedReader( | ||
new InputStreamReader(checkVerProcess.getInputStream())) | ||
.lines() | ||
.collect(Collectors.joining(" ")) | ||
.split(" ")[2].substring(1); | ||
|
||
System.out.println("MongoDB version: " + dbVer); | ||
checkVerProcess.destroy(); | ||
return Version.valueOf(dbVer); | ||
} | ||
|
||
private List<String> getMongoServerStartCommand(final String mongoExe, | ||
final boolean useWiredTiger, | ||
final Version dbVer) { | ||
List<String> command = new LinkedList<String>(); | ||
command.addAll(Arrays.asList(mongoExe, "--port", "" + port, | ||
"--dbpath", tempDir.resolve(DATA_DIR).toString())); | ||
|
||
// Starting in MongoDB 6.1, journaling is always enabled. | ||
// As a result, MongoDB removes the storage.journal.enabled option | ||
// and the corresponding --journal and --nojournal command-line options. | ||
// https://www.mongodb.com/docs/manual/release-notes/6.1/#changes-to-journaling | ||
if (dbVer.lessThan(MONGO_DB_6_1)) { | ||
command.addAll(Arrays.asList("--nojournal")); | ||
} | ||
if (useWiredTiger) { | ||
command.addAll(Arrays.asList("--storageEngine", "wiredTiger")); | ||
} | ||
return command; | ||
} | ||
|
||
private Process startProcess(List<String> command) throws Exception { | ||
ProcessBuilder servpb = new ProcessBuilder(command) | ||
.redirectErrorStream(true) | ||
.redirectOutput(getTempDir().resolve("mongo.log").toFile()); | ||
|
||
Process mongoProcess = servpb.start(); | ||
Thread.sleep(1000); //wait for server to start up | ||
return mongoProcess; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
us.kbase.common.test.controllers.mongo.MongoController ac = new us.kbase.common.test.controllers.mongo.MongoController( | ||
"/kb/runtime/bin/mongod", | ||
Paths.get("workspacetesttemp")); | ||
System.out.println(ac.getServerPort()); | ||
System.out.println(ac.getTempDir()); | ||
Scanner reader = new Scanner(System.in); | ||
System.out.println("any char to shut down"); | ||
//get user input for a | ||
reader.next(); | ||
ac.destroy(false); | ||
reader.close(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.