From 935f98a4b82d27e4e031f08f095e3cbbbde8cbc2 Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Thu, 23 Oct 2014 12:07:16 -0400 Subject: [PATCH] fix tests; improve client registeration Original commit: elastic/x-pack-elasticsearch@e0464a4a1fc40321f166d94d372b6e31031e337c --- .../license/plugin/core/LicensesService.java | 16 +++-- .../license/plugin/LicensesServiceTests.java | 67 ++++++++++++------- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java b/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java index d3b2daf8f08..c7c6bbfb7f4 100644 --- a/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java +++ b/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java @@ -315,9 +315,10 @@ public class LicensesService extends AbstractLifecycleComponent public void register(String feature, TrialLicenseOptions trialLicenseOptions, Listener listener) { registeredListeners.add(new ListenerHolder(feature, trialLicenseOptions, listener)); - // DO we need to check STATE_NOT_RECOVERED_BLOCK here - LicensesMetaData currentMetaData = clusterService.state().metaData().custom(LicensesMetaData.TYPE); - registerListeners(currentMetaData); + if (!clusterService.state().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)) { + LicensesMetaData currentMetaData = clusterService.state().metaData().custom(LicensesMetaData.TYPE); + registerListeners(currentMetaData); + } } private void registerListeners(LicensesMetaData currentMetaData) { @@ -333,8 +334,13 @@ public class LicensesService extends AbstractLifecycleComponent if (clusterService.state().nodes().localNodeMaster()) { registerTrialLicense(request); } else { - transportService.sendRequest(clusterService.state().nodes().masterNode(), - REGISTER_TRIAL_LICENSE_ACTION_NAME, request, EmptyTransportResponseHandler.INSTANCE_SAME); + DiscoveryNode masterNode; + if ((masterNode = clusterService.state().nodes().masterNode()) != null) { + transportService.sendRequest(masterNode, + REGISTER_TRIAL_LICENSE_ACTION_NAME, request, EmptyTransportResponseHandler.INSTANCE_SAME); + } else { + // could not sent register trial license request to master + } } } else { // notify feature as clusterChangedEvent may not happen diff --git a/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java b/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java index 524859a9d4f..28cd775b2ff 100644 --- a/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java +++ b/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java @@ -11,6 +11,8 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ProcessedClusterStateUpdateTask; import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse; import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.cluster.node.DiscoveryNode; +import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; @@ -23,10 +25,7 @@ import org.elasticsearch.license.plugin.action.put.PutLicenseRequest; import org.elasticsearch.license.plugin.core.*; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.InternalTestCluster; -import org.junit.After; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.*; import java.io.IOException; import java.net.URISyntaxException; @@ -36,14 +35,15 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; -import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope.SUITE; +import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope.TEST; -@ClusterScope(scope = SUITE, numDataNodes = 3) +@ClusterScope(scope = TEST, numDataNodes = 10) public class LicensesServiceTests extends ElasticsearchIntegrationTest { private static String pubKeyPath = null; private static String priKeyPath = null; + private static String node = null; @Override protected Settings nodeSettings(int nodeOrdinal) { @@ -66,8 +66,8 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { } - @After - public void afterTest() throws Exception { + @Before + public void beforeTest() throws Exception { final CountDownLatch latch = new CountDownLatch(1); masterClusterService().submitStateUpdateTask("delete licensing metadata", new ProcessedClusterStateUpdateTask() { @Override @@ -89,17 +89,27 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { }); latch.await(); clear(); + + DiscoveryNodes discoveryNodes = LicensesServiceTests.masterClusterService().state().getNodes(); + Set dataNodeSet = new HashSet<>(); + for(DiscoveryNode discoveryNode : discoveryNodes) { + if (discoveryNode.dataNode()) { + dataNodeSet.add(discoveryNode.getName()); + } + } + String[] dataNodes = dataNodeSet.toArray(new String[dataNodeSet.size()]); + node = dataNodes[randomIntBetween(0, dataNodes.length - 1)]; } @Test public void testEmptySignedLicenseCheck() { - LicensesManagerService licensesManagerService = licensesManagerService(); + LicensesManagerService licensesManagerService = masterLicensesManagerService(); assertTrue(LicensesStatus.VALID == licensesManagerService.checkLicenses(new HashSet())); } @Test public void testInvalidSignedLicenseCheck() throws Exception { - LicensesManagerService licensesManagerService = licensesManagerService(); + LicensesManagerService licensesManagerService = masterLicensesManagerService(); Map map = new HashMap<>(); TestUtils.FeatureAttributes featureAttributes = @@ -133,7 +143,7 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { String licenseOutput = TestUtils.runLicenseGenerationTool(licenseString, pubKeyPath, priKeyPath); Set licenses = ESLicenses.fromSource(licenseOutput); - LicensesManagerService licensesManagerService = licensesManagerService(); + LicensesManagerService licensesManagerService = masterLicensesManagerService(); ESLicenseManager esLicenseManager = ((LicensesService) licensesManagerService).getEsLicenseManager(); final CountDownLatch latch1 = new CountDownLatch(1); licensesManagerService.registerLicenses(new LicensesService.PutLicenseRequestHolder(new PutLicenseRequest().licenses(licenses), "test"), new ActionListener() { @@ -183,7 +193,7 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { TestUtils.isSame(licenses2, metaDataLicense); } - @Test @Ignore + @Test public void testTrialLicenseGeneration() throws Exception { LicensesClientService clientService = licensesClientService(); final CountDownLatch latch = new CountDownLatch(1); @@ -225,6 +235,7 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { @Override public void onEnabled() { if (this.shouldBeEnabled.get()) { + logger.info("onEnabled called from LicensesClientService"); processed.set(true); } else { fail("onEnabled should not have been called"); @@ -235,6 +246,7 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { @Override public void onDisabled() { if (!this.shouldBeEnabled.get()) { + logger.info("onEnabled called from LicensesClientService"); processed.set(true); } else { fail("onDisabled should not have been called"); @@ -242,7 +254,7 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { } } - @Test @Ignore + @Test public void testClientValidation() throws Exception { // start with no trial license // feature should be onDisabled @@ -251,6 +263,7 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { LicensesClientService clientService = licensesClientService(); LicensesManagerService managerService = licensesManagerService(); + LicensesManagerService masterLicensesManagerService = masterLicensesManagerService(); final TestLicenseClientListener testLicenseClientListener = new TestLicenseClientListener(false); clientService.register("shield", null, testLicenseClientListener); @@ -260,8 +273,8 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { logger.info("pass initial check"); assertFalse(testLicenseClientListener.processed.get()); - testLicenseClientListener.shouldBeEnabled.set(true); + testLicenseClientListener.shouldBeEnabled.set(true); Map map = new HashMap<>(); TestUtils.FeatureAttributes featureAttributes1 = new TestUtils.FeatureAttributes("shield", "subscription", "platinum", "foo bar Inc.", "elasticsearch", 2, "2014-12-13", "2015-12-13"); @@ -270,9 +283,8 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { String licenseOutput = TestUtils.runLicenseGenerationTool(licenseString, pubKeyPath, priKeyPath); Set licenses = ESLicenses.fromSource(licenseOutput); - LicensesManagerService licensesManagerService = licensesManagerService(); final CountDownLatch latch1 = new CountDownLatch(1); - licensesManagerService.registerLicenses(new LicensesService.PutLicenseRequestHolder(new PutLicenseRequest().licenses(licenses), "test"), new ActionListener() { + masterLicensesManagerService.registerLicenses(new LicensesService.PutLicenseRequestHolder(new PutLicenseRequest().licenses(licenses), "test"), new ActionListener() { @Override public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) { if (clusterStateUpdateResponse.isAcknowledged()) { @@ -292,8 +304,7 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { while (!testLicenseClientListener.processed.get()) { } - Set enabledFeatures = licensesManagerService.enabledFeatures(); - assertTrue(enabledFeatures.contains("shield")); + assertTrue(managerService.enabledFeatures().contains("shield")); } @@ -316,20 +327,22 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { } @Test - @Ignore public void testLicenseExpiry() throws Exception { //TODO, first figure out how to generate a license with a quick expiry in matter of seconds } - private LicensesManagerService licensesManagerService() { + private LicensesManagerService masterLicensesManagerService() { final InternalTestCluster clients = internalCluster(); return clients.getInstance(LicensesManagerService.class, clients.getMasterName()); } + private LicensesManagerService licensesManagerService() { + return internalCluster().getInstance(LicensesManagerService.class, node); + } + private LicensesClientService licensesClientService() { - final InternalTestCluster clients = internalCluster(); - return clients.getInstance(LicensesClientService.class); + return internalCluster().getInstance(LicensesClientService.class, node); } private LicensesService licensesService() { @@ -337,15 +350,19 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { return clients.getInstance(LicensesService.class, clients.getMasterName()); } - private ClusterService masterClusterService() { + private static ClusterService masterClusterService() { final InternalTestCluster clients = internalCluster(); return clients.getInstance(ClusterService.class, clients.getMasterName()); } private void clear() { final InternalTestCluster clients = internalCluster(); - LicensesService service = clients.getInstance(LicensesService.class, clients.getMasterName()); - service.clear(); + LicensesService masterService = clients.getInstance(LicensesService.class, clients.getMasterName()); + masterService.clear(); + if (node != null) { + LicensesService nodeService = clients.getInstance(LicensesService.class, node); + nodeService.clear(); + } }