From 22aa30d2c30ebc253bab36a7e5120c4ed823beed Mon Sep 17 00:00:00 2001 From: Marco Miller Date: Wed, 1 Apr 2020 14:18:29 -0400 Subject: [PATCH] Elasticsearch: Unharcode localhost for container's No longer hardcode the docker container hostname for the Elasticsearch tests. Use the hostname that was dynamically resolved from the running ElasticContainer (the 'testcontainer') instead. This is so that tests running upon non-localhost named containers may pass like in the localhost case. Change-Id: I9dbd8b76bd7b22004d1d2327bb4c8395907ff223 --- .../elasticsearch/ElasticTestUtils.java | 20 +++++++++++-------- .../ElasticV5QueryAccountsTest.java | 6 ++++-- .../ElasticV5QueryChangesTest.java | 6 ++++-- .../ElasticV5QueryGroupsTest.java | 6 ++++-- .../ElasticV5QueryProjectsTest.java | 6 ++++-- .../ElasticV6QueryAccountsTest.java | 7 +++++-- .../ElasticV6QueryChangesTest.java | 7 +++++-- .../ElasticV6QueryGroupsTest.java | 7 +++++-- .../ElasticV6QueryProjectsTest.java | 7 +++++-- .../ElasticV7QueryAccountsTest.java | 7 +++++-- .../ElasticV7QueryChangesTest.java | 7 +++++-- .../ElasticV7QueryGroupsTest.java | 7 +++++-- .../ElasticV7QueryProjectsTest.java | 7 +++++-- 13 files changed, 68 insertions(+), 32 deletions(-) diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticTestUtils.java b/javatests/com/google/gerrit/elasticsearch/ElasticTestUtils.java index 020a15834c..236ab66da4 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticTestUtils.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticTestUtils.java @@ -26,16 +26,19 @@ import org.eclipse.jgit.lib.Config; public final class ElasticTestUtils { public static class ElasticNodeInfo { + public final String hostname; public final int port; - public ElasticNodeInfo(int port) { + public ElasticNodeInfo(String hostname, int port) { + this.hostname = hostname; this.port = port; } } - public static void configure(Config config, int port, String prefix, ElasticVersion version) { + public static void configure( + Config config, String hostname, int port, String prefix, ElasticVersion version) { config.setEnum("index", null, "type", IndexType.ELASTICSEARCH); - config.setString("elasticsearch", null, "server", "http://localhost:" + port); + config.setString("elasticsearch", null, "server", "http://" + hostname + ":" + port); config.setString("elasticsearch", null, "prefix", prefix); config.setInt("index", null, "maxLimit", 10000); String password = version == ElasticVersion.V5_6 ? "changeme" : null; @@ -44,8 +47,8 @@ public final class ElasticTestUtils { } } - public static void configure(Config config, int port, String prefix) { - configure(config, port, prefix, null); + public static void configure(Config config, String hostname, int port, String prefix) { + configure(config, hostname, port, prefix, null); } public static void createAllIndexes(Injector injector) throws IOException { @@ -57,12 +60,13 @@ public final class ElasticTestUtils { } public static Config getConfig(ElasticVersion version) { - ElasticNodeInfo elasticNodeInfo; ElasticContainer container = ElasticContainer.createAndStart(version); - elasticNodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + ElasticNodeInfo elasticNodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); String indicesPrefix = UUID.randomUUID().toString(); Config cfg = new Config(); - configure(cfg, elasticNodeInfo.port, indicesPrefix, version); + configure(cfg, elasticNodeInfo.hostname, elasticNodeInfo.port, indicesPrefix, version); return cfg; } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryAccountsTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryAccountsTest.java index c8ce54a792..fb41de4270 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryAccountsTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryAccountsTest.java @@ -42,7 +42,9 @@ public class ElasticV5QueryAccountsTest extends AbstractQueryAccountsTest { } container = ElasticContainer.createAndStart(ElasticVersion.V5_6); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -64,7 +66,7 @@ public class ElasticV5QueryAccountsTest extends AbstractQueryAccountsTest { InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); ElasticTestUtils.configure( - elasticsearchConfig, nodeInfo.port, indicesPrefix, ElasticVersion.V5_6); + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix, ElasticVersion.V5_6); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryChangesTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryChangesTest.java index cfdfa983d0..2dd2363c53 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryChangesTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryChangesTest.java @@ -42,7 +42,9 @@ public class ElasticV5QueryChangesTest extends AbstractQueryChangesTest { } container = ElasticContainer.createAndStart(ElasticVersion.V5_6); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -64,7 +66,7 @@ public class ElasticV5QueryChangesTest extends AbstractQueryChangesTest { InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); ElasticTestUtils.configure( - elasticsearchConfig, nodeInfo.port, indicesPrefix, ElasticVersion.V5_6); + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix, ElasticVersion.V5_6); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryGroupsTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryGroupsTest.java index 832a7bd7fc..5cef866b38 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryGroupsTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryGroupsTest.java @@ -42,7 +42,9 @@ public class ElasticV5QueryGroupsTest extends AbstractQueryGroupsTest { } container = ElasticContainer.createAndStart(ElasticVersion.V5_6); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -64,7 +66,7 @@ public class ElasticV5QueryGroupsTest extends AbstractQueryGroupsTest { InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); ElasticTestUtils.configure( - elasticsearchConfig, nodeInfo.port, indicesPrefix, ElasticVersion.V5_6); + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix, ElasticVersion.V5_6); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryProjectsTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryProjectsTest.java index 29d3fa416a..186802a9f2 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryProjectsTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV5QueryProjectsTest.java @@ -42,7 +42,9 @@ public class ElasticV5QueryProjectsTest extends AbstractQueryProjectsTest { } container = ElasticContainer.createAndStart(ElasticVersion.V5_6); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -64,7 +66,7 @@ public class ElasticV5QueryProjectsTest extends AbstractQueryProjectsTest { InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); ElasticTestUtils.configure( - elasticsearchConfig, nodeInfo.port, indicesPrefix, ElasticVersion.V5_6); + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix, ElasticVersion.V5_6); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryAccountsTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryAccountsTest.java index 191b83ceb7..3f44e1e436 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryAccountsTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryAccountsTest.java @@ -42,7 +42,9 @@ public class ElasticV6QueryAccountsTest extends AbstractQueryAccountsTest { } container = ElasticContainer.createAndStart(ElasticVersion.V6_8); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -63,7 +65,8 @@ public class ElasticV6QueryAccountsTest extends AbstractQueryAccountsTest { Config elasticsearchConfig = new Config(config); InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); - ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix); + ElasticTestUtils.configure( + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryChangesTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryChangesTest.java index 94c5a04901..f6139dd4af 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryChangesTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryChangesTest.java @@ -48,7 +48,9 @@ public class ElasticV6QueryChangesTest extends AbstractQueryChangesTest { } container = ElasticContainer.createAndStart(ElasticVersion.V6_8); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); client = HttpAsyncClients.createDefault(); client.start(); } @@ -81,7 +83,8 @@ public class ElasticV6QueryChangesTest extends AbstractQueryChangesTest { Config elasticsearchConfig = new Config(config); InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); - ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix); + ElasticTestUtils.configure( + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryGroupsTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryGroupsTest.java index 8bc57675e0..094afb28a7 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryGroupsTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryGroupsTest.java @@ -42,7 +42,9 @@ public class ElasticV6QueryGroupsTest extends AbstractQueryGroupsTest { } container = ElasticContainer.createAndStart(ElasticVersion.V6_8); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -63,7 +65,8 @@ public class ElasticV6QueryGroupsTest extends AbstractQueryGroupsTest { Config elasticsearchConfig = new Config(config); InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); - ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix); + ElasticTestUtils.configure( + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryProjectsTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryProjectsTest.java index de8e922b57..faec8cf99b 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryProjectsTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV6QueryProjectsTest.java @@ -42,7 +42,9 @@ public class ElasticV6QueryProjectsTest extends AbstractQueryProjectsTest { } container = ElasticContainer.createAndStart(ElasticVersion.V6_8); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -63,7 +65,8 @@ public class ElasticV6QueryProjectsTest extends AbstractQueryProjectsTest { Config elasticsearchConfig = new Config(config); InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); - ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix); + ElasticTestUtils.configure( + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryAccountsTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryAccountsTest.java index 826473f2c0..d6bdeb9baf 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryAccountsTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryAccountsTest.java @@ -42,7 +42,9 @@ public class ElasticV7QueryAccountsTest extends AbstractQueryAccountsTest { } container = ElasticContainer.createAndStart(ElasticVersion.V7_6); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -63,7 +65,8 @@ public class ElasticV7QueryAccountsTest extends AbstractQueryAccountsTest { Config elasticsearchConfig = new Config(config); InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); - ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix); + ElasticTestUtils.configure( + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryChangesTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryChangesTest.java index 43a2dfc46e..82b20ccbb3 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryChangesTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryChangesTest.java @@ -48,7 +48,9 @@ public class ElasticV7QueryChangesTest extends AbstractQueryChangesTest { } container = ElasticContainer.createAndStart(ElasticVersion.V7_6); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); client = HttpAsyncClients.createDefault(); client.start(); } @@ -81,7 +83,8 @@ public class ElasticV7QueryChangesTest extends AbstractQueryChangesTest { Config elasticsearchConfig = new Config(config); InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); - ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix); + ElasticTestUtils.configure( + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryGroupsTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryGroupsTest.java index 0954e603c2..854a0a4840 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryGroupsTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryGroupsTest.java @@ -42,7 +42,9 @@ public class ElasticV7QueryGroupsTest extends AbstractQueryGroupsTest { } container = ElasticContainer.createAndStart(ElasticVersion.V7_6); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -63,7 +65,8 @@ public class ElasticV7QueryGroupsTest extends AbstractQueryGroupsTest { Config elasticsearchConfig = new Config(config); InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); - ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix); + ElasticTestUtils.configure( + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } } diff --git a/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryProjectsTest.java b/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryProjectsTest.java index e6d78770dd..3bbcfe6527 100644 --- a/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryProjectsTest.java +++ b/javatests/com/google/gerrit/elasticsearch/ElasticV7QueryProjectsTest.java @@ -42,7 +42,9 @@ public class ElasticV7QueryProjectsTest extends AbstractQueryProjectsTest { } container = ElasticContainer.createAndStart(ElasticVersion.V7_6); - nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); + nodeInfo = + new ElasticNodeInfo( + container.getHttpHost().getHostName(), container.getHttpHost().getPort()); } @AfterClass @@ -63,7 +65,8 @@ public class ElasticV7QueryProjectsTest extends AbstractQueryProjectsTest { Config elasticsearchConfig = new Config(config); InMemoryModule.setDefaults(elasticsearchConfig); String indicesPrefix = getSanitizedMethodName(); - ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix); + ElasticTestUtils.configure( + elasticsearchConfig, nodeInfo.hostname, nodeInfo.port, indicesPrefix); return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration)); } }