Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-8558 Make Ryuk shutdown hook configurable. #8732

Closed
wants to merge 2 commits into from
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 @@ -77,18 +77,20 @@ private synchronized void maybeStart() {

ryukContainer.start();

Runtime
.getRuntime()
.addShutdownHook(
new Thread(
DockerClientFactory.TESTCONTAINERS_THREAD_GROUP,
() -> {
this.dockerClient.killContainerCmd(this.ryukContainer.getContainerId())
.withSignal("SIGTERM")
.exec();
}
)
);
if (TestcontainersConfiguration.getInstance().isRyukShutdownHookEnabled()) {
Runtime
.getRuntime()
.addShutdownHook(
new Thread(
DockerClientFactory.TESTCONTAINERS_THREAD_GROUP,
() -> {
this.dockerClient.killContainerCmd(this.ryukContainer.getContainerId())
.withSignal("SIGTERM")
.exec();
}
)
);
}

CountDownLatch ryukScheduledLatch = new CountDownLatch(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ public boolean isDisableChecks() {
return Boolean.parseBoolean(getEnvVarOrUserProperty("checks.disable", "false"));
}

public boolean isRyukShutdownHookEnabled() {
return Boolean.parseBoolean(getEnvVarOrUserProperty("ryuk.container.shutdownhook", "false"));
}

@UnstableAPI
public boolean environmentSupportsReuse() {
// specifically not supported as an environment variable or classpath property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,35 @@ public void shouldTrimImageNames() {
.isEqualTo("testcontainers/ryuk:0.3.2");
}

@Test
public void shouldNotReadRyukShutdownHookClasspathProperties() {
assertThat(newConfig().isRyukShutdownHookEnabled()).as("Ryuk shutdown hook disabled by default").isFalse();

classpathProperties.setProperty("ryuk.container.shutdownhook", "true");
assertThat(newConfig().isRyukShutdownHookEnabled())
.as("Ryuk shutdown hook is not affected by classpath properties")
.isFalse();
}

@Test
public void shouldReadRyukShutdownHookFromUserProperties() {
assertThat(newConfig().isRyukShutdownHookEnabled()).as("Ryuk shutdown hook disabled by default").isFalse();

userProperties.setProperty("ryuk.container.shutdownhook", "true");
assertThat(newConfig().isRyukShutdownHookEnabled())
.as("Ryuk shutdown hook enabled via user properties")
.isTrue();
}

@Test
public void shouldReadRyukShutdownHookFromEnvironment() {
assertThat(newConfig().isRyukShutdownHookEnabled()).as("Ryuk shutdown hook disabled by default").isFalse();

userProperties.remove("ryuk.container.shutdownhook");
environment.put("TESTCONTAINERS_RYUK_CONTAINER_SHUTDOWNHOOK", "true");
assertThat(newConfig().isRyukShutdownHookEnabled()).as("Ryuk shutdown hook enabled via env var").isTrue();
}

private TestcontainersConfiguration newConfig() {
return new TestcontainersConfiguration(userProperties, classpathProperties, environment);
}
Expand Down
5 changes: 4 additions & 1 deletion docs/features/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,16 @@ Some companies disallow the usage of Docker Hub, but you can override `*.image`
> **ryuk.container.privileged = true**
> In some environments ryuk must be started in privileged mode to work properly (--privileged flag)

> **ryuk.container.shutdownhook = true**
> In order to improve the termination process you may configure a shutdown hook which will send a SIGTERM to the Ryuk container causing it to finish sooner.

### Disabling Ryuk
Ryuk must be started as a privileged container.
If your environment already implements automatic cleanup of containers after the execution,
but does not allow starting privileged containers, you can turn off the Ryuk container by setting
`TESTCONTAINERS_RYUK_DISABLED` **environment variable** to `true`.

!!!tip
!!! tip
Note that Testcontainers will continue doing the cleanup at JVM's shutdown, unless you `kill -9` your JVM process.

## Customizing image pull behaviour
Expand Down