/
TestContainers.scala
81 lines (69 loc) · 3.36 KB
/
TestContainers.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
/*
* Copyright © 2015-2021 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi
import java.net.{NetworkInterface, UnknownHostException}
import com.typesafe.config.{Config, ConfigFactory}
import org.testcontainers.containers.{BindMode, GenericContainer}
import org.testcontainers.utility.DockerImageName
import scala.jdk.CollectionConverters._
/**
* Provides all containers necessary for running tests.
*/
object TestContainers {
// get local IP address, which we need for SIPI
val localIpAddress: String = NetworkInterface.getNetworkInterfaces.asScala.toSeq
.filter(!_.isLoopback)
.flatMap(_.getInetAddresses.asScala.toSeq.filter(_.getAddress.length == 4).map(_.toString))
.headOption
.getOrElse(throw new UnknownHostException("No suitable network interface found"))
val FusekiImageName: DockerImageName = DockerImageName.parse("bazel/docker/knora-jena-fuseki:image")
val FusekiContainer = new GenericContainer(FusekiImageName)
FusekiContainer.withExposedPorts(3030)
FusekiContainer.withEnv("ADMIN_PASSWORD", "test")
FusekiContainer.withEnv("JVM_ARGS", "-Xmx3G")
FusekiContainer.start()
val SipiImageName: DockerImageName = DockerImageName.parse("bazel/docker/knora-sipi:image")
val SipiContainer = new GenericContainer(SipiImageName)
SipiContainer.withExposedPorts(1024)
SipiContainer.withEnv("SIPI_EXTERNAL_PROTOCOL", "http")
SipiContainer.withEnv("SIPI_EXTERNAL_HOSTNAME", "0.0.0.0")
SipiContainer.withEnv("SIPI_EXTERNAL_PORT", "1024")
SipiContainer.withEnv("SIPI_WEBAPI_HOSTNAME", localIpAddress)
SipiContainer.withEnv("SIPI_WEBAPI_PORT", "3333")
SipiContainer.withCommand("--config=/sipi/config/sipi.knora-docker-config.lua")
SipiContainer.withClasspathResourceMapping("/sipi/config/sipi.knora-docker-config.lua",
"/sipi/config/sipi.knora-docker-config.lua",
BindMode.READ_ONLY)
SipiContainer.start()
// Container needs to be started to get the random IP
val sipiIp: String = SipiContainer.getHost
val sipiPort: Int = SipiContainer.getFirstMappedPort
val RedisImageName: DockerImageName = DockerImageName.parse("redis:5")
val RedisContainer = new GenericContainer(RedisImageName)
RedisContainer.withExposedPorts(6379)
RedisContainer.start()
private val portMap = Map(
"app.triplestore.fuseki.port" -> FusekiContainer.getFirstMappedPort,
"app.sipi.internal-host" -> sipiIp,
"app.sipi.internal-port" -> sipiPort,
"app.cache-service.redis.port" -> RedisContainer.getFirstMappedPort
).asJava
// all tests need to be configured with these ports.
val PortConfig: Config = ConfigFactory.parseMap(portMap, "Ports from ContainerizedSpec")
}