1
1
package io .iohk .atala .prism .node .repositories
2
2
3
- import com .spotify .docker .client .DefaultDockerClient
4
- import com .whisk .docker ._
5
- import com .whisk .docker .impl .spotify .SpotifyDockerFactory
6
- import org .scalatest .concurrent .ScalaFutures ._
7
- import org .scalatest .matchers .must .Matchers ._
3
+ import com .dimafeng .testcontainers .PostgreSQLContainer
4
+ import org .testcontainers .utility .DockerImageName
8
5
9
- import java .sql .DriverManager
10
- import scala .concurrent .{ExecutionContext , Future }
6
+ object DockerPostgresService {
11
7
12
- object DockerPostgresService extends DockerKit {
8
+ private val postgresImage = " postgres:13"
9
+ private val postgresUsername = " postgres"
10
+ private val postgresPassword = " postgres"
11
+ private val databaseName = " db"
13
12
14
- import scala .concurrent .duration ._
15
-
16
- override val PullImagesTimeout = 120 .minutes
17
- override val StartContainersTimeout = 120 .seconds
18
- override val StopContainersTimeout = 120 .seconds
19
-
20
- override implicit val dockerFactory : DockerFactory = new SpotifyDockerFactory (
21
- DefaultDockerClient .fromEnv().build()
13
+ val containerDef : PostgreSQLContainer .Def = PostgreSQLContainer .Def (
14
+ dockerImageName = DockerImageName .parse(postgresImage),
15
+ databaseName = databaseName,
16
+ username = postgresUsername,
17
+ password = postgresPassword
22
18
)
23
19
24
- val PostgresImage = " postgres:13"
25
- val PostgresUsername = " postgres"
26
- val PostgresPassword = " postgres"
27
- val DatabaseName = " db"
28
-
29
- val postgresContainer = DockerContainer (PostgresImage )
30
- .withCommand(" -N 1000" )
31
- .withPorts((PostgresAdvertisedPort , Some (PostgresExposedPort )))
32
- .withEnv(
33
- s " POSTGRES_USER= $PostgresUsername" ,
34
- s " POSTGRES_PASSWORD= $PostgresPassword"
35
- )
36
- .withReadyChecker(
37
- new PostgresReadyChecker ().looped(15 , 1 .second)
38
- )
39
-
40
- override val dockerContainers : List [DockerContainer ] =
41
- postgresContainer :: super .dockerContainers
42
-
43
- def PostgresAdvertisedPort = 5432
44
- def PostgresExposedPort = 44444
20
+ lazy private val postgresContainer = containerDef.start()
45
21
46
22
private var isRunning = false
47
23
@@ -52,59 +28,24 @@ object DockerPostgresService extends DockerKit {
52
28
override def run (): Unit = {
53
29
54
30
println(" Stopping Docker container with Postgres" )
55
- stopAllQuietly ()
31
+ postgresContainer.stop ()
56
32
println(" Stopped Docker container with Postgres" )
57
33
}
58
34
})
59
35
60
36
println(" Starting Docker container with Postgres" )
61
- startAllOrFail()
62
- isContainerReady(postgresContainer).futureValue mustEqual true
37
+ postgresContainer
63
38
isRunning = true
64
39
println(" Started Docker container with Postgres" )
65
40
}
66
41
67
- val hostname = postgresContainer.hostname.getOrElse(" localhost" )
42
+ val host = postgresContainer.host
43
+ val port = postgresContainer.mappedPort(5432 )
68
44
PostgresConfig (
69
- s " $hostname : $PostgresExposedPort " ,
70
- DatabaseName ,
71
- PostgresUsername ,
72
- PostgresPassword
45
+ s " $host : $port " ,
46
+ postgresContainer.databaseName ,
47
+ postgresContainer.username ,
48
+ postgresContainer.password
73
49
)
74
50
}
75
-
76
- class PostgresReadyChecker extends DockerReadyChecker {
77
-
78
- override def apply (
79
- container : DockerContainerState
80
- )(implicit
81
- dockerExecutor : DockerCommandExecutor ,
82
- ec : ExecutionContext
83
- ): Future [Boolean ] = {
84
-
85
- container
86
- .getPorts()(dockerExecutor, ec)
87
- .map { _ =>
88
- try {
89
- Class .forName(" org.postgresql.Driver" )
90
- val url =
91
- s " jdbc:postgresql:// ${dockerExecutor.host}: $PostgresExposedPort/ "
92
- Option (
93
- DriverManager
94
- .getConnection(url, PostgresUsername , PostgresPassword )
95
- )
96
- .foreach { conn =>
97
- // NOTE: For some reason the result is always false
98
- conn.createStatement().execute(s " CREATE DATABASE $DatabaseName" )
99
- conn.close()
100
- }
101
-
102
- true
103
- } catch {
104
- case _ : Throwable =>
105
- false
106
- }
107
- }(ec)
108
- }
109
- }
110
51
}
0 commit comments