-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathGenericContainer.scala
More file actions
169 lines (146 loc) · 5.74 KB
/
GenericContainer.scala
File metadata and controls
169 lines (146 loc) · 5.74 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.dimafeng.testcontainers
import java.util.concurrent.Future
import com.dimafeng.testcontainers.GenericContainer.{FileSystemBind, DockerImage}
import org.testcontainers.containers.startupcheck.StartupCheckStrategy
import org.testcontainers.containers.wait.strategy.WaitStrategy
import org.testcontainers.containers.{BindMode, GenericContainer => JavaGenericContainer}
import org.testcontainers.images.ImagePullPolicy
import scala.collection.JavaConverters._
class GenericContainer(
override val underlyingUnsafeContainer: JavaGenericContainer[?]
) extends SingleContainer[JavaGenericContainer[?]] {
override implicit val container: JavaGenericContainer[?] = underlyingUnsafeContainer
def this(
dockerImage: DockerImage,
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: Option[WaitStrategy] = None,
labels: Map[String, String] = Map.empty,
tmpFsMapping: Map[String, String] = Map.empty,
imagePullPolicy: Option[ImagePullPolicy] = None,
fileSystemBind: Seq[FileSystemBind] = Seq(),
startupCheckStrategy: Option[StartupCheckStrategy] = None
) = this({
val underlying: JavaGenericContainer[?] = dockerImage match {
case DockerImage(Left(imageFromDockerfile)) => new JavaGenericContainer(imageFromDockerfile)
case DockerImage(Right(imageName)) => new JavaGenericContainer(imageName)
}
if (exposedPorts.nonEmpty) {
underlying.withExposedPorts(exposedPorts.map(int2Integer): _*)
}
env.foreach{ case (k, v) => underlying.withEnv(k, v) }
if (command.nonEmpty) {
underlying.withCommand(command: _*)
}
classpathResourceMapping.foreach {
case FileSystemBind(hostFilePath, containerFilePath, bindMode) =>
underlying.withClasspathResourceMapping(hostFilePath, containerFilePath, bindMode)
}
fileSystemBind.foreach {
case FileSystemBind(hostFilePath, containerFilePath, bindMode) =>
underlying.withFileSystemBind(hostFilePath, containerFilePath, bindMode)
}
waitStrategy.foreach(underlying.waitingFor)
startupCheckStrategy.foreach(underlying.withStartupCheckStrategy)
if (labels.nonEmpty) {
underlying.withLabels(labels.asJava)
}
if (tmpFsMapping.nonEmpty) {
underlying.withTmpFs(tmpFsMapping.asJava)
}
imagePullPolicy.foreach(underlying.withImagePullPolicy)
underlying
})
def this(genericContainer: GenericContainer) = this(genericContainer.underlyingUnsafeContainer)
}
object GenericContainer {
case class DockerImage(image: Either[String, Future[String]])
case class FileSystemBind(hostFilePath: String, containerFilePath: String, bindMode: BindMode)
implicit def javaFutureToDockerImage(javaFuture: Future[String]): DockerImage = {
DockerImage(Right(javaFuture))
}
implicit def stringToDockerImage(imageName: String): DockerImage = {
DockerImage(Left(imageName))
}
def apply(
dockerImage: DockerImage,
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: WaitStrategy = null,
labels: Map[String, String] = Map.empty,
tmpFsMapping: Map[String, String] = Map.empty,
imagePullPolicy: ImagePullPolicy = null,
fileSystemBind: Seq[FileSystemBind] = Seq()
): GenericContainer =
new GenericContainer(
dockerImage = dockerImage,
exposedPorts = exposedPorts,
env = env,
command = command,
classpathResourceMapping = classpathResourceMapping,
fileSystemBind = fileSystemBind,
waitStrategy = Option(waitStrategy),
labels = labels,
tmpFsMapping = tmpFsMapping,
imagePullPolicy = Option(imagePullPolicy)
)
abstract class Def[C <: GenericContainer](init: => C) extends ContainerDef {
override type Container = C
protected def createContainer(): C = init
}
object Def {
private final case class Default(
dockerImage: DockerImage,
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: WaitStrategy = null,
labels: Map[String, String] = Map.empty,
tmpFsMapping: Map[String, String] = Map.empty,
imagePullPolicy: ImagePullPolicy = null,
fileSystemBind: Seq[FileSystemBind] = Seq()
) extends Def[GenericContainer](
GenericContainer(
dockerImage = dockerImage,
exposedPorts = exposedPorts,
env = env,
command = command,
classpathResourceMapping = classpathResourceMapping,
fileSystemBind = fileSystemBind,
waitStrategy = waitStrategy,
labels = labels,
tmpFsMapping = tmpFsMapping,
imagePullPolicy = imagePullPolicy
)
)
def apply(
dockerImage: DockerImage,
exposedPorts: Seq[Int] = Seq(),
env: Map[String, String] = Map(),
command: Seq[String] = Seq(),
classpathResourceMapping: Seq[FileSystemBind] = Seq(),
waitStrategy: WaitStrategy = null,
labels: Map[String, String] = Map.empty,
tmpFsMapping: Map[String, String] = Map.empty,
imagePullPolicy: ImagePullPolicy = null,
fileSystemBind: Seq[FileSystemBind] = Seq()
): GenericContainer.Def[GenericContainer] =
Default(
dockerImage = dockerImage,
exposedPorts = exposedPorts,
env = env,
command = command,
classpathResourceMapping = classpathResourceMapping,
fileSystemBind = fileSystemBind,
waitStrategy = waitStrategy,
labels = labels,
tmpFsMapping = tmpFsMapping,
imagePullPolicy = imagePullPolicy
)
}
}