Skip to content

Commit dc56d26

Browse files
committed
wip
Signed-off-by: wind57 <[email protected]>
1 parent 780573e commit dc56d26

File tree

5 files changed

+54
-47
lines changed

5 files changed

+54
-47
lines changed

spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInitiator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ void postConstruct() {
109109
// 'blockReadinessCheck'
110110
CompletableFuture<?> ready = podReadyFuture.whenComplete((ok, error) -> {
111111
if (error != null) {
112-
LOG.error(() -> "readiness failed for : " + candidateIdentity +
113-
", leader election will not start");
112+
LOG.error(() -> "readiness failed for : " + candidateIdentity
113+
+ ", leader election will not start");
114114
}
115115
else {
116116
LOG.info(() -> candidateIdentity + " is ready");

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-leader-election/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/AbstractLeaderElection.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import org.junit.jupiter.api.extension.ExtendWith;
2828
import org.mockito.MockedStatic;
2929
import org.mockito.Mockito;
30-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
31-
import org.springframework.boot.test.context.SpringBootTest;
32-
import org.springframework.boot.test.system.OutputCaptureExtension;
3330
import org.testcontainers.k3s.K3sContainer;
3431

3532
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
34+
import org.springframework.boot.test.context.SpringBootTest;
3635
import org.springframework.boot.test.context.TestConfiguration;
36+
import org.springframework.boot.test.system.OutputCaptureExtension;
3737
import org.springframework.cloud.kubernetes.commons.leader.LeaderUtils;
3838
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
3939
import org.springframework.context.annotation.Bean;
@@ -44,10 +44,10 @@
4444
*/
4545
@ExtendWith(OutputCaptureExtension.class)
4646
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
47-
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.election.enabled=true",
48-
"logging.level.org.springframework.cloud.kubernetes.commons.leader.election=debug",
49-
"logging.level.org.springframework.cloud.kubernetes.fabric8.leader.election=debug" },
50-
classes = { App.class, AbstractLeaderElection.LocalConfiguration.class })
47+
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.election.enabled=true",
48+
"logging.level.org.springframework.cloud.kubernetes.commons.leader.election=debug",
49+
"logging.level.org.springframework.cloud.kubernetes.fabric8.leader.election=debug" },
50+
classes = { App.class, AbstractLeaderElection.LocalConfiguration.class })
5151
abstract class AbstractLeaderElection {
5252

5353
private static K3sContainer container;

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-leader-election/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionReadinessCanceledIT.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cloud.kubernetes.fabric8.leader.election;
1818

19+
import java.time.Duration;
20+
1921
import org.awaitility.Awaitility;
2022
import org.junit.jupiter.api.BeforeAll;
2123
import org.junit.jupiter.api.Test;
@@ -24,19 +26,15 @@
2426
import org.springframework.boot.test.system.CapturedOutput;
2527
import org.springframework.test.context.TestPropertySource;
2628

27-
import java.time.Duration;
28-
2929
import static org.assertj.core.api.Assertions.assertThat;
3030

3131
/**
3232
* Readiness is canceled. This is the case when pod is shut down gracefully
3333
*
3434
* @author wind57
3535
*/
36-
@TestPropertySource(properties = {
37-
"readiness.cycle.false=true",
38-
"spring.cloud.kubernetes.leader.election.wait-for-pod-ready=true"
39-
})
36+
@TestPropertySource(properties = { "readiness.cycle.false=true",
37+
"spring.cloud.kubernetes.leader.election.wait-for-pod-ready=true" })
4038
class Fabric8LeaderElectionReadinessCanceledIT extends AbstractLeaderElection {
4139

4240
@Autowired
@@ -54,36 +52,39 @@ void test(CapturedOutput output) {
5452
Awaitility.await()
5553
.atMost(Duration.ofSeconds(60))
5654
.pollInterval(Duration.ofSeconds(1))
57-
.until(() -> output.getOut().contains(
58-
"Pod : canceled-readiness-it in namespace : default is not ready, will retry in one second"
59-
));
55+
.until(() -> output.getOut()
56+
.contains("Pod : canceled-readiness-it in namespace : default is not ready, will retry in one second"));
6057

6158
initiator.preDestroy();
6259

6360
try {
6461
Thread.sleep(2_000);
65-
} catch (InterruptedException e) {
62+
}
63+
catch (InterruptedException e) {
6664
throw new RuntimeException(e);
6765
}
6866

69-
// 1. preDestroy logs what it will do
67+
// 1. preDestroy method logs what it will do
7068
assertThat(output.getOut()).contains("podReadyFuture will be canceled for : canceled-readiness-it");
7169

7270
// 2. readiness failed
73-
assertThat(output.getOut()).contains(
74-
"readiness failed for : canceled-readiness-it, leader election will not start");
71+
assertThat(output.getOut())
72+
.contains("readiness failed for : canceled-readiness-it, leader election will not start");
7573

7674
// 3. will cancel the future that is supposed to do the readiness
77-
assertThat(output.getOut()).contains(
78-
"canceling scheduled future because completable future was cancelled");
75+
assertThat(output.getOut()).contains("canceling scheduled future because completable future was cancelled");
7976

8077
// 4. podReadyWaitingExecutor is shut down also
81-
assertThat(output.getOut()).contains(
82-
"podReadyWaitingExecutor will be shutdown for : canceled-readiness-it");
78+
assertThat(output.getOut()).contains("podReadyWaitingExecutor will be shutdown for : canceled-readiness-it");
8379

8480
// 5. the scheduled executor where pod readiness is checked is shut down also
85-
assertThat(output.getOut()).contains(
86-
"Shutting down executor : podReadyExecutor");
81+
Awaitility.await()
82+
.atMost(Duration.ofSeconds(2))
83+
.pollInterval(Duration.ofMillis(100))
84+
.until(() -> output.getOut().contains("Shutting down executor : podReadyExecutor"));
85+
86+
// 6. leader election is not started, since readiness does not finish
87+
assertThat(output.getOut()).doesNotContain("leaderFuture will be canceled for");
8788

8889
}
8990

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-leader-election/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionReadinessFailsIT.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import org.junit.jupiter.api.BeforeAll;
2323
import org.junit.jupiter.api.Test;
2424

25-
import org.springframework.test.context.TestPropertySource;
2625
import org.springframework.boot.test.system.CapturedOutput;
26+
import org.springframework.test.context.TestPropertySource;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
2929

@@ -32,28 +32,27 @@
3232
*
3333
* @author wind57
3434
*/
35-
@TestPropertySource(properties = {"readiness.fails=true",
36-
"spring.cloud.kubernetes.leader.election.wait-for-pod-ready=true"
37-
})
35+
@TestPropertySource(
36+
properties = { "readiness.fails=true", "spring.cloud.kubernetes.leader.election.wait-for-pod-ready=true" })
3837
class Fabric8LeaderElectionReadinessFailsIT extends AbstractLeaderElection {
3938

4039
@BeforeAll
4140
static void beforeAll() {
4241
AbstractLeaderElection.beforeAll("readiness-fails-simple-it");
4342
}
4443

45-
/* <pre>
46-
* - readiness fails after 2 seconds
47-
* - leader election process is not started at all
48-
* </pre>
44+
/*
45+
* <pre> - readiness fails after 2 seconds - leader election process is not started at
46+
* all </pre>
4947
*/
5048
@Test
5149
void test(CapturedOutput output) {
5250
// we do not start leader election at all
5351
Awaitility.await()
5452
.atMost(Duration.ofSeconds(60))
5553
.pollInterval(Duration.ofSeconds(1))
56-
.until(() -> output.getOut().contains("leader election for : readiness-fails-simple-it will not start"));
54+
.until(() -> output.getOut()
55+
.contains("readiness failed for : readiness-fails-simple-it, leader election will not start"));
5756

5857
// let's unwind some logs to see that the process is how we expect it to be
5958

@@ -77,18 +76,22 @@ void test(CapturedOutput output) {
7776
assertThat(output.getOut()).contains("exception waiting for pod : readiness-fails-simple-it");
7877

7978
// 7. readiness failed
80-
assertThat(output.getOut()).contains(
81-
"pod readiness for : readiness-fails-simple-it failed with : readiness fails");
79+
assertThat(output.getOut())
80+
.contains("pod readiness for : readiness-fails-simple-it failed with : readiness fails");
8281

8382
// 8. we shut down the executor
8483
assertThat(output.getOut()).contains("canceling scheduled future because readiness failed");
8584

8685
// 9. leader election did not even start properly
87-
assertThat(output.getOut()).contains(
88-
"pod readiness for : readiness-fails-simple-it failed with : readiness fails");
86+
assertThat(output.getOut())
87+
.contains("pod readiness for : readiness-fails-simple-it failed with : readiness fails");
8988

9089
// 10. executor is shutdown, even when readiness failed
91-
assertThat(output.getOut()).contains("Shutting down executor : podReadyExecutor");
90+
Awaitility.await()
91+
.atMost(Duration.ofSeconds(2))
92+
.pollInterval(Duration.ofMillis(100))
93+
.until(() -> output.getOut().contains("Shutting down executor : podReadyExecutor"));
94+
9295
}
9396

9497
}

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-leader-election/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionReadinessPassesIT.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import org.junit.jupiter.api.BeforeAll;
2323
import org.junit.jupiter.api.Test;
2424

25-
import org.springframework.test.context.TestPropertySource;
2625
import org.springframework.boot.test.system.CapturedOutput;
26+
import org.springframework.test.context.TestPropertySource;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
2929

@@ -32,9 +32,9 @@
3232
*
3333
* @author wind57
3434
*/
35-
@TestPropertySource(properties = { "readiness.passes=true",
36-
"spring.cloud.kubernetes.leader.election.wait-for-pod-ready=true" })
37-
class Fabric8LeaderElectionReadinessPassesIT extends AbstractLeaderElection {
35+
@TestPropertySource(
36+
properties = { "readiness.passes=true", "spring.cloud.kubernetes.leader.election.wait-for-pod-ready=true" })
37+
class Fabric8LeaderElectionReadinessPassesIT extends AbstractLeaderElection {
3838

3939
@BeforeAll
4040
static void beforeAll() {
@@ -80,7 +80,10 @@ void test(CapturedOutput output) {
8080
assertThat(output.getOut()).contains("canceling scheduled future because readiness succeeded");
8181

8282
// 8. executor is shutdown
83-
assertThat(output.getOut()).contains("Shutting down executor : podReadyExecutor");
83+
Awaitility.await()
84+
.atMost(Duration.ofSeconds(2))
85+
.pollInterval(Duration.ofMillis(100))
86+
.until(() -> output.getOut().contains("Shutting down executor : podReadyExecutor"));
8487

8588
// 9. pod is now ready
8689
assertThat(output.getOut()).contains("readiness-passes-simple-it is ready");

0 commit comments

Comments
 (0)