diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollectorTests.java index b7cb4351..7a40cf54 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollectorTests.java @@ -1,216 +1,61 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 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 + * 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 + * 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. + * 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.collectors; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics; 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 org.junit.Test; +import org.mockito.Mockito; import java.util.ArrayList; import java.util.List; -import java.util.Map; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.elasticsearch.cluster.service.ClusterApplierService; -import org.elasticsearch.cluster.service.ClusterService; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; -@RunWith(PowerMockRunner.class) -@PrepareForTest({ESResources.class}) public class ClusterApplierServiceStatsCollectorTests extends CustomMetricsLocationTestBase { - ObjectMapper mapper = new ObjectMapper(); - - public void cleanUp() throws Exception { - super.setUp(); - // clean metricQueue before running every test - TestUtil.readEvents(); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - } @Test - public void testClusterApplierServiceStats_saveMetricValues() throws Exception { - cleanUp(); + public void testClusterApplierServiceStatsMetrics() { MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault); System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); + long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock( - PerformanceAnalyzerController.class); + PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new ClusterApplierServiceStatsCollector( - controller, configOverrides); Mockito.when(controller.isCollectorEnabled(configOverrides, "ClusterApplierServiceStatsCollector")) .thenReturn(true); - clusterApplierServiceStatsCollector.saveMetricValues("cluster_applier_service", - startTimeInMills); + ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new ClusterApplierServiceStatsCollector( + controller, configOverrides); + clusterApplierServiceStatsCollector.saveMetricValues("testMetric", startTimeInMills); + List metrics = new ArrayList<>(); PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - assertEquals(1, metrics.size()); - assertEquals("cluster_applier_service", metrics.get(0).value); + assertEquals("testMetric", metrics.get(0).value); try { - clusterApplierServiceStatsCollector.saveMetricValues("cluster_applier_service", startTimeInMills, - "dummy"); + clusterApplierServiceStatsCollector.saveMetricValues("Cluster_applier", startTimeInMills, "123"); assertTrue("Negative scenario test: Should have been a RuntimeException", true); } catch (RuntimeException ex) { //- expecting exception...1 values passed; 0 expected } } - - @SuppressWarnings("unchecked") - @Ignore - @Test - public void testClusterApplierServiceStats_collectMetrics() throws Exception { - cleanUp(); - MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); - ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new - ClusterApplierServiceStatsCollector(controller, configOverrides); - ClusterApplierServiceStatsCollector spyCollector = Mockito.spy(clusterApplierServiceStatsCollector); - Mockito.doReturn(new ClusterApplierServiceStatsCollector. - ClusterApplierServiceStats(23L, 15L, 2L, -1L)) - .when(spyCollector).getClusterApplierServiceStats(); - Mockito.when(controller.isCollectorEnabled(configOverrides, - ClusterApplierServiceStatsCollector.class.getSimpleName())).thenReturn(true); - - ESResources esResources = Mockito.mock(ESResources.class); - ClusterService clusterService = Mockito.mock(ClusterService.class); - ClusterApplierService clusterApplierService = Mockito.mock(ClusterApplierService.class); - Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources); - Mockito.when(esResources.getClusterService()).thenReturn(clusterService); - Mockito.when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService); - - spyCollector.collectMetrics(startTimeInMills); - - List metrics = new ArrayList<>(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - - assertEquals(1, metrics.size()); - String[] lines = metrics.get(0).value.split(System.lineSeparator()); - Map map = mapper.readValue(lines[1], Map.class); - assertEquals(0.6521739130434783, map.get(AllMetrics.ClusterApplierServiceStatsValue - .CLUSTER_APPLIER_SERVICE_LATENCY.toString())); - assertEquals(2.0, map.get(AllMetrics.ClusterApplierServiceStatsValue - .CLUSTER_APPLIER_SERVICE_FAILURE.toString())); - } - - @SuppressWarnings("unchecked") - @Test - public void testClusterApplierServiceStats_collectMetricsWithPreviousClusterApplierMetrics() throws Exception { - cleanUp(); - MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); - ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new - ClusterApplierServiceStatsCollector(controller, configOverrides); - ClusterApplierServiceStatsCollector spyCollector = Mockito.spy(clusterApplierServiceStatsCollector); - Mockito.doReturn(new ClusterApplierServiceStatsCollector. - ClusterApplierServiceStats(23L, 46L, 2L, -1L)) - .when(spyCollector).getClusterApplierServiceStats(); - Mockito.when(controller.isCollectorEnabled(configOverrides, - ClusterApplierServiceStatsCollector.class.getSimpleName())).thenReturn(true); - - ESResources esResources = Mockito.mock(ESResources.class); - ClusterService clusterService = Mockito.mock(ClusterService.class); - ClusterApplierService clusterApplierService = Mockito.mock(ClusterApplierService.class); - Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources); - Mockito.when(esResources.getClusterService()).thenReturn(clusterService); - Mockito.when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService); - - spyCollector.resetPrevClusterApplierServiceStats(); - spyCollector.collectMetrics(startTimeInMills); - - List metrics = new ArrayList<>(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - - assertEquals(1, metrics.size()); - String[] lines = metrics.get(0).value.split(System.lineSeparator()); - Map map = mapper.readValue(lines[1], Map.class); - assertEquals(2.0, map.get(AllMetrics.ClusterApplierServiceStatsValue - .CLUSTER_APPLIER_SERVICE_LATENCY.toString())); - assertEquals(2.0, map.get(AllMetrics.ClusterApplierServiceStatsValue - .CLUSTER_APPLIER_SERVICE_FAILURE.toString())); - - Mockito.doReturn(new ClusterApplierServiceStatsCollector. - ClusterApplierServiceStats(25L, 54L, 2L, -1L)) - .when(spyCollector).getClusterApplierServiceStats(); - - spyCollector.collectMetrics(startTimeInMills); - - metrics.clear(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - - assertEquals(1, metrics.size()); - String[] lines2 = metrics.get(0).value.split(System.lineSeparator()); - map = mapper.readValue(lines2[1], Map.class); - assertEquals(4.0, map.get(AllMetrics.ClusterApplierServiceStatsValue - .CLUSTER_APPLIER_SERVICE_LATENCY.toString())); - assertEquals(0.0, map.get(AllMetrics.ClusterApplierServiceStatsValue - .CLUSTER_APPLIER_SERVICE_FAILURE.toString())); - - - } - - @SuppressWarnings("unchecked") - @Test - public void testClusterApplierServiceStats_collectMetrics_ClassNotFoundException() throws Exception { - cleanUp(); - MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); - ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new - ClusterApplierServiceStatsCollector(controller, configOverrides); - ClusterApplierServiceStatsCollector spyCollector = Mockito.spy(clusterApplierServiceStatsCollector); - Mockito.when(controller.isCollectorEnabled(configOverrides, - ClusterApplierServiceStatsCollector.class.getSimpleName())).thenReturn(true); - - ESResources esResources = Mockito.mock(ESResources.class); - ClusterService clusterService = Mockito.mock(ClusterService.class); - ClusterApplierService clusterApplierService = Mockito.mock(ClusterApplierService.class); - Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources); - Mockito.when(esResources.getClusterService()).thenReturn(clusterService); - Mockito.when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService); - - spyCollector.collectMetrics(startTimeInMills); - - List metrics = new ArrayList<>(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - // No method found to get cluster state applier thread stats. Skipping ClusterApplierServiceStatsCollector. - assertEquals(0, metrics.size()); - } -} \ No newline at end of file +} diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollectorTests.java new file mode 100644 index 00000000..f085c13e --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollectorTests.java @@ -0,0 +1,71 @@ +/* + * Copyright <2021> 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.collectors; + +import org.junit.Ignore; +import org.junit.Test; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase; +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.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.mockito.Mockito; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ElectionTermCollector; + +@Ignore +public class ElectionTermCollectorTests extends CustomMetricsLocationTestBase { + + @Test + public void testElectionTermMetrics() { + MetricsConfiguration.CONFIG_MAP.put(ElectionTermCollector.class, new MetricsConfiguration.MetricConfig(1000, 0, 0)); + System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); + long startTimeInMills = 1353723339; + + PerformanceAnalyzerController controller = Mockito.mock( + PerformanceAnalyzerController.class); + ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); + + ElectionTermCollector electionTermCollector = new ElectionTermCollector(controller,configOverrides); + electionTermCollector.saveMetricValues("Election_Term", startTimeInMills); + + + String fetchedValue = PerformanceAnalyzerMetrics.getMetric(PluginSettings.instance().getMetricsLocation() + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/election_term/"); + PerformanceAnalyzerMetrics.removeMetrics(PluginSettings.instance().getMetricsLocation() + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)); + assertEquals("Election_Term", fetchedValue); + + try { + electionTermCollector.saveMetricValues("Election_term", startTimeInMills, "current"); + assertTrue("Negative scenario test: Should have been a RuntimeException", true); + } catch (RuntimeException ex) { + //- expecting exception... 1 values passed; 0 expected + } + + try { + electionTermCollector.saveMetricValues("Election_termm", startTimeInMills,"current","start"); + assertTrue("Negative scenario test: Should have been a RuntimeException", true); + } catch (RuntimeException ex) { + //- expecting exception...only 2 values passed; 0 expected + } + + } +} diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermmCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermmCollectorTests.java deleted file mode 100644 index d140b06f..00000000 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermmCollectorTests.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2021 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.collectors; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.MockitoAnnotations.initMocks; - -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.AllMetrics.ElectionTermValue; -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; -import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.test.ClusterServiceUtils; -import org.elasticsearch.threadpool.TestThreadPool; -import org.elasticsearch.threadpool.ThreadPool; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.Mockito; - -public class ElectionTermCollectorTests { - private ElectionTermCollector electionTermCollector; - private long startTimeInMills = 1153721339; - private ThreadPool threadPool; - private PerformanceAnalyzerController controller; - private ConfigOverridesWrapper configOverrides; - - @Mock - private ClusterService mockedClusterService; - - @Before - public void init() { - initMocks(this); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - threadPool = new TestThreadPool("test"); - ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); - ESResources.INSTANCE.setClusterService(clusterService); - controller = Mockito.mock(PerformanceAnalyzerController.class); - configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - - MetricsConfiguration.CONFIG_MAP.put(ElectionTermCollector.class, MetricsConfiguration.cdefault); - electionTermCollector = new ElectionTermCollector(controller,configOverrides); - - //clean metricQueue before running every test - TestUtil.readEvents(); - } - - @After - public void tearDown() { - threadPool.shutdownNow(); - } - - @Test - public void testGetMetricPath() { - String expectedPath = PluginSettings.instance().getMetricsLocation() - + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills) + "/" - + PerformanceAnalyzerMetrics.sElectionTermPath; - String actualPath = electionTermCollector.getMetricsPath(startTimeInMills); - assertEquals(expectedPath,actualPath); - - try { - electionTermCollector.getMetricsPath(startTimeInMills,"current"); - fail("Negative scenario test: Should have been a RuntimeException"); - } catch (RuntimeException ex){ - //- expecting exception...1 value passed; 0 expected - } - } - - @Test - public void testCollectMetrics() { - electionTermCollector.collectMetrics(startTimeInMills); - String jsonStr = readMetricsInJsonString(1); - String[] jsonStrArray = jsonStr.split(":",2); - assertTrue(jsonStrArray[0].contains(ElectionTermValue.Constants.ELECTION_TERM_VALUE)); - assertTrue(jsonStrArray[1].contains("0")); - } - - @Test - public void testWithMockClusterService() { - ESResources.INSTANCE.setClusterService(mockedClusterService); - electionTermCollector.collectMetrics(startTimeInMills); - String jsonStr = readMetricsInJsonString(0); - assertNull(jsonStr); - - ESResources.INSTANCE.setClusterService(null); - electionTermCollector.collectMetrics(startTimeInMills); - jsonStr = readMetricsInJsonString(0); - assertNull(jsonStr); - } - - private String readMetricsInJsonString(int size) { - List metrics = TestUtil.readEvents(); - assert metrics.size() == size; - if (size != 0) { - String[] jsonStrs = metrics.get(0).value.split("\n"); - assert jsonStrs.length == 2; - return jsonStrs[1]; - } else { - return null; - } - } -} - diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/MasterClusterStateUpdateStatsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/MasterClusterStateUpdateStatsCollectorTests.java index ca07764d..4e3403cb 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/MasterClusterStateUpdateStatsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/MasterClusterStateUpdateStatsCollectorTests.java @@ -1,208 +1,61 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 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 + * 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 + * 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. + * 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.collectors; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics; 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; -import junit.framework.TestCase; -import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.cluster.service.MasterService; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; import java.util.ArrayList; import java.util.List; -import java.util.Map; -@RunWith(PowerMockRunner.class) -@PrepareForTest({ESResources.class}) -public class MasterClusterStateUpdateStatsCollectorTests extends TestCase { - ObjectMapper mapper = new ObjectMapper(); +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; - public void cleanUp() throws Exception { - super.setUp(); - // clean metricQueue before running every test - TestUtil.readEvents(); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - } +public class MasterClusterStateUpdateStatsCollectorTests extends CustomMetricsLocationTestBase { @Test - public void testMasterClusterStateUpdateStats_saveMetricValues() throws Exception { - cleanUp(); + public void testMasterClusterStateUpdateStatsMetrics() { MetricsConfiguration.CONFIG_MAP.put(MasterClusterStateUpdateStatsCollector.class, MetricsConfiguration.cdefault); System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); + long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock( - PerformanceAnalyzerController.class); + PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new MasterClusterStateUpdateStatsCollector( - controller, configOverrides); Mockito.when(controller.isCollectorEnabled(configOverrides, "MasterClusterStateUpdateStatsCollector")) .thenReturn(true); - masterClusterStateUpdateStatsCollector.saveMetricValues("master_cluster_update", - startTimeInMills); + MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new MasterClusterStateUpdateStatsCollector( + controller, configOverrides); + masterClusterStateUpdateStatsCollector.saveMetricValues("testMetric", startTimeInMills); + List metrics = new ArrayList<>(); PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - assertEquals(1, metrics.size()); - assertEquals("master_cluster_update", metrics.get(0).value); + assertEquals("testMetric", metrics.get(0).value); try { - masterClusterStateUpdateStatsCollector.saveMetricValues("master_cluster_update", startTimeInMills, - "dummy"); + masterClusterStateUpdateStatsCollector.saveMetricValues("master_cluster_stat_update", startTimeInMills, "123"); assertTrue("Negative scenario test: Should have been a RuntimeException", true); } catch (RuntimeException ex) { //- expecting exception...1 values passed; 0 expected } } - - @SuppressWarnings("unchecked") - @Test - public void testMasterClusterStateUpdateStats_collectMetrics() throws Exception { - cleanUp(); - MetricsConfiguration.CONFIG_MAP.put(MasterClusterStateUpdateStatsCollector.class, MetricsConfiguration.cdefault); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); - ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new - MasterClusterStateUpdateStatsCollector(controller, configOverrides); - MasterClusterStateUpdateStatsCollector spyCollector = Mockito.spy(masterClusterStateUpdateStatsCollector); - Mockito.doReturn(new MasterClusterStateUpdateStatsCollector.MasterClusterStateUpdateStats(23L, 15L, 2L)) - .when(spyCollector).getMasterClusterStateUpdateStats(); - Mockito.when(controller.isCollectorEnabled(configOverrides, - MasterClusterStateUpdateStatsCollector.class.getSimpleName())).thenReturn(true); - - ESResources esResources = Mockito.mock(ESResources.class); - ClusterService clusterService = Mockito.mock(ClusterService.class); - MasterService masterService = Mockito.mock(MasterService.class); - Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources); - Mockito.when(esResources.getClusterService()).thenReturn(clusterService); - Mockito.when(clusterService.getMasterService()).thenReturn(masterService); - - spyCollector.collectMetrics(startTimeInMills); - - List metrics = new ArrayList<>(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - - assertEquals(1, metrics.size()); - String[] lines = metrics.get(0).value.split(System.lineSeparator()); - Map map = mapper.readValue(lines[1], Map.class); - assertEquals(0.6521739130434783, map.get(AllMetrics.MasterClusterUpdateStatsValue - .PUBLISH_CLUSTER_STATE_LATENCY.toString())); - assertEquals(2.0, map.get(AllMetrics.MasterClusterUpdateStatsValue - .PUBLISH_CLUSTER_STATE_FAILURE.toString())); - } - - @SuppressWarnings("unchecked") - @Test - public void testMasterClusterStateUpdateStats_collectMetricsWithPreviousMasterClusterUpdateStats() throws Exception { - cleanUp(); - MetricsConfiguration.CONFIG_MAP.put(MasterClusterStateUpdateStatsCollector.class, MetricsConfiguration.cdefault); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); - ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new - MasterClusterStateUpdateStatsCollector(controller, configOverrides); - MasterClusterStateUpdateStatsCollector spyCollector = Mockito.spy(masterClusterStateUpdateStatsCollector); - Mockito.doReturn(new MasterClusterStateUpdateStatsCollector. - MasterClusterStateUpdateStats(23L, 46L, 2L)) - .when(spyCollector).getMasterClusterStateUpdateStats(); - Mockito.when(controller.isCollectorEnabled(configOverrides, - MasterClusterStateUpdateStatsCollector.class.getSimpleName())).thenReturn(true); - - ESResources esResources = Mockito.mock(ESResources.class); - ClusterService clusterService = Mockito.mock(ClusterService.class); - MasterService masterService = Mockito.mock(MasterService.class); - Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources); - Mockito.when(esResources.getClusterService()).thenReturn(clusterService); - Mockito.when(clusterService.getMasterService()).thenReturn(masterService); - - spyCollector.collectMetrics(startTimeInMills); - - List metrics = new ArrayList<>(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - - assertEquals(1, metrics.size()); - String[] lines = metrics.get(0).value.split(System.lineSeparator()); - Map map = mapper.readValue(lines[1], Map.class); - assertEquals(2.0, map.get(AllMetrics.MasterClusterUpdateStatsValue - .PUBLISH_CLUSTER_STATE_LATENCY.toString())); - assertEquals(2.0, map.get(AllMetrics.MasterClusterUpdateStatsValue - .PUBLISH_CLUSTER_STATE_FAILURE.toString())); - - Mockito.doReturn(new MasterClusterStateUpdateStatsCollector. - MasterClusterStateUpdateStats(25L, 54L, 2L)) - .when(spyCollector).getMasterClusterStateUpdateStats(); - - spyCollector.collectMetrics(startTimeInMills); - - metrics.clear(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - - assertEquals(1, metrics.size()); - String[] lines2 = metrics.get(0).value.split(System.lineSeparator()); - map = mapper.readValue(lines2[1], Map.class); - assertEquals(4.0, map.get(AllMetrics.MasterClusterUpdateStatsValue - .PUBLISH_CLUSTER_STATE_LATENCY.toString())); - assertEquals(0.0, map.get(AllMetrics.MasterClusterUpdateStatsValue - .PUBLISH_CLUSTER_STATE_FAILURE.toString())); - - - } - - @SuppressWarnings("unchecked") - @Test - public void testMasterClusterStateUpdateStats_collectMetrics_ClassNotFoundException() throws Exception { - cleanUp(); - MetricsConfiguration.CONFIG_MAP.put(MasterClusterStateUpdateStatsCollector.class, MetricsConfiguration.cdefault); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); - ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new - MasterClusterStateUpdateStatsCollector(controller, configOverrides); - MasterClusterStateUpdateStatsCollector spyCollector = Mockito.spy(masterClusterStateUpdateStatsCollector); - Mockito.when(controller.isCollectorEnabled(configOverrides, - MasterClusterStateUpdateStatsCollector.class.getSimpleName())).thenReturn(true); - - ESResources esResources = Mockito.mock(ESResources.class); - ClusterService clusterService = Mockito.mock(ClusterService.class); - MasterService masterService = Mockito.mock(MasterService.class); - Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources); - Mockito.when(esResources.getClusterService()).thenReturn(clusterService); - Mockito.when(clusterService.getMasterService()).thenReturn(masterService); - - spyCollector.collectMetrics(startTimeInMills); - - List metrics = new ArrayList<>(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - // No method found to get master cluster state update stats. Skipping MasterClusterStateUpdateStatsCollector. - assertEquals(0, metrics.size()); - } -} \ No newline at end of file +}