Configuring flapdoodles embedded mongodb for build pipeline

Viewed 2000

In my Spring Application I have a part that relys on MongoDb, which I have to test.
I want to be able to run the tests locally and on the build server.

Currently I am trying to do this, adding

testCompile group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '2.2.0'

as dependency.

This package downloads the mongo executables from mongodb.org, extracts them and stores them locally. While this works like a charm locally, it is bound to fail on the build server, since that cannot access remote sites.

So I'd like to change the configuration, to use exectuables found in the artifactory repo or - if necessary - that have been added to the project repo.

An interesting pice of code seems to be de.flapdoodle.embed.mongo.config.DownloadConfigBuilder

  • It shows that some environment variable EMBEDDED_MONGO_ARTIFACTS might be used

  • It shows the default download sources, but the paths are not absolute. So I do not know, how my mirror needs to be structured. Visisting the links, I will be redirected.

    private static class PlatformDependentDownloadPath implements IDownloadPath {
        @Override
        public String getPath(Distribution distribution) {
            if (distribution.getPlatform()==Platform.Windows) {
                return "https://downloads.mongodb.org/";
            }
            return "https://fastdl.mongodb.org/";
        }   
    }
    

Then in the Documentation from flapdoodle you find:

  • Customize Download URL
        ...
    Command command = Command.MongoD;
    
    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
        .defaults(command)
        .artifactStore(new ExtractedArtifactStoreBuilder()
            .defaults(command)
            .download(new DownloadConfigBuilder()
                .defaultsForCommand(command)
                .downloadPath("http://my.custom.download.domain/")))
        .build();
    ...
    

but again, this downloadPath does not seem to be absolute.


Briefly:

  • How do I need to structure my download mirror
  • How do I configure the download path to use my mirror - especially with respect to the Spring Autoconfiguration
2 Answers

I know it has been a while since this question has been asked, but maybe it helps.

I managed to manually configure it through the following @Configuration (in your test package).

@Configuration
public class EmbeddedMongoDBConfig {

  @Bean
  public IRuntimeConfig embeddedMongoRuntimeConfig() {
    final Command command = Command.MongoD;
    final IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
        .defaults(command)
        .artifactStore(new ExtractedArtifactStoreBuilder()
            .defaults(command)
            .downloader(new Downloader())
            .download(new DownloadConfigBuilder()
                .downloadPath("http://host/path/to/mongodb/server/mongodb/3.5.5/")
                .fileNaming(new UserTempNaming())
                .downloadPrefix("")
                .artifactStorePath(new PlatformTempDir())
                .progressListener(new Slf4jProgressListener(log))
                .userAgent("")
                .packageResolver(new IPackageResolver() {

                  // this apparently is expected to return the name of the executable (in the download package) 
                  @Override
                  public FileSet getFileSet(Distribution distribution) {
                    String filename = (distribution.getPlatform().isUnixLike()) ? "bin/mongod" : "bin/mongod.exe";
                    return FileSet.builder()
                        .addEntry(FileType.Executable, filename)
                        .build();
                  }

                  @Override
                  public ArchiveType getArchiveType(Distribution distribution) {
                    if(distribution.getPlatform().isUnixLike()) return ArchiveType.TGZ;
                    else return ArchiveType.ZIP;
                  }


                  // the path (appended to the download link above)
                  @Override
                  public String getPath(Distribution distribution) {
                    return (distribution.getPlatform().isUnixLike()) ? "mongodb-3.5.5-linux-x86_64.tgz" : "mongodb-3.5.5-win32-x86_64.zip";
                  }
                })
                .build())
            .executableNaming(new UserTempNaming())
            .build())
        .build();
    return runtimeConfig;
  }

You could just provide the distribution of mongodb you want to test with in your build pipeline on your Build Server

Flapdoodle Embedded Mongodb will download the relevant mongoDB distribution to the users home directory named '.embedmongo'

For example:

.embedmongo/osx/mongodb-osx-x86_64-3.2.22.tgz'

If you download the distribution you want from https://www.mongodb.com/try/download/community and place it in the .embedmongo directory under the relevant OS path.

You can then use a separate Spring Test Profile specifying the version you have downloaded in your spring applications test properties

spring:
  mongodb:
    embedded:
      version: "3.2.22"

Embedded mongo will pick up this distribution to extract and run with.

This way solved the same issue for us. I do wish that you could provide an absolute path to the compressed mongodb distribution you want to use in the spring.mongodb.embed properties so it could be included with the src code

Related