Skip to content

Commit c3a84c2

Browse files
andythsuebyhr
authored andcommitted
Allow replacing test config with environment variable
1 parent a9b10e7 commit c3a84c2

File tree

8 files changed

+70
-43
lines changed

8 files changed

+70
-43
lines changed

gateway-ha/src/test/java/io/trino/gateway/ha/HaGatewayTestUtils.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package io.trino.gateway.ha;
1515

16+
import com.google.common.collect.ImmutableMap;
1617
import io.airlift.json.JsonCodec;
1718
import io.airlift.log.Logger;
1819
import io.trino.gateway.ha.clustermonitor.ClusterStats;
@@ -34,16 +35,17 @@
3435
import java.io.File;
3536
import java.io.IOException;
3637
import java.io.InputStream;
37-
import java.net.URL;
3838
import java.nio.file.Files;
3939
import java.nio.file.Path;
40+
import java.util.Map;
4041
import java.util.Scanner;
4142
import java.util.concurrent.TimeUnit;
4243

4344
import static com.google.common.base.Preconditions.checkState;
4445
import static com.google.common.net.HttpHeaders.CONTENT_ENCODING;
4546
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
4647
import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
48+
import static io.trino.gateway.ha.util.ConfigurationUtils.replaceEnvironmentVariables;
4749
import static java.lang.String.format;
4850
import static java.nio.charset.StandardCharsets.UTF_8;
4951
import static java.util.Objects.requireNonNull;
@@ -81,18 +83,23 @@ public static void seedRequiredData(String h2DbFilePath)
8183
public static File buildGatewayConfig(PostgreSQLContainer postgreSqlContainer, int routerPort, String configFile)
8284
throws Exception
8385
{
84-
URL resource = HaGatewayTestUtils.class.getClassLoader().getResource("auth/localhost.jks");
85-
String configStr =
86-
getResourceFileContent(configFile)
87-
.replace("REQUEST_ROUTER_PORT", String.valueOf(routerPort))
88-
.replace("POSTGRESQL_JDBC_URL", postgreSqlContainer.getJdbcUrl())
89-
.replace("POSTGRESQL_USER", postgreSqlContainer.getUsername())
90-
.replace("POSTGRESQL_PASSWORD", postgreSqlContainer.getPassword())
91-
.replace(
92-
"APPLICATION_CONNECTOR_PORT", String.valueOf(30000 + (int) (Math.random() * 1000)))
93-
.replace("ADMIN_CONNECTOR_PORT", String.valueOf(31000 + (int) (Math.random() * 1000)))
94-
.replace("LOCALHOST_JKS", Path.of(resource.toURI()).toString())
95-
.replace("RESOURCES_DIR", Path.of("src", "test", "resources").toAbsolutePath().toString());
86+
Map<String, String> additionalVars = ImmutableMap.<String, String>builder()
87+
.put("REQUEST_ROUTER_PORT", String.valueOf(routerPort))
88+
.putAll(buildPostgresVars(postgreSqlContainer))
89+
.buildOrThrow();
90+
return buildGatewayConfig(configFile, additionalVars);
91+
}
92+
93+
public static File buildGatewayConfig(String configFile, Map<String, String> additionalVars)
94+
throws Exception
95+
{
96+
Map<String, String> vars = ImmutableMap.<String, String>builder()
97+
.put("APPLICATION_CONNECTOR_PORT", String.valueOf(30000 + (int) (Math.random() * 1000)))
98+
.put("ADMIN_CONNECTOR_PORT", String.valueOf(31000 + (int) (Math.random() * 1000)))
99+
.put("RESOURCES_DIR", Path.of("src", "test", "resources").toAbsolutePath().toString())
100+
.putAll(additionalVars)
101+
.buildOrThrow();
102+
String configStr = replaceEnvironmentVariables(getResourceFileContent(configFile), vars);
96103

97104
File target = File.createTempFile("config-" + System.currentTimeMillis(), "config.yaml");
98105

@@ -104,6 +111,15 @@ public static File buildGatewayConfig(PostgreSQLContainer postgreSqlContainer, i
104111
return target;
105112
}
106113

114+
public static Map<String, String> buildPostgresVars(PostgreSQLContainer<?> postgresql)
115+
{
116+
return ImmutableMap.<String, String>builder()
117+
.put("POSTGRESQL_JDBC_URL", postgresql.getJdbcUrl())
118+
.put("POSTGRESQL_USER", postgresql.getUsername())
119+
.put("POSTGRESQL_PASSWORD", postgresql.getPassword())
120+
.buildOrThrow();
121+
}
122+
107123
public static String getResourceFileContent(String fileName)
108124
{
109125
StringBuilder sb = new StringBuilder();

gateway-ha/src/test/java/io/trino/gateway/ha/security/TestOIDC.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.fasterxml.jackson.core.JsonProcessingException;
1717
import com.fasterxml.jackson.databind.JsonNode;
1818
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import com.google.common.collect.ImmutableMap;
1920
import io.trino.gateway.ha.HaGatewayLauncher;
2021
import io.trino.gateway.ha.HaGatewayTestUtils;
2122
import okhttp3.Cookie;
@@ -44,11 +45,15 @@
4445
import java.io.File;
4546
import java.net.CookieManager;
4647
import java.net.CookiePolicy;
48+
import java.net.URL;
49+
import java.nio.file.Path;
4750
import java.security.SecureRandom;
4851
import java.security.cert.X509Certificate;
4952
import java.util.List;
53+
import java.util.Map;
5054
import java.util.Optional;
5155

56+
import static io.trino.gateway.ha.HaGatewayTestUtils.buildPostgresVars;
5257
import static io.trino.gateway.ha.security.OidcCookie.OIDC_COOKIE;
5358
import static java.lang.String.format;
5459
import static org.assertj.core.api.Assertions.assertThat;
@@ -144,8 +149,14 @@ void setup()
144149
PostgreSQLContainer gatewayBackendDatabase = new PostgreSQLContainer("postgres:16");
145150
gatewayBackendDatabase.start();
146151

152+
URL resource = HaGatewayTestUtils.class.getClassLoader().getResource("auth/localhost.jks");
153+
Map<String, String> additionalVars = ImmutableMap.<String, String>builder()
154+
.put("REQUEST_ROUTER_PORT", String.valueOf(ROUTER_PORT))
155+
.put("LOCALHOST_JKS", Path.of(resource.toURI()).toString())
156+
.putAll(buildPostgresVars(gatewayBackendDatabase))
157+
.buildOrThrow();
147158
File testConfigFile =
148-
HaGatewayTestUtils.buildGatewayConfig(gatewayBackendDatabase, ROUTER_PORT, "auth/oauth-test-config.yml");
159+
HaGatewayTestUtils.buildGatewayConfig("auth/oauth-test-config.yml", additionalVars);
149160
String[] args = {testConfigFile.getAbsolutePath()};
150161
System.out.println(ROUTER_PORT);
151162
HaGatewayLauncher.main(args);

gateway-ha/src/test/resources/auth/auth-test-config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
serverConfig:
22
node.environment: test
3-
http-server.http.port: REQUEST_ROUTER_PORT
3+
http-server.http.port: ${ENV:REQUEST_ROUTER_PORT}
44

55
dataStore:
6-
jdbcUrl: POSTGRESQL_JDBC_URL
7-
user: POSTGRESQL_USER
8-
password: POSTGRESQL_PASSWORD
6+
jdbcUrl: ${ENV:POSTGRESQL_JDBC_URL}
7+
user: ${ENV:POSTGRESQL_USER}
8+
password: ${ENV:POSTGRESQL_PASSWORD}
99
driver: org.postgresql.Driver
1010

1111
extraWhitelistPaths:

gateway-ha/src/test/resources/auth/oauth-test-config.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ serverConfig:
22
node.environment: test
33
http-server.http.port: 8080
44
http-server.https.enabled: true
5-
http-server.https.port: REQUEST_ROUTER_PORT
6-
http-server.https.keystore.path: LOCALHOST_JKS
5+
http-server.https.port: ${ENV:REQUEST_ROUTER_PORT}
6+
http-server.https.keystore.path: ${ENV:LOCALHOST_JKS}
77
http-server.https.keystore.key: 123456
88

99
dataStore:
10-
jdbcUrl: POSTGRESQL_JDBC_URL
11-
user: POSTGRESQL_USER
12-
password: POSTGRESQL_PASSWORD
10+
jdbcUrl: ${ENV:POSTGRESQL_JDBC_URL}
11+
user: ${ENV:POSTGRESQL_USER}
12+
password: ${ENV:POSTGRESQL_PASSWORD}
1313
driver: org.postgresql.Driver
1414

1515
extraWhitelistPaths:
@@ -33,8 +33,8 @@ authentication:
3333
tokenEndpoint: http://localhost:4444/oauth2/token
3434
authorizationEndpoint: http://localhost:4444/oauth2/auth
3535
jwkEndpoint: http://localhost:4444/.well-known/jwks.json
36-
redirectUrl: https://localhost:REQUEST_ROUTER_PORT/oidc/callback
37-
redirectWebUrl: https://localhost:REQUEST_ROUTER_PORT/
36+
redirectUrl: https://localhost:${ENV:REQUEST_ROUTER_PORT}/oidc/callback
37+
redirectWebUrl: https://localhost:${ENV:REQUEST_ROUTER_PORT}/
3838
userIdField: sub
3939
scopes:
4040
- openid

gateway-ha/src/test/resources/test-config-template.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
serverConfig:
22
node.environment: test
3-
http-server.http.port: REQUEST_ROUTER_PORT
3+
http-server.http.port: ${ENV:REQUEST_ROUTER_PORT}
44

55
includeClusterHostInResponse: true
66
dataStore:
7-
jdbcUrl: POSTGRESQL_JDBC_URL
8-
user: POSTGRESQL_USER
9-
password: POSTGRESQL_PASSWORD
7+
jdbcUrl: ${ENV:POSTGRESQL_JDBC_URL}
8+
user: ${ENV:POSTGRESQL_USER}
9+
password: ${ENV:POSTGRESQL_PASSWORD}
1010
driver: org.postgresql.Driver
1111

1212
clusterStatsConfiguration:

gateway-ha/src/test/resources/test-config-with-routing-rules-api.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
serverConfig:
22
node.environment: test
3-
http-server.http.port: REQUEST_ROUTER_PORT
3+
http-server.http.port: ${ENV:REQUEST_ROUTER_PORT}
44

55
dataStore:
6-
jdbcUrl: POSTGRESQL_JDBC_URL
7-
user: POSTGRESQL_USER
8-
password: POSTGRESQL_PASSWORD
6+
jdbcUrl: ${ENV:POSTGRESQL_JDBC_URL}
7+
user: ${ENV:POSTGRESQL_USER}
8+
password: ${ENV:POSTGRESQL_PASSWORD}
99
driver: org.postgresql.Driver
1010

1111
clusterStatsConfiguration:
@@ -35,4 +35,4 @@ uiConfiguration:
3535

3636
routingRules:
3737
rulesEngineEnabled: true
38-
rulesConfigPath: "RESOURCES_DIR/rules/routing_rules_update.yml"
38+
rulesConfigPath: "${ENV:RESOURCES_DIR}/rules/routing_rules_update.yml"

gateway-ha/src/test/resources/test-config-with-routing-template.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
serverConfig:
22
node.environment: test
3-
http-server.http.port: REQUEST_ROUTER_PORT
3+
http-server.http.port: ${ENV:REQUEST_ROUTER_PORT}
44

55
dataStore:
6-
jdbcUrl: POSTGRESQL_JDBC_URL
7-
user: POSTGRESQL_USER
8-
password: POSTGRESQL_PASSWORD
6+
jdbcUrl: ${ENV:POSTGRESQL_JDBC_URL}
7+
user: ${ENV:POSTGRESQL_USER}
8+
password: ${ENV:POSTGRESQL_PASSWORD}
99
driver: org.postgresql.Driver
1010

1111
clusterStatsConfiguration:
@@ -31,4 +31,4 @@ requestAnalyzerConfig:
3131

3232
routingRules:
3333
rulesEngineEnabled: true
34-
rulesConfigPath: "RESOURCES_DIR/rules/routing_rules_trino_query_properties.yml"
34+
rulesConfigPath: "${ENV:RESOURCES_DIR}/rules/routing_rules_trino_query_properties.yml"

gateway-ha/src/test/resources/test-config-without-x-forwarded-template.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
serverConfig:
22
node.environment: test
3-
http-server.http.port: REQUEST_ROUTER_PORT
3+
http-server.http.port: ${ENV:REQUEST_ROUTER_PORT}
44

55
dataStore:
6-
jdbcUrl: POSTGRESQL_JDBC_URL
7-
user: POSTGRESQL_USER
8-
password: POSTGRESQL_PASSWORD
6+
jdbcUrl: ${ENV:POSTGRESQL_JDBC_URL}
7+
user: ${ENV:POSTGRESQL_USER}
8+
password: ${ENV:POSTGRESQL_PASSWORD}
99
driver: org.postgresql.Driver
1010

1111
clusterStatsConfiguration:

0 commit comments

Comments
 (0)