|
| 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.test |
| 20 | + |
| 21 | +import com.github.dockerjava.api.command.InspectContainerResponse |
| 22 | +import org.testcontainers.containers.GenericContainer |
| 23 | +import org.testcontainers.images.builder.Transferable |
| 24 | +import org.testcontainers.utility.DockerImageName |
| 25 | +import java.nio.charset.StandardCharsets |
| 26 | + |
| 27 | +/** |
| 28 | + * Kotlin wrapper, to make testcontainers easier to use |
| 29 | + */ |
| 30 | +private const val KAFKA_PORT = 9092 |
| 31 | +private const val STARTER_SCRIPT = "/entry.sh" |
| 32 | + |
| 33 | +class RedPandaContainer(image: String, port: Int, env: Map<String, String> = emptyMap()) { |
| 34 | + class KGenericContainer(imageName: String) : GenericContainer<KGenericContainer>(DockerImageName.parse(imageName)) { |
| 35 | + var port: Int? = null |
| 36 | + |
| 37 | + override fun doStart() { |
| 38 | + // withCommand( |
| 39 | + // "sh", |
| 40 | + // "-c", |
| 41 | + // "while [ ! -f $STARTER_SCRIPT ]; do sleep 1; echo woopp; done; $STARTER_SCRIPT" |
| 42 | + // ) |
| 43 | + withCreateContainerCmdModifier { cmd -> |
| 44 | + println("Bommmp: "+cmd) |
| 45 | + cmd.withEntrypoint("sh -c while [ ! -f $STARTER_SCRIPT ]; do sleep 1; done; $STARTER_SCRIPT") |
| 46 | + } |
| 47 | + println("Command parts: ${commandParts.asList()}") |
| 48 | + super.doStart() |
| 49 | + } |
| 50 | + override fun containerIsStarting(containerInfo: InspectContainerResponse, reused: Boolean) { |
| 51 | + super.containerIsStarting(containerInfo, reused) |
| 52 | + port = getMappedPort(KAFKA_PORT) |
| 53 | + |
| 54 | + if (reused) { |
| 55 | + return |
| 56 | + } |
| 57 | + val kafkaURL = "$host:$port" |
| 58 | + val startCommand = """ |
| 59 | + #!/usr/bin/env bash |
| 60 | + echo woooop |
| 61 | + set -e |
| 62 | + supercronic -quiet /etc/cron.d/rpk-status.cron & |
| 63 | + exec rpk start --advertise-kafka-addr $kafkaURL $@ |
| 64 | + """.trimIndent() |
| 65 | + // val startCommand = "rpk start --advertise-kafka-addr $kafkaURL" |
| 66 | + copyFileToContainer( |
| 67 | + Transferable.of(startCommand.toByteArray(StandardCharsets.UTF_8), 511), // octal 777 |
| 68 | + STARTER_SCRIPT |
| 69 | + ) |
| 70 | + // "start", "--advertise-kafka-addr", "kafka:9092" |
| 71 | + |
| 72 | + } |
| 73 | + } |
| 74 | + var container: KGenericContainer = KGenericContainer(image) |
| 75 | + .apply { withExposedPorts(port)} |
| 76 | + // .apply { var self = this; port.toTypedArray().forEach { self = withExposedPorts(it) }; self } |
| 77 | + .apply { withEnv(env) } |
| 78 | + // .apply { var self = this; if(commands!=null) { self = withCommand(command)}; self } |
| 79 | + // .apply {val cmds: Array<String> = arrayOf(*commands); withCommand(arrayOf("a")) } |
| 80 | + // .apply { if(customize!=null) customize(this) else this} |
| 81 | + // var host: String |
| 82 | + var exposedPort: Int = -1 |
| 83 | + init { |
| 84 | + container.start() |
| 85 | + // host = container.host ?: "localhost" |
| 86 | + // val bindings = container.portBindings |
| 87 | + // val testHost = container.testHostIpAddress |
| 88 | + exposedPort = container.firstMappedPort ?: -1 |
| 89 | + } |
| 90 | + fun close() { |
| 91 | + container.close() |
| 92 | + } |
| 93 | +} |
0 commit comments