Skip to content

Commit 99a2696

Browse files
committed
s2a: Restore static token state mutated in tests
This is the easy-to-fix state, but GetAuthenticationMechanisms can save these temporary states used in tests, so more fixes will be necessary.
1 parent 65b32e6 commit 99a2696

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

s2a/src/main/java/io/grpc/s2a/internal/handshaker/tokenmanager/SingleTokenFetcher.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public static void setAccessToken(String token) {
4141
accessToken = token;
4242
}
4343

44+
@VisibleForTesting
45+
public static String getAccessToken() {
46+
return accessToken;
47+
}
48+
4449
private SingleTokenFetcher(String token) {
4550
this.token = token;
4651
}
@@ -54,4 +59,4 @@ public String getDefaultToken() {
5459
public String getToken(S2AIdentity identity) {
5560
return token;
5661
}
57-
}
62+
}

s2a/src/test/java/io/grpc/s2a/internal/handshaker/GetAuthenticationMechanismsTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.grpc.s2a.internal.handshaker.S2AIdentity;
2121
import io.grpc.s2a.internal.handshaker.tokenmanager.SingleTokenFetcher;
2222
import java.util.Optional;
23+
import org.junit.AfterClass;
2324
import org.junit.BeforeClass;
2425
import org.junit.Rule;
2526
import org.junit.Test;
@@ -31,13 +32,20 @@
3132
public final class GetAuthenticationMechanismsTest {
3233
@Rule public final Expect expect = Expect.create();
3334
private static final String TOKEN = "access_token";
35+
private static String originalAccessToken;
3436

3537
@BeforeClass
3638
public static void setUpClass() {
39+
originalAccessToken = SingleTokenFetcher.getAccessToken();
3740
// Set the token that the client will use to authenticate to the S2A.
3841
SingleTokenFetcher.setAccessToken(TOKEN);
3942
}
4043

44+
@AfterClass
45+
public static void tearDownClass() {
46+
SingleTokenFetcher.setAccessToken(originalAccessToken);
47+
}
48+
4149
@Test
4250
public void getAuthMechanisms_emptyIdentity_success() {
4351
expect
@@ -58,4 +66,4 @@ public void getAuthMechanisms_nonEmptyIdentity_success() {
5866
.setToken("access_token")
5967
.build()));
6068
}
61-
}
69+
}

s2a/src/test/java/io/grpc/s2a/internal/handshaker/tokenmanager/SingleTokenAccessTokenManagerTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import io.grpc.s2a.internal.handshaker.S2AIdentity;
2222
import java.util.Optional;
23+
import org.junit.After;
2324
import org.junit.Before;
2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
@@ -30,11 +31,19 @@ public final class SingleTokenAccessTokenManagerTest {
3031
private static final S2AIdentity IDENTITY = S2AIdentity.fromSpiffeId("spiffe_id");
3132
private static final String TOKEN = "token";
3233

34+
private String originalAccessToken;
35+
3336
@Before
3437
public void setUp() {
38+
originalAccessToken = SingleTokenFetcher.getAccessToken();
3539
SingleTokenFetcher.setAccessToken(null);
3640
}
3741

42+
@After
43+
public void tearDown() {
44+
SingleTokenFetcher.setAccessToken(originalAccessToken);
45+
}
46+
3847
@Test
3948
public void getDefaultToken_success() throws Exception {
4049
SingleTokenFetcher.setAccessToken(TOKEN);
@@ -68,4 +77,4 @@ public void create_success() throws Exception {
6877
public void create_noEnvironmentVariable() throws Exception {
6978
assertThat(AccessTokenManager.create()).isEmpty();
7079
}
71-
}
80+
}

0 commit comments

Comments
 (0)