Skip to content

Commit 24749c7

Browse files
fix e2e and windows
Signed-off-by: Adrian Cole <[email protected]>
1 parent 9d6e0e0 commit 24749c7

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

e2e/src/test/java/io/kubernetes/client/e2e/extended/leaderelection/LeaderElectorTest.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.kubernetes.client.openapi.apis.CoordinationV1Api;
2626
import io.kubernetes.client.openapi.apis.CoreV1Api;
2727
import io.kubernetes.client.util.ClientBuilder;
28-
import java.io.IOException;
2928
import java.net.HttpURLConnection;
3029
import java.time.Duration;
3130
import java.util.ArrayList;
@@ -37,7 +36,6 @@
3736
import java.util.concurrent.TimeUnit;
3837
import java.util.concurrent.atomic.AtomicInteger;
3938
import java.util.concurrent.atomic.AtomicReference;
40-
import org.junit.jupiter.api.BeforeEach;
4139
import org.junit.jupiter.api.Timeout;
4240
import org.junit.jupiter.params.ParameterizedTest;
4341
import org.junit.jupiter.params.provider.MethodSource;
@@ -70,17 +68,8 @@ public static Collection<Object[]> constructorFeeder() {
7068
private ApiClient apiClient;
7169
private LockType lockType;
7270

73-
public void initLeaderElectorTest(LockType lockType) {
74-
try {
75-
apiClient = ClientBuilder.defaultClient();
76-
} catch (IOException ex) {
77-
throw new RuntimeException("Couldn't create ApiClient", ex);
78-
}
79-
this.lockType = lockType;
80-
}
81-
82-
@BeforeEach
83-
void setup() throws Exception {
71+
private void initLeaderElectorTest(LockType lockType) throws Exception {
72+
apiClient = ClientBuilder.defaultClient();
8473
// delete the lock resource if it exists, or else first leader candidate might need to wait for
8574
// a whole leaseDuration configured
8675
switch (lockType) {
@@ -96,6 +85,7 @@ void setup() throws Exception {
9685
default:
9786
throw new RuntimeException("Unknown LockType " + lockType);
9887
}
88+
this.lockType = lockType;
9989
}
10090

10191
@MethodSource("constructorFeeder")
@@ -110,8 +100,8 @@ void singleCandidateLeaderElection(LockType lockType) throws Exception {
110100
makeAndRunLeaderElectorAsync(
111101
"candidate",
112102
null,
113-
() -> startLeadershipLatch.countDown(),
114-
() -> stopLeadershipLatch.countDown(),
103+
startLeadershipLatch::countDown,
104+
stopLeadershipLatch::countDown,
115105
apiClient);
116106

117107
startLeadershipLatch.await();
@@ -203,8 +193,8 @@ void leaderGracefulShutdown(LockType lockType) throws Exception {
203193
makeAndRunLeaderElectorAsync(
204194
"candidate1",
205195
null,
206-
() -> startBeingLeader1.countDown(),
207-
() -> stopBeingLeader1.countDown(),
196+
startBeingLeader1::countDown,
197+
stopBeingLeader1::countDown,
208198
apiClient);
209199

210200
// wait for candidate1 to become leader
@@ -217,8 +207,8 @@ void leaderGracefulShutdown(LockType lockType) throws Exception {
217207
makeAndRunLeaderElectorAsync(
218208
"candidate2",
219209
null,
220-
() -> startBeingLeader2.countDown(),
221-
() -> stopBeingLeader2.countDown(),
210+
startBeingLeader2::countDown,
211+
stopBeingLeader2::countDown,
222212
apiClient);
223213

224214
leaderElector1.close();

util/src/test/java/io/kubernetes/client/util/FilePersisterTest.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
1616

17-
import io.kubernetes.client.Resources;
17+
import java.io.File;
1818
import java.io.FileReader;
1919
import java.io.IOException;
20+
import java.io.Reader;
2021
import java.nio.file.Files;
2122
import java.nio.file.Path;
2223
import org.junit.jupiter.api.Test;
24+
import io.kubernetes.client.Resources;
2325
import io.kubernetes.client.persister.FilePersister;
2426
import org.junit.jupiter.api.io.TempDir;
2527

@@ -30,23 +32,27 @@ class FilePersisterTest {
3032

3133
@Test
3234
void persistence(@TempDir Path tempDir) throws IOException {
33-
Path file = Files.createTempFile(tempDir, "testconfig", null);
34-
FilePersister fp = new FilePersister(file.toString());
35+
File file = Files.createTempFile(tempDir, "testconfig", null).toFile();
3536

36-
KubeConfig config = KubeConfig.loadKubeConfig(new FileReader(KUBECONFIG_FILE_PATH));
37+
KubeConfig config;
38+
try (Reader reader = new FileReader(KUBECONFIG_FILE_PATH)) {
39+
config = KubeConfig.loadKubeConfig(reader);
40+
}
3741

42+
FilePersister fp = new FilePersister(file);
3843
fp.save(
3944
config.getContexts(),
4045
config.getClusters(),
4146
config.getUsers(),
4247
config.getPreferences(),
4348
config.getCurrentContext());
4449

45-
KubeConfig configOut = KubeConfig.loadKubeConfig(new FileReader(file.toFile()));
46-
47-
assertThat(configOut.getCurrentContext()).isEqualTo(config.getCurrentContext());
48-
assertThat(configOut.getClusters()).isEqualTo(config.getClusters());
49-
assertThat(configOut.getContexts()).isEqualTo(config.getContexts());
50-
assertThat(configOut.getUsers()).isEqualTo(config.getUsers());
50+
try (Reader reader = new FileReader(file)) {
51+
KubeConfig configOut = KubeConfig.loadKubeConfig(reader);
52+
assertThat(configOut.getCurrentContext()).isEqualTo(config.getCurrentContext());
53+
assertThat(configOut.getClusters()).isEqualTo(config.getClusters());
54+
assertThat(configOut.getContexts()).isEqualTo(config.getContexts());
55+
assertThat(configOut.getUsers()).isEqualTo(config.getUsers());
56+
}
5157
}
5258
}

util/src/test/java/io/kubernetes/client/util/KubeConfigTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.ByteArrayInputStream;
2323
import java.io.FileReader;
2424
import java.io.IOException;
25+
import java.io.Reader;
2526
import java.io.StringReader;
2627
import java.nio.charset.StandardCharsets;
2728
import java.nio.file.Files;
@@ -99,8 +100,10 @@ void gcpAuthProvider(@TempDir Path tempDir) throws Exception {
99100
Path config = Files.createTempFile(tempDir, "config", null);
100101
Files.writeString(config, GCP_CONFIG);
101102

102-
KubeConfig kc = KubeConfig.loadKubeConfig(new FileReader(config.toFile()));
103-
assertThat(kc.getCredentials()).containsEntry(KubeConfig.CRED_TOKEN_KEY, "fake-token");
103+
try (Reader reader = new FileReader(config.toFile())) {
104+
KubeConfig kc = KubeConfig.loadKubeConfig(reader);
105+
assertThat(kc.getCredentials()).containsEntry(KubeConfig.CRED_TOKEN_KEY, "fake-token");
106+
}
104107
}
105108

106109
private static String GCP_TEST_DATE_STRING =
@@ -130,8 +133,10 @@ void gcpAuthProviderStringDate(@TempDir Path tempDir) throws IOException {
130133
Path config = Files.createTempFile(tempDir, "config", null);
131134
Files.writeString(config, GCP_TEST_DATE_STRING);
132135

133-
KubeConfig kc = KubeConfig.loadKubeConfig(new FileReader(config.toFile()));
134-
assertThat(kc.getCredentials()).containsEntry(KubeConfig.CRED_TOKEN_KEY, "fake-token");
136+
try (Reader reader = new FileReader(config.toFile())) {
137+
KubeConfig kc = KubeConfig.loadKubeConfig(reader);
138+
assertThat(kc.getCredentials()).containsEntry(KubeConfig.CRED_TOKEN_KEY, "fake-token");
139+
}
135140
}
136141

137142
@Test

0 commit comments

Comments
 (0)