0

我正在使用内部使用 Hikari 连接池的 springboot。

我正在使用 testcontainers 运行我的数据库集成测试,这在本地机器上运行良好。

但是当我在 Jenkins CI/CD 管道上运行相同的测试时,它会因错误而失败。

以下是详细信息。

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
  </parent>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>testcontainers</artifactId>
      <version>1.16.1</version>
    </dependency>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>postgresql</artifactId>
      <version>1.16.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>1.16.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>rabbitmq</artifactId>
      <version>1.16.1</version>
      <scope>test</scope>
    </dependency>


   <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>5.0.0</version>
    </dependency>

测试容器初始化

import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;

@TestConfiguration
@EnableJpaRepositories
public class TestDatabaseInitializer
    implements ApplicationContextInitializer<ConfigurableApplicationContext>, DisposableBean {

  Network network = Network.newNetwork();

  @Container
  public static PostgreSQLContainer postgreSQLContainer =
      new PostgreSQLContainer("postgres:10.11")
          .withDatabaseName("report")
          .withUsername("test-user")
          .withPassword("test-password");

  @Override
  public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
    postgreSQLContainer.start();
    TestPropertyValues.of(
        "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
        "spring.datasource.username=" + postgreSQLContainer.getUsername(),
        "spring.datasource.password=" + postgreSQLContainer.getPassword())
        .applyTo(configurableApplicationContext.getEnvironment());
  }

  @Override
  public void destroy() {
    if (postgreSQLContainer != null && postgreSQLContainer.isRunning()) {
      postgreSQLContainer.stop();
    }
  }
}

我正在使用 flyway 进行数据库迁移。

此代码在本地构建上运行良好。但是当我在 Jenkins 上运行相同时,它给出了一个错误。

我在Kubernetes版本 v1.21.9 上运行我的 jenkins,并且 containerId 是containerd://1.4.12

这是堆栈跟踪。

2022-02-27;14:59:22.885;main;DEBUG;c.z.hikari.HikariConfig;allowPoolSuspension.............false
2022-02-27;14:59:22.885;main;DEBUG;c.z.hikari.HikariConfig;autoCommit......................true
2022-02-27;14:59:22.885;main;DEBUG;c.z.hikari.HikariConfig;catalog.........................none
2022-02-27;14:59:22.885;main;DEBUG;c.z.hikari.HikariConfig;connectionInitSql...............none
2022-02-27;14:59:22.885;main;DEBUG;c.z.hikari.HikariConfig;connectionTestQuery.............none
2022-02-27;14:59:22.885;main;DEBUG;c.z.hikari.HikariConfig;connectionTimeout...............30000
2022-02-27;14:59:22.885;main;DEBUG;c.z.hikari.HikariConfig;dataSource......................none
2022-02-27;14:59:22.885;main;DEBUG;c.z.hikari.HikariConfig;dataSourceClassName.............none
2022-02-27;14:59:22.885;main;DEBUG;c.z.hikari.HikariConfig;dataSourceJNDI..................none
2022-02-27;14:59:22.886;main;DEBUG;c.z.hikari.HikariConfig;dataSourceProperties............{password=<masked>}
2022-02-27;14:59:22.886;main;DEBUG;c.z.hikari.HikariConfig;driverClassName................."org.postgresql.Driver"
2022-02-27;14:59:22.886;main;DEBUG;c.z.hikari.HikariConfig;exceptionOverrideClassName......none
2022-02-27;14:59:22.886;main;DEBUG;c.z.hikari.HikariConfig;healthCheckProperties...........{}
2022-02-27;14:59:22.886;main;DEBUG;c.z.hikari.HikariConfig;healthCheckRegistry.............none
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;idleTimeout.....................600000
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;initializationFailTimeout.......1
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;isolateInternalQueries..........false
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;jdbcUrl.........................jdbc:postgresql://localhost:49214/report?loggerLevel=OFF
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;keepaliveTime...................0
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;leakDetectionThreshold..........0
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;maxLifetime.....................1800000
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;maximumPoolSize.................10
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;metricRegistry..................none
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;metricsTrackerFactory...........none
2022-02-27;14:59:22.887;main;DEBUG;c.z.hikari.HikariConfig;minimumIdle.....................10
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;password........................<masked>
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;poolName........................"HikariPool-1"
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;readOnly........................false
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;registerMbeans..................false
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;scheduledExecutor...............none
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;schema..........................none
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;threadFactory...................internal
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;transactionIsolation............default
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;username........................"test-user"
2022-02-27;14:59:22.888;main;DEBUG;c.z.hikari.HikariConfig;validationTimeout...............5000
2022-02-27;14:59:22.888;main; INFO;c.z.h.HikariDataSource;HikariPool-1 - Starting...
2022-02-27;14:59:22.919;main;DEBUG;o.p.c.v.ConnectionFactoryImpl;Trying to establish a protocol version 3 connection to localhost:49214
2022-02-27;14:59:22.929;main;DEBUG;c.z.hikari.pool.PoolBase;HikariPool-1 - Failed to create/setup connection: Connection to localhost:49214 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
2022-02-27;14:59:22.932;main;DEBUG;c.z.h.pool.HikariPool;HikariPool-1 - Cannot acquire connection from data source
org.postgresql.util.PSQLException: Connection to localhost:49214 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:284)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
    at org.postgresql.Driver.makeConnection(Driver.java:459)
    at org.postgresql.Driver.connect(Driver.java:261)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100)
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
    at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56)
    at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:80)
    at org.flywaydb.core.Flyway.execute(Flyway.java:437)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:147)
    at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:65)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$634/0x0000000025a622e0.getObject(Unknown Source)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:126)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
    at org.springframework.cloud.contract.wiremock.WireMockTestExecutionListener.applicationContextBroken(WireMockTestExecutionListener.java:123)
    at org.springframework.cloud.contract.wiremock.WireMockTestExecutionListener.beforeTestClass(WireMockTestExecutionListener.java:40)
    at org.springframework.test.context.TestContextManager.beforeTestClass(TestContextManager.java:213)
    at org.springframework.test.context.junit.jupiter.SpringExtension.beforeAll(SpringExtension.java:77)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$7(ClassBasedTestDescriptor.java:355)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor$$Lambda$261/0x0000000024edb680.execute(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:355)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:189)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:77)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:132)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$216/0x0000000024ec7490.execute(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$215/0x0000000024ec6ff0.invoke(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$214/0x0000000024ec6320.execute(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService$$Lambda$220/0x0000000024eb9900.accept(Unknown Source)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$216/0x0000000024ec7490.execute(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$215/0x0000000024ec6ff0.invoke(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$214/0x0000000024ec6320.execute(Unknown Source)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:142)
    at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:117)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:383)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:344)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:417)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:609)
    at org.postgresql.core.PGStream.<init>(PGStream.java:81)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:92)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:196)
    ... 85 common frames omitted
4

0 回答 0