testcontainers / testcontainers-scala

Docker containers for testing in scala
MIT License
637 stars 127 forks source link

sqlserver got Mapped port can only be obtained after the container is started error #251

Open qomosoloto opened 1 year ago

qomosoloto commented 1 year ago

The container is :

override def container: MSSQLServerContainer = new MSSQLServerContainer(
    dockerImageName = DockerImageName.parse("mcr.microsoft.com/mssql/server:2022-latest")
  )

And in the afterStart() hook:

    super.afterStart()
    logger.info(s"driver name: ${container.driverClassName};;;;started: ${container.container.isHealthy}")
    Class.forName(container.driverClassName)
    val properties = new Properties();
    properties.setProperty("user", container.username);
    properties.setProperty("password", container.password)
    Thread.sleep(10000)
    val connection = DriverManager.getConnection(container.jdbcUrl, properties)

    val statement = connection.createStatement()
    val resultSet = statement.executeQuery(
      "select ROUTINE_DEFINITION,ROUTINE_NAME,ROUTINE_SCHEMA,SPECIFIC_SCHEMA from INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE='PROCEDURE'"
    )
    while (resultSet.next()) {
      val ROUTINE_DEFINITION = resultSet.getString("ROUTINE_DEFINITION")
      val ROUTINE_NAME = resultSet.getString("ROUTINE_NAME")
      val ROUTINE_SCHEMA = resultSet.getString("ROUTINE_SCHEMA")
      val SPECIFIC_SCHEMA = resultSet.getString("SPECIFIC_SCHEMA")
      logger.info(
        s"ROUTINE_DEFINITION: $ROUTINE_DEFINITION, ROUTINE_NAME: $ROUTINE_NAME, ROUTINE_SCHEMA: $ROUTINE_SCHEMA, SPECIFIC_SCHEMA: $SPECIFIC_SCHEMA"
      )
    }

    // use Flyway to execute repeatable sql
    val flyway = new Flyway(
      new FluentConfiguration()
        .dataSource(container.jdbcUrl, container.username, container.password)
        .locations("db/sqlserver")
    )
    flyway.migrate()

But I got error below:

INFO] 2023-06-07 20:31:28,524 [ScalaTest-run] t.m.m.com/mssql/server:2022-latest- Container mcr.microsoft.com/mssql/server:2022-latest started in PT8.242634S
[INFO] 2023-06-07 20:31:28,524 [ScalaTest-run] t.m.m.com/mssql/server:2022-latest- Container is started (JDBC URL: jdbc:sqlserver://localhost:32965;encrypt=false)

[INFO] 2023-06-07 20:31:28,582 [ScalaTest-run] t.m.m.com/mssql/server:2022-latest- Creating container for image: mcr.microsoft.com/mssql/server:2022-latest
[INFO] 2023-06-07 20:31:28,720 [ScalaTest-run] t.m.m.com/mssql/server:2022-latest- Container mcr.microsoft.com/mssql/server:2022-latest is starting: 2ac0a4d9b9ef598056b415f5705121504b285855e458bc2a303fb525a1391466
[INFO] 2023-06-07 20:31:29,338 [ScalaTest-run] t.m.m.com/mssql/server:2022-latest- Waiting for database connection to become available at jdbc:sqlserver://localhost:32967 using query 'SELECT 1'
6月 07, 2023 8:31:29 下午 com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
警告: ConnectionID:20 ClientConnectionId: 04b36b9f-cdfc-43b2-a35e-bf12d88406a6 Prelogin error: host localhost port 32967 Error reading prelogin response: Connection reset ClientConnectionId:04b36b9f-cdfc-43b2-a35e-bf12d88406a6
6月 07, 2023 8:31:29 下午 com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
警告: ConnectionID:20 ClientConnectionId: e6f958ea-c669-45c7-af8e-a1d2b1916e3d Prelogin error: host localhost port 32967 Error reading prelogin response: Connection reset ClientConnectionId:e6f958ea-c669-45c7-af8e-a1d2b1916e3d
6月 07, 2023 8:31:29 下午 com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
警告: ConnectionID:20 ClientConnectionId: fd66379f-ead2-4844-987b-7e0548f20581 Prelogin error: host localhost port 32967 Error reading prelogin response: Connection reset ClientConnectionId:fd66379f-ead2-4844-987b-7e0548f20581
6月 07, 2023 8:31:30 下午 com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
警告: ConnectionID:20 ClientConnectionId: ac4091dd-6422-4766-8ac0-22fc3d101aa3 Prelogin error: host localhost port 32967 Unexpected end of prelogin response after 0 bytes read
6月 07, 2023 8:31:30 下午 com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
警告: ConnectionID:20 ClientConnectionId: 98f680cf-4f63-4d90-a193-63a5f81f84bb Prelogin error: host localhost port 32967 Unexpected end of prelogin response after 0 bytes read
6月 07, 2023 8:31:31 下午 com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
警告: ConnectionID:20 ClientConnectionId: 46912624-e8a2-4d1f-bc0f-2a5b55130737 Prelogin error: host localhost port 32967 Unexpected end of prelogin response after 0 bytes read
6月 07, 2023 8:31:32 下午 com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
警告: ConnectionID:20 ClientConnectionId: 78ef9c75-f70b-478c-a0ea-247dd8d95764 Prelogin error: host localhost port 32967 Unexpected end of prelogin response after 0 bytes read
6月 07, 2023 8:31:33 下午 com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
警告: ConnectionID:20 ClientConnectionId: 1bdb3757-185c-45af-af7a-d105225ce037 Prelogin error: host localhost port 32967 Unexpected end of prelogin response after 0 bytes read
[INFO] 2023-06-07 20:31:37,535 [ScalaTest-run] t.m.m.com/mssql/server:2022-latest- Container mcr.microsoft.com/mssql/server:2022-latest started in PT8.952975S
[INFO] 2023-06-07 20:31:37,536 [ScalaTest-run] t.m.m.com/mssql/server:2022-latest- Container is started (JDBC URL: jdbc:sqlserver://localhost:32967;encrypt=false)
[INFO] 2023-06-07 20:31:37,539 [ScalaTest-run] s.d.d.b.c.SqlServerDataSourceTest- driver name: com.microsoft.sqlserver.jdbc.SQLServerDriver;;;;started: false

Disconnected from the target VM, address: '127.0.0.1:43795', transport: 'socket'

Exception encountered when invoking run on a nested suite - Mapped port can only be obtained after the container is started
java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
    at org.testcontainers.shaded.com.google.common.base.Preconditions.checkState(Preconditions.java:174)
    at org.testcontainers.containers.ContainerState.getMappedPort(ContainerState.java:161)
    at org.testcontainers.containers.MSSQLServerContainer.getJdbcUrl(MSSQLServerContainer.java:105)
    at com.dimafeng.testcontainers.JdbcDatabaseContainer.jdbcUrl(JdbcDatabaseContainer.scala:13)
    at com.dimafeng.testcontainers.JdbcDatabaseContainer.jdbcUrl$(JdbcDatabaseContainer.scala:13)
    at com.dimafeng.testcontainers.MSSQLServerContainer.jdbcUrl(MSSQLServerContainer.scala:8)
    at services.data_ingestion.data_source.base.concrete.SqlServerDataSourceTest.afterStart(SqlServerDataSourceTest.scala:67)
    at com.dimafeng.testcontainers.ForAllTestContainer.run(ForAllTestContainer.scala:19)
    at com.dimafeng.testcontainers.ForAllTestContainer.run$(ForAllTestContainer.scala:13)
    at services.data_ingestion.data_source.base.concrete.SqlServerDataSourceTest.run(SqlServerDataSourceTest.scala:20)
    at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
    at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13(Runner.scala:1322)
    at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13$adapted(Runner.scala:1316)
    at scala.collection.immutable.List.foreach(List.scala:431)
    at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1316)
    at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24(Runner.scala:993)
    at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24$adapted(Runner.scala:971)
    at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1482)
    at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:971)
    at org.scalatest.tools.Runner$.run(Runner.scala:798)
    at org.scalatest.tools.Runner.run(Runner.scala)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2or3(ScalaTestRunner.java:43)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:26)

Anyone knows why and how to resolve it ? PS: under linux environment, openjdk 11 and scalatest.

altrack commented 1 year ago

Same here, but with Munit and Kafka container.

altrack commented 1 year ago

Solved the issue by running: container.start() @qomosoloto