testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

MongoDBContainer: simplify loading and executing java script files to init DB

Open silaev opened this issue 4 years ago • 3 comments

MongoDBContainer runs with the --replSet command-line option to support testing features available only for a replica set. At the docker-entrypoint-initdb.d stage, a single replica set node is not yet initialised. For example, an attempt to write and read to non-initalised replica set leads to NotWritablePrimary and NotPrimaryNoSecondaryOk errors respectively. Therefore data manipulation might be done after initialisation stage.

This PR simplifies loading and executing java script files after container start-up. It also adds the fail-fast extra check of the docker-entrypoint-initdb.d directory to be empty before the initialisation of the replica set.

silaev avatar Mar 10 '21 08:03 silaev

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this.

stale[bot] avatar Jun 18 '21 23:06 stale[bot]

Hi @silaev,

Thanks for your work to enhance the process of initialization data. Before this PR gets merged into the master, may I confirm that if it's a workaround to use withCommand() to override withCommand("--replSet", "docker-rs") in the constructor when create the container instance, but the cost is losing the replica set feature?

Here is the snippet:

private val container: MongoDBContainer = MongoDBContainer("mongo:latest")
    .withCommand()
    .withCopyFileToContainer(
        MountableFile.forClasspathResource("init/init.js"),
        "/docker-entrypoint-initdb.d/init.js"
    )
    .waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 2))
    .withStartupTimeout(Duration.ofSeconds(10))

liununu avatar Apr 25 '22 13:04 liununu

Hi @silaev,

Thanks for your work to enhance the process of initialization data. Before this PR gets merged into the master, may I confirm that if it's a workaround to use withCommand() to override withCommand("--replSet", "docker-rs") in the constructor when create the container instance, but the cost is losing the replica set feature?

Here is the snippet:

private val container: MongoDBContainer = MongoDBContainer("mongo:latest")
    .withCommand()
    .withCopyFileToContainer(
        MountableFile.forClasspathResource("init/init.js"),
        "/docker-entrypoint-initdb.d/init.js"
    )
    .waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 2))
    .withStartupTimeout(Duration.ofSeconds(10))

Hey there, sorry for answering late. Yes, that's right. Pls, find more on it here

silaev avatar Aug 08 '22 12:08 silaev