From c0646e932d53438cc37a9108a7f542ca16778d3a Mon Sep 17 00:00:00 2001 From: Yu Sun <11154022+yu-sun-77@users.noreply.github.com> Date: Wed, 16 Dec 2020 15:29:21 -0800 Subject: [PATCH] Improve test coverage up to 62% (#257) * add UT for PerformanceAnalyzerClusterConfigAction * add UT for PerformanceAnalyzerTransportRequestHandlerTests * add UT for PerformanceAnalyzerTransportChannelTests * add UT for PerformanceAnalyzerClusterConfigActionTests * add UT for PerformanceAnalyzerOverridesClusterConfigActionTests * add UT for PerformanceAnalyzerConfigActionTests * add legal header * test metrics path * change min code coverage to 0.6025 * code review changes * revert and exclude AES specific method in coverage * change min code coverage to 0.6212 --- build.gradle | 4 +- .../PerformanceAnalyzerPlugin.java | 4 +- ...erformanceAnalyzerClusterConfigAction.java | 18 +- .../PerformanceAnalyzerConfigAction.java | 28 ++- ...eAnalyzerOverridesClusterConfigAction.java | 4 +- ...rmanceAnalyzerTransportRequestHandler.java | 4 +- .../PerformanceAnalyzerPluginTests.java | 5 - .../CacheConfigMetricsCollectorTests.java | 20 +- .../CircuitBreakerCollectorTests.java | 17 ++ .../collectors/NodeDetailsCollectorTests.java | 18 ++ ...deStatsAllShardsMetricsCollectorTests.java | 28 ++- ...StatsFixedShardsMetricsCollectorTests.java | 17 ++ .../collectors/ShardStateCollectorTests.java | 21 +- .../config/ConfigOverridesTestHelper.java | 6 + ...manceAnalyzerClusterConfigActionTests.java | 145 +++++++++++++ .../PerformanceAnalyzerConfigActionTests.java | 205 ++++++++++++++++++ ...yzerOverridesClusterConfigActionTests.java | 150 +++++++++++++ ...formanceAnalyzerTransportChannelTests.java | 56 +++-- ...eAnalyzerTransportRequestHandlerTests.java | 78 +++++++ 19 files changed, 773 insertions(+), 55 deletions(-) create mode 100644 src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerClusterConfigActionTests.java create mode 100644 src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerConfigActionTests.java create mode 100644 src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerOverridesClusterConfigActionTests.java create mode 100644 src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandlerTests.java diff --git a/build.gradle b/build.gradle index 9a4ff152..15d4624d 100644 --- a/build.gradle +++ b/build.gradle @@ -145,6 +145,8 @@ jacocoTestReport { '**/com/amazon/opendistro/elasticsearch/performanceanalyzer/**', ], exclude: [ + '**/FaultDetectionMetricsCollector.class', + '**/MasterThrottlingMetricsCollector.class', ]) }) } @@ -165,7 +167,7 @@ jacocoTestCoverageVerification { violationRules { rule { limit { - minimum = 0.48 + minimum = 0.6212 } } } diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java index a4356170..124e9861 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java @@ -239,10 +239,10 @@ public void onDiscovery(Discovery discovery) { Class listenerInjector = Class.forName(LISTENER_INJECTOR_CLASS_PATH); Object listenerInjectorInstance = listenerInjector.getDeclaredConstructor().newInstance(); Method addListenerMethod = listenerInjectorInstance.getClass().getMethod(ADD_FAULT_DETECTION_METHOD, - Discovery.class); + Discovery.class); addListenerMethod.invoke(listenerInjectorInstance, discovery); } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | - IllegalAccessException e) { + IllegalAccessException e) { LOG.debug("Exception while calling addFaultDetectionListener in Discovery"); } catch (ClassNotFoundException e) { LOG.debug("No Class for ListenerInjector detected"); diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerClusterConfigAction.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerClusterConfigAction.java index c77d5d02..50d71d02 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerClusterConfigAction.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerClusterConfigAction.java @@ -29,15 +29,15 @@ */ public class PerformanceAnalyzerClusterConfigAction extends BaseRestHandler { private static final Logger LOG = LogManager.getLogger(PerformanceAnalyzerClusterConfigAction.class); - private static final String PA_CLUSTER_CONFIG_PATH = "/_opendistro/_performanceanalyzer/cluster/config"; - private static final String RCA_CLUSTER_CONFIG_PATH = "/_opendistro/_performanceanalyzer/rca/cluster/config"; - private static final String LOGGING_CLUSTER_CONFIG_PATH = "/_opendistro/_performanceanalyzer/logging/cluster/config"; - private static final String BATCH_METRICS_CLUSTER_CONFIG_PATH = "/_opendistro/_performanceanalyzer/batch/cluster/config"; - private static final String ENABLED = "enabled"; - private static final String SHARDS_PER_COLLECTION = "shardsPerCollection"; - private static final String CURRENT = "currentPerformanceAnalyzerClusterState"; - private static final String NAME = "PerformanceAnalyzerClusterConfigAction"; - private static final String BATCH_METRICS_RETENTION_PERIOD_MINUTES = "batchMetricsRetentionPeriodMinutes"; + + public static final String CURRENT = "currentPerformanceAnalyzerClusterState"; + public static final String BATCH_METRICS_RETENTION_PERIOD_MINUTES = "batchMetricsRetentionPeriodMinutes"; + public static final String PA_CLUSTER_CONFIG_PATH = "/_opendistro/_performanceanalyzer/cluster/config"; + public static final String RCA_CLUSTER_CONFIG_PATH = "/_opendistro/_performanceanalyzer/rca/cluster/config"; + public static final String LOGGING_CLUSTER_CONFIG_PATH = "/_opendistro/_performanceanalyzer/logging/cluster/config"; + public static final String BATCH_METRICS_CLUSTER_CONFIG_PATH = "/_opendistro/_performanceanalyzer/batch/cluster/config"; + public static final String ENABLED = "enabled"; + public static final String SHARDS_PER_COLLECTION = "shardsPerCollection"; private static final List ROUTES = unmodifiableList( diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerConfigAction.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerConfigAction.java index 6fe2fcfd..794aff36 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerConfigAction.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerConfigAction.java @@ -39,20 +39,24 @@ @SuppressWarnings("deprecation") public class PerformanceAnalyzerConfigAction extends BaseRestHandler { + private static final Logger LOG = LogManager.getLogger(PerformanceAnalyzerConfigAction.class); - private static final String ENABLED = "enabled"; - private static final String SHARDS_PER_COLLECTION = "shardsPerCollection"; - private static final String PA_ENABLED = "performanceAnalyzerEnabled"; - private static final String RCA_ENABLED = "rcaEnabled"; - private static final String PA_LOGGING_ENABLED = "loggingEnabled"; - private static final String BATCH_METRICS_ENABLED = "batchMetricsEnabled"; - private static final String BATCH_METRICS_RETENTION_PERIOD_MINUTES = "batchMetricsRetentionPeriodMinutes"; private static PerformanceAnalyzerConfigAction instance = null; private final PerformanceAnalyzerController performanceAnalyzerController; - private static final String RCA_CONFIG_PATH = "/_opendistro/_performanceanalyzer/rca/config"; - private static final String PA_CONFIG_PATH = "/_opendistro/_performanceanalyzer/config"; - private static final String LOGGING_CONFIG_PATH = "/_opendistro/_performanceanalyzer/logging/config"; - private static final String BATCH_METRICS_CONFIG_PATH = "/_opendistro/_performanceanalyzer/batch/config"; + + public static final String ENABLED = "enabled"; + public static final String SHARDS_PER_COLLECTION = "shardsPerCollection"; + public static final String PA_ENABLED = "performanceAnalyzerEnabled"; + public static final String RCA_ENABLED = "rcaEnabled"; + public static final String PA_LOGGING_ENABLED = "loggingEnabled"; + public static final String BATCH_METRICS_ENABLED = "batchMetricsEnabled"; + public static final String BATCH_METRICS_RETENTION_PERIOD_MINUTES = "batchMetricsRetentionPeriodMinutes"; + public static final String PERFORMANCE_ANALYZER_CONFIG_ACTION = "PerformanceAnalyzer_Config_Action"; + public static final String RCA_CONFIG_PATH = "/_opendistro/_performanceanalyzer/rca/config"; + public static final String PA_CONFIG_PATH = "/_opendistro/_performanceanalyzer/config"; + public static final String LOGGING_CONFIG_PATH = "/_opendistro/_performanceanalyzer/logging/config"; + public static final String BATCH_METRICS_CONFIG_PATH = "/_opendistro/_performanceanalyzer/batch/config"; + private static final List ROUTES = unmodifiableList( asList( @@ -154,7 +158,7 @@ protected RestChannelConsumer prepareRequest(final RestRequest request, final No @Override public String getName() { - return "PerformanceAnalyzer_Config_Action"; + return PERFORMANCE_ANALYZER_CONFIG_ACTION; } private RestChannelConsumer getChannelConsumerWithError(String error) { diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerOverridesClusterConfigAction.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerOverridesClusterConfigAction.java index 6cd03812..d5921600 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerOverridesClusterConfigAction.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerOverridesClusterConfigAction.java @@ -48,11 +48,11 @@ public class PerformanceAnalyzerOverridesClusterConfigAction extends BaseRestHan private static final Logger LOG = LogManager.getLogger(PerformanceAnalyzerOverridesClusterConfigAction.class); - private static final String PA_CONFIG_OVERRIDES_PATH = + public static final String PA_CONFIG_OVERRIDES_PATH = "/_opendistro/_performanceanalyzer/override/cluster/config"; private static final String OVERRIDES_FIELD = "overrides"; private static final String REASON_FIELD = "reason"; - private static final String OVERRIDE_TRIGGERED_FIELD = "override triggered"; + public static final String OVERRIDE_TRIGGERED_FIELD = "override triggered"; private static final List ROUTES = unmodifiableList( diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandler.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandler.java index 35df9b70..ddc6f085 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandler.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandler.java @@ -17,6 +17,7 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.StatExceptionCode; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.StatsCollector; +import com.google.common.annotations.VisibleForTesting; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.bulk.BulkShardRequest; @@ -48,7 +49,8 @@ public void messageReceived(T request, TransportChannel channel, Task task) thro actualHandler.messageReceived(request, getChannel(request, channel, task), task); } - private TransportChannel getChannel(T request, TransportChannel channel, Task task) { + @VisibleForTesting + TransportChannel getChannel(T request, TransportChannel channel, Task task) { if (!controller.isPerformanceAnalyzerEnabled()) { return channel; } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPluginTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPluginTests.java index 0bd3381d..0e756135 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPluginTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPluginTests.java @@ -41,7 +41,6 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.discovery.Discovery; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.indices.breaker.BreakerSettings; @@ -59,7 +58,6 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.mockito.Mock; @ThreadLeakScope(Scope.NONE) public class PerformanceAnalyzerPluginTests extends ESTestCase { @@ -73,9 +71,6 @@ public class PerformanceAnalyzerPluginTests extends ESTestCase { private ClusterService clusterService; private ClusterSettings clusterSettings; - @Mock - private Discovery discovery; - @Before public void setup() { initMocks(this); diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CacheConfigMetricsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CacheConfigMetricsCollectorTests.java index 313773a5..8fa2fbee 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CacheConfigMetricsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CacheConfigMetricsCollectorTests.java @@ -17,8 +17,10 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.CacheConfigMetricsCollector.CacheMaxSizeStatus; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.CacheType; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; import com.fasterxml.jackson.databind.ObjectMapper; @@ -35,6 +37,7 @@ public class CacheConfigMetricsCollectorTests extends ESSingleNodeTestCase { private static final String TEST_INDEX = "test"; private CacheConfigMetricsCollector collector; + private long startTimeInMills = 1153721339; @Before public void init() { @@ -51,9 +54,22 @@ public void tearDown() throws Exception { } @Test - public void testCollectMetrics() throws IOException { - long startTimeInMills = 1153721339; + public void testGetMetricsPath() { + String expectedPath = PluginSettings.instance().getMetricsLocation() + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+ "/" + PerformanceAnalyzerMetrics.sCacheConfigPath; + String actualPath = collector.getMetricsPath(startTimeInMills); + assertEquals(expectedPath, actualPath); + + try { + collector.getMetricsPath(startTimeInMills, "cacheConfigPath"); + fail("Negative scenario test: Should have been a RuntimeException"); + } catch (RuntimeException ex) { + //- expecting exception...1 values passed; 0 expected + } + } + @Test + public void testCollectMetrics() throws IOException { createIndex(TEST_INDEX); collector.collectMetrics(startTimeInMills); diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CircuitBreakerCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CircuitBreakerCollectorTests.java index 9f0186a6..96a931d0 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CircuitBreakerCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CircuitBreakerCollectorTests.java @@ -17,7 +17,9 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.CircuitBreakerCollector.CircuitBreakerStatus; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; import com.fasterxml.jackson.databind.ObjectMapper; @@ -52,6 +54,21 @@ public void tearDown() throws Exception { super.tearDown(); } + @Test + public void testGetMetricsPath() { + String expectedPath = PluginSettings.instance().getMetricsLocation() + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+ "/" + PerformanceAnalyzerMetrics.sCircuitBreakerPath; + String actualPath = collector.getMetricsPath(startTimeInMills); + assertEquals(expectedPath, actualPath); + + try { + collector.getMetricsPath(startTimeInMills, "circuitBreakerPath"); + fail("Negative scenario test: Should have been a RuntimeException"); + } catch (RuntimeException ex) { + //- expecting exception...1 values passed; 0 expected + } + } + @Test public void testCollectMetrics() throws IOException { createIndex(TEST_INDEX); diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeDetailsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeDetailsCollectorTests.java index 43d95afa..f76c620c 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeDetailsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeDetailsCollectorTests.java @@ -19,9 +19,11 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeDetailsCollector.NodeDetailsStatus; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.NodeRole; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; import com.fasterxml.jackson.databind.ObjectMapper; @@ -46,6 +48,7 @@ public class NodeDetailsCollectorTests extends ESTestCase { private static final String NODE_ID = "testNode"; private NodeDetailsCollector collector; private ThreadPool threadPool; + private long startTimeInMills = 1153721339; @Mock private ConfigOverridesWrapper configOverrides; @@ -72,6 +75,21 @@ public void tearDown() throws Exception { super.tearDown(); } + @Test + public void testGetMetricsPath() { + String expectedPath = PluginSettings.instance().getMetricsLocation() + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+ "/" + PerformanceAnalyzerMetrics.sNodesPath; + String actualPath = collector.getMetricsPath(startTimeInMills); + assertEquals(expectedPath, actualPath); + + try { + collector.getMetricsPath(startTimeInMills, "nodesPath"); + fail("Negative scenario test: Should have been a RuntimeException"); + } catch (RuntimeException ex) { + //- expecting exception...1 values passed; 0 expected + } + } + @Test public void testCollectMetrics() throws IOException { long startTimeInMills = 1153721339; diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollectorTests.java index 421562cd..8c84c3a9 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollectorTests.java @@ -17,7 +17,9 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeStatsAllShardsMetricsCollector.NodeStatsMetricsAllShardsPerCollectionStatus; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; import com.fasterxml.jackson.databind.ObjectMapper; @@ -34,6 +36,7 @@ public class NodeStatsAllShardsMetricsCollectorTests extends ESSingleNodeTestCase { private static final String TEST_INDEX = "test"; private NodeStatsAllShardsMetricsCollector nodeStatsAllShardsMetricsCollector; + private long startTimeInMills = 1153721339;; @Before public void init() { @@ -49,9 +52,32 @@ public void tearDown() throws Exception { super.tearDown(); } + @Test + public void testGetNodeIndicesStatsByShardField() { + try { + nodeStatsAllShardsMetricsCollector.getNodeIndicesStatsByShardField(); + } catch (Exception e) { + assertTrue("There shouldn't be any exception in the code; Please check the reflection code for any changes", true); + } + } + + @Test + public void testGetMetricsPath() { + String expectedPath = PluginSettings.instance().getMetricsLocation() + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+ "/" + PerformanceAnalyzerMetrics.sIndicesPath + "/NodesStatsIndex/55"; + String actualPath = nodeStatsAllShardsMetricsCollector.getMetricsPath(startTimeInMills, "NodesStatsIndex", "55"); + assertEquals(expectedPath, actualPath); + + try { + nodeStatsAllShardsMetricsCollector.getMetricsPath(startTimeInMills, "NodesStatsIndex"); + fail("Negative scenario test: Should have been a RuntimeException"); + } catch (RuntimeException ex) { + //- expecting exception...only 1 values passed; 2 expected + } + } + @Test public void testCollectMetrics() throws IOException { - long startTimeInMills = 1153721339; createIndex(TEST_INDEX); nodeStatsAllShardsMetricsCollector.collectMetrics(startTimeInMills); diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsFixedShardsMetricsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsFixedShardsMetricsCollectorTests.java index f64390ce..599df6f0 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsFixedShardsMetricsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsFixedShardsMetricsCollectorTests.java @@ -19,8 +19,10 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.ShardStatsValue; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; import java.util.List; @@ -56,6 +58,21 @@ public void tearDown() throws Exception { super.tearDown(); } + @Test + public void testGetMetricsPath() { + String expectedPath = PluginSettings.instance().getMetricsLocation() + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+ "/" + PerformanceAnalyzerMetrics.sIndicesPath + "/NodesStatsIndex/55"; + String actualPath = collector.getMetricsPath(startTimeInMills, "NodesStatsIndex", "55"); + assertEquals(expectedPath, actualPath); + + try { + collector.getMetricsPath(startTimeInMills, "NodesStatsIndex"); + fail("Negative scenario test: Should have been a RuntimeException"); + } catch (RuntimeException ex) { + //- expecting exception...only 1 values passed; 2 expected + } + } + @Test public void testNodeStatsMetrics() { try { diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ShardStateCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ShardStateCollectorTests.java index 87cc754f..38421f1b 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ShardStateCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ShardStateCollectorTests.java @@ -19,11 +19,14 @@ import static com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.ShardType.SHARD_REPLICA; import static org.elasticsearch.test.ESTestCase.settings; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; import com.carrotsearch.randomizedtesting.RandomizedRunner; @@ -51,6 +54,7 @@ public class ShardStateCollectorTests { private static final int NUMBER_OF_PRIMARY_SHARDS = 1; private static final int NUMBER_OF_REPLICAS = 1; + private long startTimeInMills = 1153721339; private ShardStateCollector shardStateCollector; private ClusterService clusterService; private PerformanceAnalyzerController controller; @@ -68,9 +72,24 @@ public void init() { shardStateCollector = new ShardStateCollector(controller, configOverrides); } + @Test + public void testGetMetricsPath() { + String expectedPath = PluginSettings.instance().getMetricsLocation() + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+ "/" + PerformanceAnalyzerMetrics.sShardStatePath; + String actualPath = shardStateCollector.getMetricsPath(startTimeInMills); + assertEquals(expectedPath, actualPath); + + try { + shardStateCollector.getMetricsPath(startTimeInMills, "shardStatePath"); + fail("Negative scenario test: Should have been a RuntimeException"); + } catch (RuntimeException ex) { + //- expecting exception...1 values passed; 0 expected + } + } + @Test public void testCollectMetrics() throws IOException { - long startTimeInMills = 1153721339; + Mockito.when(controller.isCollectorEnabled(configOverrides, "ShardsStateCollector")) .thenReturn(true); Mockito.when(clusterService.state()).thenReturn(generateClusterState()); diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/ConfigOverridesTestHelper.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/ConfigOverridesTestHelper.java index e830f393..712904ed 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/ConfigOverridesTestHelper.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/ConfigOverridesTestHelper.java @@ -62,4 +62,10 @@ public static ConfigOverrides buildValidConfigOverrides() { public static String getValidConfigOverridesJson() throws JsonProcessingException { return MAPPER.writeValueAsString(buildValidConfigOverrides()); } + + public static String getInvalidConfigOverridesJson() throws JsonProcessingException { + ConfigOverrides overrides = buildValidConfigOverrides(); + overrides.getDisable().setRcas(ENABLED_RCAS_LIST); + return MAPPER.writeValueAsString(overrides); + } } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerClusterConfigActionTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerClusterConfigActionTests.java new file mode 100644 index 00000000..8a387f9b --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerClusterConfigActionTests.java @@ -0,0 +1,145 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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 com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.ClusterSettingsManager; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.handler.NodeStatsSettingHandler; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.handler.PerformanceAnalyzerClusterSettingHandler; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.settings.ClusterSettings; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.indices.breaker.BreakerSettings; +import org.elasticsearch.indices.breaker.CircuitBreakerService; +import org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestHandler.Route; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.test.rest.FakeRestChannel; +import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.threadpool.TestThreadPool; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.usage.UsageService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +public class PerformanceAnalyzerClusterConfigActionTests { + private PerformanceAnalyzerClusterConfigAction configAction; + private RestController restController; + private ThreadPool threadPool; + private NodeClient nodeClient; + private CircuitBreakerService circuitBreakerService; + private ClusterSettings clusterSettings; + private PerformanceAnalyzerClusterSettingHandler clusterSettingHandler; + private NodeStatsSettingHandler nodeStatsSettingHandler; + + @Mock private PerformanceAnalyzerController controller; + @Mock private ClusterSettingsManager clusterSettingsManager; + + @Before + public void init() { + initMocks(this); + + clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + circuitBreakerService = new HierarchyCircuitBreakerService(Settings.EMPTY, new ArrayList(), clusterSettings); + UsageService usageService = new UsageService(); + threadPool = new TestThreadPool("test"); + nodeClient = new NodeClient(Settings.EMPTY, threadPool); + restController = new RestController(Collections.emptySet(), null, nodeClient, circuitBreakerService, usageService); + clusterSettingHandler = new PerformanceAnalyzerClusterSettingHandler(controller, clusterSettingsManager); + nodeStatsSettingHandler = new NodeStatsSettingHandler(controller, clusterSettingsManager); + configAction = new PerformanceAnalyzerClusterConfigAction(Settings.EMPTY, restController, clusterSettingHandler, nodeStatsSettingHandler); + restController.registerHandler(configAction); + } + + @After + public void tearDown() throws Exception { + threadPool.shutdownNow(); + } + + @Test + public void testRoutes() { + List routes = configAction.routes(); + assertEquals(8, routes.size()); + } + + @Test + public void testGetName() { + assertEquals(PerformanceAnalyzerClusterConfigAction.class.getSimpleName(), configAction.getName()); + } + + @Test + public void testUpdateRcaSetting() throws IOException { + testWithRequestPath(PerformanceAnalyzerClusterConfigAction.RCA_CLUSTER_CONFIG_PATH); + } + + @Test + public void testUpdateLoggingSetting() throws IOException { + testWithRequestPath(PerformanceAnalyzerClusterConfigAction.LOGGING_CLUSTER_CONFIG_PATH); + } + + @Test + public void testUpdateBatchMetricsSetting() throws IOException { + testWithRequestPath(PerformanceAnalyzerClusterConfigAction.BATCH_METRICS_CLUSTER_CONFIG_PATH); + } + + @Test + public void testUpdatePerformanceAnalyzerSetting() throws IOException { + testWithRequestPath(PerformanceAnalyzerClusterConfigAction.PA_CLUSTER_CONFIG_PATH); + } + + private void testWithRequestPath(String requestPath) throws IOException { + final FakeRestRequest fakeRestRequest = buildRequest(requestPath); + final FakeRestChannel channel = new FakeRestChannel(fakeRestRequest, true, 10); + restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY)); + assertEquals(RestStatus.OK, channel.capturedResponse().status()); + + String responseStr = channel.capturedResponse().content().utf8ToString(); + assertTrue(responseStr.contains(PerformanceAnalyzerClusterConfigAction.CURRENT)); + assertTrue(responseStr.contains(PerformanceAnalyzerClusterConfigAction.SHARDS_PER_COLLECTION)); + assertTrue(responseStr.contains(PerformanceAnalyzerClusterConfigAction.BATCH_METRICS_RETENTION_PERIOD_MINUTES)); + } + + private FakeRestRequest buildRequest(String requestPath) throws IOException { + final XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .field(PerformanceAnalyzerClusterConfigAction.ENABLED, true) + .field(PerformanceAnalyzerClusterConfigAction.SHARDS_PER_COLLECTION, 1) + .endObject(); + + return new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) + .withMethod(RestRequest.Method.POST) + .withPath(requestPath) + .withContent(BytesReference.bytes(builder), builder.contentType()) + .build(); + } +} diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerConfigActionTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerConfigActionTests.java new file mode 100644 index 00000000..0def6b86 --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerConfigActionTests.java @@ -0,0 +1,205 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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 com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.settings.ClusterSettings; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.indices.breaker.BreakerSettings; +import org.elasticsearch.indices.breaker.CircuitBreakerService; +import org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestHandler.Route; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.test.rest.FakeRestChannel; +import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.threadpool.TestThreadPool; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.usage.UsageService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +public class PerformanceAnalyzerConfigActionTests { + private PerformanceAnalyzerConfigAction configAction; + private RestController restController; + private ThreadPool threadPool; + private NodeClient nodeClient; + private CircuitBreakerService circuitBreakerService; + private ClusterSettings clusterSettings; + + @Mock private PerformanceAnalyzerController controller; + + @Before + public void init() { + initMocks(this); + + clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + circuitBreakerService = new HierarchyCircuitBreakerService(Settings.EMPTY, new ArrayList(), clusterSettings); + UsageService usageService = new UsageService(); + threadPool = new TestThreadPool("test"); + nodeClient = new NodeClient(Settings.EMPTY, threadPool); + restController = new RestController(Collections.emptySet(), null, nodeClient, circuitBreakerService, usageService); + configAction = new PerformanceAnalyzerConfigAction(restController, controller); + restController.registerHandler(configAction); + + PerformanceAnalyzerConfigAction.setInstance(configAction); + assertEquals(configAction, PerformanceAnalyzerConfigAction.getInstance()); + + } + + @After + public void tearDown() throws Exception { + threadPool.shutdownNow(); + } + + @Test + public void testRoutes() { + List routes = configAction.routes(); + assertEquals(8, routes.size()); + } + + @Test + public void testGetName() { + assertEquals(PerformanceAnalyzerConfigAction.PERFORMANCE_ANALYZER_CONFIG_ACTION, configAction.getName()); + } + + @Test + public void testUpdateRcaState_ShouldEnable_paEnabled() throws IOException { + test(PerformanceAnalyzerConfigAction.RCA_CONFIG_PATH, true, true); + } + + @Test + public void testUpdateRcaState_ShouldEnable_paDisabled() throws IOException { + test(PerformanceAnalyzerConfigAction.RCA_CONFIG_PATH, true, false); + } + @Test + public void testUpdateRcaState_ShouldDisable_paEnabled() throws IOException { + test(PerformanceAnalyzerConfigAction.RCA_CONFIG_PATH, false, true); + } + + @Test + public void testUpdateRcaState_ShouldDisable_paDisabled() throws IOException { + test(PerformanceAnalyzerConfigAction.RCA_CONFIG_PATH, false, false); + } + + @Test + public void testUpdateLoggingState_ShouldEnable_paEnabled() throws IOException { + test(PerformanceAnalyzerConfigAction.LOGGING_CONFIG_PATH, true, true); + } + + @Test + public void testUpdateLoggingState_ShouldEnable_paDisabled() throws IOException { + test(PerformanceAnalyzerConfigAction.LOGGING_CONFIG_PATH, true, false); + } + @Test + public void testUpdateLoggingState_ShouldDisable_paEnabled() throws IOException { + test(PerformanceAnalyzerConfigAction.LOGGING_CONFIG_PATH, false, true); + } + + @Test + public void testUpdateLoggingState_ShouldDisable_paDisabled() throws IOException { + test(PerformanceAnalyzerConfigAction.LOGGING_CONFIG_PATH, false, false); + } + + @Test + public void testUpdateBatchMetricsState_ShouldEnable_paEnabled() throws IOException { + test(PerformanceAnalyzerConfigAction.BATCH_METRICS_CONFIG_PATH, true, true); + } + + @Test + public void testUpdateBatchMetricsState_ShouldEnable_paDisabled() throws IOException { + test(PerformanceAnalyzerConfigAction.BATCH_METRICS_CONFIG_PATH, true, false); + } + @Test + public void testUpdateBatchMetricsState_ShouldDisable_paEnabled() throws IOException { + test(PerformanceAnalyzerConfigAction.BATCH_METRICS_CONFIG_PATH, false, true); + } + + @Test + public void testUpdateBatchMetricsState_ShouldDisable_paDisabled() throws IOException { + test(PerformanceAnalyzerConfigAction.BATCH_METRICS_CONFIG_PATH, false, false); + } + + @Test + public void testUpdatePerformanceAnalyzerState_ShouldEnable_paEnabled() throws IOException { + test(PerformanceAnalyzerConfigAction.PA_CONFIG_PATH, true, true); + } + + @Test + public void testUpdatePerformanceAnalyzerState_ShouldDisable_paEnabled() throws IOException { + test(PerformanceAnalyzerConfigAction.PA_CONFIG_PATH, false, true); + } + + @Test + public void testUpdatePerformanceAnalyzerState_ShouldDisable_paDisabled() throws IOException { + test(PerformanceAnalyzerConfigAction.PA_CONFIG_PATH, false, false); + } + + private void test(String requestPath, boolean shouldEnable, boolean paEnabled) throws IOException { + final FakeRestRequest fakeRestRequest = buildRequest(requestPath, shouldEnable); + final FakeRestChannel channel = new FakeRestChannel(fakeRestRequest, true, 10); + Mockito.when(controller.isPerformanceAnalyzerEnabled()).thenReturn(paEnabled); + restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY)); + boolean testWithError = shouldEnable && !paEnabled; + if (testWithError) { + assertEquals(RestStatus.BAD_REQUEST, channel.capturedResponse().status()); + } else { + assertEquals(RestStatus.OK, channel.capturedResponse().status()); + String responseStr = channel.capturedResponse().content().utf8ToString(); + assertTrue(responseStr.contains(PerformanceAnalyzerConfigAction.PA_ENABLED)); + assertTrue(responseStr.contains(PerformanceAnalyzerConfigAction.RCA_ENABLED)); + assertTrue(responseStr.contains(PerformanceAnalyzerConfigAction.PA_LOGGING_ENABLED)); + assertTrue(responseStr.contains(PerformanceAnalyzerConfigAction.SHARDS_PER_COLLECTION)); + assertTrue(responseStr.contains(PerformanceAnalyzerConfigAction.BATCH_METRICS_ENABLED)); + assertTrue(responseStr.contains(PerformanceAnalyzerConfigAction.BATCH_METRICS_RETENTION_PERIOD_MINUTES)); + } + } + + private FakeRestRequest buildRequest(String requestPath, boolean shouldEnable) throws IOException { + final XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .field(PerformanceAnalyzerConfigAction.ENABLED, shouldEnable) + .field(PerformanceAnalyzerConfigAction.SHARDS_PER_COLLECTION, 1) + .endObject(); + + return new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) + .withMethod(RestRequest.Method.POST) + .withPath(requestPath) + .withContent(BytesReference.bytes(builder), builder.contentType()) + .build(); + } + + + +} diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerOverridesClusterConfigActionTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerOverridesClusterConfigActionTests.java new file mode 100644 index 00000000..d3d942f1 --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/http_action/config/PerformanceAnalyzerOverridesClusterConfigActionTests.java @@ -0,0 +1,150 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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 com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config; + +import static com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerOverridesClusterConfigAction.PA_CONFIG_OVERRIDES_PATH; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.ConfigOverridesTestHelper; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.handler.ConfigOverridesClusterSettingHandler; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.settings.ClusterSettings; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.indices.breaker.BreakerSettings; +import org.elasticsearch.indices.breaker.CircuitBreakerService; +import org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestHandler.Route; +import org.elasticsearch.rest.RestRequest.Method; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.test.rest.FakeRestChannel; +import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.threadpool.TestThreadPool; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.usage.UsageService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +public class PerformanceAnalyzerOverridesClusterConfigActionTests { + private PerformanceAnalyzerOverridesClusterConfigAction configAction; + private RestController restController; + private ThreadPool threadPool; + private NodeClient nodeClient; + private CircuitBreakerService circuitBreakerService; + private ClusterSettings clusterSettings; + + @Mock private ConfigOverridesClusterSettingHandler configOverridesClusterSettingHandler; + @Mock private ConfigOverridesWrapper overridesWrapper; + + @Before + public void init() { + initMocks(this); + + clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + circuitBreakerService = new HierarchyCircuitBreakerService(Settings.EMPTY, new ArrayList(), clusterSettings); + UsageService usageService = new UsageService(); + threadPool = new TestThreadPool("test"); + nodeClient = new NodeClient(Settings.EMPTY, threadPool); + restController = new RestController(Collections.emptySet(), null, nodeClient, circuitBreakerService, usageService); + configAction = new PerformanceAnalyzerOverridesClusterConfigAction(Settings.EMPTY, restController, configOverridesClusterSettingHandler, overridesWrapper); + restController.registerHandler(configAction); + } + + @After + public void tearDown() throws Exception { + threadPool.shutdownNow(); + } + + @Test + public void testRoutes() { + List routes = configAction.routes(); + assertEquals(2, routes.size()); + } + + @Test + public void testGetName() { + assertEquals(PerformanceAnalyzerOverridesClusterConfigAction.class.getSimpleName(), configAction.getName()); + } + + @Test + public void testWithGetMethod() throws IOException { + final FakeRestRequest fakeRestRequest = buildRequest( + PA_CONFIG_OVERRIDES_PATH, + Method.GET, + ConfigOverridesTestHelper.getValidConfigOverridesJson()); + final FakeRestChannel channel = new FakeRestChannel(fakeRestRequest, true, 10); + restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY)); + assertEquals(RestStatus.OK, channel.capturedResponse().status()); + } + + @Test + public void testWithPostMethod() throws IOException { + final FakeRestRequest fakeRestRequest = buildRequest( + PA_CONFIG_OVERRIDES_PATH, + Method.POST, + ConfigOverridesTestHelper.getValidConfigOverridesJson()); + final FakeRestChannel channel = new FakeRestChannel(fakeRestRequest, true, 10); + restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY)); + assertEquals(RestStatus.OK, channel.capturedResponse().status()); + String responseStr = channel.capturedResponse().content().utf8ToString(); + assertTrue(responseStr.contains(PerformanceAnalyzerOverridesClusterConfigAction.OVERRIDE_TRIGGERED_FIELD)); + } + + @Test + public void testWithUnsupportedMethod() throws IOException { + final FakeRestRequest fakeRestRequest = buildRequest( + PA_CONFIG_OVERRIDES_PATH, + Method.PUT, + ConfigOverridesTestHelper.getValidConfigOverridesJson()); + final FakeRestChannel channel = new FakeRestChannel(fakeRestRequest, true, 10); + restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY)); + assertEquals(RestStatus.METHOD_NOT_ALLOWED, channel.capturedResponse().status()); + } + + @Test + public void testWithInvalidOverrides() throws IOException { + final FakeRestRequest fakeRestRequest = buildRequest( + PA_CONFIG_OVERRIDES_PATH, + Method.POST, + ConfigOverridesTestHelper.getInvalidConfigOverridesJson()); + final FakeRestChannel channel = new FakeRestChannel(fakeRestRequest, true, 10); + restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY)); + assertEquals(RestStatus.BAD_REQUEST, channel.capturedResponse().status()); + } + + private FakeRestRequest buildRequest(String requestPath, Method requestMethod, String configOverridesJson) throws IOException { + ByteBuffer byteBuffer = ByteBuffer.wrap(configOverridesJson.getBytes()); + return new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) + .withMethod(requestMethod) + .withPath(requestPath) + .withContent(BytesReference.fromByteBuffer(byteBuffer), XContentType.JSON) + .build(); + } +} diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportChannelTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportChannelTests.java index 140d74c7..08272b02 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportChannelTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportChannelTests.java @@ -1,5 +1,5 @@ /* - * Copyright <2019> Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright <2020> Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. @@ -15,27 +15,45 @@ package com.amazon.opendistro.elasticsearch.performanceanalyzer.transport; -import org.junit.Ignore; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verify; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.IOException; +import org.apache.commons.lang3.SystemUtils; +import org.elasticsearch.transport.TransportChannel; +import org.elasticsearch.transport.TransportResponse; +import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; -import static org.junit.Assert.assertEquals; +public class PerformanceAnalyzerTransportChannelTests { + private PerformanceAnalyzerTransportChannel channel; + + @Mock private TransportChannel originalChannel; + @Mock private TransportResponse response; + + @Before + public void init() { + // this test only runs in Linux system + // as some of the static members of the ThreadList class are specific to Linux + org.junit.Assume.assumeTrue(SystemUtils.IS_OS_LINUX); + + initMocks(this); + channel = new PerformanceAnalyzerTransportChannel(); + channel.set(originalChannel, 0, "testIndex", 1, 0, false); + assertEquals("PerformanceAnalyzerTransportChannelProfile", channel.getProfileName()); + assertEquals("PerformanceAnalyzerTransportChannelType", channel.getChannelType()); + assertEquals(originalChannel, channel.getInnerChannel()); + } -@Ignore -public class PerformanceAnalyzerTransportChannelTests extends CustomMetricsLocationTestBase { @Test - public void testShardBulkMetrics() { - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1593719339; - PerformanceAnalyzerTransportChannel performanceanalyzerTransportChannel = new PerformanceAnalyzerTransportChannel(); - performanceanalyzerTransportChannel.saveMetricValues("ABCDEF", startTimeInMills, "BulkThread", "ShardBulkId", "start"); - String fetchedValue = PerformanceAnalyzerMetrics.getMetric( - PluginSettings.instance().getMetricsLocation() + - PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/threads/BulkThread/shardbulk/ShardBulkId/start"); - PerformanceAnalyzerMetrics.removeMetrics(PluginSettings.instance().getMetricsLocation() - + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)); - assertEquals("ABCDEF", fetchedValue); + public void testResponse() throws IOException { + channel.sendResponse(response); + verify(originalChannel).sendResponse(response); + + Exception exception = new Exception("dummy exception"); + channel.sendResponse(exception); + verify(originalChannel).sendResponse(exception); } } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandlerTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandlerTests.java new file mode 100644 index 00000000..b631295c --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandlerTests.java @@ -0,0 +1,78 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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 com.amazon.opendistro.elasticsearch.performanceanalyzer.transport; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.verify; +import static org.mockito.MockitoAnnotations.initMocks; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; +import org.apache.commons.lang3.SystemUtils; +import org.elasticsearch.action.bulk.BulkItemRequest; +import org.elasticsearch.action.bulk.BulkShardRequest; +import org.elasticsearch.action.support.replication.TransportReplicationAction.ConcreteShardRequest; +import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.tasks.Task; +import org.elasticsearch.transport.TransportChannel; +import org.elasticsearch.transport.TransportRequest; +import org.elasticsearch.transport.TransportRequestHandler; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +@SuppressWarnings("unchecked") +public class PerformanceAnalyzerTransportRequestHandlerTests { + private PerformanceAnalyzerTransportRequestHandler handler; + private ConcreteShardRequest concreteShardRequest; + + @Mock private TransportRequestHandler transportRequestHandler; + @Mock private PerformanceAnalyzerController controller; + @Mock private TransportChannel channel; + @Mock private TransportRequest request; + @Mock private BulkShardRequest bulkShardRequest; + @Mock private Task task; + @Mock private ShardId shardId; + + @Before + public void init() { + // this test only runs in Linux system + // as some of the static members of the ThreadList class are specific to Linux + org.junit.Assume.assumeTrue(SystemUtils.IS_OS_LINUX); + + initMocks(this); + handler = new PerformanceAnalyzerTransportRequestHandler(transportRequestHandler, controller); + handler.set(transportRequestHandler); + Mockito.when(controller.isPerformanceAnalyzerEnabled()).thenReturn(true); + } + + @Test + public void testMessageReceived() throws Exception { + handler.messageReceived(request, channel, task); + verify(transportRequestHandler).messageReceived(request, channel, task); + } + + @Test + public void testGetChannel() { + concreteShardRequest = new ConcreteShardRequest(bulkShardRequest, "id", 1); + handler.getChannel(concreteShardRequest, channel, task); + + Mockito.when(bulkShardRequest.shardId()).thenReturn(shardId); + Mockito.when(bulkShardRequest.items()).thenReturn(new BulkItemRequest[1]); + TransportChannel actualChannel = handler.getChannel(concreteShardRequest, channel, task); + assertTrue(actualChannel instanceof PerformanceAnalyzerTransportChannel); + } +}