亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過測試容器啟動 Informix?

如何通過測試容器啟動 Informix?

互換的青春 2023-03-09 14:17:17
我對在我的項目中使用測試容器非常感興趣。但是,我很難將其設置為與 Informix 一起使用。請注意,我可以使用 Docker-for-Mac 啟動一個 informix 容器,它將構建并啟動。雖然不確定它是否可以與測試容器一起使用。我希望它會。這是我到目前為止所擁有的測試類package com.example.demo;import com.github.dockerjava.api.command.CreateContainerCmd;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import org.testcontainers.containers.GenericContainer;import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;import org.testcontainers.containers.wait.strategy.WaitAllStrategy;import java.time.Duration;import static org.junit.Assert.assertEquals;@RunWith(SpringRunner.class)@SpringBootTestpublic class DemoApplicationTests {  private static GenericContainer informix;  @BeforeClass  public static void init() {    informix = new GenericContainer("ibmcom/informix-innovator-c")        .withExposedPorts(9088)        .withEnv("LICENSE", "accept")        .withPrivilegedMode(true)        .withCreateContainerCmdModifier(command -> ((CreateContainerCmd)command).withTty(Boolean.TRUE))        .waitingFor(new WaitAllStrategy().withStrategy(new LogMessageWaitStrategy().withRegEx(".*listener on port.*\n"))            .withStrategy(new HostPortWaitStrategy())            .withStartupTimeout(Duration.ofMinutes(2)));    informix.start();  }  @AfterClass  public static void destroy(){    informix.close();  }  @Test  public void testDemo() {    int foo = 1;    assertEquals(foo, 1);  }}容器啟動然后永遠掛起,永遠不會進入測試
查看完整描述

1 回答

?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

Informix 的 docker 鏡像配置錯誤。在 docker 容器中啟動的服務器只會監聽主機名,而不是本地主機。Testcontainers 使用“localhost”作為網絡接口來連接到您的容器。因此,當您使用.withExposedPorts(9088)該端口時,該端口實際上并未暴露在 TestContainers 可以連接到的網絡接口上。


這就是為什么即使您等待日志消息,您仍然很可能遇到問題,您也在端口上等待并且它永遠不可用。


好消息是,這個問題現在已經修復,可以通過下載最新的 Informix docker 鏡像來使用


ibmcom/informix-developer-database:latest獲取最新的 14.10 docker 鏡像


下面是我運行的代碼,用于驗證新圖像是否與 TestContainers 一起更好地工作。

public class DockerTest {

    GenericContainer<?>container  = new GenericContainer<>("ibmcom/informix-developer-database:latest")

        .withExposedPorts(9088, 9089, 27017, 27018, 27883).withEnv("LICENSE", "accept");

@Test

public void testIfxContainer() throws Exception {

    container.start();

    System.out.println("Informix started");

    //test the connection

    try(Connection c = DriverManager.getConnection("jdbc:informix-sqli:localhost:"  + container.getFirstMappedPort() + "/sysmaster:user=informix;password=your-password")) {

      try(Statement s = c.createStatement(); ResultSet rs = s.executeQuery("SELECT FIRST 10 tabname from systables");) {

        while(rs.next()) {

          System.out.println(r.getString(1));

        }

      }

    }

  }

}


查看完整回答
反對 回復 2023-03-09
  • 1 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號