flapdoodle-oss / de.flapdoodle.embed.mongo

...will provide a platform neutral way for running mongodb in unittests.
Apache License 2.0
908 stars 160 forks source link

Is it possible to create replica set on new flapdoodle library 3.0.0, please share code snippet #349

Closed ShinojShayin closed 2 years ago

masa2146 commented 3 years ago

Hello I created mongo replica set for my tests. Here is my code

@TestConfiguration
public class EmbeddedMongoConfig {

    @Value("${spring.data.mongodb.database:my_db}")
    private String mongoDbName;

    @Value("${spring.data.mongodb.port:27018}")
    private Integer port;

    private static final String IP = "localhost";

    private MongoClient client;

    private MongodExecutable mongodExecutable;

    @PostConstruct
    public void createMongoDB() throws IOException {
        String mongoUri = "mongodb://" + IP + ":" + port;
        MongoCmdOptions cmdOptions = MongoCmdOptions.builder()
                .useNoPrealloc(false)
                .useSmallFiles(false)
                .isVerbose(false)
                .master(false)
                .useNoJournal(false)
                .syncDelay(0)
                .build();

        MongodConfig mongodConfig = MongodConfig.builder().version(Version.Main.PRODUCTION)
                .net(new Net(IP, port, Network.localhostIsIPv6()))
                .replication(new Storage(null, "testRepSet", 5000))
                .cmdOptions(cmdOptions)
                .build();

        MongodStarter starter = MongodStarter.getDefaultInstance();
        mongodExecutable = starter.prepare(mongodConfig);
        mongodExecutable.start();

        client = MongoClients.create(mongoUri);

        try {
            client.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document()));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @PreDestroy
    public void onDestroy() {
        if (mongodExecutable != null) {
            mongodExecutable.stop();
        }
    }

    @Bean(name = "mongoTestTemplate")
    public MongoTemplate mongoTemplateTest() {
        return new MongoTemplate(client, mongoDbName);
    }

    @Bean(destroyMethod = "stop")
    public MongodProcess mongodProcess(MongodExecutable mongodExecutable) throws IOException {
        return mongodExecutable.start();
    }

    @Bean(destroyMethod = "stop")
    public MongodExecutable mongodExecutable(MongodStarter mongodStarter, MongodConfig mongodConfig) {
        return mongodStarter.prepare(mongodConfig);
    }

    @Bean
    public MongodConfig mongodConfig() {
        return MongodConfig.builder().version(Version.Main.PRODUCTION).build();
    }

    @Bean
    public MongodStarter mongodStarter() {
        return MongodStarter.getDefaultInstance();
    }
}
michaelmosmann commented 3 years ago

Maybe i add this to the documentation...

noway1979 commented 2 years ago

I followed https://blog.devgenius.io/how-to-enable-replica-set-in-embbedded-mongo-with-spring-boot-ddeaa079c1c8 and the main catch is to enable journaling (similar to @masa2146 example).

and then forked https://github.com/noway1979/springboot-embedded-mongo-replset-demo which appears to be working without copying the original AutoConfiguration class and therefore less code duplication.

I dont know if that is actually the canonical way to do it.

Maybe it would be better if this project allows to declare some EmbeddedMongoDBConfigCustomizer bean, like it can be seen for other SpringBoot Autoconfigurations.

Or expose the builder as bean and construct default MongodConfig bean by builder.build().

Last but not least, I was wondering why journaling is not enabled by default whenever a replica set is configured. But maybe I am missing some facts here...

michaelmosmann commented 2 years ago

As part of a big refactoring i was able to create a working setup as described in https://www.mongodb.com/docs/manual/tutorial/deploy-shard-cluster/

the code is here: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/embed-mongo-4.x/src/test/java/de/flapdoodle/embed/mongo/transitions/MongosTest.java

but it is using this beta version: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/tree/embed-mongo-4.x

matching spring integration: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/tree/spring-2.7.x--embed-mongo-4.x

michaelmosmann commented 2 years ago

I will close this issue:)

Azbesciak commented 1 year ago

@michaelmosmann Maybe it would be good to provide some utility for that to prepare just a cluster for tests, which probably is mostly used? I just updated to 4.3.2 (need for spring boot 3.0) and there is no MongodConfig for example. In general, I do not want to get into these details, especially when it is only for tests.