-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
ContainerHelper.kt
89 lines (80 loc) · 4.27 KB
/
ContainerHelper.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.github.aysnc.sql.db.integration
import com.github.jasync.sql.db.Configuration
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
import io.github.oshai.kotlinlogging.KotlinLogging
import org.testcontainers.containers.BindMode
import org.testcontainers.containers.PostgreSQLContainer
import java.util.concurrent.TimeUnit
private val logger = KotlinLogging.logger {}
private const val version = "10"
/**
* See run-docker-postgresql.sh to run a local instance of postgreSql.
*/
object ContainerHelper {
var postgresql: MyPostgreSQLContainer? = null
/**
* default config is a local instance already running on port 15432 (i.e. a docker postgresql)
*/
var defaultConfiguration: Configuration
init {
val username = System.getenv("PGUSER") ?: "postresql_async"
val host = System.getenv("PGHOST") ?: "localhost"
val port = Integer.parseInt(System.getenv("PGPORT") ?: "15432")
val password = System.getenv("PGPASSWORD") ?: "root"
val database = System.getenv("PGDATABASE") ?: "netty_driver_test"
defaultConfiguration = Configuration(username, host, port, password, database)
try {
PostgreSQLConnection(defaultConfiguration).connect().get(1, TimeUnit.SECONDS)
logger.info("Using local postgresql instance $defaultConfiguration")
} catch (e: Exception) {
// If local instance isn't running, start a docker postgresql on random port
if (postgresql == null) {
configurePostgres()
}
if (!postgresql!!.isRunning()) {
postgresql!!.start()
}
defaultConfiguration = Configuration(
postgresql!!.getUsername(),
"localhost",
postgresql!!.getFirstMappedPort(),
postgresql!!.getPassword(),
postgresql!!.getDatabaseName()
)
logger.info("PORT is " + defaultConfiguration.port)
logger.info("Using test container instance {}", defaultConfiguration)
} finally {
try {
val connection = PostgreSQLConnection(defaultConfiguration).connect().get(1, TimeUnit.SECONDS)
logger.info("got connection " + connection.isConnected())
connection.sendQuery(
"""
DROP TYPE IF EXISTS example_mood; CREATE TYPE example_mood AS ENUM ('sad', 'ok', 'happy')
"""
).get()
connection.sendQuery(
"""
CREATE USER postgres_cleartext WITH PASSWORD 'postgres_cleartext'; GRANT ALL PRIVILEGES ON DATABASE ${defaultConfiguration.database} to postgres_cleartext;
-- We set the md5 password as an explicit md5 hash (of 'postgres_md5postgres_md5') as this is the way to get an md5 hash into a server configured for scram-sha-256
CREATE USER postgres_md5 WITH PASSWORD 'md501ed5f6e413b5607536c812f61ba2748'; GRANT ALL PRIVILEGES ON DATABASE ${defaultConfiguration.database} to postgres_md5;
CREATE USER postgres_scram WITH PASSWORD 'postgres_scram'; GRANT ALL PRIVILEGES ON DATABASE ${defaultConfiguration.database} to postgres_scram;
CREATE USER postgres_kerberos WITH PASSWORD 'postgres_kerberos'; GRANT ALL PRIVILEGES ON DATABASE ${defaultConfiguration.database} to postgres_kerberos;
"""
).get()
} catch (e: Exception) {
logger.error(e.localizedMessage, e)
}
}
}
private fun configurePostgres() {
postgresql = MyPostgreSQLContainer()
.withDatabaseName("netty_driver_test")
.withPassword("root")
.withUsername("postresql_async")
.withClasspathResourceMapping("pg_hba.conf", "/docker-entrypoint-initdb.d/pg_hba.conf", BindMode.READ_WRITE)
.withClasspathResourceMapping("server.cert.txt", "/docker-entrypoint-initdb.d/server.crt", BindMode.READ_WRITE)
.withClasspathResourceMapping("server.key.txt", "/docker-entrypoint-initdb.d/server.key", BindMode.READ_WRITE)
.withClasspathResourceMapping("update-config.sh", "/docker-entrypoint-initdb.d/update-config.sh", BindMode.READ_ONLY)
}
}
class MyPostgreSQLContainer : PostgreSQLContainer<MyPostgreSQLContainer>("postgres:$version")