/
BaseContainers.scala
81 lines (67 loc) · 2.98 KB
/
BaseContainers.scala
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
package com.acme.containers
import com.dimafeng.testcontainers._
import com.dimafeng.testcontainers.scalatest._
import com.acme.containers.BaseContainers.{AppPort, WaitStrategyForAPI}
import org.scalatest.Suite
import org.testcontainers.containers.wait.strategy.{HttpWaitStrategy, LogMessageWaitStrategy, WaitAllStrategy, WaitStrategy}
import org.testcontainers.containers.{Network, GenericContainer => JavaGenericContainer}
import org.testcontainers.images.builder.ImageFromDockerfile
import org.testcontainers.lifecycle.Startable
import org.testcontainers.utility.DockerImageName
import java.nio.file.Path
import scala.jdk.CollectionConverters._
trait BaseContainers extends TestContainersForAll {
self: Suite =>
protected val sharedNetwork: Network = Network.newNetwork()
protected lazy val kafkaContainer: KafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0")) {
container.withNetwork(sharedNetwork)
container.withNetworkAliases("kafka")
}
protected lazy val mySQLContainer: MySQLContainer = new MySQLContainer(
mysqlImageVersion = Some(DockerImageName.parse("mysql:8.0")),
) {
container.withNetwork(sharedNetwork)
container.withNetworkAliases("mysql")
container.withEnv("ENVIRONMENT", "local")
}
protected def baseAppContainer(
name: String,
jarName: String,
mainClass: String,
baseFolder: String,
port: Int = AppPort,
//waitStrategy: WaitStrategy = WaitStrategyForAPI,
envVars: Map[String, String] = Map.empty,
containerDependencies: List[Startable]
): GenericContainer = new GenericContainer({
val c = new JavaGenericContainer(
new ImageFromDockerfile()
.withFileFromPath(jarName, Path.of(s"$baseFolder/$jarName").toAbsolutePath)
.withDockerfileFromBuilder { builder =>
builder
.from("openjdk:11-jre-slim")
.copy(jarName, s"/$jarName")
.cmd("java", "-Xmx256m", "-cp", s"/$jarName", mainClass)
.build()
}
)
c.dependsOn(containerDependencies.asJavaCollection)
c.withEnv(envVars.asJava)
//c.withStartupAttempts(3)
c.withEnv("ENVIRONMENT", "local")
c.withEnv("APP_PORT", port.toString)
c.withEnv("KAFKA_BOOTSTRAP_SERVERS", "kafka:9092")
//c.setWaitStrategy(waitStrategy)
c.withExposedPorts(port)
c.withNetwork(sharedNetwork)
c.withNetworkAliases(name)
c
})
}
object BaseContainers {
val AppPort = 9000
val WaitStrategyForAPI: WaitStrategy = new HttpWaitStrategy().forPath("/health").forStatusCode(200)
val WaitStrategyForService: WaitStrategy = new WaitAllStrategy()
.withStrategy(WaitStrategyForAPI)
.withStrategy(new LogMessageWaitStrategy().withRegEx("^.*Resetting offset for partition.*$"))
}