Skip to content
Snippets Groups Projects
Verified Commit 60452e1a authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Cleanup API and documentation

parent 0b4842e3
No related branches found
No related tags found
No related merge requests found
Pipeline #619 passed
# Kotlin Bitflags
# Testcontainers-CI
Kotlin-Bitflags is a utility library to simplify implementing bitflags in Kotlin. It integrates with Kotlin unsigned
types and Java Enumsets. This especially useful when interacting with binary protocols from Kotlin.
Testcontainers-CI allows you to easily use testcontainers whenever you can, but use containers created through other
means whenever you can’t.
## Using Kotlin-Bitflags
When running your CI in docker, you can’t launch new containers without DinD, but thanks to Gitlab Ci Services you can
run a docker container with the desired service and use it in your code easily.
After adding this module to your dependencies, you'll have to implement the related interfaces in your classes:
This project simplifies this process, by providing a façade for both types of containers.
```kotlin
enum class MessageFlag(
override val value: UInt,
) : Flag<UInt> {
Self(0x01u),
Highlight(0x02u),
Redirected(0x04u),
ServerMsg(0x08u),
Backlog(0x80u);
companion object : Flags<UInt, MessageFlag> {
override val all: Set<MessageFlag> = values().toEnumSet()
}
}
```
## Using Testcontainers-CI
This allows you to then use this elsewhere to e.g initialize a field from discrete values
```kotlin
// Construct from varargs or an array
val field = MessageFlag.of(MessageFlag.Self, MessageFlag.Highlight)
val values = listOf(MessageFlag.Self, MessageFlag.Highlight)
// Or from a collection
val field = MessageFlag.of(values)
// Or use the to helper
val field = values.toEnumSet()
```
After adding this module to your dependencies, you can just wrap your calls creating testcontainers with the
`providedContainer` function, and it’ll return either the Testcontainer, or the provided container, whichever is
available in the current environment.
You can also convert such a field into the raw binary value easily
```kotlin
// Returns in this case UInt
field.toBits()
@CiContainers
class MyLittleTest {
private val container = providedContainer("REDIS_CONTAINER") {
GenericContainer(DockerImageName.parse("redis:5.0.3-alpine"))
.withExposedPorts(6379);
}
@Test
fun test() {
println(container.address)
println(container.getMappedPort(6379))
}
}
```
Additional utility functions are available:
```kotlin
// Empty field
MessageFlag.none()
// Get all non-null values
MessageFlag.validValues()
```yaml
services:
- name: "redis:5.0.3-alpine"
alias: "test_redis_instance"
variables:
REDIS_CONTAINER: "test_redis_instance"
```
......@@ -10,16 +10,18 @@
package de.justjanne.testcontainersci.api
import de.justjanne.testcontainersci.implementation.GitlabCiProvidedContainer
import de.justjanne.testcontainersci.implementation.TestContainersProvidedContainer
import org.testcontainers.containers.GenericContainer
import java.net.InetAddress
/**
* Build a provided container from an environment variable and a builder for a local container
*/
fun providedContainer(
fun <T : GenericContainer<T>> providedContainer(
envVariable: String,
containerBuilder: () -> ProvidedContainer
containerBuilder: () -> T
) = when {
!System.getenv(envVariable).isNullOrEmpty() ->
GitlabCiProvidedContainer(InetAddress.getByName(System.getenv(envVariable)))
else -> containerBuilder()
else -> TestContainersProvidedContainer(containerBuilder())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment