Skip to content

Commit

Permalink
Ref #534: Remove docker images on exit (#536)
Browse files Browse the repository at this point in the history
## Motivation

The build runs out of disk space which prevents adding new integration tests

## Modifications:

* Free up some disk space by:
  * following the workaround described [in this ticket](actions/runner-images#2840 (comment))
  * removing pre-pulled docker images
* Add a new System property `camel.karaf.itest.keep.docker.images` to indicate whether the docker images should be removed after the test. Locally, you can leverage the Maven property `keep.docker.images.on.exit` to keep them or not. By default, the docker images are cleaned up to prevent the disk space leak
* Extend the forked process exit timeout to ensure that pax-exam can stop properly
  • Loading branch information
essobedo authored Oct 23, 2024
1 parent f1940c4 commit a308c55
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Free disk space
run: |
sudo rm -rf /usr/share/dotnet \
/opt/ghc \
"/usr/local/share/boost" \
"$AGENT_TOOLSDIRECTORY"
docker system prune -af
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.ResourceReaper;

/**
* A JUnit ExternalResource that starts and stops a TestContainer.
Expand All @@ -35,6 +36,7 @@
public class GenericContainerResource<T extends GenericContainer<T>> implements ExternalResource {

private static final Logger LOG = LoggerFactory.getLogger(GenericContainerResource.class);
private static final String CAMEL_KARAF_INTEGRATION_TEST_KEEP_DOCKER_IMAGES_PROPERTY = "camel.karaf.itest.keep.docker.images";
private final T container;
private final Map<String, String> properties = new HashMap<>();
private final List<ExternalResource> dependencies = new ArrayList<>();
Expand Down Expand Up @@ -63,7 +65,7 @@ public void before() {
dependency.before();
dependency.properties().forEach(this::setProperty);
}
LOG.info("Container {} started", container.getDockerImageName());
LOG.info("Container {}/{} started", container.getDockerImageName(), container.getContainerId());
}

@Override
Expand All @@ -75,8 +77,23 @@ public void after() {
LOG.warn("Error cleaning dependency: {}", dependency.getClass().getName(), e);
}
}
String containerId = container.getContainerId();
container.stop();
LOG.info("Container {} stopped", container.getDockerImageName());
LOG.info("Container {}/{} stopped", container.getDockerImageName(), containerId);
if (cleanupDockerImagesOnExit()) {
removeDockerImage();
}
}

/**
* Remove the Docker image of the container.
*/
private void removeDockerImage() {
ResourceReaper resourceReaper = ResourceReaper.instance();
String dockerImageName = container.getDockerImageName();
resourceReaper.registerImageForCleanup(dockerImageName);
resourceReaper.performCleanup();
LOG.info("Docker Image {} removed", dockerImageName);
}

@Override
Expand All @@ -95,4 +112,11 @@ public void setProperty(String key, String value) {
public void addDependency(ExternalResource dependency) {
dependencies.add(dependency);
}

/**
* Indicates whether the Docker images should be removed after the test.
*/
private static boolean cleanupDockerImagesOnExit() {
return !Boolean.parseBoolean(System.getProperty(CAMEL_KARAF_INTEGRATION_TEST_KEEP_DOCKER_IMAGES_PROPERTY, "false"));
}
}
2 changes: 1 addition & 1 deletion tests/examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
<project.target>${project.build.directory}</project.target>
<org.ops4j.pax.logging.DefaultServiceLog.level>WARN</org.ops4j.pax.logging.DefaultServiceLog.level>
</systemPropertyVariables>
<forkedProcessExitTimeoutInSeconds>5</forkedProcessExitTimeoutInSeconds>
<forkedProcessExitTimeoutInSeconds>10</forkedProcessExitTimeoutInSeconds>
</configuration>
</execution>
</executions>
Expand Down
2 changes: 1 addition & 1 deletion tests/features/camel-cxf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<org.ops4j.pax.logging.DefaultServiceLog.level>WARN</org.ops4j.pax.logging.DefaultServiceLog.level>
<cxf.version>${cxf-version}</cxf.version>
</systemPropertyVariables>
<forkedProcessExitTimeoutInSeconds>5</forkedProcessExitTimeoutInSeconds>
<forkedProcessExitTimeoutInSeconds>10</forkedProcessExitTimeoutInSeconds>
</configuration>
</execution>
</executions>
Expand Down
4 changes: 3 additions & 1 deletion tests/features/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

<properties>
<dump.logs.on.failure>false</dump.logs.on.failure>
<keep.docker.images.on.exit>false</keep.docker.images.on.exit>
<users.file.location>${project.basedir}/../../camel-integration-test/src/main/resources/etc/users.properties</users.file.location>
</properties>

Expand Down Expand Up @@ -297,13 +298,14 @@
</includes>
<systemPropertyVariables>
<camel.karaf.itest.dump.logs>${dump.logs.on.failure}</camel.karaf.itest.dump.logs>
<camel.karaf.itest.keep.docker.images>${keep.docker.images.on.exit}</camel.karaf.itest.keep.docker.images>
<camel.karaf.version>${project.version}</camel.karaf.version>
<project.version>${project.version}</project.version>
<project.target>${project.build.directory}</project.target>
<users.file.location>${users.file.location}</users.file.location>
<org.ops4j.pax.logging.DefaultServiceLog.level>WARN</org.ops4j.pax.logging.DefaultServiceLog.level>
</systemPropertyVariables>
<forkedProcessExitTimeoutInSeconds>5</forkedProcessExitTimeoutInSeconds>
<forkedProcessExitTimeoutInSeconds>10</forkedProcessExitTimeoutInSeconds>
</configuration>
</execution>
</executions>
Expand Down

0 comments on commit a308c55

Please sign in to comment.