From 3c444c471902a18190bb9c0af3a0b8dbb78c3dd7 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Fri, 10 Nov 2017 14:16:11 -0500 Subject: [PATCH] SQL: Fix test when random JDK TZ doesn't exist in Joda (elastic/x-pack-elasticsearch#2903) JodaTime timezone db can be out of date compared to that of the JDK which causes the JDBC Connection to fail when the randomized tests pick a timezone that's available in the JDK but not in Joda, like SystemV/PST8. This is happening because JdbcConnection configuration is using system default timezone and tries to pass it to Elasticsearch that is using joda. This commit, explicitly sets the time zone on JdbcConnection to a time zone randomly selected from a list of timezones that are known to both JDK and Joda. relates elastic/x-pack-elasticsearch#2812 Original commit: elastic/x-pack-elasticsearch@b02e9794a87c9a90413c57b76c42fb1f3f919b2a --- .../qa/sql/jdbc/JdbcIntegrationTestCase.java | 26 ++++++++++++++++++- .../sql/jdbc/jdbc/JdbcConfiguration.java | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/JdbcIntegrationTestCase.java b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/JdbcIntegrationTestCase.java index 3cee58fc94f..7207202fec8 100644 --- a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/JdbcIntegrationTestCase.java +++ b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/JdbcIntegrationTestCase.java @@ -16,13 +16,22 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.qa.sql.embed.EmbeddedJdbcServer; +import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcConfiguration; +import org.joda.time.DateTimeZone; import org.junit.ClassRule; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; import java.util.Properties; +import java.util.Set; +import java.util.TimeZone; import static java.util.Collections.singletonMap; @@ -85,6 +94,21 @@ public abstract class JdbcIntegrationTestCase extends ESRestTestCase { * The properties used to build the connection. */ protected Properties connectionProperties() { - return new Properties(); + Properties connectionProperties = new Properties(); + connectionProperties.put(JdbcConfiguration.TIME_ZONE, randomKnownTimeZone()); + return connectionProperties; } + + public static String randomKnownTimeZone() { + // We use system default timezone for the connection that is selected randomly by TestRuleSetupAndRestoreClassEnv + // from all available JDK timezones. While Joda and JDK are generally in sync, some timezones might not be known + // to the current version of Joda and in this case the test might fail. To avoid that, we specify a timezone + // known for both Joda and JDK + Set timeZones = new HashSet<>(DateTimeZone.getAvailableIDs()); + timeZones.retainAll(Arrays.asList(TimeZone.getAvailableIDs())); + List ids = new ArrayList<>(timeZones); + Collections.sort(ids); + return randomFrom(ids); + } + } diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcConfiguration.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcConfiguration.java index 41eaae42efa..f7e4855c034 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcConfiguration.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcConfiguration.java @@ -43,7 +43,7 @@ public class JdbcConfiguration extends ConnectionConfiguration { // can be out/err/url static final String DEBUG_OUTPUT_DEFAULT = "err"; - static final String TIME_ZONE = "timezone"; + public static final String TIME_ZONE = "timezone"; // follow the JDBC spec and use the JVM default... // to avoid inconsistency, the default is picked up once at startup and reused across connections // to cater to the principle of least surprise