|
| 1 | +/* |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertNull; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | +import static org.junit.Assert.fail; |
| 22 | +import static org.mockito.MockitoAnnotations.initMocks; |
| 23 | + |
| 24 | +import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; |
| 25 | +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; |
| 26 | +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; |
| 27 | +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper; |
| 28 | +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.ElectionTermValue; |
| 29 | +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; |
| 30 | +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; |
| 31 | +import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; |
| 32 | +import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; |
| 33 | +import java.util.List; |
| 34 | +import org.elasticsearch.cluster.service.ClusterService; |
| 35 | +import org.elasticsearch.test.ClusterServiceUtils; |
| 36 | +import org.elasticsearch.threadpool.TestThreadPool; |
| 37 | +import org.elasticsearch.threadpool.ThreadPool; |
| 38 | +import org.junit.After; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | +import org.mockito.Mock; |
| 42 | +import org.mockito.Mockito; |
| 43 | + |
| 44 | +public class ElectionTermCollectorTests { |
| 45 | + private ElectionTermCollector electionTermCollector; |
| 46 | + private long startTimeInMills = 1153721339; |
| 47 | + private ThreadPool threadPool; |
| 48 | + private PerformanceAnalyzerController controller; |
| 49 | + private ConfigOverridesWrapper configOverrides; |
| 50 | + |
| 51 | + @Mock |
| 52 | + private ClusterService mockedClusterService; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void init() { |
| 56 | + initMocks(this); |
| 57 | + System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); |
| 58 | + threadPool = new TestThreadPool("test"); |
| 59 | + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); |
| 60 | + ESResources.INSTANCE.setClusterService(clusterService); |
| 61 | + controller = Mockito.mock(PerformanceAnalyzerController.class); |
| 62 | + configOverrides = Mockito.mock(ConfigOverridesWrapper.class); |
| 63 | + |
| 64 | + MetricsConfiguration.CONFIG_MAP.put(ElectionTermCollector.class, MetricsConfiguration.cdefault); |
| 65 | + electionTermCollector = new ElectionTermCollector(controller,configOverrides); |
| 66 | + |
| 67 | + //clean metricQueue before running every test |
| 68 | + TestUtil.readEvents(); |
| 69 | + } |
| 70 | + |
| 71 | + @After |
| 72 | + public void tearDown() { |
| 73 | + threadPool.shutdownNow(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testGetMetricPath() { |
| 78 | + String expectedPath = PluginSettings.instance().getMetricsLocation() |
| 79 | + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills) + "/" |
| 80 | + + PerformanceAnalyzerMetrics.sElectionTermPath; |
| 81 | + String actualPath = electionTermCollector.getMetricsPath(startTimeInMills); |
| 82 | + assertEquals(expectedPath,actualPath); |
| 83 | + |
| 84 | + try { |
| 85 | + electionTermCollector.getMetricsPath(startTimeInMills,"current"); |
| 86 | + fail("Negative scenario test: Should have been a RuntimeException"); |
| 87 | + } catch (RuntimeException ex){ |
| 88 | + //- expecting exception...1 value passed; 0 expected |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testCollectMetrics() { |
| 94 | + electionTermCollector.collectMetrics(startTimeInMills); |
| 95 | + String jsonStr = readMetricsInJsonString(1); |
| 96 | + String[] jsonStrArray = jsonStr.split(":",2); |
| 97 | + assertTrue(jsonStrArray[0].contains(ElectionTermValue.Constants.ELECTION_TERM_VALUE)); |
| 98 | + assertTrue(jsonStrArray[1].contains("0")); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testWithMockClusterService() { |
| 103 | + ESResources.INSTANCE.setClusterService(mockedClusterService); |
| 104 | + electionTermCollector.collectMetrics(startTimeInMills); |
| 105 | + String jsonStr = readMetricsInJsonString(0); |
| 106 | + assertNull(jsonStr); |
| 107 | + |
| 108 | + ESResources.INSTANCE.setClusterService(null); |
| 109 | + electionTermCollector.collectMetrics(startTimeInMills); |
| 110 | + jsonStr = readMetricsInJsonString(0); |
| 111 | + assertNull(jsonStr); |
| 112 | + } |
| 113 | + |
| 114 | + private String readMetricsInJsonString(int size) { |
| 115 | + List<Event> metrics = TestUtil.readEvents(); |
| 116 | + assert metrics.size() == size; |
| 117 | + if (size != 0) { |
| 118 | + String[] jsonStrs = metrics.get(0).value.split("\n"); |
| 119 | + assert jsonStrs.length == 2; |
| 120 | + return jsonStrs[1]; |
| 121 | + } else { |
| 122 | + return null; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
0 commit comments