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

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

commit eee931573451621f220b1423155c019dfb68e31e
Author: AurĂ©lien Pupier <[email protected]>
AuthorDate: Mon Nov 23 15:23:53 2026 +0100

    CAMEL-26387: test-infra: check container image architecture (workaround to 
improve robustness)
    
    Workaround to improve the robustness of the test-infra on ppc64le and s390x
    nodes, where an image with the wrong architecture (amd64) is sometimes used.
    
    Root cause: when pulling an image fails (for instance the registry mirror
    replies "toomanyrequests: Quota exceeded", see INFRA-28663),
    Testcontainers' RemoteDockerImage silently retries the pull with
    withPlatform("linux/amd64") (fallback to x86 added in Testcontainers
    1.16.0). On ppc64le/s390x this leaves an amd64 image in the local cache
    under the regular tag. The default pull policy never pulls an image that is
    available locally, so all subsequent runs reuse the wrong image and fail
    with "exec format error".
    
    This adds HostArchitectureImagePullPolicy, registered as the Testcontainers
    pull.policy through a testcontainers.properties file in
    camel-test-infra-common:
    - if the local image architecture differs from the host one, log a warning,
      remove the image and pull it again
    - if the image is missing, pull it explicitly for linux/<host arch>, so
      Testcontainers cannot fall back to linux/amd64
    - if the pull fails, throw a ContainerFetchException, which is retried with
      backoff by TestServiceUtil.tryInitialize
    - verify the architecture after the pull and fail clearly on a mismatch
    
    The check is enabled by default only on ppc64le and s390x hosts. It can be
    forced on or off with -Dcamel.test.infra.image.arch.check=true|false. When
    disabled, the behavior is the one of the Testcontainers default policy. A
    pull.policy defined in ~/.testcontainers.properties still takes precedence.
    
    This does not fix the root cause (the mirror quota), it only avoids the
    silent usage of an image with the wrong architecture.
    
    Co-authored-by: IBM Bob 2.2.1
    Signed-off-by: AurĂ©lien Pupier <[email protected]>
---
 .../common/HostArchitectureImagePullPolicy.java    | 163 +++++++++++++++++++++
 .../src/main/resources/testcontainers.properties   |  17 +++
 .../HostArchitectureImagePullPolicyTest.java       |  45 ++++++
 3 files changed, 225 insertions(+)

diff --git 
a/test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/HostArchitectureImagePullPolicy.java
 
b/test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/HostArchitectureImagePullPolicy.java
new file mode 100644
index 000000000000..0a760f38529e
--- /dev/null
+++ 
b/test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/HostArchitectureImagePullPolicy.java
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.infra.common;
+
+import java.util.Locale;
+
+import com.github.dockerjava.api.DockerClient;
+import com.github.dockerjava.api.command.InspectImageResponse;
+import com.github.dockerjava.api.exception.NotFoundException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.DockerClientFactory;
+import org.testcontainers.containers.ContainerFetchException;
+import org.testcontainers.images.ImagePullPolicy;
+import org.testcontainers.images.TimeLimitedLoggedPullImageResultCallback;
+import org.testcontainers.utility.DockerImageName;
+import org.testcontainers.utility.DockerLoggerFactory;
+
+/**
+ * Testcontainers {@link ImagePullPolicy} making sure that the image used 
matches the architecture of the host.
+ * <p>
+ * When pulling an image fails (for instance because the registry mirror 
replies {@code toomanyrequests}),
+ * Testcontainers silently retries the pull for {@code linux/amd64}. On hosts 
with another architecture (ppc64le, s390x)
+ * this leaves an image with the wrong architecture in the local cache, that 
is then reused by all subsequent runs and
+ * fails with {@code exec format error}.
+ * <p>
+ * When the check is enabled, this policy:
+ * <ul>
+ * <li>removes a local image whose architecture differs from the host one, and 
logs it</li>
+ * <li>pulls the image itself, explicitly for the host platform, without any 
fallback to another architecture. If this
+ * fails a {@link ContainerFetchException} is thrown, which is retried by
+ * {@link 
org.apache.camel.test.infra.common.services.TestServiceUtil#tryInitialize}</li>
+ * </ul>
+ * The check is enabled by default on ppc64le and s390x hosts, and can be 
forced on or off with the
+ * {@value #CHECK_PROPERTY} system property. When disabled, the behavior is 
the same as the Testcontainers default
+ * policy: an image is only pulled if it is not available locally.
+ * <p>
+ * This policy is registered through the {@code testcontainers.properties} 
file of this module.
+ */
+public class HostArchitectureImagePullPolicy implements ImagePullPolicy {
+
+    public static final String CHECK_PROPERTY = 
"camel.test.infra.image.arch.check";
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(HostArchitectureImagePullPolicy.class);
+
+    private final String hostArch;
+    private final boolean enabled;
+
+    public HostArchitectureImagePullPolicy() {
+        this.hostArch = normalizeArch(System.getProperty("os.arch"));
+        this.enabled = Boolean.parseBoolean(
+                System.getProperty(CHECK_PROPERTY, 
String.valueOf(isCheckedByDefault(hostArch))));
+    }
+
+    @Override
+    public boolean shouldPull(DockerImageName imageName) {
+        DockerClient client = DockerClientFactory.lazyClient();
+        String name = imageName.asCanonicalNameString();
+
+        String localArch = inspectLocalArch(client, name);
+        if (!enabled) {
+            return localArch == null;
+        }
+
+        if (localArch != null) {
+            if (hostArch.equals(normalizeArch(localArch))) {
+                return false;
+            }
+            LOG.warn("Local image {} has architecture {} but the host is {}, 
removing it and pulling it again",
+                    name, localArch, hostArch);
+            removeImage(client, name);
+        }
+
+        pullForHostPlatform(client, imageName);
+        return false;
+    }
+
+    private void pullForHostPlatform(DockerClient client, DockerImageName 
imageName) {
+        String name = imageName.asCanonicalNameString();
+        String platform = "linux/" + hostArch;
+        LOG.info("Pulling image {} for platform {}", name, platform);
+        try {
+            client.pullImageCmd(imageName.getUnversionedPart())
+                    .withTag(imageName.getVersionPart())
+                    .withPlatform(platform)
+                    .exec(new 
TimeLimitedLoggedPullImageResultCallback(DockerLoggerFactory.getLogger(name)))
+                    .awaitCompletion();
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new ContainerFetchException("Interrupted while pulling image 
" + name + " for platform " + platform, e);
+        } catch (RuntimeException e) {
+            // Do not let Testcontainers fall back to linux/amd64, this would 
leave an image that cannot run on this host
+            throw new ContainerFetchException(
+                    "Failed to pull image " + name + " for platform " + 
platform + " (set -D" + CHECK_PROPERTY
+                                              + "=false to disable the 
architecture check): " + e.getMessage(),
+                    e);
+        }
+
+        String pulledArch = inspectLocalArch(client, name);
+        if (pulledArch != null && !hostArch.equals(normalizeArch(pulledArch))) 
{
+            LOG.warn("Image {} has architecture {} after pulling it for 
platform {}, removing it", name, pulledArch,
+                    platform);
+            removeImage(client, name);
+            throw new ContainerFetchException(
+                    "Image " + name + " has architecture " + pulledArch + " 
but the host is " + hostArch);
+        }
+    }
+
+    private static String inspectLocalArch(DockerClient client, String name) {
+        try {
+            InspectImageResponse image = client.inspectImageCmd(name).exec();
+            return image.getArch();
+        } catch (NotFoundException e) {
+            return null;
+        }
+    }
+
+    private static void removeImage(DockerClient client, String name) {
+        try {
+            client.removeImageCmd(name).withForce(true).exec();
+        } catch (RuntimeException e) {
+            LOG.warn("Failed to remove image {}: {}", name, e.getMessage());
+        }
+    }
+
+    static boolean isCheckedByDefault(String normalizedHostArch) {
+        return "ppc64le".equals(normalizedHostArch) || 
"s390x".equals(normalizedHostArch);
+    }
+
+    /**
+     * Converts the value of the {@code os.arch} system property, or the 
architecture reported by Docker, to the
+     * architecture name used by the container images (amd64, arm64, ppc64le, 
s390x).
+     */
+    static String normalizeArch(String arch) {
+        if (arch == null) {
+            return "";
+        }
+        switch (arch.toLowerCase(Locale.ROOT)) {
+            case "x86_64":
+            case "amd64":
+                return "amd64";
+            case "aarch64":
+            case "arm64":
+                return "arm64";
+            default:
+                return arch.toLowerCase(Locale.ROOT);
+        }
+    }
+}
diff --git 
a/test-infra/camel-test-infra-common/src/main/resources/testcontainers.properties
 
b/test-infra/camel-test-infra-common/src/main/resources/testcontainers.properties
new file mode 100644
index 000000000000..a8ded24cdd77
--- /dev/null
+++ 
b/test-infra/camel-test-infra-common/src/main/resources/testcontainers.properties
@@ -0,0 +1,17 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License.  You may obtain a copy of the License at
+##
+##      http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+pull.policy=org.apache.camel.test.infra.common.HostArchitectureImagePullPolicy
diff --git 
a/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/HostArchitectureImagePullPolicyTest.java
 
b/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/HostArchitectureImagePullPolicyTest.java
new file mode 100644
index 000000000000..6302b767d594
--- /dev/null
+++ 
b/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/HostArchitectureImagePullPolicyTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.infra.common;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class HostArchitectureImagePullPolicyTest {
+
+    @Test
+    void normalizesJavaAndDockerArchitectures() {
+        assertEquals("amd64", 
HostArchitectureImagePullPolicy.normalizeArch("amd64"));
+        assertEquals("amd64", 
HostArchitectureImagePullPolicy.normalizeArch("x86_64"));
+        assertEquals("arm64", 
HostArchitectureImagePullPolicy.normalizeArch("aarch64"));
+        assertEquals("arm64", 
HostArchitectureImagePullPolicy.normalizeArch("arm64"));
+        assertEquals("ppc64le", 
HostArchitectureImagePullPolicy.normalizeArch("ppc64le"));
+        assertEquals("s390x", 
HostArchitectureImagePullPolicy.normalizeArch("s390x"));
+        assertEquals("", HostArchitectureImagePullPolicy.normalizeArch(null));
+    }
+
+    @Test
+    void checkIsEnabledByDefaultOnlyOnPowerAndZ() {
+        
assertTrue(HostArchitectureImagePullPolicy.isCheckedByDefault("ppc64le"));
+        
assertTrue(HostArchitectureImagePullPolicy.isCheckedByDefault("s390x"));
+        
assertFalse(HostArchitectureImagePullPolicy.isCheckedByDefault("amd64"));
+        
assertFalse(HostArchitectureImagePullPolicy.isCheckedByDefault("arm64"));
+    }
+}

Reply via email to