Skip to content

Commit f66aee1

Browse files
Add lang and version labels to containers (#6124)
The following labels are added `org.testcontainers.lang` and `org.testcontainers.version`. Those are good identifiers when debugging and sharing logs. Co-authored-by: Sergei Egorov <[email protected]>
1 parent bd6ee9c commit f66aee1

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

core/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ test.maxParallelForks = 4
1010

1111
idea.module.testSourceDirs += sourceSets.jarFileTest.allSource.srcDirs
1212

13+
jar {
14+
manifest {
15+
attributes('Implementation-Version': project.getProperty("version"))
16+
}
17+
}
18+
1319
shadowJar {
1420
[
1521
'META-INF/NOTICE',

core/src/main/java/org/testcontainers/DockerClientFactory.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.github.dockerjava.api.model.Version;
1414
import com.github.dockerjava.api.model.Volume;
1515
import com.google.common.annotations.VisibleForTesting;
16-
import com.google.common.collect.ImmutableMap;
1716
import lombok.Getter;
1817
import lombok.SneakyThrows;
1918
import lombok.Synchronized;
@@ -34,6 +33,7 @@
3433
import java.io.InputStream;
3534
import java.net.URI;
3635
import java.util.ArrayList;
36+
import java.util.Collections;
3737
import java.util.HashMap;
3838
import java.util.List;
3939
import java.util.Map;
@@ -56,9 +56,24 @@ public class DockerClientFactory {
5656

5757
public static final String TESTCONTAINERS_SESSION_ID_LABEL = TESTCONTAINERS_LABEL + ".sessionId";
5858

59+
public static final String TESTCONTAINERS_LANG_LABEL = TESTCONTAINERS_LABEL + ".lang";
60+
61+
public static final String TESTCONTAINERS_VERSION_LABEL = TESTCONTAINERS_LABEL + ".version";
62+
5963
public static final String SESSION_ID = UUID.randomUUID().toString();
6064

61-
public static final Map<String, String> DEFAULT_LABELS = ImmutableMap.of(TESTCONTAINERS_LABEL, "true");
65+
public static final Map<String, String> DEFAULT_LABELS = markerLabels();
66+
67+
static Map<String, String> markerLabels() {
68+
String implementationVersion = DockerClientFactory.class.getPackage().getImplementationVersion();
69+
String testcontainersVersion = implementationVersion == null ? "unspecified" : implementationVersion;
70+
71+
Map<String, String> labels = new HashMap<>();
72+
labels.put(TESTCONTAINERS_LABEL, "true");
73+
labels.put(TESTCONTAINERS_LANG_LABEL, "java");
74+
labels.put(TESTCONTAINERS_VERSION_LABEL, testcontainersVersion);
75+
return Collections.unmodifiableMap(labels);
76+
}
6277

6378
private static final DockerImageName TINY_IMAGE = DockerImageName.parse("alpine:3.16");
6479

core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ public void customLabelTest() {
269269

270270
Map<String, String> labels = alpineCustomLabel.getCurrentContainerInfo().getConfig().getLabels();
271271
assertThat(labels).as("org.testcontainers label is present").containsKey("org.testcontainers");
272+
assertThat(labels).as("org.testcontainers.lang label is present").containsKey("org.testcontainers.lang");
273+
assertThat(labels)
274+
.as("org.testcontainers.lang label is present")
275+
.containsEntry("org.testcontainers.lang", "java");
276+
assertThat(labels)
277+
.as("org.testcontainers.version label is present")
278+
.containsKey("org.testcontainers.version");
272279
assertThat(labels).as("our.custom label is present").containsKey("our.custom");
273280
assertThat(labels).as("our.custom label value is label").containsEntry("our.custom", "label");
274281
}

0 commit comments

Comments
 (0)