-
Notifications
You must be signed in to change notification settings - Fork 67
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
Check if url has https protocol scheme and add test #71
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
gateway-ha/src/test/java/io/trino/gateway/ha/clustermonitor/TestClusterStatsJdbcMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package io.trino.gateway.ha.clustermonitor; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.when; | ||
|
||
import io.trino.gateway.ha.config.BackendStateConfiguration; | ||
import io.trino.gateway.ha.config.ProxyBackendConfiguration; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.util.Properties; | ||
import java.util.stream.Stream; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.TestInstance.Lifecycle; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
@Slf4j | ||
@TestInstance(Lifecycle.PER_CLASS) | ||
public class TestClusterStatsJdbcMonitor { | ||
ClusterStatsJdbcMonitor clusterStatsJdbcMonitor; | ||
@Mock | ||
private Connection connection; | ||
@Mock | ||
private PreparedStatement preparedStatement; | ||
@Mock | ||
private ResultSet resultSet; | ||
|
||
private Properties properties; | ||
|
||
@BeforeEach | ||
public void initMocks() { | ||
log.info("initializing test"); | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@AfterAll | ||
public void resetMocks() { | ||
log.info("resetting mocks"); | ||
Mockito.reset(connection); | ||
Mockito.reset(preparedStatement); | ||
Mockito.reset(resultSet); | ||
} | ||
|
||
@BeforeAll | ||
public void setUp() { | ||
BackendStateConfiguration backendStateConfiguration = new BackendStateConfiguration(); | ||
backendStateConfiguration.setUsername("Trino"); | ||
properties = new Properties(); | ||
properties.setProperty("user", backendStateConfiguration.getUsername()); | ||
properties.setProperty("password", backendStateConfiguration.getPassword()); | ||
properties.setProperty("SSL", String.valueOf(backendStateConfiguration.getSsl())); | ||
clusterStatsJdbcMonitor = new ClusterStatsJdbcMonitor(backendStateConfiguration); | ||
} | ||
|
||
private static Stream<Arguments> provideProtocolAndPortAndSsl(){ | ||
return Stream.of( | ||
Arguments.of("https", "90", "jdbc:trino://trino.example.com:90/system", "true"), | ||
Arguments.of("http", "90", "jdbc:trino://trino.example.com:90/system", "false"), | ||
Arguments.of("https", "", "jdbc:trino://trino.example.com:443/system", "true"), | ||
Arguments.of("http", "", "jdbc:trino://trino.example.com:80/system", "false") | ||
); | ||
} | ||
@ParameterizedTest | ||
@MethodSource("provideProtocolAndPortAndSsl") | ||
public void testProtocolAndSsl(String scheme, String port, String expectedJdbcUrl, String expectedSsl) throws java.sql.SQLException { | ||
try(MockedStatic<DriverManager> m = Mockito.mockStatic(java.sql.DriverManager.class)) { | ||
m.when(() -> DriverManager.getConnection(anyString(), any())).thenReturn(connection); | ||
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement); | ||
when(preparedStatement.executeQuery()).thenReturn(resultSet); | ||
ProxyBackendConfiguration proxyBackend = new ProxyBackendConfiguration(); | ||
proxyBackend.setProxyTo(String.format("%s://trino.example.com:%s", scheme, port)); | ||
proxyBackend.setName("abc"); | ||
|
||
clusterStatsJdbcMonitor.monitor(proxyBackend); | ||
|
||
properties.setProperty("SSL", expectedSsl); | ||
|
||
m.verify(() -> DriverManager.getConnection(expectedJdbcUrl, properties)); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,11 +54,6 @@ | |
<artifactId>commons-lang3</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removing this as this is a dup of line 42 |
||
</dependencies> | ||
<build> | ||
<plugins> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are getting smarter about setting the port based on the protocol, we should also use the backend URL to set the SSL parameter. Currently it is set in
BackendStateConfiguration
, so if you have a mixture ofhttp
andhttps
backends it will necessarily be invalid for some of them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with SSL parameters. So I'm assuming SSL can be set depending on http or https?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep
https => SSL=true
http => SSL=false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets add that and then I think we should be good to merge this soon.