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

Removing old datasources model integ test and added unit test. #2594

Merged
merged 1 commit into from
Mar 26, 2024
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 @@ -5,6 +5,7 @@

package org.opensearch.sql.datasources.storage;

import static org.opensearch.sql.datasource.model.DataSourceStatus.ACTIVE;
import static org.opensearch.sql.datasources.storage.OpenSearchDataSourceMetadataStorage.DATASOURCE_INDEX_NAME;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -103,6 +104,39 @@ public void testGetDataSourceMetadata() {
"basicauth", dataSourceMetadata.getProperties().get("prometheus.auth.type"));
}

@SneakyThrows
@Test
public void testGetOldDataSourceMetadata() {
Mockito.when(clusterService.state().routingTable().hasIndex(DATASOURCE_INDEX_NAME))
.thenReturn(true);
Mockito.when(client.search(ArgumentMatchers.any())).thenReturn(searchResponseActionFuture);
Mockito.when(searchResponseActionFuture.actionGet()).thenReturn(searchResponse);
Mockito.when(searchResponse.status()).thenReturn(RestStatus.OK);
Mockito.when(searchResponse.getHits())
.thenReturn(
new SearchHits(
new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F));
Mockito.when(searchHit.getSourceAsString())
.thenReturn(getOldDataSourceMetadataStringWithOutStatusEnum());
Mockito.when(encryptor.decrypt("password")).thenReturn("password");
Mockito.when(encryptor.decrypt("username")).thenReturn("username");

Optional<DataSourceMetadata> dataSourceMetadataOptional =
openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME);

Assertions.assertFalse(dataSourceMetadataOptional.isEmpty());
DataSourceMetadata dataSourceMetadata = dataSourceMetadataOptional.get();
Assertions.assertEquals(TEST_DATASOURCE_INDEX_NAME, dataSourceMetadata.getName());
Assertions.assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata.getConnector());
Assertions.assertEquals(
"password", dataSourceMetadata.getProperties().get("prometheus.auth.password"));
Assertions.assertEquals(
"username", dataSourceMetadata.getProperties().get("prometheus.auth.username"));
Assertions.assertEquals(
"basicauth", dataSourceMetadata.getProperties().get("prometheus.auth.type"));
Assertions.assertEquals(ACTIVE, dataSourceMetadata.getStatus());
}

@SneakyThrows
@Test
public void testGetDataSourceMetadataWith404SearchResponse() {
Expand Down Expand Up @@ -615,6 +649,10 @@ private String getBasicDataSourceMetadataString() throws JsonProcessingException
return objectMapper.writeValueAsString(dataSourceMetadata);
}

private String getOldDataSourceMetadataStringWithOutStatusEnum() {
return "{\"name\":\"testDS\",\"description\":\"\",\"connector\":\"PROMETHEUS\",\"allowedRoles\":[\"prometheus_access\"],\"properties\":{\"prometheus.auth.password\":\"password\",\"prometheus.auth.username\":\"username\",\"prometheus.auth.uri\":\"https://localhost:9090\",\"prometheus.auth.type\":\"basicauth\"},\"resultIndex\":\"query_execution_result_testds\"}";
}

private String getAWSSigv4DataSourceMetadataString() throws JsonProcessingException {
Map<String, String> properties = new HashMap<>();
properties.put("prometheus.auth.type", "awssigv4");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import static org.opensearch.sql.datasources.utils.XContentParserUtils.DESCRIPTION_FIELD;
import static org.opensearch.sql.datasources.utils.XContentParserUtils.NAME_FIELD;
import static org.opensearch.sql.datasources.utils.XContentParserUtils.STATUS_FIELD;
import static org.opensearch.sql.legacy.TestUtils.createIndexByRestClient;
import static org.opensearch.sql.legacy.TestUtils.getResponseBody;
import static org.opensearch.sql.legacy.TestUtils.isIndexExist;
import static org.opensearch.sql.legacy.TestUtils.loadDataByRestClient;

import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
Expand Down Expand Up @@ -70,10 +67,6 @@ protected static void deleteDataSourcesCreated() throws IOException {
deleteRequest = getDeleteDataSourceRequest("patch_prometheus");
deleteResponse = client().performRequest(deleteRequest);
Assert.assertEquals(204, deleteResponse.getStatusLine().getStatusCode());

deleteRequest = getDeleteDataSourceRequest("old_prometheus");
deleteResponse = client().performRequest(deleteRequest);
Assert.assertEquals(204, deleteResponse.getStatusLine().getStatusCode());
}

@SneakyThrows
Expand Down Expand Up @@ -392,35 +385,6 @@ public void patchDataSourceAPITest() {
Assert.assertEquals("test", dataSourceMetadata.getDescription());
}

@SneakyThrows
@Test
public void testOldDataSourceModelLoadingThroughGetDataSourcesAPI() {
Index index = Index.DATASOURCES;
String indexName = index.getName();
String mapping = index.getMapping();
String dataSet = index.getDataSet();
if (!isIndexExist(client(), indexName)) {
createIndexByRestClient(client(), indexName, mapping);
}
loadDataByRestClient(client(), indexName, dataSet);
// waiting for loaded indices.
Thread.sleep(1000);
// get datasource to validate the creation.
Request getRequest = getFetchDataSourceRequest(null);
Response getResponse = client().performRequest(getRequest);
Assert.assertEquals(200, getResponse.getStatusLine().getStatusCode());
String getResponseString = getResponseBody(getResponse);
Type listType = new TypeToken<List<DataSourceMetadata>>() {}.getType();
List<DataSourceMetadata> dataSourceMetadataList =
new Gson().fromJson(getResponseString, listType);
Assert.assertTrue(
dataSourceMetadataList.stream()
.anyMatch(
dataSourceMetadata ->
dataSourceMetadata.getName().equals("old_prometheus")
&& dataSourceMetadata.getStatus().equals(ACTIVE)));
}

public DataSourceMetadata mockDataSourceMetadata(String name) {
return new DataSourceMetadata.Builder()
.setName(name)
Expand Down
Loading