Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Improve test coverage up to 62% (#257)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yu-sun-77 authored Dec 16, 2020
1 parent 4d6e1c9 commit c0646e9
Show file tree
Hide file tree
Showing 19 changed files with 773 additions and 55 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ jacocoTestReport {
'**/com/amazon/opendistro/elasticsearch/performanceanalyzer/**',
],
exclude: [
'**/FaultDetectionMetricsCollector.class',
'**/MasterThrottlingMetricsCollector.class',
])
})
}
Expand All @@ -165,7 +167,7 @@ jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.48
minimum = 0.6212
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Route> ROUTES =
unmodifiableList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Route> ROUTES =
unmodifiableList(
asList(
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Route> ROUTES =
unmodifiableList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit c0646e9

Please sign in to comment.