Skip to content

Commit

Permalink
chore: Use Env Var junit tool
Browse files Browse the repository at this point in the history
  • Loading branch information
lqiu96 committed May 23, 2024
1 parent be5dbbc commit 9bbb706
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
10 changes: 10 additions & 0 deletions gax-java/gax-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -776,13 +776,6 @@ public Builder setAttemptDirectPathXds() {
return this;
}

/** Used by tests to configure the `DIRECT_PATH_ENV_DISABLE_DIRECT_PATH` Env Var */
@VisibleForTesting
Builder setEnvProvider(EnvironmentProvider envProvider) {
this.envProvider = envProvider;
return this;
}

@VisibleForTesting
Builder setSystemProductName(String systemProductName) {
this.systemProductName = systemProductName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.TransportChannel;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.gax.rpc.internal.EnvironmentProvider;
import com.google.api.gax.rpc.mtls.AbstractMtlsTransportChannelTest;
import com.google.api.gax.rpc.mtls.MtlsProvider;
import com.google.auth.Credentials;
Expand Down Expand Up @@ -71,14 +70,14 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.threeten.bp.Duration;

class InstantiatingGrpcChannelProviderTest extends AbstractMtlsTransportChannelTest {
private static final String DEFAULT_ENDPOINT = "test.googleapis.com:443";
private static String originalOSName;
private EnvironmentProvider envProvider;
private ComputeEngineCredentials computeEngineCredentials;

@BeforeAll
Expand All @@ -89,12 +88,6 @@ public static void setupClass() {
@BeforeEach
public void setup() throws IOException {
System.setProperty("os.name", "Linux");
// These mocks are the default mocks used to enable DirectPath
envProvider = Mockito.mock(EnvironmentProvider.class);
Mockito.when(
envProvider.getenv(
InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH))
.thenReturn("false");
computeEngineCredentials = Mockito.mock(ComputeEngineCredentials.class);
}

Expand Down Expand Up @@ -668,7 +661,6 @@ void testLogDirectPathMisconfigNotOnGCE() throws Exception {
public void canUseDirectPath_happyPath() {
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEnvProvider(envProvider)
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
Expand All @@ -678,15 +670,12 @@ public void canUseDirectPath_happyPath() {
}

@Test
@SetEnvironmentVariable(
key = InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH,
value = "true")
public void canUseDirectPath_directPathEnvVarDisabled() {
EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);
Mockito.when(
envProvider.getenv(
InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH))
.thenReturn("true");
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEnvProvider(envProvider)
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
Expand All @@ -697,15 +686,8 @@ public void canUseDirectPath_directPathEnvVarDisabled() {

@Test
public void canUseDirectPath_directPathEnvVarNotSet_attemptDirectPathIsTrue() {
EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);
// If system property is not set, then System.getProperty() returns null
Mockito.when(
envProvider.getenv(
InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH))
.thenReturn(null);
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEnvProvider(envProvider)
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
Expand All @@ -716,14 +698,8 @@ public void canUseDirectPath_directPathEnvVarNotSet_attemptDirectPathIsTrue() {

@Test
public void canUseDirectPath_directPathEnvVarNotSet_attemptDirectPathIsFalse() {
EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);
Mockito.when(
envProvider.getenv(
InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH))
.thenReturn(null);
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEnvProvider(envProvider)
.setAttemptDirectPath(false)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
Expand All @@ -733,11 +709,13 @@ public void canUseDirectPath_directPathEnvVarNotSet_attemptDirectPathIsFalse() {
}

@Test
@SetEnvironmentVariable(
key = InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH,
value = "false")
public void canUseDirectPath_nonComputeCredentials() {
Credentials credentials = Mockito.mock(Credentials.class);
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEnvProvider(envProvider)
.setAttemptDirectPath(true)
.setCredentials(credentials)
.setEndpoint(DEFAULT_ENDPOINT)
Expand All @@ -747,11 +725,13 @@ public void canUseDirectPath_nonComputeCredentials() {
}

@Test
@SetEnvironmentVariable(
key = InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH,
value = "false")
public void canUseDirectPath_isNotOnComputeEngine_invalidOsNameSystemProperty() {
System.setProperty("os.name", "Not Linux");
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEnvProvider(envProvider)
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
Expand All @@ -761,10 +741,12 @@ public void canUseDirectPath_isNotOnComputeEngine_invalidOsNameSystemProperty()
}

@Test
@SetEnvironmentVariable(
key = InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH,
value = "false")
public void canUseDirectPath_isNotOnComputeEngine_invalidSystemProductName() {
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEnvProvider(envProvider)
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
Expand All @@ -774,10 +756,12 @@ public void canUseDirectPath_isNotOnComputeEngine_invalidSystemProductName() {
}

@Test
@SetEnvironmentVariable(
key = InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH,
value = "false")
public void canUseDirectPath_isNotOnComputeEngine_unableToGetSystemProductName() {
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEnvProvider(envProvider)
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
Expand All @@ -787,11 +771,13 @@ public void canUseDirectPath_isNotOnComputeEngine_unableToGetSystemProductName()
}

@Test
@SetEnvironmentVariable(
key = InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH,
value = "false")
public void canUseDirectPath_nonGDUUniverseDomain() {
String nonGDUEndpoint = "test.random.com:443";
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEnvProvider(envProvider)
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(nonGDUEndpoint)
Expand Down
6 changes: 6 additions & 0 deletions gax-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
Expand Down

0 comments on commit 9bbb706

Please sign in to comment.