forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integ test for EC2 special network addresses (elastic#118560) (el…
…astic#118616) Replaces the `Ec2NetworkTests` unit test suite with an integ test suite to cover the resolution process end-to-end.
- Loading branch information
1 parent
bc7aae5
commit 788cac4
Showing
11 changed files
with
247 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...lusterTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2NetworkAddressesTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.discovery.ec2; | ||
|
||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.util.CollectionUtils; | ||
import org.elasticsearch.http.HttpServerTransport; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
@ESIntegTestCase.ClusterScope(numDataNodes = 0) | ||
public abstract class DiscoveryEc2NetworkAddressesTestCase extends ESIntegTestCase { | ||
|
||
@Override | ||
protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
return CollectionUtils.appendToCopyNoNullElements(super.nodePlugins(), Ec2DiscoveryPlugin.class); | ||
} | ||
|
||
@Override | ||
protected boolean addMockHttpTransport() { | ||
return false; | ||
} | ||
|
||
void verifyPublishAddress(String publishAddressSetting, String expectedAddress) throws IOException { | ||
final var node = internalCluster().startNode(Settings.builder().put("http.publish_host", publishAddressSetting)); | ||
assertEquals( | ||
expectedAddress, | ||
internalCluster().getInstance(HttpServerTransport.class, node).boundAddress().publishAddress().getAddress() | ||
); | ||
internalCluster().stopNode(node); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...usterTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2RegularNetworkAddressesIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.discovery.ec2; | ||
|
||
import fixture.aws.imds.Ec2ImdsHttpFixture; | ||
import fixture.aws.imds.Ec2ImdsServiceBuilder; | ||
import fixture.aws.imds.Ec2ImdsVersion; | ||
|
||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.transport.BindTransportException; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public class DiscoveryEc2RegularNetworkAddressesIT extends DiscoveryEc2NetworkAddressesTestCase { | ||
public void testLocalIgnoresImds() { | ||
Ec2ImdsHttpFixture.runWithFixture(new Ec2ImdsServiceBuilder(randomFrom(Ec2ImdsVersion.values())), imdsFixture -> { | ||
try (var ignored = Ec2ImdsHttpFixture.withEc2MetadataServiceEndpointOverride(imdsFixture.getAddress())) { | ||
verifyPublishAddress("_local_", "127.0.0.1"); | ||
} | ||
}); | ||
} | ||
|
||
public void testImdsNotAvailable() throws IOException { | ||
try (var ignored = Ec2ImdsHttpFixture.withEc2MetadataServiceEndpointOverride("http://127.0.0.1")) { | ||
// if IMDS is not running, regular values like `_local_` should still work | ||
verifyPublishAddress("_local_", "127.0.0.1"); | ||
|
||
// but EC2 addresses will cause the node to fail to start | ||
final var assertionError = expectThrows( | ||
AssertionError.class, | ||
() -> internalCluster().startNode(Settings.builder().put("http.publish_host", "_ec2_")) | ||
); | ||
final var executionException = asInstanceOf(ExecutionException.class, assertionError.getCause()); | ||
final var bindTransportException = asInstanceOf(BindTransportException.class, executionException.getCause()); | ||
assertEquals("Failed to resolve publish address", bindTransportException.getMessage()); | ||
final var ioException = asInstanceOf(IOException.class, bindTransportException.getCause()); | ||
assertThat(ioException.getMessage(), containsString("/latest/meta-data/local-ipv4")); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...usterTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2SpecialNetworkAddressesIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.discovery.ec2; | ||
|
||
import fixture.aws.imds.Ec2ImdsHttpFixture; | ||
import fixture.aws.imds.Ec2ImdsServiceBuilder; | ||
import fixture.aws.imds.Ec2ImdsVersion; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.Name; | ||
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
public class DiscoveryEc2SpecialNetworkAddressesIT extends DiscoveryEc2NetworkAddressesTestCase { | ||
|
||
private final String imdsAddressName; | ||
private final String elasticsearchAddressName; | ||
private final Ec2ImdsVersion imdsVersion; | ||
|
||
public DiscoveryEc2SpecialNetworkAddressesIT( | ||
@Name("imdsAddressName") String imdsAddressName, | ||
@Name("elasticsearchAddressName") String elasticsearchAddressName, | ||
@Name("imdsVersion") Ec2ImdsVersion imdsVersion | ||
) { | ||
this.imdsAddressName = imdsAddressName; | ||
this.elasticsearchAddressName = elasticsearchAddressName; | ||
this.imdsVersion = imdsVersion; | ||
} | ||
|
||
@ParametersFactory | ||
public static Iterable<Object[]> parameters() { | ||
return Map.of( | ||
"_ec2:privateIpv4_", | ||
"local-ipv4", | ||
"_ec2:privateDns_", | ||
"local-hostname", | ||
"_ec2:publicIpv4_", | ||
"public-ipv4", | ||
"_ec2:publicDns_", | ||
"public-hostname", | ||
"_ec2:publicIp_", | ||
"public-ipv4", | ||
"_ec2:privateIp_", | ||
"local-ipv4", | ||
"_ec2_", | ||
"local-ipv4" | ||
) | ||
.entrySet() | ||
.stream() | ||
.flatMap( | ||
addresses -> Stream.of(Ec2ImdsVersion.values()) | ||
.map(ec2ImdsVersion -> new Object[] { addresses.getValue(), addresses.getKey(), ec2ImdsVersion }) | ||
) | ||
.toList(); | ||
} | ||
|
||
public void testSpecialNetworkAddresses() { | ||
final var publishAddress = "10.0." + between(0, 255) + "." + between(0, 255); | ||
Ec2ImdsHttpFixture.runWithFixture( | ||
new Ec2ImdsServiceBuilder(imdsVersion).addInstanceAddress(imdsAddressName, publishAddress), | ||
imdsFixture -> { | ||
try (var ignored = Ec2ImdsHttpFixture.withEc2MetadataServiceEndpointOverride(imdsFixture.getAddress())) { | ||
verifyPublishAddress(elasticsearchAddressName, publishAddress); | ||
} | ||
} | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.