Skip to content

Commit 6cf7107

Browse files
committed
Improve the "temp-extensions" workaround:
- Copy extensions to the temp dir only if required. It is required when removing prepackaged extensions. - Custom copyToContainer calls that specify /opt/hivemq/extensions/... as container path automatically apply the temp dir workaround if required.
1 parent 46fe670 commit 6cf7107

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

modules/hivemq/src/main/java/org/testcontainers/hivemq/HiveMQContainer.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.testcontainers.containers.GenericContainer;
1111
import org.testcontainers.containers.wait.strategy.Wait;
1212
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
13+
import org.testcontainers.images.builder.Transferable;
1314
import org.testcontainers.utility.DockerImageName;
1415
import org.testcontainers.utility.MountableFile;
1516

@@ -76,6 +77,8 @@ public class HiveMQContainer extends GenericContainer<HiveMQContainer> {
7677
@NotNull
7778
private final WaitAllStrategy waitStrategy = new WaitAllStrategy();
7879

80+
private boolean isStarted = false;
81+
7982
public HiveMQContainer(final @NotNull DockerImageName dockerImageName) {
8083
super(dockerImageName);
8184
dockerImageName.assertCompatibleWith(DEFAULT_HIVEMQ_CE_IMAGE_NAME, DEFAULT_HIVEMQ_EE_IMAGE_NAME);
@@ -115,10 +118,10 @@ public HiveMQContainer(final @NotNull DockerImageName dockerImageName) {
115118

116119
@Override
117120
protected void configure() {
118-
final String removeCommand;
119-
withCreateContainerCmdModifier(it -> it.withEntrypoint("/bin/sh"));
121+
if (removePrepackagedExtensions()) {
122+
withCreateContainerCmdModifier(it -> it.withEntrypoint("/bin/sh"));
120123

121-
if (removeAllPrepackagedExtensions || !prepackagedExtensionsToRemove.isEmpty()) {
124+
final String removeCommand;
122125
if (removeAllPrepackagedExtensions) {
123126
removeCommand = "rm -rf /opt/hivemq/extensions/** &&";
124127
} else {
@@ -128,19 +131,38 @@ protected void configure() {
128131
.map(extensionId -> "rm -rf /opt/hivemq/extensions/" + extensionId + "&&")
129132
.collect(Collectors.joining());
130133
}
131-
} else {
132-
removeCommand = "";
134+
setCommand(
135+
"-c",
136+
removeCommand +
137+
"cp -r '/opt/hivemq/temp-extensions/'* /opt/hivemq/extensions/ ; " +
138+
"chmod -R 777 /opt/hivemq/extensions ; " +
139+
"/opt/docker-entrypoint.sh /opt/hivemq/bin/run.sh"
140+
);
133141
}
134-
setCommand(
135-
"-c",
136-
removeCommand +
137-
"cp -r '/opt/hivemq/temp-extensions/'* /opt/hivemq/extensions/ ; " +
138-
"chmod -R 777 /opt/hivemq/extensions ; " +
139-
"/opt/docker-entrypoint.sh /opt/hivemq/bin/run.sh"
140-
);
142+
}
143+
144+
private boolean removePrepackagedExtensions() {
145+
return removeAllPrepackagedExtensions || !prepackagedExtensionsToRemove.isEmpty();
146+
}
147+
148+
@Override
149+
public void copyFileToContainer(final @NotNull MountableFile mountableFile, @NotNull String containerPath) {
150+
if (!isStarted && removePrepackagedExtensions() && containerPath.startsWith("/opt/hivemq/extensions/")) {
151+
containerPath = "/opt/hivemq/temp-extensions/" + containerPath.substring("/opt/hivemq/extensions/".length());
152+
}
153+
super.copyFileToContainer(mountableFile, containerPath);
154+
}
155+
156+
@Override
157+
public void copyFileToContainer(final @NotNull Transferable transferable, @NotNull String containerPath) {
158+
if (!isStarted && removePrepackagedExtensions() && containerPath.startsWith("/opt/hivemq/extensions/")) {
159+
containerPath = "/opt/hivemq/temp-extensions/" + containerPath.substring("/opt/hivemq/extensions/".length());
160+
}
161+
super.copyFileToContainer(transferable, containerPath);
141162
}
142163

143164
protected void containerIsStarted(final @NotNull InspectContainerResponse containerInfo) {
165+
isStarted = true;
144166
if (controlCenterEnabled) {
145167
LOGGER.info(
146168
"The HiveMQ Control Center is reachable under: http://{}:{}",
@@ -208,11 +230,9 @@ protected void containerIsStarted(final @NotNull InspectContainerResponse contai
208230

209231
/**
210232
* Wraps the given class and all its subclasses into an extension
211-
* and puts it into '/opt/hivemq/temp-extensions/{extension-id}' inside the container.
233+
* and puts it into '/opt/hivemq/extensions/{extension-id}' inside the container.
212234
* <p>
213235
* Must be called before the container is started.
214-
* <p>
215-
* The contents of the '/opt/hivemq/temp-extensions/' directory are copied to '/opt/hivemq/extensions/' before the container is started.
216236
*
217237
* @param hiveMQExtension the {@link HiveMQExtension} of the extension
218238
* @return self
@@ -221,21 +241,19 @@ protected void containerIsStarted(final @NotNull InspectContainerResponse contai
221241
try {
222242
final File extension = hiveMQExtension.createExtension(hiveMQExtension);
223243
final MountableFile mountableExtension = MountableFile.forHostPath(extension.getPath(), MODE);
224-
withCopyFileToContainer(mountableExtension, "/opt/hivemq/temp-extensions/" + hiveMQExtension.getId());
244+
withCopyFileToContainer(mountableExtension, "/opt/hivemq/extensions/" + hiveMQExtension.getId());
225245
} catch (final Exception e) {
226246
throw new ContainerLaunchException(e.getMessage() == null ? "" : e.getMessage(), e);
227247
}
228248
return self();
229249
}
230250

231251
/**
232-
* Puts the given extension folder into '/opt/hivemq/temp-extensions/{directory-name}' inside the container.
252+
* Puts the given extension folder into '/opt/hivemq/extensions/{directory-name}' inside the container.
233253
* It must at least contain a valid hivemq-extension.xml and a valid extension.jar in order to be executed.
234254
* The directory-name is taken from the id defined in the hivemq-extension.xml.
235255
* <p>
236256
* Must be called before the container is started.
237-
* <p>
238-
* The contents of the '/opt/hivemq/temp-extensions/' directory are copied to '/opt/hivemq/extensions/' before the container is started.
239257
*
240258
* @param mountableExtension the extension folder on the host machine
241259
* @return self
@@ -256,7 +274,7 @@ protected void containerIsStarted(final @NotNull InspectContainerResponse contai
256274
}
257275
try {
258276
final String extensionDirName = getExtensionDirectoryName(extensionDir);
259-
final String containerPath = "/opt/hivemq/temp-extensions/" + extensionDirName;
277+
final String containerPath = "/opt/hivemq/extensions/" + extensionDirName;
260278
withCopyFileToContainer(cloneWithFileMode(mountableExtension), containerPath);
261279
LOGGER.info("Putting extension '{}' into '{}'", extensionDirName, containerPath);
262280
} catch (final Exception e) {
@@ -350,11 +368,9 @@ protected void containerIsStarted(final @NotNull InspectContainerResponse contai
350368
}
351369

352370
/**
353-
* Puts the given file into the root of the extension's home '/opt/hivemq/temp-extensions/{extensionId}/'.
371+
* Puts the given file into the root of the extension's home '/opt/hivemq/extensions/{extensionId}/'.
354372
* <p>
355373
* Must be called before the container is started.
356-
* <p>
357-
* The contents of the '/opt/hivemq/temp-extensions/' directory are copied to '/opt/hivemq/extensions/' before the container is started.
358374
*
359375
* @param file the file on the host machine
360376
* @param extensionId the extension
@@ -368,11 +384,9 @@ protected void containerIsStarted(final @NotNull InspectContainerResponse contai
368384
}
369385

370386
/**
371-
* Puts the given file into given subdirectory of the extensions's home '/opt/hivemq/temp-extensions/{id}/{pathInExtensionHome}/'
387+
* Puts the given file into given subdirectory of the extensions's home '/opt/hivemq/extensions/{id}/{pathInExtensionHome}/'
372388
* <p>
373389
* Must be called before the container is started.
374-
* <p>
375-
* The contents of the '/opt/hivemq/temp-extensions/' directory are copied to '/opt/hivemq/extensions/' before the container is started.
376390
*
377391
* @param file the file on the host machine
378392
* @param extensionId the extension
@@ -386,7 +400,7 @@ protected void containerIsStarted(final @NotNull InspectContainerResponse contai
386400
) {
387401
return withFileInHomeFolder(
388402
file,
389-
"/temp-extensions/" + extensionId + PathUtil.prepareAppendPath(pathInExtensionHome)
403+
"/extensions/" + extensionId + PathUtil.prepareAppendPath(pathInExtensionHome)
390404
);
391405
}
392406

0 commit comments

Comments
 (0)