You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polaris.apache.org by di...@apache.org on 2026/06/18 20:39:21 UTC

(polaris) branch main updated: Improve ContainerSpecHelper partial image configuration error message (#4807)

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

dimas-b pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/polaris.git


The following commit(s) were added to refs/heads/main by this push:
     new 5d08f04413 Improve ContainerSpecHelper partial image configuration error message (#4807)
5d08f04413 is described below

commit 5d08f04413592f5a8a73813bd9155c7247e6938f
Author: venkateshwaran shanmugham <ve...@gmail.com>
AuthorDate: Fri Jun 19 02:09:17 2026 +0530

    Improve ContainerSpecHelper partial image configuration error message (#4807)
    
    * Improve ContainerSpecHelper partial image configuration error message
    
    * Improve ContainerSpecHelper testability
---
 .../polaris/containerspec/ContainerSpecHelper.java |  35 +++--
 .../containerspec/ContainerSpecHelperTest.java     | 153 +++++++++++++++++++++
 .../polaris/containerspec/Dockerfile-test-version  |  21 +++
 3 files changed, 200 insertions(+), 9 deletions(-)

diff --git a/tools/container-spec-helper/src/main/java/org/apache/polaris/containerspec/ContainerSpecHelper.java b/tools/container-spec-helper/src/main/java/org/apache/polaris/containerspec/ContainerSpecHelper.java
index 2d5a12ea3d..ff076ad6a6 100644
--- a/tools/container-spec-helper/src/main/java/org/apache/polaris/containerspec/ContainerSpecHelper.java
+++ b/tools/container-spec-helper/src/main/java/org/apache/polaris/containerspec/ContainerSpecHelper.java
@@ -24,6 +24,7 @@ import java.io.InputStreamReader;
 import java.net.URL;
 import java.util.Locale;
 import java.util.Objects;
+import java.util.function.Function;
 import org.testcontainers.utility.DockerImageName;
 
 // Spotless/Java doesn't really "like" Javadoc @snippet
@@ -72,14 +73,30 @@ import org.testcontainers.utility.DockerImageName;
 public final class ContainerSpecHelper {
   private final String name;
   private final Class<?> containerClass;
+  private final Function<String, String> systemPropertyLookup;
+  private final Function<String, String> envLookup;
 
   public static ContainerSpecHelper containerSpecHelper(String name, Class<?> containerClass) {
-    return new ContainerSpecHelper(name, containerClass);
+    return containerSpecHelper(name, containerClass, System::getProperty, System::getenv);
   }
 
-  private ContainerSpecHelper(String name, Class<?> containerClass) {
+  static ContainerSpecHelper containerSpecHelper(
+      String name,
+      Class<?> containerClass,
+      Function<String, String> systemPropertyLookup,
+      Function<String, String> envLookup) {
+    return new ContainerSpecHelper(name, containerClass, systemPropertyLookup, envLookup);
+  }
+
+  private ContainerSpecHelper(
+      String name,
+      Class<?> containerClass,
+      Function<String, String> systemPropertyLookup,
+      Function<String, String> envLookup) {
     this.name = name;
     this.containerClass = containerClass;
+    this.systemPropertyLookup = systemPropertyLookup;
+    this.envLookup = envLookup;
   }
 
   public String name() {
@@ -103,19 +120,19 @@ public final class ContainerSpecHelper {
     String systemPropPrefix2 = "polaris.testing." + name() + ".";
     String envPrefix = name().toUpperCase(Locale.ROOT).replaceAll("-", "_") + "_DOCKER_";
 
-    String explicitImage = System.getProperty(systemPropPrefix1 + "image");
+    String explicitImage = systemPropertyLookup.apply(systemPropPrefix1 + "image");
     if (explicitImage == null) {
-      explicitImage = System.getProperty(systemPropPrefix2 + "image");
+      explicitImage = systemPropertyLookup.apply(systemPropPrefix2 + "image");
     }
     if (explicitImage == null) {
-      explicitImage = System.getenv(envPrefix + "IMAGE");
+      explicitImage = envLookup.apply(envPrefix + "IMAGE");
     }
-    String explicitTag = System.getProperty(systemPropPrefix1 + "tag");
+    String explicitTag = systemPropertyLookup.apply(systemPropPrefix1 + "tag");
     if (explicitTag == null) {
-      explicitTag = System.getProperty(systemPropPrefix2 + "tag");
+      explicitTag = systemPropertyLookup.apply(systemPropPrefix2 + "tag");
     }
     if (explicitTag == null) {
-      explicitTag = System.getenv(envPrefix + "TAG");
+      explicitTag = envLookup.apply(envPrefix + "TAG");
     }
 
     if (explicitImage != null && explicitTag != null) {
@@ -141,7 +158,7 @@ public final class ContainerSpecHelper {
 
       if (explicitImage != null || explicitTag != null) {
         throw new IllegalArgumentException(
-            "Must specify either BOTH, image name AND tag via system properties or environment  or omit and leave it to the default "
+            "Must specify both image name and tag via system properties or environment variables, or omit both to use the default "
                 + fullImageName
                 + " from "
                 + dockerfile);
diff --git a/tools/container-spec-helper/src/test/java/org/apache/polaris/containerspec/ContainerSpecHelperTest.java b/tools/container-spec-helper/src/test/java/org/apache/polaris/containerspec/ContainerSpecHelperTest.java
new file mode 100644
index 0000000000..b1ed6ad538
--- /dev/null
+++ b/tools/container-spec-helper/src/test/java/org/apache/polaris/containerspec/ContainerSpecHelperTest.java
@@ -0,0 +1,153 @@
+/*
+ * 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.polaris.containerspec;
+
+import static org.apache.polaris.containerspec.ContainerSpecHelper.containerSpecHelper;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.testcontainers.utility.DockerImageName;
+
+class ContainerSpecHelperTest {
+
+  private static final String CONTAINER_NAME = "test";
+  private static final String DEFAULT_IMAGE = "docker.io/test/image:1.2.3";
+  private static final String EXPECTED_PARTIAL_CONFIG_MESSAGE =
+      "Must specify both image name and tag via system properties or environment variables, or omit both to use the default "
+          + DEFAULT_IMAGE
+          + " from Dockerfile-test-version";
+
+  private static ContainerSpecHelper testHelper(
+      Map<String, String> systemProperties, Map<String, String> environmentVariables) {
+    return containerSpecHelper(
+        CONTAINER_NAME,
+        ContainerSpecHelperTest.class,
+        systemProperties::get,
+        environmentVariables::get);
+  }
+
+  @Test
+  void dockerImageNameUsesDefaultFromDockerfileWhenNoOverrides() {
+    DockerImageName imageName = testHelper(Map.of(), Map.of()).dockerImageName(null);
+
+    assertThat(imageName).isEqualTo(DockerImageName.parse(DEFAULT_IMAGE));
+  }
+
+  @Test
+  void dockerImageNameUsesExplicitImageNameWhenProvided() {
+    DockerImageName imageName =
+        testHelper(Map.of(), Map.of()).dockerImageName("docker.io/explicit:4.5.6");
+
+    assertThat(imageName).isEqualTo(DockerImageName.parse("docker.io/explicit:4.5.6"));
+  }
+
+  @Test
+  void dockerImageNameUsesBothSystemPropertiesWhenProvided() {
+    DockerImageName imageName =
+        testHelper(
+                Map.of(
+                    "polaris.testing.test.image",
+                    "docker.io/custom/image",
+                    "polaris.testing.test.tag",
+                    "9.9.9"),
+                Map.of())
+            .dockerImageName(null);
+
+    assertThat(imageName).isEqualTo(DockerImageName.parse("docker.io/custom/image:9.9.9"));
+  }
+
+  @Test
+  void dockerImageNameUsesBothEnvironmentVariablesWhenProvided() {
+    DockerImageName imageName =
+        testHelper(
+                Map.of(),
+                Map.of("TEST_DOCKER_IMAGE", "docker.io/env/image", "TEST_DOCKER_TAG", "8.8.8"))
+            .dockerImageName(null);
+
+    assertThat(imageName).isEqualTo(DockerImageName.parse("docker.io/env/image:8.8.8"));
+  }
+
+  @Test
+  void dockerImageNamePrefersSystemPropertyOverEnvironmentVariable() {
+    DockerImageName imageName =
+        testHelper(
+                Map.of(
+                    "polaris.testing.test.image",
+                    "docker.io/prop/image",
+                    "polaris.testing.test.tag",
+                    "1.0.0"),
+                Map.of("TEST_DOCKER_IMAGE", "docker.io/env/image", "TEST_DOCKER_TAG", "2.0.0"))
+            .dockerImageName(null);
+
+    assertThat(imageName).isEqualTo(DockerImageName.parse("docker.io/prop/image:1.0.0"));
+  }
+
+  @Test
+  void dockerImageNameRejectsImageWithoutTag() {
+    assertThatThrownBy(
+            () ->
+                testHelper(Map.of("polaris.testing.test.image", "docker.io/custom/image"), Map.of())
+                    .dockerImageName(null))
+        .hasRootCauseInstanceOf(IllegalArgumentException.class)
+        .hasRootCauseMessage(EXPECTED_PARTIAL_CONFIG_MESSAGE);
+  }
+
+  @Test
+  void dockerImageNameRejectsTagWithoutImage() {
+    assertThatThrownBy(
+            () ->
+                testHelper(Map.of("polaris.testing.test.tag", "9.9.9"), Map.of())
+                    .dockerImageName(null))
+        .hasRootCauseInstanceOf(IllegalArgumentException.class)
+        .hasRootCauseMessage(EXPECTED_PARTIAL_CONFIG_MESSAGE);
+  }
+
+  @Test
+  void dockerImageNameRejectsImageWithoutTagFromEnvironmentVariable() {
+    assertThatThrownBy(
+            () ->
+                testHelper(Map.of(), Map.of("TEST_DOCKER_IMAGE", "docker.io/env/image"))
+                    .dockerImageName(null))
+        .hasRootCauseInstanceOf(IllegalArgumentException.class)
+        .hasRootCauseMessage(EXPECTED_PARTIAL_CONFIG_MESSAGE);
+  }
+
+  @Test
+  void dockerImageNameRejectsTagWithoutImageFromEnvironmentVariable() {
+    assertThatThrownBy(
+            () -> testHelper(Map.of(), Map.of("TEST_DOCKER_TAG", "8.8.8")).dockerImageName(null))
+        .hasRootCauseInstanceOf(IllegalArgumentException.class)
+        .hasRootCauseMessage(EXPECTED_PARTIAL_CONFIG_MESSAGE);
+  }
+
+  @ParameterizedTest
+  @ValueSource(strings = {"it.polaris.container.test.image", "polaris.testing.test.image"})
+  void dockerImageNameRejectsPartialImageOverrideFromAnySystemPropertyPrefix(String imageProperty) {
+    assertThatThrownBy(
+            () ->
+                testHelper(Map.of(imageProperty, "docker.io/custom/image"), Map.of())
+                    .dockerImageName(null))
+        .hasRootCauseInstanceOf(IllegalArgumentException.class)
+        .hasRootCauseMessage(EXPECTED_PARTIAL_CONFIG_MESSAGE);
+  }
+}
diff --git a/tools/container-spec-helper/src/test/resources/org/apache/polaris/containerspec/Dockerfile-test-version b/tools/container-spec-helper/src/test/resources/org/apache/polaris/containerspec/Dockerfile-test-version
new file mode 100644
index 0000000000..09a2f5ba99
--- /dev/null
+++ b/tools/container-spec-helper/src/test/resources/org/apache/polaris/containerspec/Dockerfile-test-version
@@ -0,0 +1,21 @@
+#
+# 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.
+#
+
+# Dockerfile to provide the image name and tag to a test.
+FROM docker.io/test/image:1.2.3