diff --git a/.github/labeler.yml b/.github/labeler.yml index 9f6f50c54..53dc0443c 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -1,4 +1,4 @@ -backport 2.x: +backport 1.x: - "*" - "*/*" - "*/**/*" diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml index 5ed1dcdce..374d4d986 100644 --- a/.github/workflows/backport.yml +++ b/.github/workflows/backport.yml @@ -7,6 +7,7 @@ on: jobs: backport: + if: github.event.pull_request.merged == true runs-on: ubuntu-latest permissions: contents: write @@ -25,4 +26,5 @@ jobs: uses: VachaShah/backport@v2.2.0 with: github_token: ${{ steps.github_app_token.outputs.token }} - branch_name: backport/backport-${{ github.event.number }} + head_template: backport/backport-<%= number %>-to-<%= base %> + failure_labels: backport-failed diff --git a/.github/workflows/test_build_multi_platform.yml b/.github/workflows/test_build_multi_platform.yml index 7456e70a6..915ea6734 100644 --- a/.github/workflows/test_build_multi_platform.yml +++ b/.github/workflows/test_build_multi_platform.yml @@ -46,7 +46,7 @@ jobs: - name: Build and Run Tests run: | - ./gradlew build + ./gradlew build -x spotlessJava - name: Publish to Maven Local run: | ./gradlew publishToMavenLocal @@ -91,7 +91,7 @@ jobs: run: | chown -R 1000:1000 `pwd` su `id -un 1000` -c "./gradlew assemble && - ./gradlew build && + ./gradlew build -x spotlessJava && ./gradlew publishToMavenLocal && ./gradlew integTest -PnumNodes=3" - name: Upload Coverage Report @@ -127,7 +127,7 @@ jobs: ./gradlew assemble - name: Build and Run Tests run: | - ./gradlew build + ./gradlew build -x spotlessJava - name: Publish to Maven Local run: | ./gradlew publishToMavenLocal diff --git a/build.gradle b/build.gradle index 33ddf7e14..ad21b8b5f 100644 --- a/build.gradle +++ b/build.gradle @@ -35,7 +35,7 @@ buildscript { js_resource_folder = "src/test/resources/job-scheduler" common_utils_version = System.getProperty("common_utils.version", opensearch_build) job_scheduler_version = System.getProperty("job_scheduler.version", opensearch_build) - bwcVersionShort = "2.10.0" + bwcVersionShort = "1.3.2" bwcVersion = bwcVersionShort + ".0" bwcOpenSearchADDownload = 'https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/' + bwcVersionShort + '/latest/linux/x64/tar/builds/' + 'opensearch/plugins/opensearch-anomaly-detection-' + bwcVersion + '.zip' @@ -125,10 +125,10 @@ dependencies { implementation group: 'com.yahoo.datasketches', name: 'sketches-core', version: '0.13.4' implementation group: 'com.yahoo.datasketches', name: 'memory', version: '0.12.2' implementation group: 'commons-lang', name: 'commons-lang', version: '2.6' - implementation group: 'org.apache.commons', name: 'commons-pool2', version: '2.11.1' - implementation 'software.amazon.randomcutforest:randomcutforest-serialization:3.8.0' - implementation 'software.amazon.randomcutforest:randomcutforest-parkservices:3.8.0' - implementation 'software.amazon.randomcutforest:randomcutforest-core:3.8.0' + implementation group: 'org.apache.commons', name: 'commons-pool2', version: '2.12.0' + implementation 'software.amazon.randomcutforest:randomcutforest-serialization:4.0.0' + implementation 'software.amazon.randomcutforest:randomcutforest-parkservices:4.0.0' + implementation 'software.amazon.randomcutforest:randomcutforest-core:4.0.0' // we inherit jackson-core from opensearch core implementation "com.fasterxml.jackson.core:jackson-databind:2.16.1" @@ -149,6 +149,9 @@ dependencies { exclude group: 'org.ow2.asm', module: 'asm-tree' } + // used for output encoding of config descriptions + implementation group: 'org.owasp.encoder' , name: 'encoder', version: '1.2.3' + testImplementation group: 'pl.pragmatists', name: 'JUnitParams', version: '1.1.1' testImplementation group: 'org.mockito', name: 'mockito-core', version: '5.9.0' testImplementation group: 'org.objenesis', name: 'objenesis', version: '3.3' @@ -538,7 +541,7 @@ List> plugins = [ // Creates 2 test clusters with 3 nodes of the old version. 2.times {i -> - task "${baseName}#oldVersionClusterTask$i"(type: StandaloneRestIntegTestTask) { + task "${baseName}#oldVersionClusterTask$i"(type: RestIntegTestTask) { useCluster testClusters."${baseName}$i" filter { includeTestsMatching "org.opensearch.ad.bwc.*IT" @@ -554,7 +557,7 @@ List> plugins = [ // Upgrades one node of the old cluster to new OpenSearch version with upgraded plugin version // This results in a mixed cluster with 2 nodes on the old version and 1 upgraded node. // This is also used as a one third upgraded cluster for a rolling upgrade. -task "${baseName}#mixedClusterTask"(type: StandaloneRestIntegTestTask) { +task "${baseName}#mixedClusterTask"(type: RestIntegTestTask) { useCluster testClusters."${baseName}0" dependsOn "${baseName}#oldVersionClusterTask0" doFirst { @@ -573,7 +576,7 @@ task "${baseName}#mixedClusterTask"(type: StandaloneRestIntegTestTask) { // Upgrades the second node to new OpenSearch version with upgraded plugin version after the first node is upgraded. // This results in a mixed cluster with 1 node on the old version and 2 upgraded nodes. // This is used for rolling upgrade. -task "${baseName}#twoThirdsUpgradedClusterTask"(type: StandaloneRestIntegTestTask) { +task "${baseName}#twoThirdsUpgradedClusterTask"(type: RestIntegTestTask) { dependsOn "${baseName}#mixedClusterTask" useCluster testClusters."${baseName}0" doFirst { @@ -592,7 +595,7 @@ task "${baseName}#twoThirdsUpgradedClusterTask"(type: StandaloneRestIntegTestTas // Upgrades the third node to new OpenSearch version with upgraded plugin version after the second node is upgraded. // This results in a fully upgraded cluster. // This is used for rolling upgrade. -task "${baseName}#rollingUpgradeClusterTask"(type: StandaloneRestIntegTestTask) { +task "${baseName}#rollingUpgradeClusterTask"(type: RestIntegTestTask) { dependsOn "${baseName}#twoThirdsUpgradedClusterTask" useCluster testClusters."${baseName}0" doFirst { @@ -611,7 +614,7 @@ task "${baseName}#rollingUpgradeClusterTask"(type: StandaloneRestIntegTestTask) // Upgrades all the nodes of the old cluster to new OpenSearch version with upgraded plugin version // at the same time resulting in a fully upgraded cluster. -task "${baseName}#fullRestartClusterTask"(type: StandaloneRestIntegTestTask) { +task "${baseName}#fullRestartClusterTask"(type: RestIntegTestTask) { dependsOn "${baseName}#oldVersionClusterTask1" useCluster testClusters."${baseName}1" doFirst { @@ -627,7 +630,7 @@ task "${baseName}#fullRestartClusterTask"(type: StandaloneRestIntegTestTask) { } // A bwc test suite which runs all the bwc tasks combined. -task bwcTestSuite(type: StandaloneRestIntegTestTask) { +task bwcTestSuite(type: RestIntegTestTask) { exclude '**/*Test*' exclude '**/*IT*' dependsOn tasks.named("${baseName}#mixedClusterTask") diff --git a/src/main/java/org/opensearch/ad/ml/CheckpointDao.java b/src/main/java/org/opensearch/ad/ml/CheckpointDao.java index adb097cb6..fd5fd50c5 100644 --- a/src/main/java/org/opensearch/ad/ml/CheckpointDao.java +++ b/src/main/java/org/opensearch/ad/ml/CheckpointDao.java @@ -744,7 +744,8 @@ private Optional convertToTRCF(Optional roles) { - if (roles == null) { - LOG.warn("Cannot inject empty roles in thread context"); - return; - } - if (rolesInjectorHelper == null) { - // lazy init - rolesInjectorHelper = new InjectSecurity(id, settings, tc); - } - rolesInjectorHelper.inject(user, roles); - } - - @Override - public void close() { - if (rolesInjectorHelper != null) { - rolesInjectorHelper.close(); - } - } -} diff --git a/src/test/java/org/opensearch/ad/ODFERestTestCase.java b/src/test/java/org/opensearch/ad/ODFERestTestCase.java index f57c64930..52be8a214 100644 --- a/src/test/java/org/opensearch/ad/ODFERestTestCase.java +++ b/src/test/java/org/opensearch/ad/ODFERestTestCase.java @@ -35,6 +35,7 @@ import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.message.BasicHeader; @@ -187,9 +188,8 @@ protected static void configureHttpsClient(RestClientBuilder builder, Settings s String password = Optional .ofNullable(System.getProperty("password")) .orElseThrow(() -> new RuntimeException("password is missing")); - BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - final AuthScope anyScope = new AuthScope(null, -1); - credentialsProvider.setCredentials(anyScope, new UsernamePasswordCredentials(userName, password)); + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); try { return httpClientBuilder .setDefaultCredentialsProvider(credentialsProvider) diff --git a/src/test/java/org/opensearch/ad/bwc/ADBackwardsCompatibilityIT.java b/src/test/java/org/opensearch/ad/bwc/ADBackwardsCompatibilityIT.java index c60118b88..89488f1da 100644 --- a/src/test/java/org/opensearch/ad/bwc/ADBackwardsCompatibilityIT.java +++ b/src/test/java/org/opensearch/ad/bwc/ADBackwardsCompatibilityIT.java @@ -8,6 +8,9 @@ package org.opensearch.ad.bwc; +import static org.opensearch.ad.rest.ADRestTestUtils.DetectorType.MULTI_CATEGORY_HC_DETECTOR; +import static org.opensearch.ad.rest.ADRestTestUtils.DetectorType.SINGLE_CATEGORY_HC_DETECTOR; +import static org.opensearch.ad.rest.ADRestTestUtils.DetectorType.SINGLE_ENTITY_DETECTOR; import static org.opensearch.ad.rest.ADRestTestUtils.countADResultOfDetector; import static org.opensearch.ad.rest.ADRestTestUtils.countDetectors; import static org.opensearch.ad.rest.ADRestTestUtils.createAnomalyDetector; @@ -21,9 +24,6 @@ import static org.opensearch.ad.rest.ADRestTestUtils.stopHistoricalAnalysis; import static org.opensearch.ad.rest.ADRestTestUtils.stopRealtimeJob; import static org.opensearch.ad.rest.ADRestTestUtils.waitUntilTaskDone; -import static org.opensearch.ad.rest.ADRestTestUtils.DetectorType.MULTI_CATEGORY_HC_DETECTOR; -import static org.opensearch.ad.rest.ADRestTestUtils.DetectorType.SINGLE_CATEGORY_HC_DETECTOR; -import static org.opensearch.ad.rest.ADRestTestUtils.DetectorType.SINGLE_ENTITY_DETECTOR; import static org.opensearch.timeseries.util.RestHandlerUtils.ANOMALY_DETECTOR_JOB; import static org.opensearch.timeseries.util.RestHandlerUtils.HISTORICAL_ANALYSIS_TASK; import static org.opensearch.timeseries.util.RestHandlerUtils.REALTIME_TASK; @@ -167,7 +167,7 @@ public void testBackwardsCompatibility() throws Exception { case MIXED: // TODO: We have no way to specify whether send request to old node or new node now. // Add more test later when it's possible to specify request node. - Assert.assertTrue(pluginNames.contains("opensearch-anomaly-detection")); + Assert.assertTrue(pluginNames.contains("opensearch-time-series-analytics")); Assert.assertTrue(pluginNames.contains("opensearch-job-scheduler")); // Create single entity detector and start realtime job diff --git a/src/test/java/org/opensearch/ad/ml/CheckpointDaoTests.java b/src/test/java/org/opensearch/ad/ml/CheckpointDaoTests.java index 72358af10..e0fa115a7 100644 --- a/src/test/java/org/opensearch/ad/ml/CheckpointDaoTests.java +++ b/src/test/java/org/opensearch/ad/ml/CheckpointDaoTests.java @@ -1067,27 +1067,22 @@ public void testDeserializeTRCFModel() throws Exception { coldStartData.add(sample4); coldStartData.add(sample5); - // This scores were generated with the sample data but on RCF3.0-rc1 and we are comparing them - // to the scores generated by the imported RCF3.0-rc2.1 + // This scores were generated with the sample data on RCF4.0. RCF4.0 changed implementation + // and we are seeing different rcf scores between 4.0 and 3.8. This is verified by switching + // rcf version between 3.8 and 4.0 while other code in AD unchanged. But we get different scores. List scores = new ArrayList<>(); - scores.add(4.814651669367903); - scores.add(5.566968073093689); - scores.add(5.919907610660049); - scores.add(5.770278090352401); - scores.add(5.319779117320102); - - List grade = new ArrayList<>(); - grade.add(1.0); - grade.add(0.0); - grade.add(0.0); - grade.add(0.0); - grade.add(0.0); + scores.add(5.052069275347555); + scores.add(6.117465704461799); + scores.add(6.6401649744661055); + scores.add(6.918514609476484); + scores.add(6.928318158276434); + // rcf 3.8 has a number of improvements on thresholder and predictor corrector. // We don't expect the results have the same anomaly grade. for (int i = 0; i < coldStartData.size(); i++) { forest.process(coldStartData.get(i), 0); AnomalyDescriptor descriptor = forest.process(coldStartData.get(i), 0); - assertEquals(descriptor.getRCFScore(), scores.get(i), 1e-9); + assertEquals(scores.get(i), descriptor.getRCFScore(), 1e-9); } } @@ -1133,21 +1128,22 @@ public void testDeserialize_rcf3_rc3_single_stream_model() throws Exception { coldStartData.add(sample4); coldStartData.add(sample5); - // This scores were generated with the sample data but on RCF3.0-rc1 and we are comparing them - // to the scores generated by the imported RCF3.0-rc2.1 + // This scores were generated with the sample data on RCF4.0. RCF4.0 changed implementation + // and we are seeing different rcf scores between 4.0 and 3.8. This is verified by switching + // rcf version between 3.8 and 4.0 while other code in AD unchanged. But we get different scores. List scores = new ArrayList<>(); - scores.add(3.3830441158587066); - scores.add(2.825961659490065); - scores.add(2.4685871670647384); - scores.add(2.3123460886413647); - scores.add(2.1401987653477135); + scores.add(3.678754481587072); + scores.add(3.6809634269790252); + scores.add(3.683659822587799); + scores.add(3.6852688612219646); + scores.add(3.6859330728661064); // rcf 3.8 has a number of improvements on thresholder and predictor corrector. // We don't expect the results have the same anomaly grade. for (int i = 0; i < coldStartData.size(); i++) { forest.process(coldStartData.get(i), 0); AnomalyDescriptor descriptor = forest.process(coldStartData.get(i), 0); - assertEquals(descriptor.getRCFScore(), scores.get(i), 1e-9); + assertEquals(scores.get(i), descriptor.getRCFScore(), 1e-9); } } @@ -1190,21 +1186,22 @@ public void testDeserialize_rcf3_rc3_hc_model() throws Exception { coldStartData.add(sample4); coldStartData.add(sample5); - // This scores were generated with the sample data but on RCF3.0-rc1 and we are comparing them - // to the scores generated by the imported RCF3.0-rc2.1 + // This scores were generated with the sample data but on RCF4.0 that changed implementation + // and we are seeing different rcf scores between 4.0 and 3.8. This is verified by switching + // rcf version between 3.8 and 4.0 while other code in AD unchanged. But we get different scores. List scores = new ArrayList<>(); - scores.add(1.86645896573027); - scores.add(1.8760247712797833); - scores.add(1.6809181763279901); - scores.add(1.7126716645678555); - scores.add(1.323776514074674); + scores.add(2.119532552959117); + scores.add(2.7347456872746325); + scores.add(3.066704948143919); + scores.add(3.2965580521876725); + scores.add(3.1888920146607047); // rcf 3.8 has a number of improvements on thresholder and predictor corrector. // We don't expect the results have the same anomaly grade. for (int i = 0; i < coldStartData.size(); i++) { forest.process(coldStartData.get(i), 0); AnomalyDescriptor descriptor = forest.process(coldStartData.get(i), 0); - assertEquals(descriptor.getRCFScore(), scores.get(i), 1e-9); + assertEquals(scores.get(i), descriptor.getRCFScore(), 1e-9); } } diff --git a/src/test/java/org/opensearch/ad/ml/EntityColdStarterTests.java b/src/test/java/org/opensearch/ad/ml/EntityColdStarterTests.java index 188146f69..aea14a245 100644 --- a/src/test/java/org/opensearch/ad/ml/EntityColdStarterTests.java +++ b/src/test/java/org/opensearch/ad/ml/EntityColdStarterTests.java @@ -740,7 +740,7 @@ public void testAccuracyOneMinuteIntervalNoInterpolation() throws Exception { clusterService ); - accuracyTemplate(1, 0.6f, 0.6f); + accuracyTemplate(1, 0.5f, 0.5f); } private ModelState createStateForCacheRelease() { diff --git a/src/test/java/org/opensearch/ad/ml/ThresholdingResultTests.java b/src/test/java/org/opensearch/ad/ml/ThresholdingResultTests.java index cd63f60d1..111041858 100644 --- a/src/test/java/org/opensearch/ad/ml/ThresholdingResultTests.java +++ b/src/test/java/org/opensearch/ad/ml/ThresholdingResultTests.java @@ -20,9 +20,6 @@ import junitparams.JUnitParamsRunner; import junitparams.Parameters; -import junitparams.JUnitParamsRunner; -import junitparams.Parameters; - @RunWith(JUnitParamsRunner.class) public class ThresholdingResultTests { diff --git a/src/test/java/org/opensearch/ad/rest/SecureADRestIT.java b/src/test/java/org/opensearch/ad/rest/SecureADRestIT.java index 9017ec898..5d73a89fc 100644 --- a/src/test/java/org/opensearch/ad/rest/SecureADRestIT.java +++ b/src/test/java/org/opensearch/ad/rest/SecureADRestIT.java @@ -84,8 +84,9 @@ public static String generatePassword(String username) { @Before public void setupSecureTests() throws IOException { - if (!isHttps()) + if (!isHttps()) { throw new IllegalArgumentException("Secure Tests are running but HTTPS is not set"); + } createIndexRole(indexAllAccessRole, "*"); createSearchRole(indexSearchAccessRole, "*"); String alicePassword = generatePassword(aliceUser); diff --git a/src/test/java/org/opensearch/ad/stats/ADStatsTests.java b/src/test/java/org/opensearch/ad/stats/ADStatsTests.java index 02be33aab..6db1ac5cc 100644 --- a/src/test/java/org/opensearch/ad/stats/ADStatsTests.java +++ b/src/test/java/org/opensearch/ad/stats/ADStatsTests.java @@ -48,8 +48,6 @@ import com.amazon.randomcutforest.RandomCutForest; -import com.amazon.randomcutforest.RandomCutForest; - import test.org.opensearch.ad.util.MLUtil; import test.org.opensearch.ad.util.RandomModelStateConfig; diff --git a/src/test/java/org/opensearch/ad/task/ADTaskManagerTests.java b/src/test/java/org/opensearch/ad/task/ADTaskManagerTests.java index f9df58903..c8cebcb1f 100644 --- a/src/test/java/org/opensearch/ad/task/ADTaskManagerTests.java +++ b/src/test/java/org/opensearch/ad/task/ADTaskManagerTests.java @@ -79,7 +79,6 @@ import org.opensearch.action.search.SearchResponse; import org.opensearch.action.search.ShardSearchFailure; import org.opensearch.action.update.UpdateResponse; -import org.opensearch.ad.ADUnitTestCase; import org.opensearch.ad.cluster.HashRing; import org.opensearch.ad.indices.ADIndexManagement; import org.opensearch.ad.mock.model.MockSimpleLog; @@ -89,6 +88,7 @@ import org.opensearch.ad.model.ADTaskType; import org.opensearch.ad.model.AnomalyDetector; import org.opensearch.ad.rest.handler.IndexAnomalyDetectorJobActionHandler; +import org.opensearch.ad.settings.AnomalyDetectorSettings; import org.opensearch.ad.stats.InternalStatNames; import org.opensearch.ad.transport.ADStatsNodeResponse; import org.opensearch.ad.transport.ADStatsNodesResponse; @@ -120,6 +120,7 @@ import org.opensearch.search.aggregations.InternalAggregations; import org.opensearch.search.internal.InternalSearchResponse; import org.opensearch.threadpool.ThreadPool; +import org.opensearch.timeseries.AbstractTimeSeriesTest; import org.opensearch.timeseries.TestHelpers; import org.opensearch.timeseries.common.exception.DuplicateTaskException; import org.opensearch.timeseries.constant.CommonName; @@ -139,7 +140,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -public class ADTaskManagerTests extends ADUnitTestCase { +public class ADTaskManagerTests extends AbstractTimeSeriesTest { private Settings settings; private Client client; @@ -1447,10 +1448,22 @@ public void testForwardRequestToLeadNodeWithNotExistingNode() throws IOException @SuppressWarnings("unchecked") public void testScaleTaskLaneOnCoordinatingNode() { ADTask adTask = mock(ADTask.class); - when(adTask.getCoordinatingNode()).thenReturn(node1.getId()); - when(nodeFilter.getEligibleDataNodes()).thenReturn(new DiscoveryNode[] { node1, node2 }); - ActionListener listener = mock(ActionListener.class); - adTaskManager.scaleTaskLaneOnCoordinatingNode(adTask, 2, transportService, listener); + try { + // bring up real transport service as mockito cannot mock final method + // and transportService.sendRequest is called. A lot of null pointer + // exception will be thrown if we use mocked transport service. + setUpThreadPool(ADTaskManagerTests.class.getSimpleName()); + setupTestNodes(AnomalyDetectorSettings.AD_MAX_ENTITIES_PER_QUERY, AnomalyDetectorSettings.AD_PAGE_SIZE); + when(adTask.getCoordinatingNode()).thenReturn(testNodes[1].getNodeId()); + when(nodeFilter.getEligibleDataNodes()) + .thenReturn(new DiscoveryNode[] { testNodes[0].discoveryNode(), testNodes[1].discoveryNode() }); + ActionListener listener = mock(ActionListener.class); + + adTaskManager.scaleTaskLaneOnCoordinatingNode(adTask, 2, testNodes[1].transportService, listener); + } finally { + tearDownTestNodes(); + tearDownThreadPool(); + } } @SuppressWarnings("unchecked") diff --git a/src/test/java/org/opensearch/ad/transport/AnomalyDetectorJobTransportActionTests.java b/src/test/java/org/opensearch/ad/transport/AnomalyDetectorJobTransportActionTests.java index 6f7629039..75d76841b 100644 --- a/src/test/java/org/opensearch/ad/transport/AnomalyDetectorJobTransportActionTests.java +++ b/src/test/java/org/opensearch/ad/transport/AnomalyDetectorJobTransportActionTests.java @@ -171,11 +171,11 @@ public void testStartHistoricalAnalysisForSingleCategoryHCWithUser() throws IOEx waitUntil(() -> { try { ADTask task = getADTask(response.getId()); - return !TestHelpers.HISTORICAL_ANALYSIS_RUNNING_STATS.contains(task.getState()); + return HISTORICAL_ANALYSIS_FINISHED_FAILED_STATS.contains(task.getState()); } catch (IOException e) { return false; } - }, 20, TimeUnit.SECONDS); + }, 60, TimeUnit.SECONDS); ADTask adTask = getADTask(response.getId()); assertEquals(ADTaskType.HISTORICAL_HC_DETECTOR.toString(), adTask.getTaskType()); assertTrue(HISTORICAL_ANALYSIS_FINISHED_FAILED_STATS.contains(adTask.getState())); diff --git a/src/test/java/org/opensearch/timeseries/AbstractTimeSeriesTest.java b/src/test/java/org/opensearch/timeseries/AbstractTimeSeriesTest.java index d625971bf..dcc80b282 100644 --- a/src/test/java/org/opensearch/timeseries/AbstractTimeSeriesTest.java +++ b/src/test/java/org/opensearch/timeseries/AbstractTimeSeriesTest.java @@ -16,7 +16,10 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.opensearch.cluster.node.DiscoveryNodeRole.BUILT_IN_ROLES; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -30,6 +33,8 @@ import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.logging.log4j.Level; @@ -40,6 +45,9 @@ import org.apache.logging.log4j.core.config.Property; import org.apache.logging.log4j.core.layout.PatternLayout; import org.apache.logging.log4j.util.StackLocatorUtil; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.MockitoAnnotations; import org.opensearch.Version; import org.opensearch.action.support.PlainActionFuture; import org.opensearch.ad.model.AnomalyDetector; @@ -47,11 +55,14 @@ import org.opensearch.ad.model.DetectorInternalState; import org.opensearch.cluster.metadata.AliasMetadata; import org.opensearch.cluster.metadata.IndexMetadata; +import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.common.logging.Loggers; +import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Settings; import org.opensearch.core.action.ActionResponse; import org.opensearch.core.common.bytes.BytesReference; +import org.opensearch.core.common.transport.TransportAddress; import org.opensearch.core.rest.RestStatus; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.http.HttpRequest; @@ -67,10 +78,22 @@ import org.opensearch.transport.TransportInterceptor; import org.opensearch.transport.TransportService; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Sets; + import test.org.opensearch.ad.util.FakeNode; public class AbstractTimeSeriesTest extends OpenSearchTestCase { + @Captor + protected ArgumentCaptor exceptionCaptor; + + @Override + public void setUp() throws Exception { + super.setUp(); + MockitoAnnotations.initMocks(this); + } + protected static final Logger LOG = (Logger) LogManager.getLogger(AbstractTimeSeriesTest.class); // transport test node @@ -452,4 +475,39 @@ protected void setUpADThreadPool(ThreadPool mockThreadPool) { return null; }).when(executorService).execute(any(Runnable.class)); } + + /** + * Create cluster setting. + * + * @param settings cluster settings + * @param setting add setting if the code to be tested contains setting update consumer + * @return instance of ClusterSettings + */ + public ClusterSettings clusterSetting(Settings settings, Setting... setting) { + final Set> settingsSet = Stream + .concat(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS.stream(), Sets.newHashSet(setting).stream()) + .collect(Collectors.toSet()); + ClusterSettings clusterSettings = new ClusterSettings(settings, settingsSet); + return clusterSettings; + } + + protected DiscoveryNode createNode(String nodeId) { + return new DiscoveryNode( + nodeId, + new TransportAddress(TransportAddress.META_ADDRESS, 9300), + ImmutableMap.of(), + BUILT_IN_ROLES, + Version.CURRENT + ); + } + + protected DiscoveryNode createNode(String nodeId, String ip, int port, Map attributes) throws UnknownHostException { + return new DiscoveryNode( + nodeId, + new TransportAddress(InetAddress.getByName(ip), port), + attributes, + BUILT_IN_ROLES, + Version.CURRENT + ); + } } diff --git a/src/test/java/org/opensearch/timeseries/TestHelpers.java b/src/test/java/org/opensearch/timeseries/TestHelpers.java index 65b6898e0..b3ba38389 100644 --- a/src/test/java/org/opensearch/timeseries/TestHelpers.java +++ b/src/test/java/org/opensearch/timeseries/TestHelpers.java @@ -18,14 +18,7 @@ import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; import static org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; import static org.opensearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO; -import static org.opensearch.test.OpenSearchTestCase.buildNewFakeTransportAddress; -import static org.opensearch.test.OpenSearchTestCase.randomAlphaOfLength; -import static org.opensearch.test.OpenSearchTestCase.randomBoolean; -import static org.opensearch.test.OpenSearchTestCase.randomDouble; -import static org.opensearch.test.OpenSearchTestCase.randomDoubleBetween; -import static org.opensearch.test.OpenSearchTestCase.randomInt; -import static org.opensearch.test.OpenSearchTestCase.randomIntBetween; -import static org.opensearch.test.OpenSearchTestCase.randomLong; +import static org.opensearch.test.OpenSearchTestCase.*; import java.io.IOException; import java.nio.ByteBuffer; diff --git a/src/test/resources/security/sample.pem b/src/test/resources/security/sample.pem index a1fc20a77..b690a603d 100644 --- a/src/test/resources/security/sample.pem +++ b/src/test/resources/security/sample.pem @@ -1,9 +1,9 @@ -----BEGIN CERTIFICATE----- -MIIEPDCCAySgAwIBAgIUZjrlDPP8azRDPZchA/XEsx0X2iIwDQYJKoZIhvcNAQEL +MIIEPDCCAySgAwIBAgIUaYSlET3nzsotWTrWueVPPh10yLYwDQYJKoZIhvcNAQEL BQAwgY8xEzARBgoJkiaJk/IsZAEZFgNjb20xFzAVBgoJkiaJk/IsZAEZFgdleGFt cGxlMRkwFwYDVQQKDBBFeGFtcGxlIENvbSBJbmMuMSEwHwYDVQQLDBhFeGFtcGxl IENvbSBJbmMuIFJvb3QgQ0ExITAfBgNVBAMMGEV4YW1wbGUgQ29tIEluYy4gUm9v -dCBDQTAeFw0yMzA4MjkwNDIzMTJaFw0zMzA4MjYwNDIzMTJaMFcxCzAJBgNVBAYT +dCBDQTAeFw0yNDAyMjAxNzAzMjVaFw0zNDAyMTcxNzAzMjVaMFcxCzAJBgNVBAYT AmRlMQ0wCwYDVQQHDAR0ZXN0MQ0wCwYDVQQKDARub2RlMQ0wCwYDVQQLDARub2Rl MRswGQYDVQQDDBJub2RlLTAuZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUA A4IBDwAwggEKAoIBAQCm93kXteDQHMAvbUPNPW5pyRHKDD42XGWSgq0k1D29C/Ud @@ -16,10 +16,10 @@ BEAwPogFKgMEBQWCEm5vZGUtMC5leGFtcGxlLmNvbYIJbG9jYWxob3N0hxAAAAAA AAAAAAAAAAAAAAABhwR/AAABMAsGA1UdDwQEAwIF4DAdBgNVHSUEFjAUBggrBgEF BQcDAQYIKwYBBQUHAwIwDAYDVR0TAQH/BAIwADAdBgNVHQ4EFgQU0/qDQaY10jIo wCjLUpz/HfQXyt8wHwYDVR0jBBgwFoAUF4ffoFrrZhKn1dD4uhJFPLcrAJwwDQYJ -KoZIhvcNAQELBQADggEBAD2hkndVih6TWxoe/oOW0i2Bq7ScNO/n7/yHWL04HJmR -MaHv/Xjc8zLFLgHuHaRvC02ikWIJyQf5xJt0Oqu2GVbqXH9PBGKuEP2kCsRRyU27 -zTclAzfQhqmKBTYQ/3lJ3GhRQvXIdYTe+t4aq78TCawp1nSN+vdH/1geG6QjMn5N -1FU8tovDd4x8Ib/0dv8RJx+n9gytI8n/giIaDCEbfLLpe4EkV5e5UNpOnRgJjjuy -vtZutc81TQnzBtkS9XuulovDE0qI+jQrKkKu8xgGLhgH0zxnPkKtUg2I3Aq6zl1L -zYkEOUF8Y25J6WeY88Yfnc0iigI+Pnz5NK8R9GL7TYo= +KoZIhvcNAQELBQADggEBAGbij5WyF0dKhQodQfTiFDb73ygU6IyeJkFSnxF67gDz +pQJZKFvXuVBa3cGP5e7Qp3TK50N+blXGH0xXeIV9lXeYUk4hVfBlp9LclZGX8tGi +7Xa2enMvIt5q/Yg3Hh755ZxnDYxCoGkNOXUmnMusKstE0YzvZ5Gv6fcRKFBUgZLh +hUBqIEAYly1EqH/y45APiRt3Nor1yF6zEI4TnL0yNrHw6LyQkUNCHIGMJLfnJQ9L +camMGIXOx60kXNMTigF9oXXwixWAnDM9y3QT8QXA7hej/4zkbO+vIeV/7lGUdkyg +PAi92EvyxmsliEMyMR0VINl8emyobvfwa7oMeWMR+hg= -----END CERTIFICATE----- diff --git a/src/test/resources/security/test-kirk.jks b/src/test/resources/security/test-kirk.jks index 6dbc51e71..6c8c5ef77 100644 Binary files a/src/test/resources/security/test-kirk.jks and b/src/test/resources/security/test-kirk.jks differ