Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: vamsi-amazon <reddyvam@amazon.com>
  • Loading branch information
vmmusings committed Apr 10, 2023
1 parent a9111a9 commit 6a6542b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/sql-test-and-build-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
distribution: 'temurin'
java-version: ${{ matrix.entry.java }}

- name: Setup tmate session
uses: mxschmitt/action-tmate@v3

- name: Build with Gradle
run: ./gradlew --continue build ${{ matrix.entry.os_build_args }}

Expand Down
9 changes: 5 additions & 4 deletions datasources/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ jacocoTestCoverageVerification {
rule {
element = 'CLASS'
excludes = [
'org.opensearch.sql.datasources.settings.DataSourceSettings',
'org.opensearch.sql.datasources.exceptions.*',
'org.opensearch.sql.datasources.rest.*'
'org.opensearch.sql.datasources.settings.DataSourceSettings',
'org.opensearch.sql.datasources.exceptions.*',
'org.opensearch.sql.datasources.model.*',
'org.opensearch.sql.datasources.rest.*'
]
limit {
counter = 'LINE'
minimum = 1.0
}
limit {
counter = 'BRANCH'
minimum = 1.0
minimum = 0.9
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,41 @@ public void testGetDataSourceMetadataWithAWSSigV4() {
dataSourceMetadata.getProperties().get("prometheus.auth.type"));
}

@SneakyThrows
@Test
public void testGetDataSourceMetadataWithBasicAuth() {
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(getDataSourceMetadataStringWithBasicAuthentication());
Mockito.when(encryptor.decrypt("username")).thenReturn("username");
Mockito.when(encryptor.decrypt("password")).thenReturn("password");

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("username",
dataSourceMetadata.getProperties().get("prometheus.auth.username"));
Assertions.assertEquals("password",
dataSourceMetadata.getProperties().get("prometheus.auth.password"));
Assertions.assertEquals("basicauth",
dataSourceMetadata.getProperties().get("prometheus.auth.type"));
}


@SneakyThrows
@Test
Expand Down Expand Up @@ -590,6 +625,22 @@ private String getAWSSigv4DataSourceMetadataString() throws JsonProcessingExcept
return objectMapper.writeValueAsString(dataSourceMetadata);
}

private String getDataSourceMetadataStringWithBasicAuthentication()
throws JsonProcessingException {
DataSourceMetadata dataSourceMetadata = new DataSourceMetadata();
dataSourceMetadata.setName("testDS");
dataSourceMetadata.setConnector(DataSourceType.PROMETHEUS);
dataSourceMetadata.setAllowedRoles(Collections.singletonList("prometheus_access"));
Map<String, String> properties = new HashMap<>();
properties.put("prometheus.auth.uri", "https://localhost:9090");
properties.put("prometheus.auth.type", "basicauth");
properties.put("prometheus.auth.username", "username");
properties.put("prometheus.auth.password", "password");
dataSourceMetadata.setProperties(properties);
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(dataSourceMetadata);
}

private String getDataSourceMetadataStringWithNoAuthentication() throws JsonProcessingException {
DataSourceMetadata dataSourceMetadata = new DataSourceMetadata();
dataSourceMetadata.setName("testDS");
Expand Down
3 changes: 1 addition & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ include 'legacy'
include 'sql'
include 'prometheus'
include 'benchmarks'
include 'datasources'

include 'datasources'

0 comments on commit 6a6542b

Please sign in to comment.