Skip to content

Commit

Permalink
Add support for adding labels to containers launches with @QuarkusInt…
Browse files Browse the repository at this point in the history
…egrationTest

Relates to: #33806
  • Loading branch information
geoand committed Jun 6, 2023
1 parent b54e11d commit e3f49d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ public static class Container {
*/
@ConfigItem
Map<String, String> additionalExposedPorts;

/**
* A set of labels to add to the launched container
*/
@ConfigItem
Map<String, String> labels;
}

public enum Mode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class DefaultDockerContainerLauncher implements DockerContainerArtifactLa
private String containerImage;
private boolean pullRequired;
private Map<Integer, Integer> additionalExposedPorts;

private Map<String, String> labels;
private final Map<String, String> systemProps = new HashMap<>();
private boolean isSsl;
private final String containerName = "quarkus-integration-test-" + RandomStringUtils.random(5, true, false);
Expand All @@ -60,6 +62,7 @@ public void init(DockerContainerArtifactLauncher.DockerInitContext initContext)
this.containerImage = initContext.containerImage();
this.pullRequired = initContext.pullRequired();
this.additionalExposedPorts = initContext.additionalExposedPorts();
this.labels = initContext.labels();
}

@Override
Expand Down Expand Up @@ -136,6 +139,11 @@ public void start() throws IOException {
for (var e : env.entrySet()) {
args.addAll(envAsLaunchArg(e.getKey(), e.getValue()));
}

for (var e : labels.entrySet()) {
args.add("--label");
args.add(e.getKey() + "=" + e.getValue());
}
args.add(containerImage);

final Path logFile = PropertyTestUtil.getLogFilePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ interface DockerInitContext extends InitContext {
boolean pullRequired();

Map<Integer, Integer> additionalExposedPorts();

Map<String, String> labels();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public DockerContainerArtifactLauncher create(CreateContext context) {
context.devServicesLaunchResult(),
containerImage,
pullRequired,
additionalExposedPorts(config)));
additionalExposedPorts(config),
labels(config)));
return launcher;
} else {
throw new IllegalStateException("The container image to be launched could not be determined");
Expand All @@ -66,20 +67,32 @@ private Map<Integer, Integer> additionalExposedPorts(SmallRyeConfig config) {
}
}

private Map<String, String> labels(SmallRyeConfig config) {
try {
return config.getValues("quarkus.test.container.labels", String.class, String.class);
} catch (NoSuchElementException e) {
return Collections.emptyMap();
}
}

static class DefaultDockerInitContext extends DefaultInitContextBase
implements DockerContainerArtifactLauncher.DockerInitContext {
private final String containerImage;
private final boolean pullRequired;
private final Map<Integer, Integer> additionalExposedPorts;
private Map<String, String> labels;

public DefaultDockerInitContext(int httpPort, int httpsPort, Duration waitTime, String testProfile,
List<String> argLine, Map<String, String> env,
ArtifactLauncher.InitContext.DevServicesLaunchResult devServicesLaunchResult,
String containerImage, boolean pullRequired, Map<Integer, Integer> additionalExposedPorts) {
String containerImage, boolean pullRequired,
Map<Integer, Integer> additionalExposedPorts,
Map<String, String> labels) {
super(httpPort, httpsPort, waitTime, testProfile, argLine, env, devServicesLaunchResult);
this.containerImage = containerImage;
this.pullRequired = pullRequired;
this.additionalExposedPorts = additionalExposedPorts;
this.labels = labels;
}

@Override
Expand All @@ -96,5 +109,10 @@ public boolean pullRequired() {
public Map<Integer, Integer> additionalExposedPorts() {
return additionalExposedPorts;
}

@Override
public Map<String, String> labels() {
return labels;
}
}
}

0 comments on commit e3f49d8

Please sign in to comment.