Skip to content

Commit

Permalink
Removing old datasources model test (#2594)
Browse files Browse the repository at this point in the history
Signed-off-by: Vamsi Manohar <reddyvam@amazon.com>
(cherry picked from commit e153609)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Mar 26, 2024
1 parent 4d54078 commit 8135111
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
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

0 comments on commit 8135111

Please sign in to comment.