This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-karaf.git


The following commit(s) were added to refs/heads/main by this push:
     new a308c557e Ref #534: Remove docker images on exit (#536)
a308c557e is described below

commit a308c557e0f41c7d4f524227719c57228ed41c66
Author: Nicolas Filotto <[email protected]>
AuthorDate: Wed Oct 23 14:17:09 2024 +0200

    Ref #534: Remove docker images on exit (#536)
    
    ## 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](https://github.com/actions/runner-images/issues/2840#issuecomment-790492173)
      * 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
---
 .github/workflows/main.yml                         |  7 ++++++
 .../camel/itests/GenericContainerResource.java     | 28 ++++++++++++++++++++--
 tests/examples/pom.xml                             |  2 +-
 tests/features/camel-cxf/pom.xml                   |  2 +-
 tests/features/pom.xml                             |  4 +++-
 5 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index f6da17ca9..6961f041a 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -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:
diff --git 
a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/GenericContainerResource.java
 
b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/GenericContainerResource.java
index 39c4ad90b..83c1a4018 100644
--- 
a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/GenericContainerResource.java
+++ 
b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/GenericContainerResource.java
@@ -26,6 +26,7 @@ import java.util.function.Consumer;
 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.
@@ -35,6 +36,7 @@ import org.testcontainers.containers.GenericContainer;
 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<>();
@@ -63,7 +65,7 @@ public class GenericContainerResource<T extends 
GenericContainer<T>> implements
             dependency.before();
             dependency.properties().forEach(this::setProperty);
         }
-        LOG.info("Container {} started", container.getDockerImageName());
+        LOG.info("Container {}/{} started", container.getDockerImageName(), 
container.getContainerId());
     }
 
     @Override
@@ -75,8 +77,23 @@ public class GenericContainerResource<T extends 
GenericContainer<T>> implements
                 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
@@ -95,4 +112,11 @@ public class GenericContainerResource<T extends 
GenericContainer<T>> implements
     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"));
+    }
 }
diff --git a/tests/examples/pom.xml b/tests/examples/pom.xml
index 9721116d3..562565680 100644
--- a/tests/examples/pom.xml
+++ b/tests/examples/pom.xml
@@ -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>
diff --git a/tests/features/camel-cxf/pom.xml b/tests/features/camel-cxf/pom.xml
index 265ac7733..a49ca140c 100644
--- a/tests/features/camel-cxf/pom.xml
+++ b/tests/features/camel-cxf/pom.xml
@@ -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>
diff --git a/tests/features/pom.xml b/tests/features/pom.xml
index 741f58572..535332a12 100644
--- a/tests/features/pom.xml
+++ b/tests/features/pom.xml
@@ -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>
 
@@ -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>

Reply via email to