-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Prudhvi Godithi <[email protected]>
- Loading branch information
1 parent
5e1416b
commit 84746aa
Showing
9 changed files
with
626 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
src/test/java/org/opensearch/jobscheduler/JobSchedulerPluginTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.jobscheduler; | ||
|
||
import org.opensearch.Version; | ||
import org.opensearch.cluster.metadata.IndexMetadata; | ||
import org.opensearch.cluster.node.DiscoveryNodes; | ||
import org.opensearch.common.settings.ClusterSettings; | ||
import org.opensearch.common.settings.IndexScopedSettings; | ||
import org.opensearch.common.settings.SettingsFilter; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.apache.lucene.tests.index.AssertingDirectoryReader; | ||
import org.junit.Before; | ||
import org.mockito.Mock; | ||
import org.opensearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.opensearch.common.settings.Setting; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.core.index.Index; | ||
import org.opensearch.env.Environment; | ||
import org.opensearch.index.IndexModule; | ||
import org.opensearch.index.IndexSettings; | ||
import org.opensearch.index.analysis.AnalysisRegistry; | ||
import org.opensearch.index.engine.EngineConfigFactory; | ||
import org.opensearch.jobscheduler.rest.action.RestGetJobDetailsAction; | ||
import org.opensearch.jobscheduler.rest.action.RestGetLockAction; | ||
import org.opensearch.jobscheduler.rest.action.RestReleaseLockAction; | ||
import org.opensearch.jobscheduler.spi.JobSchedulerExtension; | ||
import org.opensearch.jobscheduler.spi.ScheduledJobParser; | ||
import org.opensearch.jobscheduler.spi.ScheduledJobRunner; | ||
import org.opensearch.jobscheduler.utils.JobDetailsService; | ||
import org.opensearch.plugins.ExtensiblePlugin; | ||
import org.opensearch.rest.RestController; | ||
import org.opensearch.rest.RestHandler; | ||
import org.opensearch.test.IndexSettingsModule; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import org.opensearch.test.engine.MockEngineFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
|
||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class JobSchedulerPluginTests extends OpenSearchTestCase { | ||
|
||
private JobSchedulerPlugin plugin; | ||
|
||
private Index index; | ||
private Settings settings; | ||
private Settings sweeperSettings; | ||
private IndexSettings indexSettings; | ||
private IndexModule indexModule; | ||
private AnalysisRegistry emptyAnalysisRegistry; | ||
|
||
@Mock | ||
private RestController restController; | ||
@Mock | ||
private ClusterSettings clusterSettings; | ||
@Mock | ||
private IndexScopedSettings indexScopedSettings; | ||
@Mock | ||
private SettingsFilter settingsFilter; | ||
@Mock | ||
private IndexNameExpressionResolver indexNameExpressionResolver; | ||
@Mock | ||
private Supplier<DiscoveryNodes> nodesInCluster; | ||
|
||
@Before | ||
public void setup() { | ||
plugin = new JobSchedulerPlugin(); | ||
settings = Settings.builder() | ||
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.build(); | ||
indexSettings = IndexSettingsModule.newIndexSettings(JobDetailsService.JOB_DETAILS_INDEX_NAME, settings); | ||
index = indexSettings.getIndex(); | ||
final MockEngineFactory engineFactory = new MockEngineFactory(AssertingDirectoryReader.class); | ||
indexModule = new IndexModule( | ||
indexSettings, | ||
emptyAnalysisRegistry, | ||
engineFactory, | ||
new EngineConfigFactory(indexSettings), | ||
Collections.emptyMap(), | ||
() -> true, | ||
new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)), | ||
Collections.emptyMap() | ||
); | ||
sweeperSettings = Settings.builder() | ||
.put(JobSchedulerSettings.SWEEP_PERIOD.getKey(), TimeValue.timeValueMinutes(1)) | ||
.put(JobSchedulerSettings.SWEEP_PAGE_SIZE.getKey(), 10) | ||
.put(JobSchedulerSettings.SWEEP_BACKOFF_MILLIS.getKey(), TimeValue.timeValueMillis(100)) | ||
.put(JobSchedulerSettings.SWEEP_BACKOFF_RETRY_COUNT.getKey(), 5) | ||
.build(); | ||
} | ||
|
||
public void testLoadExtensions() { | ||
ExtensiblePlugin.ExtensionLoader mockLoader = mock(ExtensiblePlugin.ExtensionLoader.class); | ||
JobSchedulerExtension mockExtension1 = mock(JobSchedulerExtension.class); | ||
JobSchedulerExtension mockExtension2 = mock(JobSchedulerExtension.class); | ||
when(mockLoader.loadExtensions(JobSchedulerExtension.class)).thenReturn(Arrays.asList(mockExtension1, mockExtension2)); | ||
when(mockExtension1.getJobType()).thenReturn("jobType1"); | ||
when(mockExtension1.getJobIndex()).thenReturn("index1"); | ||
when(mockExtension2.getJobType()).thenReturn("jobType2"); | ||
when(mockExtension2.getJobIndex()).thenReturn("index2"); | ||
ScheduledJobParser mockParser = mock(ScheduledJobParser.class); | ||
ScheduledJobRunner mockRunner = mock(ScheduledJobRunner.class); | ||
when(mockExtension1.getJobParser()).thenReturn(mockParser); | ||
when(mockExtension1.getJobRunner()).thenReturn(mockRunner); | ||
plugin.loadExtensions(mockLoader); | ||
assertEquals(2, plugin.getIndexToJobProviders().size()); | ||
assertTrue(plugin.getIndicesToListen().contains("index1")); | ||
assertTrue(plugin.getIndicesToListen().contains("index2")); | ||
} | ||
|
||
public void testGetSettings_returnsSettingsList() { | ||
List<Setting<?>> settings = plugin.getSettings(); | ||
assertNotNull(settings); | ||
assertEquals(12, settings.size()); | ||
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.SWEEP_PAGE_SIZE)); | ||
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.REQUEST_TIMEOUT)); | ||
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.SWEEP_BACKOFF_MILLIS)); | ||
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.SWEEP_BACKOFF_RETRY_COUNT)); | ||
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.SWEEP_PERIOD)); | ||
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.JITTER_LIMIT)); | ||
assertTrue(settings.contains(JobSchedulerSettings.SWEEP_PAGE_SIZE)); | ||
assertTrue(settings.contains(JobSchedulerSettings.REQUEST_TIMEOUT)); | ||
assertTrue(settings.contains(JobSchedulerSettings.SWEEP_BACKOFF_MILLIS)); | ||
assertTrue(settings.contains(JobSchedulerSettings.SWEEP_BACKOFF_RETRY_COUNT)); | ||
assertTrue(settings.contains(JobSchedulerSettings.SWEEP_PERIOD)); | ||
assertTrue(settings.contains(JobSchedulerSettings.JITTER_LIMIT)); | ||
} | ||
|
||
public void testOnIndexModule() { | ||
assertEquals(indexModule.getIndex().toString(), index.toString()); | ||
assertEquals(index.getName(), JobDetailsService.JOB_DETAILS_INDEX_NAME); | ||
} | ||
|
||
public void testGetRestHandlers() { | ||
List<RestHandler> restHandlers = plugin.getRestHandlers( | ||
settings, | ||
restController, | ||
clusterSettings, | ||
indexScopedSettings, | ||
settingsFilter, | ||
indexNameExpressionResolver, | ||
nodesInCluster | ||
); | ||
assertThat( | ||
restHandlers, | ||
containsInAnyOrder( | ||
instanceOf(RestGetJobDetailsAction.class), | ||
instanceOf(RestGetLockAction.class), | ||
instanceOf(RestReleaseLockAction.class) | ||
) | ||
); | ||
} | ||
|
||
public void testGetIndicesToListen() { | ||
Set<String> expectedIndices = new HashSet<>(); | ||
expectedIndices.add("index1"); | ||
expectedIndices.add("index2"); | ||
plugin.getIndicesToListen().addAll(expectedIndices); | ||
Set<String> actualIndices = plugin.getIndicesToListen(); | ||
assertEquals(expectedIndices, actualIndices); | ||
} | ||
|
||
public void testGetIndexToJobProviders() { | ||
Map<String, ScheduledJobProvider> expectedProviders = plugin.getIndexToJobProviders(); | ||
ScheduledJobParser mockParser = mock(ScheduledJobParser.class); | ||
ScheduledJobRunner mockRunner = mock(ScheduledJobRunner.class); | ||
expectedProviders.put("index1", new ScheduledJobProvider("test-job-1", "test-job-index-1", mockParser, mockRunner)); | ||
Map<String, ScheduledJobProvider> actualProviders = plugin.getIndexToJobProviders(); | ||
assertEquals(expectedProviders, actualProviders); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/test/java/org/opensearch/jobscheduler/ScheduledJobProviderTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.jobscheduler; | ||
|
||
import org.junit.Before; | ||
import org.mockito.Mock; | ||
import org.opensearch.jobscheduler.spi.ScheduledJobParser; | ||
import org.opensearch.jobscheduler.spi.ScheduledJobRunner; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
public class ScheduledJobProviderTests extends OpenSearchTestCase { | ||
|
||
private static final String JOB_TYPE = "test_job_type"; | ||
private static final String JOB_INDEX_NAME = "test_job_index"; | ||
|
||
@Mock | ||
private ScheduledJobParser jobParser; | ||
|
||
@Mock | ||
private ScheduledJobRunner jobRunner; | ||
|
||
private ScheduledJobProvider scheduledJobProvider; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
scheduledJobProvider = new ScheduledJobProvider(JOB_TYPE, JOB_INDEX_NAME, jobParser, jobRunner); | ||
} | ||
|
||
public void testGetJobType() { | ||
assertEquals(JOB_TYPE, scheduledJobProvider.getJobType()); | ||
} | ||
|
||
public void testGetJobIndexName() { | ||
assertEquals(JOB_INDEX_NAME, scheduledJobProvider.getJobIndexName()); | ||
} | ||
|
||
public void testGetJobParser() { | ||
assertEquals(jobParser, scheduledJobProvider.getJobParser()); | ||
} | ||
|
||
public void testGetJobRunner() { | ||
assertEquals(jobRunner, scheduledJobProvider.getJobRunner()); | ||
} | ||
|
||
public void testConstructor() { | ||
ScheduledJobParser parser = mock(ScheduledJobParser.class); | ||
ScheduledJobRunner runner = mock(ScheduledJobRunner.class); | ||
ScheduledJobProvider provider = new ScheduledJobProvider(JOB_TYPE, JOB_INDEX_NAME, parser, runner); | ||
assertEquals(JOB_TYPE, provider.getJobType()); | ||
assertEquals(JOB_INDEX_NAME, provider.getJobIndexName()); | ||
assertEquals(parser, provider.getJobParser()); | ||
assertEquals(runner, provider.getJobRunner()); | ||
} | ||
} |
Oops, something went wrong.