Skip to content

Commit

Permalink
Imporve plugin Code Coverage (#616)
Browse files Browse the repository at this point in the history
Signed-off-by: Prudhvi Godithi <[email protected]>
  • Loading branch information
prudhvigodithi authored Apr 30, 2024
1 parent 5e1416b commit 84746aa
Show file tree
Hide file tree
Showing 9 changed files with 626 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
su `id -un 1000` -c "./gradlew build && ./gradlew publishToMavenLocal"
- name: Upload Coverage Report
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

Expand Down Expand Up @@ -84,7 +84,7 @@ jobs:
./gradlew publishToMavenLocal
- name: Upload Coverage Report
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

Expand Down Expand Up @@ -124,7 +124,7 @@ jobs:
./gradlew.bat publishToMavenLocal
- name: Upload Coverage Report
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ dependencies {
implementation project(path: ":${rootProject.name}-spi", configuration: 'shadow')
implementation group: 'com.google.guava', name: 'guava', version:'32.1.3-jre'
implementation group: 'com.google.guava', name: 'failureaccess', version:'1.0.2'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '5.11.0'
javaRestTestImplementation project.sourceSets.main.runtimeClasspath
//spotless
implementation('com.google.googlejavaformat:google-java-format:1.22.0') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public class JobSchedulerPlugin extends Plugin implements ActionPlugin, Extensib
public static final String JS_BASE_URI = "/_plugins/_job_scheduler";

private static final Logger log = LogManager.getLogger(JobSchedulerPlugin.class);

private JobSweeper sweeper;
private JobScheduler scheduler;
private LockService lockService;
Expand All @@ -83,6 +82,14 @@ public JobSchedulerPlugin() {
this.indexToJobProviders = new HashMap<>();
}

public Set<String> getIndicesToListen() {
return indicesToListen;
}

public Map<String, ScheduledJobProvider> getIndexToJobProviders() {
return indexToJobProviders;
}

@Override
public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) {
return Collections.singletonList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public static JobDetails parse(XContentParser parser) throws IOException {
String extensionUniqueId = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);

while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
Expand Down
190 changes: 190 additions & 0 deletions src/test/java/org/opensearch/jobscheduler/JobSchedulerPluginTests.java
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);
}
}
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());
}
}
Loading

0 comments on commit 84746aa

Please sign in to comment.