【问题标题】:Configuring flapdoodles embedded mongodb for build pipeline为构建管道配置fladdoodles嵌入式mongodb
【发布时间】:2020-08-27 09:53:09
【问题描述】:

在我的 Spring 应用程序中,我有一部分依赖于 MongoDb,我必须对其进行测试。
我希望能够在本地和构建服务器上运行测试。

目前我正在尝试这样做,添加

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

作为依赖。

这个包从 mongodb.org 下载 mongo 可执行文件,提取它们并存储在本地。 虽然这在本地很有效,但在构建服务器上肯定会失败,因为它无法访问远程站点。

所以我想更改配置,使用工件存储库中的可执行文件,或者 - 如果需要 - 已添加到项目存储库中。

一段有趣的代码似乎是de.flapdoodle.embed.mongo.config.DownloadConfigBuilder

  • 这表明可能使用了一些环境变量EMBEDDED_MONGO_ARTIFACTS

  • 它显示了默认的下载源,但路径不是绝对的。所以我不知道,我的镜子需要如何构造。访问链接,我将被重定向。

    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/";
        }   
    }
    

然后在来自 fladdoodle 的文档中,您会发现:

  • 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();
    ...
    

但同样,这个downloadPath似乎不是绝对的。


简单地说:

  • 我需要如何构建我的下载镜像
  • 如何配置下载路径以使用我的镜像 - 特别是关于 Spring 自动配置

【问题讨论】:

    标签: spring mongodb spring-data-mongodb spring-test


    【解决方案1】:

    我知道这个问题被问到已经有一段时间了,但也许它会有所帮助。

    我设法通过以下@Configuration(在您的测试包中)手动配置它。

    @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;
      }
    

    【讨论】:

      【解决方案2】:

      您可以在构建服务器上的构建管道中提供您想要测试的 mongodb 分布

      Flapdoodle Embedded Mongodb 会将相关的 mongoDB 分发下载到名为“.embedmongo”的用户主目录

      例如:

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

      如果你从https://www.mongodb.com/try/download/community下载你想要的发行版,并将它放在相关操作系统路径下的.embedmongo目录中。

      然后您可以使用单独的 Spring 测试配置文件指定您在 Spring 应用程序测试属性中下载的版本

      spring:
        mongodb:
          embedded:
            version: "3.2.22"
      

      嵌入式 mongo 将选择此发行版进行提取和运行。

      这种方式为我们解决了同样的问题。我确实希望您可以提供要在 spring.mongodb.embed 属性中使用的压缩 mongodb 发行版的绝对路径,以便它可以包含在 src 代码中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-06
        • 2018-02-07
        • 1970-01-01
        • 1970-01-01
        • 2019-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多