Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public class TestcontainersExtension

private static final String LOCAL_LIFECYCLE_AWARE_CONTAINERS = "localLifecycleAwareContainers";

private static final String TEST_CLASS_LABEL = "testcontainers/test-class";

private static final String TEST_FIELD_LABEL = "testcontainers/test-field";

private final DockerAvailableDetector dockerDetector = new DockerAvailableDetector();

@Override
Expand Down Expand Up @@ -267,12 +271,24 @@ private static class StoreAdapter implements CloseableResource, AutoCloseable {

private Startable container;

private Class<?> declaringClass;

private String fieldName;

private StoreAdapter(Class<?> declaringClass, String fieldName, Startable container) {
this.declaringClass = declaringClass;
this.fieldName = fieldName;
this.key = declaringClass.getName() + "." + fieldName;
this.container = container;
}

private StoreAdapter start() {
if (container instanceof org.testcontainers.containers.Container) {
((org.testcontainers.containers.Container<?>) container)
.withLabel(TEST_CLASS_LABEL, declaringClass.getName());
((org.testcontainers.containers.Container<?>) container)
.withLabel(TEST_FIELD_LABEL, fieldName);
}
container.start();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.testcontainers.junit.jupiter;

import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;
import org.testcontainers.utility.DockerImageName;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class ContainerLabelingTest {

private static final DockerImageName IMAGE = DockerImageName.parse("nginx:1.27-alpine");

@Testcontainers
static class LabeledContainerTest {

@Container
@SuppressWarnings("unused")
static GenericContainer<?> container = new GenericContainer<>(IMAGE)
.withExposedPorts(80)
.waitingFor(new HostPortWaitStrategy());

@Test
void shouldHaveTestClassLabel() {
Map<String, String> labels = container.getContainerInfo().getConfig().getLabels();
assertThat(labels)
.containsEntry("testcontainers/test-class",
ContainerLabelingTest.class.getName() + "$LabeledContainerTest")
.containsEntry("testcontainers/test-field", "container");
}
}
}