Skip to content

Commit 2291f17

Browse files
committed
added some Red Panda based tests
1 parent 9249f94 commit 2291f17

File tree

4 files changed

+133
-94
lines changed

4 files changed

+133
-94
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package io.floodplain.integration
20+
21+
import io.floodplain.test.InstantiatedContainer
22+
import io.floodplain.test.InstantiatedKafkaContainer
23+
import io.floodplain.test.InstantiatedRedPandaContainer
24+
import org.apache.kafka.clients.admin.AdminClient
25+
import org.junit.FixMethodOrder
26+
import org.junit.Test
27+
import org.junit.runners.MethodSorters
28+
import java.util.HashMap
29+
import java.util.UUID
30+
31+
private val logger = mu.KotlinLogging.logger {}
32+
33+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
34+
class TestRedPandaPerformance {
35+
// private val kafkaContainer = InstantiatedKafkaContainer()// KafkaContainer("5.5.3").withEmbeddedZookeeper().withExposedPorts(9092)
36+
// private val containerNetwork = Network.newNetwork()
37+
// private val kafkaContainer = KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag ("5.5.3")).withEmbeddedZookeeper().withExposedPorts(9092)
38+
39+
//
40+
@Test
41+
fun `Environment Warmup`() {
42+
val postgresContainer = InstantiatedContainer("floodplain/floodplain-postgres-demo:1.0.0", 5432, mapOf())
43+
}
44+
45+
@Test
46+
fun `Test Red Panda until topic list`() {
47+
val container = InstantiatedRedPandaContainer()
48+
val port = container.exposedPort
49+
val config: MutableMap<String, Any> = HashMap()
50+
config["bootstrap.servers"] = "localhost:$port" // "localhost:${redPandaContainer.exposedPort}"
51+
config["client.id"] = UUID.randomUUID().toString()
52+
val adminClient = AdminClient.create(config)
53+
val topics = adminClient.listTopics().names().get()
54+
println("Topics: $topics")
55+
}
56+
57+
@Test
58+
fun `Test Kafka until topic list`() {
59+
val container = InstantiatedKafkaContainer()
60+
val port = container.exposedPort
61+
val config: MutableMap<String, Any> = HashMap()
62+
config["bootstrap.servers"] = "localhost:$port" // "localhost:${redPandaContainer.exposedPort}"
63+
config["client.id"] = UUID.randomUUID().toString()
64+
val adminClient = AdminClient.create(config)
65+
val topics = adminClient.listTopics().names().get()
66+
println("Topics: $topics")
67+
}
68+
}

floodplain-test/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import io.floodplain.build.FloodplainDeps
33
dependencies {
44
implementation(FloodplainDeps.testContainer)
55
implementation(FloodplainDeps.testContainerKafka)
6+
implementation(FloodplainDeps.kotlinLogging)
67
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
78
}

floodplain-test/src/main/kotlin/io/floodplain/test/InstantiatedContainer.kt

+64-1
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,51 @@
1818
*/
1919
package io.floodplain.test
2020

21+
import com.github.dockerjava.api.command.InspectContainerResponse
2122
import org.testcontainers.containers.GenericContainer
2223
import org.testcontainers.containers.KafkaContainer
24+
import org.testcontainers.containers.wait.strategy.Wait
25+
import org.testcontainers.images.builder.Transferable
2326
import org.testcontainers.utility.DockerImageName
27+
import java.nio.charset.StandardCharsets
2428
import java.time.Duration
2529

2630
val useIntegraton: Boolean by lazy {
2731
System.getenv("NO_INTEGRATION") == null
2832
}
2933

34+
private val logger = mu.KotlinLogging.logger {}
35+
3036
class KGenericContainer(imageName: String) : GenericContainer<KGenericContainer>(DockerImageName.parse(imageName))
3137

3238
/**
3339
* Kotlin wrapper, to make testcontainers easier to use
3440
*/
35-
class InstantiatedContainer(image: String, port: Int, env: Map<String, String> = emptyMap(), customizer: ((KGenericContainer) -> KGenericContainer)? = null) {
41+
class InstantiatedContainer(
42+
image: String,
43+
port: Int,
44+
env: Map<String, String> = emptyMap(),
45+
customizer: ((KGenericContainer) -> KGenericContainer)? = null
46+
) {
3647

3748
var container: KGenericContainer? = KGenericContainer(image)
3849
.apply { withExposedPorts(port) }
3950
.apply { withEnv(env) }
4051
.apply { customizer?.invoke(this) }
4152
var host: String
4253
var exposedPort: Int = -1
54+
4355
init {
4456
container?.start()
4557
host = container?.host ?: "localhost"
4658
exposedPort = container?.firstMappedPort ?: -1
4759
}
60+
4861
fun close() {
4962
container?.close()
5063
}
5164
}
65+
5266
// KafkaContainer("5.5.3").withEmbeddedZookeeper().withExposedPorts(9092,9093)
5367
class InstantiatedKafkaContainer(customizer: ((KafkaContainer) -> KafkaContainer)? = null) {
5468
// class KGenericContainer(imageName: String) : GenericContainer<KGenericContainer>(DockerImageName.parse(imageName))
@@ -59,12 +73,61 @@ class InstantiatedKafkaContainer(customizer: ((KafkaContainer) -> KafkaContainer
5973
.apply { customizer?.invoke(this) }
6074
var host: String
6175
var exposedPort: Int = -1
76+
6277
init {
6378
container.start()
6479
host = container.host ?: "localhost"
6580
exposedPort = container.getMappedPort(9093) ?: -1
6681
}
82+
83+
fun close() {
84+
container.close()
85+
}
86+
}
87+
88+
class InstantiatedRedPandaContainer(customizer: ((RedpandaContainer) -> RedpandaContainer)? = null) {
89+
private val container = RedpandaContainer()
90+
.apply { customizer?.invoke(this) }
91+
var host: String
92+
var exposedPort: Int = -1
93+
94+
init {
95+
container.start()
96+
host = container.host ?: "localhost"
97+
exposedPort = container.getMappedPort(9092) ?: -1
98+
}
99+
67100
fun close() {
68101
container.close()
69102
}
70103
}
104+
105+
// withEnv("KAFKA_LISTENERS", "PLAINTEXT://0.0.0.0:" + KAFKA_PORT + ",BROKER://0.0.0.0:9092");
106+
107+
private const val STARTER_SCRIPT = "/testcontainers_start.sh"
108+
109+
class RedpandaContainer : GenericContainer<RedpandaContainer?>("vectorized/redpanda:v21.4.13") {
110+
override fun containerIsStarting(containerInfo: InspectContainerResponse?) {
111+
super.containerIsStarting(containerInfo)
112+
var command = "#!/bin/bash\n"
113+
command += "/usr/bin/rpk redpanda start --check=false --node-id 0 "
114+
command += "--kafka-addr PLAINTEXT://0.0.0.0:29092,OUTSIDE://0.0.0.0:9092 "
115+
command += "--advertise-kafka-addr PLAINTEXT://broker:29092,OUTSIDE://$host:" + getMappedPort(
116+
29092
117+
)
118+
logger.info("command: $command")
119+
logger.info("mapped port: $host:${getMappedPort(9092)}")
120+
copyFileToContainer(
121+
Transferable.of(command.toByteArray(StandardCharsets.UTF_8), 511),
122+
STARTER_SCRIPT
123+
)
124+
logger.info("Copied to location: $STARTER_SCRIPT")
125+
}
126+
127+
init {
128+
withExposedPorts(9092)
129+
withCreateContainerCmdModifier { cmd -> cmd.withEntrypoint("sh") }
130+
withCommand("-c", "while [ ! -f $STARTER_SCRIPT ]; do sleep 0.1; done; $STARTER_SCRIPT")
131+
waitingFor(Wait.forLogMessage(".*Started Kafka API server.*", 1))
132+
}
133+
}

floodplain-test/src/main/kotlin/io/floodplain/test/RedPandaContainer.kt

-93
This file was deleted.

0 commit comments

Comments
 (0)