From 79dd5a3aa373404fed87665ebe3728b1522457b6 Mon Sep 17 00:00:00 2001 From: Vikash Kumar Date: Tue, 17 Dec 2024 13:42:21 +0530 Subject: [PATCH] Add and use SystemEnvUtils#isEnvSet method --- .../java/io/trino/tests/product/launcher/cli/TestRun.java | 3 ++- .../java/io/trino/testing/containers/TestContainers.java | 3 ++- .../src/main/java/io/trino/testing/SystemEnvUtils.java | 5 +++++ .../testing/services/junit/LogTestDurationListener.java | 3 ++- .../io/trino/testng/services/FlakyTestRetryAnalyzer.java | 3 ++- .../io/trino/testng/services/LogTestDurationListener.java | 3 ++- .../io/trino/testng/services/ProgressLoggingListener.java | 3 ++- 7 files changed, 17 insertions(+), 6 deletions(-) diff --git a/testing/trino-product-tests-launcher/src/main/java/io/trino/tests/product/launcher/cli/TestRun.java b/testing/trino-product-tests-launcher/src/main/java/io/trino/tests/product/launcher/cli/TestRun.java index 27c05f8bbc68..e5c8ea131470 100644 --- a/testing/trino-product-tests-launcher/src/main/java/io/trino/tests/product/launcher/cli/TestRun.java +++ b/testing/trino-product-tests-launcher/src/main/java/io/trino/tests/product/launcher/cli/TestRun.java @@ -53,6 +53,7 @@ import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.collect.ImmutableList.toImmutableList; +import static io.trino.testing.SystemEnvUtils.isEnvSet; import static io.trino.tests.product.launcher.env.DockerContainer.cleanOrCreateHostPath; import static io.trino.tests.product.launcher.env.EnvironmentContainers.TESTS; import static io.trino.tests.product.launcher.env.EnvironmentListener.getStandardListeners; @@ -335,7 +336,7 @@ private Environment getEnvironment() unsafelyExposePort(container, 5007); // debug port } - if (System.getenv("CONTINUOUS_INTEGRATION") != null) { + if (isEnvSet("CONTINUOUS_INTEGRATION")) { container.withEnv("CONTINUOUS_INTEGRATION", "true"); } diff --git a/testing/trino-testing-containers/src/main/java/io/trino/testing/containers/TestContainers.java b/testing/trino-testing-containers/src/main/java/io/trino/testing/containers/TestContainers.java index 03330b35f54c..9d1dba2ae5dc 100644 --- a/testing/trino-testing-containers/src/main/java/io/trino/testing/containers/TestContainers.java +++ b/testing/trino-testing-containers/src/main/java/io/trino/testing/containers/TestContainers.java @@ -31,6 +31,7 @@ import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Strings.padEnd; import static com.google.common.collect.ImmutableList.toImmutableList; +import static io.trino.testing.SystemEnvUtils.isEnvSet; import static io.trino.testing.containers.ConditionalPullPolicy.TESTCONTAINERS_NEVER_PULL; import static java.lang.Boolean.parseBoolean; import static java.lang.System.getenv; @@ -72,7 +73,7 @@ public static String getPathFromClassPathResource(String resourcePath) public static void exposeFixedPorts(GenericContainer container) { - checkState(System.getenv("CONTINUOUS_INTEGRATION") == null, "" + + checkState(isEnvSet("CONTINUOUS_INTEGRATION"), "" + "Exposing fixed ports should not be used in regular test code. This could break parallel test execution. " + "This method is supposed to be invoked from local development helpers only e.g. QueryRunner.main(), " + "hence it should never run on CI"); diff --git a/testing/trino-testing-services/src/main/java/io/trino/testing/SystemEnvUtils.java b/testing/trino-testing-services/src/main/java/io/trino/testing/SystemEnvUtils.java index 4589243bb238..2a309af4226b 100644 --- a/testing/trino-testing-services/src/main/java/io/trino/testing/SystemEnvUtils.java +++ b/testing/trino-testing-services/src/main/java/io/trino/testing/SystemEnvUtils.java @@ -26,4 +26,9 @@ public static String requireEnv(String variable) { return requireNonNull(System.getenv(variable), "environment variable not set: " + variable); } + + public static boolean isEnvSet(String variable) + { + return System.getenv(variable) != null; + } } diff --git a/testing/trino-testing-services/src/main/java/io/trino/testing/services/junit/LogTestDurationListener.java b/testing/trino-testing-services/src/main/java/io/trino/testing/services/junit/LogTestDurationListener.java index 33127d251bb2..5e8d9a3b3dff 100644 --- a/testing/trino-testing-services/src/main/java/io/trino/testing/services/junit/LogTestDurationListener.java +++ b/testing/trino-testing-services/src/main/java/io/trino/testing/services/junit/LogTestDurationListener.java @@ -37,6 +37,7 @@ import static com.google.common.base.Throwables.getStackTraceAsString; import static io.airlift.concurrent.Threads.daemonThreadsNamed; import static io.airlift.units.Duration.nanosSince; +import static io.trino.testing.SystemEnvUtils.isEnvSet; import static io.trino.testing.services.junit.Listeners.reportListenerFailure; import static java.lang.String.format; import static java.lang.management.ManagementFactory.getThreadMXBean; @@ -73,7 +74,7 @@ private static boolean isEnabled() if (System.getProperty("LogTestDurationListener.enabled") != null) { return Boolean.getBoolean("LogTestDurationListener.enabled"); } - if (System.getenv("CONTINUOUS_INTEGRATION") != null) { + if (isEnvSet("CONTINUOUS_INTEGRATION")) { return true; } // For local development, logging durations is not typically useful. diff --git a/testing/trino-testing-services/src/main/java/io/trino/testng/services/FlakyTestRetryAnalyzer.java b/testing/trino-testing-services/src/main/java/io/trino/testng/services/FlakyTestRetryAnalyzer.java index 849403bea407..82083f0521cc 100644 --- a/testing/trino-testing-services/src/main/java/io/trino/testng/services/FlakyTestRetryAnalyzer.java +++ b/testing/trino-testing-services/src/main/java/io/trino/testng/services/FlakyTestRetryAnalyzer.java @@ -28,6 +28,7 @@ import java.util.regex.Pattern; import static com.google.common.base.Throwables.getStackTraceAsString; +import static io.trino.testing.SystemEnvUtils.isEnvSet; import static java.lang.String.format; public class FlakyTestRetryAnalyzer @@ -54,7 +55,7 @@ public boolean retry(ITestResult result) Optional enabledSystemPropertyValue = Optional.ofNullable(System.getProperty(ENABLED_SYSTEM_PROPERTY)); if (!enabledSystemPropertyValue.map(Boolean::parseBoolean) - .orElseGet(() -> System.getenv("CONTINUOUS_INTEGRATION") != null)) { + .orElseGet(() -> isEnvSet("CONTINUOUS_INTEGRATION"))) { log.info( "FlakyTestRetryAnalyzer not enabled: " + "CONTINUOUS_INTEGRATION environment is not detected or " + diff --git a/testing/trino-testing-services/src/main/java/io/trino/testng/services/LogTestDurationListener.java b/testing/trino-testing-services/src/main/java/io/trino/testng/services/LogTestDurationListener.java index f5e0e4d726b7..b51287093bce 100644 --- a/testing/trino-testing-services/src/main/java/io/trino/testng/services/LogTestDurationListener.java +++ b/testing/trino-testing-services/src/main/java/io/trino/testng/services/LogTestDurationListener.java @@ -39,6 +39,7 @@ import static com.google.common.base.Throwables.getStackTraceAsString; import static io.airlift.concurrent.Threads.daemonThreadsNamed; import static io.airlift.units.Duration.nanosSince; +import static io.trino.testing.SystemEnvUtils.isEnvSet; import static io.trino.testng.services.Listeners.formatTestName; import static io.trino.testng.services.Listeners.reportListenerFailure; import static java.lang.String.format; @@ -79,7 +80,7 @@ private static boolean isEnabled() if (System.getProperty("LogTestDurationListener.enabled") != null) { return Boolean.getBoolean("LogTestDurationListener.enabled"); } - if (System.getenv("CONTINUOUS_INTEGRATION") != null) { + if (isEnvSet("CONTINUOUS_INTEGRATION")) { return true; } // LogTestDurationListener does not support concurrent invocations of same test method diff --git a/testing/trino-testing-services/src/main/java/io/trino/testng/services/ProgressLoggingListener.java b/testing/trino-testing-services/src/main/java/io/trino/testng/services/ProgressLoggingListener.java index 3f61b2496044..fa7b82a636a5 100644 --- a/testing/trino-testing-services/src/main/java/io/trino/testng/services/ProgressLoggingListener.java +++ b/testing/trino-testing-services/src/main/java/io/trino/testng/services/ProgressLoggingListener.java @@ -25,6 +25,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; +import static io.trino.testing.SystemEnvUtils.isEnvSet; import static io.trino.testng.services.Listeners.formatTestName; import static java.lang.String.format; @@ -47,7 +48,7 @@ private static boolean isEnabled() if (System.getProperty("ProgressLoggingListener.enabled") != null) { return Boolean.getBoolean("ProgressLoggingListener.enabled"); } - if (System.getenv("CONTINUOUS_INTEGRATION") != null) { + if (isEnvSet("CONTINUOUS_INTEGRATION")) { return true; } // most often not useful for local development