Spring-boot testcontainers 在 3.1 版本中针对特定版本的 Postgres

huangapple go评论69阅读模式
英文:

Spring-boot testcontainers in 3.1 for specific version of Postgres

问题

以下是您提供的文本的翻译:

"spring-boot 3.1的新版本增加了在开发中运行应用程序时管理testcontainers的支持:https://docs.spring.io/spring-boot/docs/3.1.0-SNAPSHOT/reference/html/features.html#features.testing.testcontainers.at-development-time。我尝试让它与我项目中使用的特定postgres版本一起工作,但遇到了问题。

我的第一次尝试:

public class TestDemoApplication {
  public static void main(String[] args) {
    SpringApplication.from(DemoApplication::main)
        .with(MyContainersConfiguration.class)
        .run(args);
  }

  @TestConfiguration
  public static class MyContainersConfiguration {
    @Bean
    @ServiceConnection
    public PostgreSQLContainer<?> postgresContainer() {
      return new PostgreSQLContainer<>("postgresql:15-alpine3.17");
    }
  }
}

这给了我错误:

Caused by: java.lang.IllegalStateException: Failed to verify that image 'postgresql:15-alpine3.17' is a compatible substitute for 'postgres'. This generally means that you are trying to use an image that Testcontainers has not been designed to use. If this is deliberate, and if you are confident that the image is compatible, you should declare compatibility in code using the asCompatibleSubstituteFor method. For example:
DockerImageName myImage = DockerImageName.parse("postgresql:15-alpine3.17").asCompatibleSubstituteFor("postgres");
and then use myImage instead.

所以我尝试了那个建议,将容器定义更改为:

    @Bean
    @ServiceConnection
    public PostgreSQLContainer<?> postgresContainer() {
      return new PostgreSQLContainer<>(
          DockerImageName.parse("postgresql:15-alpine3.17")
              .asCompatibleSubstituteFor("postgres"));
    }

这给了我错误:

Caused by: com.github.dockerjava.api.exception.NotFoundException: Status 404: {"message":"pull access denied for postgresql, repository does not exist or may require 'docker login': denied: requested access to the resource is denied"}

	at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:241) ~[testcontainers-1.18.0.jar:1.18.0]
	at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269) ~[testcontainers-1.18.0.jar:1.18.0]
	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

有关如何使其正常工作的想法吗?

英文:

The new release of spring-boot 3.1 added support for managing testcontainers when running your app in development: https://docs.spring.io/spring-boot/docs/3.1.0-SNAPSHOT/reference/html/features.html#features.testing.testcontainers.at-development-time. I'm attempting to get it to work with the specific postgres version I'm using for my project, but I'm running into issues.

My first attempt:

public class TestDemoApplication {
  public static void main(String[] args) {
    SpringApplication.from(DemoApplication::main)
        .with(MyContainersConfiguration.class)
        .run(args);
  }

  @TestConfiguration
  public static class MyContainersConfiguration {
    @Bean
    @ServiceConnection
    public PostgreSQLContainer&lt;?&gt; postgresContainer() {
      return new PostgreSQLContainer&lt;&gt;(&quot;postgresql:15-alpine3.17&quot;);
    }
  }
}

This gives me the error:
> Caused by: java.lang.IllegalStateException: Failed to verify that image 'postgresql:15-alpine3.17' is a compatible substitute for 'postgres'. This generally means that you are trying to use an image that Testcontainers has not been designed to use. If this is deliberate, and if you are confident that the image is compatible, you should declare compatibility in code using the asCompatibleSubstituteFor method. For example:
DockerImageName myImage = DockerImageName.parse("postgresql:15-alpine3.17").asCompatibleSubstituteFor("postgres");
and then use myImage instead.

So I try out that suggestion and change the container definition to:

    @Bean
    @ServiceConnection
    public PostgreSQLContainer&lt;?&gt; postgresContainer() {
      return new PostgreSQLContainer&lt;&gt;(
          DockerImageName.parse(&quot;postgresql:15-alpine3.17&quot;)
              .asCompatibleSubstituteFor(&quot;postgres&quot;));
    }

That gives me the error:

Caused by: com.github.dockerjava.api.exception.NotFoundException: Status 404: {&quot;message&quot;:&quot;pull access denied for postgresql, repository does not exist or may require &#39;docker login&#39;: denied: requested access to the resource is denied&quot;}

	at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:241) ~[testcontainers-1.18.0.jar:1.18.0]
	at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269) ~[testcontainers-1.18.0.jar:1.18.0]
	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

Any ideas about how to get this working?

答案1

得分: 1

右侧的图像是postgres:15-alpine3.17,你可以在DockerHub上找到它。关于jdbc:tc:postgresql:15-alpine3.17:///test?TC_TMPFS=/testtmpfs:rw的疑问,postgresql是Testcontainers中的数据库类型名称,与镜像名称无关。你可以在这里找到一些示例2。此外,异常消息明确指出了要使用的镜像:)

英文:

The right image is postgres:15-alpine3.17, which you can find in DockerHub. Regarding to the doubt about jdbc:tc:postgresql:15-alpine3.17:///test?TC_TMPFS=/testtmpfs:rw, postgresql is the database type name in Testcontainers and it is not related to the image name. You can find some examples here. Also, the exception message was clear about the image to use Spring-boot testcontainers 在 3.1 版本中针对特定版本的 Postgres

答案2

得分: 0

Andrey的建议是正确的。我在application.yml中使用了与当前配置相同的图像名称。但是,由于某种原因,图像名称需要更改。

这是旧的配置,它曾经有效:

spring:
  datasource:
    url: jdbc:tc:postgresql:15-alpine3.17:///test?TC_TMPFS=/testtmpfs:rw

要使spring-boot的Java风格配置起作用,这是等效的:

    @Bean
    @ServiceConnection
    public PostgreSQLContainer<?> postgresContainer() {
      return new PostgreSQLContainer<>(
          DockerImageName.parse("postgres:15-alpine3.17"));
    }

对我来说不太清楚为什么图像名称不同,或者这在哪里有记录。

英文:

Andrey's suggestion was correct. I used the same image name from my current configuration in application.yml. However, the image name needs to change for some reason.

This is the old configuration that was working:

spring:
  datasource:
    url: jdbc:tc:postgresql:15-alpine3.17:///test?TC_TMPFS=/testtmpfs:rw

To get the spring-boot java-style configuration working, this is the equivalent:

    @Bean
    @ServiceConnection
    public PostgreSQLContainer&lt;?&gt; postgresContainer() {
      return new PostgreSQLContainer&lt;&gt;(
          DockerImageName.parse(&quot;postgres:15-alpine3.17&quot;));
    }

It's not clear to me why the image name is different, or where this is documented.

huangapple
  • 本文由 发表于 2023年5月21日 07:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定