Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Changes in invalid input handling #375

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.opensearch.geospatial.exceptions.ConcurrentModificationException;
import org.opensearch.geospatial.exceptions.IncompatibleDatasourceException;
import org.opensearch.geospatial.ip2geo.common.DatasourceManifest;
import org.opensearch.geospatial.ip2geo.common.DatasourceState;
import org.opensearch.geospatial.ip2geo.common.Ip2GeoLockService;
import org.opensearch.geospatial.ip2geo.dao.DatasourceDao;
import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource;
Expand Down Expand Up @@ -94,6 +95,11 @@ protected void doExecute(final Task task, final UpdateDatasourceRequest request,
if (datasource == null) {
throw new ResourceNotFoundException("no such datasource exist");
}
if (DatasourceState.AVAILABLE.equals(datasource.getState()) == false) {
throw new IllegalArgumentException(
String.format(Locale.ROOT, "data source is not in an [%s] state", DatasourceState.AVAILABLE)
);
}
validate(request, datasource);
updateIfChanged(request, datasource);
lockService.releaseLock(lock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
Expand Down Expand Up @@ -192,7 +193,9 @@ protected CSVParser internalGetDatabaseReader(final DatasourceManifest manifest,
}
return new CSVParser(new BufferedReader(new InputStreamReader(zipIn)), CSVFormat.RFC4180);
}
throw new OpenSearchException("database file [{}] does not exist in the zip file [{}]", manifest.getDbName(), manifest.getUrl());
throw new IllegalArgumentException(
String.format(Locale.ROOT, "database file [%s] does not exist in the zip file [%s]", manifest.getDbName(), manifest.getUrl())
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.geospatial.exceptions.IncompatibleDatasourceException;
import org.opensearch.geospatial.ip2geo.Ip2GeoTestCase;
import org.opensearch.geospatial.ip2geo.common.DatasourceState;
import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource;
import org.opensearch.geospatial.ip2geo.jobscheduler.DatasourceTask;
import org.opensearch.jobscheduler.spi.LockModel;
Expand Down Expand Up @@ -87,6 +88,7 @@ private void validateDoExecuteWithLockError(final Exception exception) {
@SneakyThrows
public void testDoExecute_whenValidInput_thenUpdate() {
Datasource datasource = randomDatasource(Instant.now().minusSeconds(60));
datasource.setState(DatasourceState.AVAILABLE);
datasource.setTask(DatasourceTask.DELETE_UNUSED_INDICES);
Instant originalStartTime = datasource.getSchedule().getStartTime();
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
Expand Down Expand Up @@ -124,6 +126,7 @@ public void testDoExecute_whenValidInput_thenUpdate() {
@SneakyThrows
public void testDoExecute_whenNoChangesInValues_thenNoUpdate() {
Datasource datasource = randomDatasource();
datasource.setState(DatasourceState.AVAILABLE);
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
request.setEndpoint(datasource.getEndpoint());

Expand Down Expand Up @@ -177,9 +180,40 @@ public void testDoExecute_whenNoDatasource_thenError() {
verify(ip2GeoLockService).releaseLock(eq(lockModel));
}

@SneakyThrows
public void testDoExecute_whenNotInAvailableState_thenError() {
Datasource datasource = randomDatasource();
datasource.setState(DatasourceState.CREATE_FAILED);
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
request.setEndpoint(datasource.getEndpoint());

Task task = mock(Task.class);
when(datasourceDao.getDatasource(datasource.getName())).thenReturn(datasource);
ActionListener<AcknowledgedResponse> listener = mock(ActionListener.class);
LockModel lockModel = randomLockModel();

// Run
action.doExecute(task, request, listener);

// Verify
ArgumentCaptor<ActionListener<LockModel>> captor = ArgumentCaptor.forClass(ActionListener.class);
verify(ip2GeoLockService).acquireLock(eq(datasource.getName()), anyLong(), captor.capture());

// Run
captor.getValue().onResponse(lockModel);

// Verify
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
verify(listener).onFailure(exceptionCaptor.capture());
assertEquals(IllegalArgumentException.class, exceptionCaptor.getValue().getClass());
exceptionCaptor.getValue().getMessage().contains("not in an available");
verify(ip2GeoLockService).releaseLock(eq(lockModel));
}

@SneakyThrows
public void testDoExecute_whenIncompatibleFields_thenError() {
Datasource datasource = randomDatasource();
datasource.setState(DatasourceState.AVAILABLE);
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
request.setEndpoint(sampleManifestUrl());

Expand Down Expand Up @@ -211,6 +245,7 @@ public void testDoExecute_whenIncompatibleFields_thenError() {
@SneakyThrows
public void testDoExecute_whenLargeUpdateInterval_thenError() {
Datasource datasource = randomDatasource();
datasource.setState(DatasourceState.AVAILABLE);
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
request.setUpdateInterval(TimeValue.timeValueDays(datasource.getDatabase().getValidForInDays()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testGetDatabaseReaderNoFile() throws Exception {
Instant.now().toEpochMilli(),
"tester"
);
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> noOpsGeoIpDataDao.getDatabaseReader(manifest));
Exception exception = expectThrows(IllegalArgumentException.class, () -> noOpsGeoIpDataDao.getDatabaseReader(manifest));
assertTrue(exception.getMessage().contains("does not exist"));
}

Expand Down
Loading