Skip to content

Commit 1463d64

Browse files
lqiu96blakeli0
andauthored
feat: Universe Domain Environment Variable Support (#2485)
Support for reading the `GOOGLE_CLOUD_UNIVERSE_DOMAIN` env var. Since we are still on JUnit 4 and there is no dedicated support for testing Env Vars, we created a new step in the CI to test this Env Var test: ![image](https://github.com/googleapis/sdk-platform-java/assets/6621793/1649fce6-ed80-4f95-9011-d6fac8504fb2) --------- Co-authored-by: Blake Li <[email protected]>
1 parent 46c08a2 commit 1463d64

File tree

6 files changed

+119
-5
lines changed

6 files changed

+119
-5
lines changed

.github/workflows/ci.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ jobs:
2222
run: |
2323
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
2424
-Dfmt.skip -DenableTestCoverage
25+
# The `envVarTest` profile runs tests that require an environment variable
26+
- name: Env Var Tests
27+
run: |
28+
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
29+
-Dfmt.skip -DenableTestCoverage -PenvVarTest
30+
# Set the Env Var for this step only
31+
env:
32+
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
2533
- run: bazelisk version
2634
- name: Install Maven modules
2735
run: |
@@ -64,6 +72,14 @@ jobs:
6472
run: |
6573
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
6674
-Dfmt.skip -DenableTestCoverage
75+
# The `envVarTest` profile runs tests that require an environment variable
76+
- name: Env Var Tests
77+
run: |
78+
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
79+
-Dfmt.skip -DenableTestCoverage -PenvVarTest
80+
# Set the Env Var for this step only
81+
env:
82+
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
6783
- run: bazelisk version
6884
- name: Install Maven modules
6985
run: |
@@ -98,6 +114,22 @@ jobs:
98114
mvn verify --batch-mode --no-transfer-progress -Dcheckstyle.skip \
99115
-Dfmt.skip \
100116
-Djvm="${JAVA8_HOME}/bin/java"
117+
# The `envVarTest` profile runs tests that require an environment variable
118+
- name: Compile with Java 17 and run tests with Java 8 (Env Var Tests)
119+
shell: bash
120+
run: |
121+
set -x
122+
export JAVA_HOME=$JAVA_HOME
123+
export PATH=${JAVA_HOME}/bin:$PATH
124+
# Maven surefire plugin lets us to specify the JVM when running tests via
125+
# the "jvm" system property.
126+
export GOOGLE_CLOUD_UNIVERSE_DOMAIN=random.com
127+
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
128+
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
129+
-PenvVarTest
130+
# Set the Env Var for this step only
131+
env:
132+
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
101133

102134
build-java8-gapic-generator-java:
103135
name: "build(8) for gapic-generator-java"

.github/workflows/sonar.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ jobs:
4747
tar -xf showcase-*
4848
./gapic-showcase run &
4949
cd -
50+
# Intentionally do not run the Env Var Tests (no -PenvVarTests) as setting the Env Var
51+
# may alter the results for other tests that use Env Var in the logic. Adding a Sonar
52+
# step for a few tests (env var tests) may be overkill and should be better covered
53+
# when we can upgrade to JUnit 5 (https://github.com/googleapis/sdk-platform-java/issues/1611#issuecomment-1970079325)
5054
- name: Build and analyze for full test coverage
5155
env:
5256
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
@@ -59,7 +63,6 @@ jobs:
5963
-Dsonar.projectKey=googleapis_gapic-generator-java \
6064
-Dsonar.organization=googleapis \
6165
-Dsonar.host.url=https://sonarcloud.io
62-
6366
- name: Build and analyze Showcase Integration Tests Coverage
6467
env:
6568
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any

gapic-generator-java-pom-parent/pom.xml

+17
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@
177177
</plugins>
178178
</build>
179179
</profile>
180+
<profile>
181+
<id>envVarTest</id>
182+
<build>
183+
<plugins>
184+
<plugin>
185+
<groupId>org.apache.maven.plugins</groupId>
186+
<artifactId>maven-surefire-plugin</artifactId>
187+
<configuration>
188+
<!-- Unless overriden in the submodules, this profile run no tests in a submodule -->
189+
<excludes>
190+
<exclude>**/*.java</exclude>
191+
</excludes>
192+
</configuration>
193+
</plugin>
194+
</plugins>
195+
</build>
196+
</profile>
180197
</profiles>
181198
<repositories>
182199
<repository>

gax-java/gax/pom.xml

+19
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,27 @@
9999
<artifactId>maven-surefire-plugin</artifactId>
100100
<configuration>
101101
<argLine>-Djava.util.logging.SimpleFormatter.format="%1$tY %1$tl:%1$tM:%1$tS.%1$tL %2$s %4$s: %5$s%6$s%n"</argLine>
102+
<!-- These tests require an Env Var to be set. Use -PenvVarTest to ONLY run these tests -->
103+
<test>!EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet+endpointContextBuild_multipleUniverseDomainConfigurations_clientSettingsHasPriority</test>
102104
</configuration>
103105
</plugin>
104106
</plugins>
105107
</build>
108+
109+
<profiles>
110+
<profile>
111+
<id>envVarTest</id>
112+
<build>
113+
<plugins>
114+
<plugin>
115+
<groupId>org.apache.maven.plugins</groupId>
116+
<artifactId>maven-surefire-plugin</artifactId>
117+
<configuration>
118+
<test>EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet+endpointContextBuild_multipleUniverseDomainConfigurations_clientSettingsHasPriority</test>
119+
</configuration>
120+
</plugin>
121+
</plugins>
122+
</build>
123+
</profile>
124+
</profiles>
106125
</project>

gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
@InternalApi
4848
@AutoValue
4949
public abstract class EndpointContext {
50+
private static final String GOOGLE_CLOUD_UNIVERSE_DOMAIN = "GOOGLE_CLOUD_UNIVERSE_DOMAIN";
5051
private static final String INVALID_UNIVERSE_DOMAIN_ERROR_TEMPLATE =
5152
"The configured universe domain (%s) does not match the universe domain found in the credentials (%s). If you haven't configured the universe domain explicitly, `googleapis.com` is the default.";
5253
public static final String UNABLE_TO_RETRIEVE_CREDENTIALS_ERROR_MESSAGE =
@@ -201,20 +202,27 @@ public abstract static class Builder {
201202
abstract EndpointContext autoBuild();
202203

203204
private String determineUniverseDomain() {
205+
String universeDomain = universeDomain();
204206
if (usingGDCH()) {
205207
// GDC-H has no concept of Universe Domain. User should not set a custom value
206-
if (universeDomain() != null) {
208+
if (universeDomain != null) {
207209
throw new IllegalArgumentException(
208210
"Universe domain configuration is incompatible with GDC-H");
209211
}
210212
return Credentials.GOOGLE_DEFAULT_UNIVERSE;
211213
}
212214
// Check for "" (empty string)
213-
if (universeDomain() != null && universeDomain().isEmpty()) {
215+
if (universeDomain != null && universeDomain.isEmpty()) {
214216
throw new IllegalArgumentException("The universe domain value cannot be empty.");
215217
}
216-
// Override with user set universe domain if provided
217-
return universeDomain() != null ? universeDomain() : Credentials.GOOGLE_DEFAULT_UNIVERSE;
218+
// If the universe domain wasn't configured explicitly in the settings, check the
219+
// environment variable for the value
220+
if (universeDomain == null) {
221+
universeDomain = System.getenv(GOOGLE_CLOUD_UNIVERSE_DOMAIN);
222+
}
223+
// If the universe domain is configured by the user, the universe domain will either be
224+
// from the settings or from the env var. The value from ClientSettings has priority.
225+
return universeDomain != null ? universeDomain : Credentials.GOOGLE_DEFAULT_UNIVERSE;
218226
}
219227

220228
/** Determines the fully resolved endpoint and universe domain values */

gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,41 @@ public void endpointContextBuild_gdchFlow_noUniverseDomain_customEndpoint() thro
340340
.isEqualTo(Credentials.GOOGLE_DEFAULT_UNIVERSE);
341341
}
342342

343+
// This Universe Domain should match the `GOOGLE_CLOUD_UNIVERSE_DOMAIN` Env Var
344+
// For this test running locally or in CI, check that the Env Var is set properly.
345+
// This test should only run when the maven profile `EnvVarTest` is enabled.
346+
@Test
347+
public void endpointContextBuild_universeDomainEnvVarSet() throws IOException {
348+
String envVarUniverseDomain = "random.com";
349+
EndpointContext endpointContext =
350+
defaultEndpointContextBuilder
351+
.setUniverseDomain(null)
352+
.setClientSettingsEndpoint(null)
353+
.build();
354+
Truth.assertThat(endpointContext.resolvedEndpoint()).isEqualTo("test.random.com:443");
355+
Truth.assertThat(endpointContext.resolvedUniverseDomain()).isEqualTo(envVarUniverseDomain);
356+
}
357+
358+
// This Universe Domain should match the `GOOGLE_CLOUD_UNIVERSE_DOMAIN` Env Var
359+
// For this test running locally or in CI, check that the Env Var is set properly.
360+
// This test should only run when the maven profile `EnvVarTest` is enabled.
361+
@Test
362+
public void endpointContextBuild_multipleUniverseDomainConfigurations_clientSettingsHasPriority()
363+
throws IOException {
364+
// This test has `GOOGLE_CLOUD_UNIVERSE_DOMAIN` = `random.com`
365+
String clientSettingsUniverseDomain = "clientSettingsUniverseDomain.com";
366+
EndpointContext endpointContext =
367+
defaultEndpointContextBuilder
368+
.setUniverseDomain(clientSettingsUniverseDomain)
369+
.setClientSettingsEndpoint(null)
370+
.build();
371+
Truth.assertThat(endpointContext.resolvedEndpoint())
372+
.isEqualTo("test.clientSettingsUniverseDomain.com:443");
373+
// Client Settings Universe Domain (if set) takes priority
374+
Truth.assertThat(endpointContext.resolvedUniverseDomain())
375+
.isEqualTo(clientSettingsUniverseDomain);
376+
}
377+
343378
@Test
344379
public void hasValidUniverseDomain_gdchFlow_anyCredentials() throws IOException {
345380
Credentials noCredentials = NoCredentialsProvider.create().getCredentials();

0 commit comments

Comments
 (0)