From 098f82f086a5d1d06802e0c5ac590330de8c384f Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Fri, 22 Dec 2017 09:58:02 +0100 Subject: [PATCH] [Test] Do not rely on MockZenPing for Azure tests (#27945) This commit changes some Azure tests so that they do not rely on MockZenPing and TestZenDiscovery anymore, but instead use a mocked AzureComputeService that exposes internal test cluster nodes as if they were real Azure nodes. Related to #27859 Closes #27917, #11533 --- .../plugins/DiscoveryPlugin.java | 1 - .../classic/AzureUnicastHostsProvider.java | 62 ++++---- .../azure/classic/AzureDiscoveryPlugin.java | 12 +- .../AbstractAzureComputeServiceTestCase.java | 141 ++++++++++++++++-- .../AzureComputeServiceSimpleMock.java | 83 ----------- .../AzureComputeServiceTwoNodesMock.java | 102 ------------- .../AzureComputeServiceAbstractMock.java | 47 ------ .../classic/AzureMinimumMasterNodesTests.java | 62 +++----- .../azure/classic/AzureSimpleTests.java | 37 ++--- .../classic/AzureTwoStartedNodesTests.java | 52 ++----- 10 files changed, 209 insertions(+), 390 deletions(-) delete mode 100644 plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AzureComputeServiceSimpleMock.java delete mode 100644 plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AzureComputeServiceTwoNodesMock.java delete mode 100644 plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeServiceAbstractMock.java diff --git a/core/src/main/java/org/elasticsearch/plugins/DiscoveryPlugin.java b/core/src/main/java/org/elasticsearch/plugins/DiscoveryPlugin.java index 912bcdc9d85..1d5ee13f50d 100644 --- a/core/src/main/java/org/elasticsearch/plugins/DiscoveryPlugin.java +++ b/core/src/main/java/org/elasticsearch/plugins/DiscoveryPlugin.java @@ -19,7 +19,6 @@ package org.elasticsearch.plugins; -import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.function.BiConsumer; diff --git a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/discovery/azure/classic/AzureUnicastHostsProvider.java b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/discovery/azure/classic/AzureUnicastHostsProvider.java index 51d0fd4313a..2bc6cc4b130 100644 --- a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/discovery/azure/classic/AzureUnicastHostsProvider.java +++ b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/discovery/azure/classic/AzureUnicastHostsProvider.java @@ -62,6 +62,10 @@ public class AzureUnicastHostsProvider extends AbstractComponent implements Unic this.type = type ; } + public String getType() { + return type; + } + public static HostType fromString(String type) { for (HostType hostType : values()) { if (hostType.type.equalsIgnoreCase(type)) { @@ -196,43 +200,7 @@ public class AzureUnicastHostsProvider extends AbstractComponent implements Unic // In other case, it should be the right deployment so we can add it to the list of instances for (RoleInstance instance : deployment.getRoleInstances()) { - String networkAddress = null; - // Let's detect if we want to use public or private IP - switch (hostType) { - case PRIVATE_IP: - InetAddress privateIp = instance.getIPAddress(); - - if (privateIp != null) { - if (privateIp.equals(ipAddress)) { - logger.trace("adding ourselves {}", NetworkAddress.format(ipAddress)); - } - networkAddress = InetAddresses.toUriString(privateIp); - } else { - logger.trace("no private ip provided. ignoring [{}]...", instance.getInstanceName()); - } - break; - case PUBLIC_IP: - for (InstanceEndpoint endpoint : instance.getInstanceEndpoints()) { - if (!publicEndpointName.equals(endpoint.getName())) { - logger.trace("ignoring endpoint [{}] as different than [{}]", - endpoint.getName(), publicEndpointName); - continue; - } - - networkAddress = NetworkAddress.format(new InetSocketAddress(endpoint.getVirtualIPAddress(), - endpoint.getPort())); - } - - if (networkAddress == null) { - logger.trace("no public ip provided. ignoring [{}]...", instance.getInstanceName()); - } - break; - default: - // This could never happen! - logger.warn("undefined host_type [{}]. Please check your settings.", hostType); - return cachedDiscoNodes; - } - + final String networkAddress = resolveInstanceAddress(hostType, instance); if (networkAddress == null) { // We have a bad parameter here or not enough information from azure logger.warn("no network address found. ignoring [{}]...", instance.getInstanceName()); @@ -257,4 +225,24 @@ public class AzureUnicastHostsProvider extends AbstractComponent implements Unic return cachedDiscoNodes; } + + protected String resolveInstanceAddress(final HostType hostType, final RoleInstance instance) { + if (hostType == HostType.PRIVATE_IP) { + final InetAddress privateIp = instance.getIPAddress(); + if (privateIp != null) { + return InetAddresses.toUriString(privateIp); + } else { + logger.trace("no private ip provided. ignoring [{}]...", instance.getInstanceName()); + } + } else if (hostType == HostType.PUBLIC_IP) { + for (InstanceEndpoint endpoint : instance.getInstanceEndpoints()) { + if (publicEndpointName.equals(endpoint.getName())) { + return NetworkAddress.format(new InetSocketAddress(endpoint.getVirtualIPAddress(), endpoint.getPort())); + } else { + logger.trace("ignoring endpoint [{}] as different than [{}]", endpoint.getName(), publicEndpointName); + } + } + } + return null; + } } diff --git a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/plugin/discovery/azure/classic/AzureDiscoveryPlugin.java b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/plugin/discovery/azure/classic/AzureDiscoveryPlugin.java index a4fd985e4b5..4255fc210fb 100644 --- a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/plugin/discovery/azure/classic/AzureDiscoveryPlugin.java +++ b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/plugin/discovery/azure/classic/AzureDiscoveryPlugin.java @@ -61,10 +61,16 @@ public class AzureDiscoveryPlugin extends Plugin implements DiscoveryPlugin { public Map> getZenHostsProviders(TransportService transportService, NetworkService networkService) { return Collections.singletonMap(AZURE, - () -> new AzureUnicastHostsProvider(settings, createComputeService(), transportService, networkService)); + () -> createUnicastHostsProvider(settings, createComputeService(), transportService, networkService)); } - + // Used for testing + protected AzureUnicastHostsProvider createUnicastHostsProvider(final Settings settings, + final AzureComputeService azureComputeService, + final TransportService transportService, + final NetworkService networkService) { + return new AzureUnicastHostsProvider(settings, azureComputeService, transportService, networkService); + } @Override public List> getSettings() { @@ -79,6 +85,4 @@ public class AzureDiscoveryPlugin extends Plugin implements DiscoveryPlugin { AzureComputeService.Discovery.DEPLOYMENT_SLOT_SETTING, AzureComputeService.Discovery.ENDPOINT_NAME_SETTING); } - - } diff --git a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AbstractAzureComputeServiceTestCase.java b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AbstractAzureComputeServiceTestCase.java index 6b36a183800..709c7253a5b 100644 --- a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AbstractAzureComputeServiceTestCase.java +++ b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AbstractAzureComputeServiceTestCase.java @@ -19,28 +19,45 @@ package org.elasticsearch.cloud.azure.classic; +import com.microsoft.windowsazure.management.compute.models.DeploymentSlot; +import com.microsoft.windowsazure.management.compute.models.DeploymentStatus; +import com.microsoft.windowsazure.management.compute.models.HostedServiceGetDetailedResponse; +import com.microsoft.windowsazure.management.compute.models.InstanceEndpoint; +import com.microsoft.windowsazure.management.compute.models.RoleInstance; +import com.microsoft.windowsazure.management.compute.models.RoleInstancePowerState; import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; +import org.elasticsearch.cloud.azure.classic.management.AzureComputeService; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService.Discovery; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService.Management; +import org.elasticsearch.cluster.node.DiscoveryNode; +import org.elasticsearch.common.network.NetworkAddress; +import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.discovery.azure.classic.AzureUnicastHostsProvider; +import org.elasticsearch.discovery.zen.ZenDiscovery; +import org.elasticsearch.plugin.discovery.azure.classic.AzureDiscoveryPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.transport.TransportService; +import org.junit.After; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static org.elasticsearch.common.util.CollectionUtils.newSingletonArrayList; public abstract class AbstractAzureComputeServiceTestCase extends ESIntegTestCase { - private Class mockPlugin; + private static final Map nodes = new ConcurrentHashMap<>(); - public AbstractAzureComputeServiceTestCase(Class mockPlugin) { - // We want to inject the Azure API Mock - this.mockPlugin = mockPlugin; - } - - @Override - protected boolean addTestZenDiscovery() { - return false; + @After + public void clearAzureNodes() { + nodes.clear(); } @Override @@ -49,6 +66,10 @@ public abstract class AbstractAzureComputeServiceTestCase extends ESIntegTestCas .put(super.nodeSettings(nodeOrdinal)) .put("discovery.zen.hosts_provider", "azure"); + // Make the test run faster + builder.put(ZenDiscovery.JOIN_TIMEOUT_SETTING.getKey(), "1s") + .put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "500ms"); + // We add a fake subscription_id to start mock compute service builder.put(Management.SUBSCRIPTION_ID_SETTING.getKey(), "fake") .put(Discovery.REFRESH_SETTING.getKey(), "5s") @@ -60,13 +81,107 @@ public abstract class AbstractAzureComputeServiceTestCase extends ESIntegTestCas @Override protected Collection> nodePlugins() { - return Arrays.asList(mockPlugin); + return Collections.singletonList(TestPlugin.class); } - protected void checkNumberOfNodes(int expected) { - NodesInfoResponse nodeInfos = client().admin().cluster().prepareNodesInfo().execute().actionGet(); + @Override + protected boolean addTestZenDiscovery() { + return false; + } + + /** + * Register an existing node as a Azure node, exposing its address and details htrough + * + * @param nodeName the name of the node + */ + protected void registerAzureNode(final String nodeName) { + TransportService transportService = internalCluster().getInstance(TransportService.class, nodeName); + assertNotNull(transportService); + DiscoveryNode discoveryNode = transportService.getLocalNode(); + assertNotNull(discoveryNode); + if (nodes.put(discoveryNode.getName(), discoveryNode) != null) { + throw new IllegalArgumentException("Node [" + discoveryNode.getName() + "] cannot be registered twice in Azure"); + } + } + + protected void assertNumberOfNodes(int expected) { + NodesInfoResponse nodeInfos = client().admin().cluster().prepareNodesInfo().clear().execute().actionGet(); assertNotNull(nodeInfos); assertNotNull(nodeInfos.getNodes()); assertEquals(expected, nodeInfos.getNodes().size()); } + + /** + * Test plugin that exposes internal test cluster nodes as if they were real Azure nodes. + * Use {@link #registerAzureNode(String)} method to expose nodes in the tests. + */ + public static class TestPlugin extends AzureDiscoveryPlugin { + + public TestPlugin(Settings settings) { + super(settings); + } + + @Override + protected AzureComputeService createComputeService() { + return () -> { + final List instances = new ArrayList<>(); + for (Map.Entry node : nodes.entrySet()) { + final String name = node.getKey(); + final DiscoveryNode discoveryNode = node.getValue(); + + RoleInstance instance = new RoleInstance(); + instance.setInstanceName(name); + instance.setHostName(discoveryNode.getHostName()); + instance.setPowerState(RoleInstancePowerState.Started); + + // Set the private IP address + final TransportAddress transportAddress = discoveryNode.getAddress(); + instance.setIPAddress(transportAddress.address().getAddress()); + + // Set the public IP address + final InstanceEndpoint endpoint = new InstanceEndpoint(); + endpoint.setName(Discovery.ENDPOINT_NAME_SETTING.getDefault(Settings.EMPTY)); + endpoint.setVirtualIPAddress(transportAddress.address().getAddress()); + endpoint.setPort(transportAddress.address().getPort()); + instance.setInstanceEndpoints(new ArrayList<>(Collections.singletonList(endpoint))); + instances.add(instance); + } + + final HostedServiceGetDetailedResponse.Deployment deployment = new HostedServiceGetDetailedResponse.Deployment(); + deployment.setName("dummy"); + deployment.setDeploymentSlot(DeploymentSlot.Production); + deployment.setStatus(DeploymentStatus.Running); + deployment.setRoleInstances(new ArrayList<>(Collections.unmodifiableList(instances))); + + final HostedServiceGetDetailedResponse response = new HostedServiceGetDetailedResponse(); + response.setDeployments(newSingletonArrayList(deployment)); + + return response; + }; + } + + /** + * Defines a {@link AzureUnicastHostsProvider} for testing purpose that is able to resolve + * network addresses for Azure instances running on the same host but different ports. + */ + @Override + protected AzureUnicastHostsProvider createUnicastHostsProvider(final Settings settings, + final AzureComputeService azureComputeService, + final TransportService transportService, + final NetworkService networkService) { + return new AzureUnicastHostsProvider(settings, azureComputeService, transportService, networkService) { + @Override + protected String resolveInstanceAddress(final HostType hostType, final RoleInstance instance) { + if (hostType == HostType.PRIVATE_IP) { + DiscoveryNode discoveryNode = nodes.get(instance.getInstanceName()); + if (discoveryNode != null) { + // Format the InetSocketAddress to a format that contains the port number + return NetworkAddress.format(discoveryNode.getAddress().address()); + } + } + return super.resolveInstanceAddress(hostType, instance); + } + }; + } + } } diff --git a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AzureComputeServiceSimpleMock.java b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AzureComputeServiceSimpleMock.java deleted file mode 100644 index 2fbb4d7c4d7..00000000000 --- a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AzureComputeServiceSimpleMock.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch 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.elasticsearch.cloud.azure.classic; - -import java.net.InetAddress; - -import com.microsoft.windowsazure.management.compute.models.DeploymentSlot; -import com.microsoft.windowsazure.management.compute.models.DeploymentStatus; -import com.microsoft.windowsazure.management.compute.models.HostedServiceGetDetailedResponse; -import com.microsoft.windowsazure.management.compute.models.InstanceEndpoint; -import com.microsoft.windowsazure.management.compute.models.RoleInstance; -import org.elasticsearch.cloud.azure.classic.management.AzureComputeService; -import org.elasticsearch.cloud.azure.classic.management.AzureComputeServiceAbstractMock; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.plugin.discovery.azure.classic.AzureDiscoveryPlugin; - -/** - * Mock Azure API with a single started node - */ -public class AzureComputeServiceSimpleMock extends AzureComputeServiceAbstractMock { - - public static class TestPlugin extends AzureDiscoveryPlugin { - public TestPlugin(Settings settings) { - super(settings); - } - @Override - protected AzureComputeService createComputeService() { - return new AzureComputeServiceSimpleMock(settings); - } - } - - private AzureComputeServiceSimpleMock(Settings settings) { - super(settings); - } - - @Override - public HostedServiceGetDetailedResponse getServiceDetails() { - HostedServiceGetDetailedResponse response = new HostedServiceGetDetailedResponse(); - HostedServiceGetDetailedResponse.Deployment deployment = new HostedServiceGetDetailedResponse.Deployment(); - - // Fake the deployment - deployment.setName("dummy"); - deployment.setDeploymentSlot(DeploymentSlot.Production); - deployment.setStatus(DeploymentStatus.Running); - - // Fake an instance - RoleInstance instance = new RoleInstance(); - instance.setInstanceName("dummy1"); - - // Fake the private IP - instance.setIPAddress(InetAddress.getLoopbackAddress()); - - // Fake the public IP - InstanceEndpoint endpoint = new InstanceEndpoint(); - endpoint.setName("elasticsearch"); - endpoint.setVirtualIPAddress(InetAddress.getLoopbackAddress()); - endpoint.setPort(9400); - instance.setInstanceEndpoints(CollectionUtils.newSingletonArrayList(endpoint)); - - deployment.setRoleInstances(CollectionUtils.newSingletonArrayList(instance)); - response.setDeployments(CollectionUtils.newSingletonArrayList(deployment)); - - return response; - } -} diff --git a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AzureComputeServiceTwoNodesMock.java b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AzureComputeServiceTwoNodesMock.java deleted file mode 100644 index 5b787e1ce44..00000000000 --- a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/AzureComputeServiceTwoNodesMock.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch 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.elasticsearch.cloud.azure.classic; - -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.Arrays; - -import com.microsoft.windowsazure.management.compute.models.DeploymentSlot; -import com.microsoft.windowsazure.management.compute.models.DeploymentStatus; -import com.microsoft.windowsazure.management.compute.models.HostedServiceGetDetailedResponse; -import com.microsoft.windowsazure.management.compute.models.InstanceEndpoint; -import com.microsoft.windowsazure.management.compute.models.RoleInstance; -import org.elasticsearch.cloud.azure.classic.management.AzureComputeService; -import org.elasticsearch.cloud.azure.classic.management.AzureComputeServiceAbstractMock; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.plugin.discovery.azure.classic.AzureDiscoveryPlugin; - -import static org.elasticsearch.common.util.CollectionUtils.newSingletonArrayList; - - -/** - * Mock Azure API with two started nodes - */ -public class AzureComputeServiceTwoNodesMock extends AzureComputeServiceAbstractMock { - - public static class TestPlugin extends AzureDiscoveryPlugin { - public TestPlugin(Settings settings) { - super(settings); - } - @Override - protected AzureComputeService createComputeService() { - return new AzureComputeServiceTwoNodesMock(settings); - } - } - - private AzureComputeServiceTwoNodesMock(Settings settings) { - super(settings); - } - - @Override - public HostedServiceGetDetailedResponse getServiceDetails() { - HostedServiceGetDetailedResponse response = new HostedServiceGetDetailedResponse(); - HostedServiceGetDetailedResponse.Deployment deployment = new HostedServiceGetDetailedResponse.Deployment(); - - // Fake the deployment - deployment.setName("dummy"); - deployment.setDeploymentSlot(DeploymentSlot.Production); - deployment.setStatus(DeploymentStatus.Running); - - // Fake a first instance - RoleInstance instance1 = new RoleInstance(); - instance1.setInstanceName("dummy1"); - - // Fake the private IP - instance1.setIPAddress(InetAddress.getLoopbackAddress()); - - // Fake the public IP - InstanceEndpoint endpoint1 = new InstanceEndpoint(); - endpoint1.setName("elasticsearch"); - endpoint1.setVirtualIPAddress(InetAddress.getLoopbackAddress()); - endpoint1.setPort(9400); - instance1.setInstanceEndpoints(newSingletonArrayList(endpoint1)); - - // Fake a first instance - RoleInstance instance2 = new RoleInstance(); - instance2.setInstanceName("dummy1"); - - // Fake the private IP - instance2.setIPAddress(InetAddress.getLoopbackAddress()); - - // Fake the public IP - InstanceEndpoint endpoint2 = new InstanceEndpoint(); - endpoint2.setName("elasticsearch"); - endpoint2.setVirtualIPAddress(InetAddress.getLoopbackAddress()); - endpoint2.setPort(9401); - instance2.setInstanceEndpoints(newSingletonArrayList(endpoint2)); - - deployment.setRoleInstances(new ArrayList<>(Arrays.asList(instance1, instance2))); - - response.setDeployments(newSingletonArrayList(deployment)); - - return response; - } -} diff --git a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeServiceAbstractMock.java b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeServiceAbstractMock.java deleted file mode 100644 index de63d0d77e6..00000000000 --- a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeServiceAbstractMock.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch 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.elasticsearch.cloud.azure.classic.management; - -import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.common.component.AbstractLifecycleComponent; -import org.elasticsearch.common.settings.Settings; - -public abstract class AzureComputeServiceAbstractMock extends AbstractLifecycleComponent - implements AzureComputeService { - - protected AzureComputeServiceAbstractMock(Settings settings) { - super(settings); - logger.debug("starting Azure Mock [{}]", this.getClass().getSimpleName()); - } - - @Override - protected void doStart() throws ElasticsearchException { - logger.debug("starting Azure Api Mock"); - } - - @Override - protected void doStop() throws ElasticsearchException { - logger.debug("stopping Azure Api Mock"); - } - - @Override - protected void doClose() throws ElasticsearchException { - } -} diff --git a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureMinimumMasterNodesTests.java b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureMinimumMasterNodesTests.java index 3b4e854be9b..f3f70faf92d 100644 --- a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureMinimumMasterNodesTests.java +++ b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureMinimumMasterNodesTests.java @@ -20,17 +20,12 @@ package org.elasticsearch.discovery.azure.classic; import org.elasticsearch.cloud.azure.classic.AbstractAzureComputeServiceTestCase; -import org.elasticsearch.cloud.azure.classic.AzureComputeServiceTwoNodesMock; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.discovery.MasterNotDiscoveredException; -import org.elasticsearch.discovery.zen.ZenDiscovery; import org.elasticsearch.test.ESIntegTestCase; import java.io.IOException; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; - /** * Reported issue in #15 * (https://github.com/elastic/elasticsearch-cloud-azure/issues/15) @@ -42,52 +37,35 @@ import static org.hamcrest.Matchers.nullValue; autoMinMasterNodes = false) public class AzureMinimumMasterNodesTests extends AbstractAzureComputeServiceTestCase { - public AzureMinimumMasterNodesTests() { - super(AzureComputeServiceTwoNodesMock.TestPlugin.class); - } - @Override protected Settings nodeSettings(int nodeOrdinal) { - Settings.Builder builder = Settings.builder() + return Settings.builder() .put(super.nodeSettings(nodeOrdinal)) .put("discovery.zen.minimum_master_nodes", 2) - // Make the test run faster - .put(ZenDiscovery.JOIN_TIMEOUT_SETTING.getKey(), "50ms") - .put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "10ms") - .put("discovery.initial_state_timeout", "100ms"); - return builder.build(); + .put("discovery.initial_state_timeout", "1s") + .build(); } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/27917") public void testSimpleOnlyMasterNodeElection() throws IOException { - logger.info("--> start data node / non master node"); - internalCluster().startNode(); - try { - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("100ms").get().getState().nodes().getMasterNodeId(), - nullValue()); - fail("should not be able to find master"); - } catch (MasterNotDiscoveredException e) { - // all is well, no master elected - } - logger.info("--> start another node"); - internalCluster().startNode(); - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId(), - notNullValue()); + final String node1 = internalCluster().startNode(); + registerAzureNode(node1); + expectThrows(MasterNotDiscoveredException.class, () -> + // master is not elected yet + client().admin().cluster().prepareState().setMasterNodeTimeout("100ms").get().getState().nodes().getMasterNodeId() + ); + + final String node2 = internalCluster().startNode(); + registerAzureNode(node2); + assertNotNull(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId()); - logger.info("--> stop master node"); internalCluster().stopCurrentMasterNode(); + expectThrows(MasterNotDiscoveredException.class, () -> + // master has been stopped + client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId() + ); - try { - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId(), - nullValue()); - fail("should not be able to find master"); - } catch (MasterNotDiscoveredException e) { - // all is well, no master elected - } - - logger.info("--> start another node"); - internalCluster().startNode(); - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId(), - notNullValue()); + final String node3 = internalCluster().startNode(); + registerAzureNode(node3); + assertNotNull(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId()); } } diff --git a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureSimpleTests.java b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureSimpleTests.java index d6d68e75843..1a36e6c55bd 100644 --- a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureSimpleTests.java +++ b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureSimpleTests.java @@ -20,33 +20,30 @@ package org.elasticsearch.discovery.azure.classic; import org.elasticsearch.cloud.azure.classic.AbstractAzureComputeServiceTestCase; -import org.elasticsearch.cloud.azure.classic.AzureComputeServiceSimpleMock; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService.Discovery; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService.Management; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESIntegTestCase; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.notNullValue; -@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, transportClientRatio = 0.0, numClientNodes = 0) +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, + numDataNodes = 0, + transportClientRatio = 0.0, + numClientNodes = 0) public class AzureSimpleTests extends AbstractAzureComputeServiceTestCase { - public AzureSimpleTests() { - super(AzureComputeServiceSimpleMock.TestPlugin.class); - } public void testOneNodeShouldRunUsingPrivateIp() { Settings.Builder settings = Settings.builder() .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") .put(Discovery.HOST_TYPE_SETTING.getKey(), "private_ip"); - logger.info("--> start one node"); - internalCluster().startNode(settings); - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId(), - notNullValue()); + final String node1 = internalCluster().startNode(settings); + registerAzureNode(node1); + assertNotNull(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId()); // We expect having 1 node as part of the cluster, let's test that - checkNumberOfNodes(1); + assertNumberOfNodes(1); } public void testOneNodeShouldRunUsingPublicIp() { @@ -54,13 +51,12 @@ public class AzureSimpleTests extends AbstractAzureComputeServiceTestCase { .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") .put(Discovery.HOST_TYPE_SETTING.getKey(), "public_ip"); - logger.info("--> start one node"); - internalCluster().startNode(settings); - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId(), - notNullValue()); + final String node1 = internalCluster().startNode(settings); + registerAzureNode(node1); + assertNotNull(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId()); // We expect having 1 node as part of the cluster, let's test that - checkNumberOfNodes(1); + assertNumberOfNodes(1); } public void testOneNodeShouldRunUsingWrongSettings() { @@ -68,12 +64,7 @@ public class AzureSimpleTests extends AbstractAzureComputeServiceTestCase { .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") .put(Discovery.HOST_TYPE_SETTING.getKey(), "do_not_exist"); - logger.info("--> start one node"); - try { - internalCluster().startNode(settings); - fail("Expected IllegalArgumentException on startup"); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), containsString("invalid value for host type [do_not_exist]")); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> internalCluster().startNode(settings)); + assertThat(e.getMessage(), containsString("invalid value for host type [do_not_exist]")); } } diff --git a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureTwoStartedNodesTests.java b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureTwoStartedNodesTests.java index 35844c9b383..a47a2b95bf3 100644 --- a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureTwoStartedNodesTests.java +++ b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureTwoStartedNodesTests.java @@ -20,61 +20,37 @@ package org.elasticsearch.discovery.azure.classic; import org.elasticsearch.cloud.azure.classic.AbstractAzureComputeServiceTestCase; -import org.elasticsearch.cloud.azure.classic.AzureComputeServiceTwoNodesMock; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService.Discovery; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService.Management; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESIntegTestCase; -import static org.hamcrest.Matchers.notNullValue; - @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, transportClientRatio = 0.0, numClientNodes = 0) public class AzureTwoStartedNodesTests extends AbstractAzureComputeServiceTestCase { - public AzureTwoStartedNodesTests() { - super(AzureComputeServiceTwoNodesMock.TestPlugin.class); - } + public void testTwoNodesShouldRunUsingPrivateOrPublicIp() { + final String hostType = randomFrom(AzureUnicastHostsProvider.HostType.values()).getType(); + logger.info("--> using azure host type " + hostType); - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/11533") - public void testTwoNodesShouldRunUsingPrivateIp() { - Settings.Builder settings = Settings.builder() - .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") - .put(Discovery.HOST_TYPE_SETTING.getKey(), "private_ip"); + final Settings settings = Settings.builder() + .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") + .put(Discovery.HOST_TYPE_SETTING.getKey(), hostType) + .build(); logger.info("--> start first node"); - internalCluster().startNode(settings); - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId(), - notNullValue()); + final String node1 = internalCluster().startNode(settings); + registerAzureNode(node1); + assertNotNull(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId()); logger.info("--> start another node"); - internalCluster().startNode(settings); - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId(), - notNullValue()); + final String node2 = internalCluster().startNode(settings); + registerAzureNode(node2); + assertNotNull(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId()); // We expect having 2 nodes as part of the cluster, let's test that - checkNumberOfNodes(2); - } - - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/11533") - public void testTwoNodesShouldRunUsingPublicIp() { - Settings.Builder settings = Settings.builder() - .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") - .put(Discovery.HOST_TYPE_SETTING.getKey(), "public_ip"); - - logger.info("--> start first node"); - internalCluster().startNode(settings); - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId(), - notNullValue()); - - logger.info("--> start another node"); - internalCluster().startNode(settings); - assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("1s").get().getState().nodes().getMasterNodeId(), - notNullValue()); - - // We expect having 2 nodes as part of the cluster, let's test that - checkNumberOfNodes(2); + assertNumberOfNodes(2); } }