Skip to content

Commit

Permalink
Allow the use of "podman kill"
Browse files Browse the repository at this point in the history
The method used to stop containers is now configurable. STOP is the old
"podman stop" method. KILL allows for the use of "podman kill". This
may avoid any container surviving a test execution.

Affects: #2
  • Loading branch information
io7m committed Mar 31, 2024
1 parent a57738a commit f00bde9
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@
* @param startupWaitTime The startup wait time
* @param livenessCheckPauseTime The pause time between liveness checks
* @param debugLogging Whether debug logging is enabled
* @param stopMethod The stop method used for containers
*/

public record EContainerConfiguration(
RDottedName projectName,
String podmanExecutable,
Duration startupWaitTime,
Duration livenessCheckPauseTime,
boolean debugLogging)
boolean debugLogging,
EContainerStop stopMethod)
{
/**
* The container configuration.
Expand All @@ -47,6 +49,7 @@ public record EContainerConfiguration(
* @param startupWaitTime The startup wait time
* @param livenessCheckPauseTime The pause time between liveness checks
* @param debugLogging Whether debug logging is enabled
* @param stopMethod The stop method used for containers
*/

public EContainerConfiguration
Expand All @@ -55,6 +58,7 @@ public record EContainerConfiguration(
Objects.requireNonNull(podmanExecutable, "podmanExecutable");
Objects.requireNonNull(startupWaitTime, "startupWaitTime");
Objects.requireNonNull(livenessCheckPauseTime, "livenessCheckPauseTime");
Objects.requireNonNull(stopMethod, "stopMethod");
}

/**
Expand All @@ -71,7 +75,8 @@ public static EContainerConfiguration defaults(
"podman",
Duration.ofSeconds(30L),
Duration.ofMillis(500L),
false
false,
EContainerStop.STOP
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright © 2024 Mark Raynsford <[email protected]> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


package com.io7m.ervilla.api;

/**
* The method used to stop a container.
*/

public enum EContainerStop
{
/**
* Instruct the container to perform a clean shutdown.
*/

STOP,

/**
* Kill the container with a KILL signal.
*/

KILL
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ void copyFrom(
* Stop the container but do not destroy it. The container can be started
* again with {@link #start()}.
*
* @param stop The method used to stop the container
*
* @throws InterruptedException On interruption
* @throws IOException On errors
*/

void stop()
void stop(EContainerStop stop)
throws InterruptedException, IOException;

/**
* Start the container. This is only useful after a {@link #stop()} as
* Start the container. This is only useful after a {@link #stop(EContainerStop)} as
* containers are automatically started up when created.
*
* @throws InterruptedException On interruption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.io7m.ervilla.api.EContainerFactoryType;
import com.io7m.ervilla.api.EContainerReference;
import com.io7m.ervilla.api.EContainerSpec;
import com.io7m.ervilla.api.EContainerStop;
import com.io7m.ervilla.api.EContainerSupervisorScope;
import com.io7m.ervilla.api.EContainerSupervisorType;
import com.io7m.ervilla.api.EContainerType;
Expand Down Expand Up @@ -587,14 +588,41 @@ private void executeContainerStop(
final var stopProc =
this.executeLogged(
Optional.of(name),
"close",
"stop",
closeArgs,
IGNORING
);

try {
MDC.pushByKey("PID", Long.toUnsignedString(stopProc.pid()));
LOG.debug("Waiting for 'close' invocation.");
LOG.debug("Waiting for 'stop' invocation.");
stopProc.waitFor(3L, TimeUnit.SECONDS);
LOG.debug("Status {}", stopProc);
} finally {
MDC.popByKey("PID");
}
}

private void executeContainerKill(
final String name)
throws IOException, InterruptedException
{
final var closeArgs = new ArrayList<String>(6);
closeArgs.addAll(this.podman());
closeArgs.add("kill");
closeArgs.add(name);

final var stopProc =
this.executeLogged(
Optional.of(name),
"kill",
closeArgs,
IGNORING
);

try {
MDC.pushByKey("PID", Long.toUnsignedString(stopProc.pid()));
LOG.debug("Waiting for 'kill' invocation.");
stopProc.waitFor(3L, TimeUnit.SECONDS);
LOG.debug("Status {}", stopProc);
} finally {
Expand Down Expand Up @@ -956,13 +984,20 @@ private void mdcPush()
}

@Override
public void stop()
public void stop(
final EContainerStop stop)
throws InterruptedException, IOException
{
Objects.requireNonNull(stop, "stop");

try {
this.mdcPush();

this.supervisor.executeContainerStop(this.name);
switch (stop) {
case STOP -> this.supervisor.executeContainerStop(this.name);
case KILL -> this.supervisor.executeContainerKill(this.name);
}

this.process.waitFor(5L, TimeUnit.SECONDS);
if (this.process.isAlive()) {
throw new IOException(
Expand Down Expand Up @@ -1025,7 +1060,10 @@ public void close()
*/

LOG.debug("Shutting down.");
this.supervisor.executeContainerStop(this.name);
switch (this.configuration.stopMethod()) {
case STOP -> this.supervisor.executeContainerStop(this.name);
case KILL -> this.supervisor.executeContainerKill(this.name);
}
this.supervisor.executeContainerRemove(this.name);

LOG.debug("Waiting for container shutdown.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.io7m.ervilla.test_extension;

import com.io7m.ervilla.api.EContainerStop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -77,4 +79,10 @@
*/

boolean debugLogging() default false;

/**
* @return The stop method used to shut down containers
*/

EContainerStop stopMethod() default EContainerStop.STOP;
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ public static EContainerConfiguration supervisorGetConfiguration(
annotation.livenessCheckPauseTime(),
annotation.livenessCheckPauseTimeUnit().toChronoUnit()
),
annotation.debugLogging()
annotation.debugLogging(),
annotation.stopMethod()
);
}
return configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.io7m.ervilla.api.EContainerConfiguration;
import com.io7m.ervilla.api.EContainerSpec;
import com.io7m.ervilla.api.EContainerStop;
import com.io7m.ervilla.api.EPortAddressType;
import com.io7m.ervilla.api.EPortPublish;
import com.io7m.ervilla.api.EVolumeMount;
Expand All @@ -43,6 +44,8 @@
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static com.io7m.ervilla.api.EContainerStop.KILL;
import static com.io7m.ervilla.api.EContainerStop.STOP;
import static com.io7m.ervilla.api.EContainerSupervisorScope.PER_TEST;
import static com.io7m.ervilla.api.EPortProtocol.TCP;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -101,7 +104,8 @@ public void testWrongExecutableNotSupported()
"THIS-DOES-NOT-EXIST",
Duration.ofSeconds(30L),
Duration.ofMillis(250L),
true
true,
STOP
)
);

Expand Down Expand Up @@ -251,9 +255,9 @@ public void testRunExec(
assertEquals("HELLO!", Files.readString(fileOut));
assertTrue(c.name().startsWith("ERVILLA-"));

c.stop();
c.stop(STOP);
c.start();
c.stop();
c.stop(KILL);
c.start();
}
}
Expand Down Expand Up @@ -285,11 +289,11 @@ public void testRunStopStart()
);

LOG.info("### STOP!");
c.stop();
c.stop(STOP);
LOG.info("### START!");
c.start();
LOG.info("### STOP!");
c.stop();
c.stop(KILL);
LOG.info("### START!");
c.start();

Expand Down

0 comments on commit f00bde9

Please sign in to comment.